Skip to content

Commit

Permalink
🔄 Merge pull request #3 from steeeee0223/feature/prisma
Browse files Browse the repository at this point in the history
Support database connections with prisma
  • Loading branch information
steeeee0223 authored Jan 23, 2024
2 parents 96bd669 + 7e4f6ba commit 1de058f
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ yarn-error.log*
.turbo

# storybook
storybook-static/
storybook-static/

# generated
.generated
8 changes: 8 additions & 0 deletions packages/prisma/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DB_AUTH='postgresql://johndoe:randompassword@localhost:5432/auth?schema=public'
DB_WORXPACE='postgresql://johndoe:randompassword@localhost:5432/worxpace?schema=public'
40 changes: 40 additions & 0 deletions packages/prisma/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@acme/prisma",
"private": true,
"version": "0.1.0",
"type": "module",
"exports": {
".": "./src/index.ts"
},
"license": "MIT",
"scripts": {
"build": "pnpm db:format && pnpm db:generate && pnpm db:push",
"clean": "rm -rf .turbo node_modules",
"db:format": "bash ./scripts/prisma.sh format",
"db:generate": "bash ./scripts/prisma.sh generate",
"db:push": "bash ./scripts/prisma.sh db push --skip-generate",
"dev": "prisma studio",
"format": "prettier --check . --ignore-path ../../.gitignore",
"lint": "eslint .",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@acme/eslint-config": "workspace:^0.2.0",
"@acme/prettier-config": "workspace:^0.1.0",
"@acme/tsconfig": "workspace:^0.1.0",
"@types/node": "^20.10.6",
"eslint": "^8.56.0",
"prettier": "^3.1.1",
"prisma": "^5.8.1",
"typescript": "^5.3.3"
},
"eslintConfig": {
"extends": [
"@acme/eslint-config/base"
]
},
"prettier": "@acme/prettier-config",
"dependencies": {
"@prisma/client": "^5.8.1"
}
}
40 changes: 40 additions & 0 deletions packages/prisma/prisma/auth.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
output = "../.generated/auth"
}

datasource db {
provider = "mongodb"
url = env("DB_AUTH")
}

model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String?
email String? @unique
emailVerified DateTime?
image String?
password String?
accounts Account[]
}

model Account {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String @db.ObjectId
type String
provider String
providerAccountId String
refresh_token String? @db.String
access_token String? @db.String
expires_at Int?
token_type String?
scope String?
id_token String? @db.String
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
}
141 changes: 141 additions & 0 deletions packages/prisma/prisma/worxpace.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
output = "../.generated/worxpace"
}

datasource db {
provider = "mongodb"
url = env("DB_WORXPACE")
}

type Image {
imageId String
thumbUrl String
fullUrl String
username String
linkHTML String
}

model Board {
id String @id @default(auto()) @map("_id") @db.ObjectId
clientId String
title String
image Image?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
List List[]
@@index([clientId])
}

model List {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String
order Int
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
boardId String @db.ObjectId
board Board @relation(fields: [boardId], references: [id], onDelete: Cascade)
cards Card[]
@@index([boardId])
}

model Card {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String
order Int
description String? @db.String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
listId String @db.ObjectId
list List @relation(fields: [listId], references: [id], onDelete: Cascade)
@@index([listId])
}

enum ACTION {
CREATE
UPDATE
DELETE
}

enum ENTITY_TYPE {
BOARD
LIST
CARD
DOCUMENT
}

type User {
userId String
name String @db.String
image String
}

type Entity {
entityId String @db.ObjectId
type ENTITY_TYPE
title String
}

model AuditLog {
id String @id @default(auto()) @map("_id") @db.ObjectId
user User
orgId String?
role ROLE
action ACTION
entity Entity
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([role, user.userId], map: "role_user_id")
@@index([entity.entityId])
}

enum ROLE {
USER
ORG
}

model Limitation {
id String @id @default(auto()) @map("_id") @db.ObjectId
role ROLE
clientId String @unique
count Int @default(0)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model Subscription {
id String @id @default(auto()) @map("_id") @db.ObjectId
role ROLE
clientId String @unique
stripeCustomerId String? @unique @map(name: "stripe_customer_id")
stripeSubscriptionId String? @unique @map(name: "stripe_subscription_id")
stripePriceId String? @map(name: "stripe_price_id")
stripeCurrentPeriodEnd DateTime? @map(name: "stripe_current_period_end")
}

model Document {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String
clientId String
isArchived Boolean
parentId String? @db.ObjectId
content String?
coverImage String?
icon String?
isPublished Boolean
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
parent Document? @relation("DocumentParent", onUpdate: NoAction, onDelete: NoAction, fields: [parentId], references: [id])
children Document[] @relation("DocumentParent")
@@index([clientId], name: "byClient")
@@index([clientId, parentId], name: "byClientParent")
}
8 changes: 8 additions & 0 deletions packages/prisma/scripts/prisma.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
prisma-action() {
echo "> Running prisma $@"
for file in ./prisma/*.prisma; do
prisma $@ --schema $file
done
}

prisma-action $@
19 changes: 19 additions & 0 deletions packages/prisma/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable no-var */
import { PrismaClient as Auth } from "../.generated/auth";
import { PrismaClient as Worxpace } from "../.generated/worxpace";

declare global {
var authClient: Auth | undefined;
var worxpaceClient: Worxpace | undefined;
}

export * from "@prisma/client";

/** Schemas */
export const auth = globalThis.authClient ?? new Auth();
export const worxpace = globalThis.worxpaceClient ?? new Worxpace();

if (process.env.NODE_ENV !== "production") {
globalThis.authClient = auth;
globalThis.worxpaceClient = worxpace;
}
8 changes: 8 additions & 0 deletions packages/prisma/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@acme/tsconfig/base.json",
"compilerOptions": {
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": ["*.ts", "src"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 1de058f

Please sign in to comment.