Skip to content

Commit

Permalink
INIT:monorepo database package
Browse files Browse the repository at this point in the history
  • Loading branch information
anisharaz committed Oct 14, 2024
1 parent 0e16ab0 commit 7d57baf
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ yarn-error.log*
# Misc
.DS_Store
*.pem

migrations
8 changes: 6 additions & 2 deletions apps/web/app/page.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<div className="text-red-500">Hello world!!</div>
<div>{user?.id}</div>
<div>{data?.id}</div>
<Button>Button</Button>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"@repo/ui": "workspace:*",
"@repo/database": "workspace:*",
"next": "14.2.6",
"react": "18.3.1",
"react-dom": "18.3.1"
Expand Down
2 changes: 2 additions & 0 deletions packages/database/.env-example
Original file line number Diff line number Diff line change
@@ -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"
28 changes: 28 additions & 0 deletions packages/database/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
76 changes: 76 additions & 0 deletions packages/database/prisma/schema_mongo.prisma
Original file line number Diff line number Diff line change
@@ -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
}
85 changes: 85 additions & 0 deletions packages/database/prisma/schema_pg.prisma
Original file line number Diff line number Diff line change
@@ -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])
}
2 changes: 2 additions & 0 deletions packages/database/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { PG_PRISMA_CLIENT } from "./lib/pg_db";
export { MONGO_PRISMA_CLIENT } from "./lib/mongo_db";
15 changes: 15 additions & 0 deletions packages/database/src/lib/mongo_db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PrismaClient } from "@prisma/mongo_client";

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

declare const globalThis: {
mongo_prismaGlobal: ReturnType<typeof prismaClientSingleton>;
} & typeof global;

export const MONGO_PRISMA_CLIENT =
globalThis.mongo_prismaGlobal ?? prismaClientSingleton();

if (process.env.NODE_ENV !== "production")
globalThis.mongo_prismaGlobal = MONGO_PRISMA_CLIENT;
15 changes: 15 additions & 0 deletions packages/database/src/lib/pg_db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PrismaClient } from "@prisma/pg_client";

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

declare const globalThis: {
PG_prismaGlobal: ReturnType<typeof prismaClientSingleton>;
} & typeof global;

export const PG_PRISMA_CLIENT =
globalThis.PG_prismaGlobal ?? prismaClientSingleton();

if (process.env.NODE_ENV !== "production")
globalThis.PG_prismaGlobal = PG_PRISMA_CLIENT;
13 changes: 13 additions & 0 deletions packages/database/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "@repo/typescript-config/base.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"baseUrl": ".",
"paths": {
"@/*": [
"./*"
]
}
}
}
Loading

0 comments on commit 7d57baf

Please sign in to comment.