Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: test ts db (#5224)
Browse files Browse the repository at this point in the history
bigint authored Aug 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 824f19d + a6b31d4 commit f5961de
Showing 11 changed files with 94 additions and 6 deletions.
1 change: 0 additions & 1 deletion apps/api/src/routes/leafwatch/events.ts
Original file line number Diff line number Diff line change
@@ -69,7 +69,6 @@ export const post = [
browser: ua.browser.name || null,
city: cfIpCity || null,
country: cfIpCountry || null,
created: new Date().toISOString().slice(0, 19).replace('T', ' '),
fingerprint: fingerprint || null,
ip: ip || null,
name,
3 changes: 3 additions & 0 deletions apps/cron/src/batchProcessEvents.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import clickhouseClient from '@hey/db/clickhouseClient';
import leafwatch from '@hey/db/prisma/leafwatch/client';
import { lRangeRedis, lTrimRedis } from '@hey/db/redisClient';
import logger from '@hey/helpers/logger';

@@ -20,6 +21,8 @@ const batchProcessEvents = async () => {
values: parsedEvents
});

await leafwatch.event.createMany({ data: parsedEvents });

const endTime = Date.now();
const timeTaken = endTime - startTime;

1 change: 1 addition & 0 deletions packages/db/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generated
1 change: 1 addition & 0 deletions packages/db/env.d.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ declare namespace NodeJS {
CLICKHOUSE_PASSWORD: string;
CLICKHOUSE_URL: string;
DATABASE_URL: string;
LEAFWATCH_DATABASE_URL: string;
LENS_DATABASE_PASSWORD: string;
NODE_ENV: string;
REDIS_URL: string;
13 changes: 9 additions & 4 deletions packages/db/package.json
Original file line number Diff line number Diff line change
@@ -4,15 +4,20 @@
"private": true,
"license": "AGPL-3.0",
"scripts": {
"codegen": "prisma generate --schema ./prisma/db/schema.prisma > /dev/null",
"db:codegen": "prisma generate --schema ./prisma/db/schema.prisma > /dev/null",
"leafwatch:codegen": "prisma generate --schema ./prisma/leafwatch/schema.prisma > /dev/null",
"codegen": "pnpm db:codegen && pnpm leafwatch:codegen",
"postinstall": "pnpm codegen",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --fix --ext .ts",
"prettier": "prettier --check \"**/*.{js,ts,tsx,md}\" --cache",
"prettier:fix": "prettier --write \"**/*.{js,ts,tsx,md}\" --cache",
"prisma:clean": "prisma migrate reset --schema ./prisma/db/schema.prisma",
"prisma:format": "prisma format --schema ./prisma/db/schema.prisma",
"prisma:migrate": "prisma migrate dev --schema ./prisma/db/schema.prisma",
"db:prisma:clean": "prisma migrate reset --schema ./prisma/db/schema.prisma",
"db:prisma:format": "prisma format --schema ./prisma/db/schema.prisma",
"db:prisma:migrate": "prisma migrate dev --schema ./prisma/db/schema.prisma",
"leafwatch:prisma:clean": "prisma migrate reset --schema ./prisma/leafwatch/schema.prisma",
"leafwatch:prisma:format": "prisma format --schema ./prisma/leafwatch/schema.prisma",
"leafwatch:prisma:migrate": "prisma migrate dev --schema ./prisma/leafwatch/schema.prisma",
"typecheck": "tsc --pretty"
},
"dependencies": {
2 changes: 1 addition & 1 deletion packages/db/prisma/db/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaClient } from '@prisma/client';
import { PrismaClient } from './generated/db.ts';

const prismaClientSingleton = () => {
return new PrismaClient();
1 change: 1 addition & 0 deletions packages/db/prisma/db/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
generator client {
provider = "prisma-client-js"
output = "./generated/db.ts"
}

datasource db {
19 changes: 19 additions & 0 deletions packages/db/prisma/leafwatch/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { PrismaClient } from './generated/leafwatch.ts';

const leafwatchClientSingleton = () => {
return new PrismaClient();
};

type LeafwatchClientSingleton = ReturnType<typeof leafwatchClientSingleton>;

const globalForLeafwatch = globalThis as unknown as {
leafwatch: LeafwatchClientSingleton | undefined;
};

const leafwatch = globalForLeafwatch.leafwatch ?? leafwatchClientSingleton();

export default leafwatch;

if (process.env.NODE_ENV !== 'production') {
globalForLeafwatch.leafwatch = leafwatch;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- CreateTable
CREATE TABLE "Event" (
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
"actor" TEXT,
"fingerprint" TEXT,
"name" TEXT NOT NULL,
"properties" JSONB,
"referrer" TEXT,
"url" TEXT,
"browser" TEXT,
"ip" TEXT,
"city" TEXT,
"country" TEXT,
"created" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "Event_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Impression" (
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
"publication" TEXT NOT NULL,
"viewed" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "Impression_pkey" PRIMARY KEY ("id")
);
3 changes: 3 additions & 0 deletions packages/db/prisma/leafwatch/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
30 changes: 30 additions & 0 deletions packages/db/prisma/leafwatch/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
generator client {
provider = "prisma-client-js"
output = "./generated/leafwatch.ts"
}

datasource db {
provider = "postgresql"
url = env("LEAFWATCH_DATABASE_URL")
}

model Event {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
actor String?
fingerprint String?
name String
properties Json?
referrer String?
url String?
browser String?
ip String?
city String?
country String?
created DateTime @default(now())
}

model Impression {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
publication String
viewed DateTime @default(now())
}

1 comment on commit f5961de

@vercel
Copy link

@vercel vercel bot commented on f5961de Aug 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

web – ./

heyxyz.vercel.app
web-heyxyz.vercel.app
hey.xyz
web-git-main-heyxyz.vercel.app

Please sign in to comment.