From 7d57bafe4a6c38c4c8ab96b432a5c1b51cf4441f Mon Sep 17 00:00:00 2001 From: Anish Araz Date: Mon, 14 Oct 2024 15:53:36 +0530 Subject: [PATCH] INIT:monorepo database package --- .gitignore | 2 + apps/web/app/page.tsx | 8 +- apps/web/package.json | 1 + packages/database/.env-example | 2 + packages/database/package.json | 28 +++++++ packages/database/prisma/schema_mongo.prisma | 76 +++++++++++++++++ packages/database/prisma/schema_pg.prisma | 85 ++++++++++++++++++++ packages/database/src/index.ts | 2 + packages/database/src/lib/mongo_db.ts | 15 ++++ packages/database/src/lib/pg_db.ts | 15 ++++ packages/database/tsconfig.json | 13 +++ pnpm-lock.yaml | 77 +++++++++++++++++- 12 files changed, 320 insertions(+), 4 deletions(-) create mode 100644 packages/database/.env-example create mode 100644 packages/database/package.json create mode 100644 packages/database/prisma/schema_mongo.prisma create mode 100644 packages/database/prisma/schema_pg.prisma create mode 100644 packages/database/src/index.ts create mode 100644 packages/database/src/lib/mongo_db.ts create mode 100644 packages/database/src/lib/pg_db.ts create mode 100644 packages/database/tsconfig.json diff --git a/.gitignore b/.gitignore index 96fab4f..7bb1bd1 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,5 @@ yarn-error.log* # Misc .DS_Store *.pem + +migrations \ No newline at end of file diff --git a/apps/web/app/page.tsx b/apps/web/app/page.tsx index 9f04097..03df956 100644 --- a/apps/web/app/page.tsx +++ b/apps/web/app/page.tsx @@ -1,9 +1,13 @@ import { Button } from "@repo/ui"; - -export default function Home() { +import { PG_PRISMA_CLIENT, MONGO_PRISMA_CLIENT } from "@repo/database"; +export default async function Home() { + const user = await PG_PRISMA_CLIENT.user.findFirst(); + const data = await MONGO_PRISMA_CLIENT.idea.findFirst(); return (
Hello world!!
+
{user?.id}
+
{data?.id}
); diff --git a/apps/web/package.json b/apps/web/package.json index b1b7037..63eec7b 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -10,6 +10,7 @@ }, "dependencies": { "@repo/ui": "workspace:*", + "@repo/database": "workspace:*", "next": "14.2.6", "react": "18.3.1", "react-dom": "18.3.1" diff --git a/packages/database/.env-example b/packages/database/.env-example new file mode 100644 index 0000000..4497df2 --- /dev/null +++ b/packages/database/.env-example @@ -0,0 +1,2 @@ +MONGODB_DATABASE_URL="mongodb+srv://user:pass@url/database?retryWrites=true&w=majority&appName=main" +POSTGRESQL_DATABASE_URL="postgresql://postgres:postgresql@192.168.122.2:5432/imp?schema=public" \ No newline at end of file diff --git a/packages/database/package.json b/packages/database/package.json new file mode 100644 index 0000000..031da25 --- /dev/null +++ b/packages/database/package.json @@ -0,0 +1,28 @@ +{ + "name": "@repo/database", + "version": "1.0.0", + "description": "", + "scripts": { + "build": "tsup src/index.ts --minify --dts --out-dir dist", + "dev": "tsup src/index.ts --watch --dts --out-dir dist", + "prisma:generate": "prisma generate --schema ./prisma/schema_mongo.prisma & prisma generate --schema ./prisma/schema_pg.prisma", + "prisma:migrate:pg": "prisma migrate dev --schema ./prisma/schema_pg.prisma" + }, + "keywords": [], + "author": "", + "license": "ISC", + "exports": { + ".": { + "default": "./dist/index.js" + } + }, + "dependencies": { + "@prisma/client": "^5.20.0", + "@repo/typescript-config": "workspace:*" + }, + "devDependencies": { + "@types/node": "^20", + "prisma": "^5.20.0", + "tsup": "^8.3.0" + } +} diff --git a/packages/database/prisma/schema_mongo.prisma b/packages/database/prisma/schema_mongo.prisma new file mode 100644 index 0000000..5b2198d --- /dev/null +++ b/packages/database/prisma/schema_mongo.prisma @@ -0,0 +1,76 @@ +// This is your Prisma schema file, +// learn more about it in the docs: https://pris.ly/d/prisma-schema + +// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions? +// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init + +generator client { + provider = "prisma-client-js" + output = "../node_modules/@prisma/mongo_client" +} + +datasource db { + provider = "mongodb" + url = env("MONGODB_DATABASE_URL") +} + +model idea { + id String @id @default(auto()) @map("_id") @db.ObjectId + title String + description String + author_username String + author_user_Id String + media Json? + upvotes_count Int @default(0) + downvotes_count Int @default(0) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + idea_comments idea_comments[] + upvotes upvotes[] + downvotes downvotes[] +} + +model idea_comments { + id String @id @default(auto()) @map("_id") @db.ObjectId + author_username String + author_user_Id String + content String + upvotes_count Int @default(0) + downvotes_count Int @default(0) + media Json? + idea idea @relation(fields: [ideaId], references: [id]) + ideaId String @db.ObjectId + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + upvotes upvotes[] + downvotes downvotes[] +} + +enum POST_TYPE { + COMMON_POST + COMMENT_POST +} + +model upvotes { + id String @id @default(auto()) @map("_id") @db.ObjectId + author_username String + author_user_Id String + POST_TYPE POST_TYPE + createdAt DateTime @default(now()) + idea idea? @relation(fields: [ideaId], references: [id]) + ideaId String? @db.ObjectId + comment idea_comments? @relation(fields: [commentId], references: [id]) + commentId String? @db.ObjectId +} + +model downvotes { + id String @id @default(auto()) @map("_id") @db.ObjectId + author_username String + author_user_Id String + POST_TYPE POST_TYPE + createdAt DateTime @default(now()) + idea idea? @relation(fields: [ideaId], references: [id]) + ideaId String? @db.ObjectId + comment idea_comments? @relation(fields: [commentId], references: [id]) + commentId String? @db.ObjectId +} diff --git a/packages/database/prisma/schema_pg.prisma b/packages/database/prisma/schema_pg.prisma new file mode 100644 index 0000000..510640b --- /dev/null +++ b/packages/database/prisma/schema_pg.prisma @@ -0,0 +1,85 @@ +// This is your Prisma schema file, +// learn more about it in the docs: https://pris.ly/d/prisma-schema + +// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions? +// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init + +generator client { + provider = "prisma-client-js" + output = "../node_modules/@prisma/pg_client" +} + +datasource db { + provider = "postgresql" + url = env("POSTGRESQL_DATABASE_URL") +} + +model User { + id String @id @default(cuid()) + name String? + email String @unique + emailVerified DateTime? + image String? + accounts Account[] + sessions Session[] + // Optional for WebAuthn support + Authenticator Authenticator[] + + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt +} + +model Account { + userId String + type String + provider String + providerAccountId String + refresh_token String? + access_token String? + expires_at Int? + token_type String? + scope String? + id_token String? + session_state String? + + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + + @@id([provider, providerAccountId]) +} + +model Session { + sessionToken String @unique + userId String + expires DateTime + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt +} + +model VerificationToken { + identifier String + token String + expires DateTime + + @@id([identifier, token]) +} + +// Optional for WebAuthn support +model Authenticator { + credentialID String @unique + userId String + providerAccountId String + credentialPublicKey String + counter Int + credentialDeviceType String + credentialBackedUp Boolean + transports String? + + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + + @@id([userId, credentialID]) +} diff --git a/packages/database/src/index.ts b/packages/database/src/index.ts new file mode 100644 index 0000000..9b1cd16 --- /dev/null +++ b/packages/database/src/index.ts @@ -0,0 +1,2 @@ +export { PG_PRISMA_CLIENT } from "./lib/pg_db"; +export { MONGO_PRISMA_CLIENT } from "./lib/mongo_db"; diff --git a/packages/database/src/lib/mongo_db.ts b/packages/database/src/lib/mongo_db.ts new file mode 100644 index 0000000..e3e1e21 --- /dev/null +++ b/packages/database/src/lib/mongo_db.ts @@ -0,0 +1,15 @@ +import { PrismaClient } from "@prisma/mongo_client"; + +const prismaClientSingleton = () => { + return new PrismaClient(); +}; + +declare const globalThis: { + mongo_prismaGlobal: ReturnType; +} & typeof global; + +export const MONGO_PRISMA_CLIENT = + globalThis.mongo_prismaGlobal ?? prismaClientSingleton(); + +if (process.env.NODE_ENV !== "production") + globalThis.mongo_prismaGlobal = MONGO_PRISMA_CLIENT; diff --git a/packages/database/src/lib/pg_db.ts b/packages/database/src/lib/pg_db.ts new file mode 100644 index 0000000..b0c44ff --- /dev/null +++ b/packages/database/src/lib/pg_db.ts @@ -0,0 +1,15 @@ +import { PrismaClient } from "@prisma/pg_client"; + +const prismaClientSingleton = () => { + return new PrismaClient(); +}; + +declare const globalThis: { + PG_prismaGlobal: ReturnType; +} & typeof global; + +export const PG_PRISMA_CLIENT = + globalThis.PG_prismaGlobal ?? prismaClientSingleton(); + +if (process.env.NODE_ENV !== "production") + globalThis.PG_prismaGlobal = PG_PRISMA_CLIENT; diff --git a/packages/database/tsconfig.json b/packages/database/tsconfig.json new file mode 100644 index 0000000..7f62d3f --- /dev/null +++ b/packages/database/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "@repo/typescript-config/base.json", + "compilerOptions": { + "outDir": "dist", + "rootDir": "src", + "baseUrl": ".", + "paths": { + "@/*": [ + "./*" + ] + } + } +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e3638f3..aa4cf2b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -20,6 +20,9 @@ importers: apps/web: dependencies: + '@repo/database': + specifier: workspace:* + version: link:../../packages/database '@repo/ui': specifier: workspace:* version: link:../../packages/ui @@ -67,6 +70,25 @@ importers: specifier: ^5 version: 5.6.2 + packages/database: + dependencies: + '@prisma/client': + specifier: ^5.20.0 + version: 5.20.0(prisma@5.20.0) + '@repo/typescript-config': + specifier: workspace:* + version: link:../typescript-config + devDependencies: + '@types/node': + specifier: ^20 + version: 20.16.5 + prisma: + specifier: ^5.20.0 + version: 5.20.0 + tsup: + specifier: ^8.3.0 + version: 8.3.0(postcss@8.4.47)(typescript@5.6.2) + packages/eslint-config: devDependencies: '@typescript-eslint/eslint-plugin': @@ -782,6 +804,46 @@ packages: engines: {node: ^12.20.0 || ^14.18.0 || >=16.0.0} dev: true + /@prisma/client@5.20.0(prisma@5.20.0): + resolution: {integrity: sha512-CLv55ZuMuUawMsxoqxGtLT3bEZoa2W8L3Qnp6rDIFWy+ZBrUcOFKdoeGPSnbBqxc3SkdxJrF+D1veN/WNynZYA==} + engines: {node: '>=16.13'} + requiresBuild: true + peerDependencies: + prisma: '*' + peerDependenciesMeta: + prisma: + optional: true + dependencies: + prisma: 5.20.0 + dev: false + + /@prisma/debug@5.20.0: + resolution: {integrity: sha512-oCx79MJ4HSujokA8S1g0xgZUGybD4SyIOydoHMngFYiwEwYDQ5tBQkK5XoEHuwOYDKUOKRn/J0MEymckc4IgsQ==} + + /@prisma/engines-version@5.20.0-12.06fc58a368dc7be9fbbbe894adf8d445d208c284: + resolution: {integrity: sha512-Lg8AS5lpi0auZe2Mn4gjuCg081UZf88k3cn0RCwHgR+6cyHHpttPZBElJTHf83ZGsRNAmVCZCfUGA57WB4u4JA==} + + /@prisma/engines@5.20.0: + resolution: {integrity: sha512-DtqkP+hcZvPEbj8t8dK5df2b7d3B8GNauKqaddRRqQBBlgkbdhJkxhoJTrOowlS3vaRt2iMCkU0+CSNn0KhqAQ==} + requiresBuild: true + dependencies: + '@prisma/debug': 5.20.0 + '@prisma/engines-version': 5.20.0-12.06fc58a368dc7be9fbbbe894adf8d445d208c284 + '@prisma/fetch-engine': 5.20.0 + '@prisma/get-platform': 5.20.0 + + /@prisma/fetch-engine@5.20.0: + resolution: {integrity: sha512-JVcaPXC940wOGpCOwuqQRTz6I9SaBK0c1BAyC1pcz9xBi+dzFgUu3G/p9GV1FhFs9OKpfSpIhQfUJE9y00zhqw==} + dependencies: + '@prisma/debug': 5.20.0 + '@prisma/engines-version': 5.20.0-12.06fc58a368dc7be9fbbbe894adf8d445d208c284 + '@prisma/get-platform': 5.20.0 + + /@prisma/get-platform@5.20.0: + resolution: {integrity: sha512-8/+CehTZZNzJlvuryRgc77hZCWrUDYd/PmlZ7p2yNXtmf2Una4BWnTbak3us6WVdqoz5wmptk6IhsXdG2v5fmA==} + dependencies: + '@prisma/debug': 5.20.0 + /@radix-ui/react-compose-refs@1.1.0(@types/react@18.3.6)(react@18.3.1): resolution: {integrity: sha512-b4inOtiaOnYf9KWyO3jAeeCG6FeyfY6ldiEPanbUjWd+xIk5wZeHa8yVwmrJ2vderhu/BQvzCrJI0lHd+wIiqw==} peerDependencies: @@ -2260,7 +2322,7 @@ packages: debug: 4.3.7 enhanced-resolve: 5.17.1 eslint: 8.57.1 - eslint-module-utils: 2.11.0(@typescript-eslint/parser@6.21.0)(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1) + eslint-module-utils: 2.11.0(@typescript-eslint/parser@6.21.0)(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1) eslint-plugin-import: 2.30.0(@typescript-eslint/parser@7.18.0)(eslint@8.57.1) fast-glob: 3.3.2 get-tsconfig: 4.8.1 @@ -2303,7 +2365,7 @@ packages: - supports-color dev: true - /eslint-module-utils@2.11.0(@typescript-eslint/parser@6.21.0)(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1): + /eslint-module-utils@2.11.0(@typescript-eslint/parser@6.21.0)(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.3)(eslint@8.57.1): resolution: {integrity: sha512-gbBE5Hitek/oG6MUVj6sFuzEjA/ClzNflVrLovHi/JgLdC7fiN5gLAY1WIPW1a0V5I999MnsrvVrCOGmmVqDBQ==} engines: {node: '>=4'} peerDependencies: @@ -2327,6 +2389,7 @@ packages: '@typescript-eslint/parser': 6.21.0(eslint@8.57.1)(typescript@5.6.2) debug: 3.2.7 eslint: 8.57.1 + eslint-import-resolver-node: 0.3.9 eslint-import-resolver-typescript: 3.6.3(@typescript-eslint/parser@6.21.0)(eslint-plugin-import@2.30.0)(eslint@8.57.1) transitivePeerDependencies: - supports-color @@ -3967,6 +4030,16 @@ packages: engines: {node: '>=14'} dev: true + /prisma@5.20.0: + resolution: {integrity: sha512-6obb3ucKgAnsGS9x9gLOe8qa51XxvJ3vLQtmyf52CTey1Qcez3A6W6ROH5HIz5Q5bW+0VpmZb8WBohieMFGpig==} + engines: {node: '>=16.13'} + hasBin: true + requiresBuild: true + dependencies: + '@prisma/engines': 5.20.0 + optionalDependencies: + fsevents: 2.3.3 + /prop-types@15.8.1: resolution: {integrity: sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==} dependencies: