diff --git a/apps/api/src/routes/leafwatch/events.ts b/apps/api/src/routes/leafwatch/events.ts index 8aa9f2f312d9..204f12e3030f 100644 --- a/apps/api/src/routes/leafwatch/events.ts +++ b/apps/api/src/routes/leafwatch/events.ts @@ -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, diff --git a/apps/cron/src/batchProcessEvents.ts b/apps/cron/src/batchProcessEvents.ts index 4bb8c51883f6..59b5b5c1eeea 100644 --- a/apps/cron/src/batchProcessEvents.ts +++ b/apps/cron/src/batchProcessEvents.ts @@ -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; diff --git a/packages/db/.gitignore b/packages/db/.gitignore new file mode 100644 index 000000000000..86d4c2dd380e --- /dev/null +++ b/packages/db/.gitignore @@ -0,0 +1 @@ +generated diff --git a/packages/db/env.d.ts b/packages/db/env.d.ts index 545aed0b92b3..e9e32baec09d 100644 --- a/packages/db/env.d.ts +++ b/packages/db/env.d.ts @@ -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; diff --git a/packages/db/package.json b/packages/db/package.json index e63af10ea7b2..2593b5cb8823 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -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": { diff --git a/packages/db/prisma/db/client.ts b/packages/db/prisma/db/client.ts index 5f646f6229f9..247604702683 100644 --- a/packages/db/prisma/db/client.ts +++ b/packages/db/prisma/db/client.ts @@ -1,4 +1,4 @@ -import { PrismaClient } from '@prisma/client'; +import { PrismaClient } from './generated/db.ts'; const prismaClientSingleton = () => { return new PrismaClient(); diff --git a/packages/db/prisma/db/schema.prisma b/packages/db/prisma/db/schema.prisma index 8338db6c939d..5456b230ce64 100644 --- a/packages/db/prisma/db/schema.prisma +++ b/packages/db/prisma/db/schema.prisma @@ -1,5 +1,6 @@ generator client { provider = "prisma-client-js" + output = "./generated/db.ts" } datasource db { diff --git a/packages/db/prisma/leafwatch/client.ts b/packages/db/prisma/leafwatch/client.ts new file mode 100644 index 000000000000..8f087f644b8a --- /dev/null +++ b/packages/db/prisma/leafwatch/client.ts @@ -0,0 +1,19 @@ +import { PrismaClient } from './generated/leafwatch.ts'; + +const leafwatchClientSingleton = () => { + return new PrismaClient(); +}; + +type LeafwatchClientSingleton = ReturnType; + +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; +} diff --git a/packages/db/prisma/leafwatch/migrations/20240810071922_init/migration.sql b/packages/db/prisma/leafwatch/migrations/20240810071922_init/migration.sql new file mode 100644 index 000000000000..d14278936d3f --- /dev/null +++ b/packages/db/prisma/leafwatch/migrations/20240810071922_init/migration.sql @@ -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") +); diff --git a/packages/db/prisma/leafwatch/migrations/migration_lock.toml b/packages/db/prisma/leafwatch/migrations/migration_lock.toml new file mode 100644 index 000000000000..fbffa92c2bb7 --- /dev/null +++ b/packages/db/prisma/leafwatch/migrations/migration_lock.toml @@ -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" \ No newline at end of file diff --git a/packages/db/prisma/leafwatch/schema.prisma b/packages/db/prisma/leafwatch/schema.prisma new file mode 100644 index 000000000000..16aff1a3f357 --- /dev/null +++ b/packages/db/prisma/leafwatch/schema.prisma @@ -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()) +}