2
0

🏗️ Add compatibility with different prisma clients

This commit is contained in:
Baptiste Arnaud
2023-02-08 10:51:32 +01:00
parent c879c6f83a
commit caf54321ec
65 changed files with 942 additions and 211 deletions

View File

@ -1,6 +1,6 @@
Dockerfile
.dockerignore
node_modules
**/node_modules
npm-debug.log
README.md
.next
@ -10,5 +10,5 @@ README.md
landing-page
docs
scripts
packages/scripts
wordpress

View File

@ -4,12 +4,23 @@ ARG SCOPE
ENV SCOPE=${SCOPE}
RUN npm --global install pnpm
FROM base AS pruner
RUN npm --global install turbo
WORKDIR /app
COPY . .
RUN turbo prune --scope=${SCOPE} --docker
FROM base AS builder
RUN apt-get -qy update && apt-get -qy --no-install-recommends install openssl git
COPY pnpm-lock.yaml .npmrc pnpm-workspace.yaml ./
RUN pnpm fetch
ADD . ./
RUN pnpm install -r --offline
WORKDIR /app
COPY .gitignore .gitignore
COPY .npmrc .pnpmfile.cjs ./
COPY --from=pruner /app/out/json/ .
COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
RUN pnpm install
COPY --from=pruner /app/out/full/ .
COPY turbo.json turbo.json
RUN pnpm turbo run build:docker --filter=${SCOPE}...
FROM base AS runner
@ -21,7 +32,7 @@ RUN apt-get -qy update \
&& apt-get autoremove -yq \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
COPY ./packages/db/prisma ./prisma
COPY ./packages/db ./packages/db
COPY ./apps/${SCOPE}/.env.docker ./apps/${SCOPE}/.env.production
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/apps/${SCOPE}/public ./apps/${SCOPE}/public

View File

@ -6,15 +6,15 @@ import { importTypebotInDatabase } from 'utils/playwright/databaseActions'
import { typebotViewer } from 'utils/playwright/testHelpers'
import { getTestAsset } from '@/test/utils/playwright'
const mockSmtpCredentials: SmtpCredentialsData = {
export const mockSmtpCredentials: SmtpCredentialsData = {
from: {
email: 'ted.kreiger@ethereal.email',
name: 'Ted Kreiger',
email: 'sunny.cremin66@ethereal.email',
name: 'Sunny Cremin',
},
host: 'smtp.ethereal.email',
port: 587,
username: 'ted.kreiger@ethereal.email',
password: 'bFYFHfWjFxB9MK28zS',
username: 'sunny.cremin66@ethereal.email',
password: 'yJDHkf2bYbNydaRvTq',
}
test.beforeAll(async () => {

View File

@ -7,13 +7,13 @@ import { getTestAsset } from '@/test/utils/playwright'
const mockSmtpCredentials: SmtpCredentialsData = {
from: {
email: 'ted.kreiger@ethereal.email',
name: 'Ted Kreiger',
email: 'sunny.cremin66@ethereal.email',
name: 'Sunny Cremin',
},
host: 'smtp.ethereal.email',
port: 587,
username: 'ted.kreiger@ethereal.email',
password: 'bFYFHfWjFxB9MK28zS',
username: 'sunny.cremin66@ethereal.email',
password: 'yJDHkf2bYbNydaRvTq',
}
test.beforeAll(async () => {

View File

@ -2,11 +2,18 @@
ENVSH_ENV=./apps/builder/.env.production ENVSH_OUTPUT=./apps/builder/public/__env.js bash env.sh
./node_modules/.bin/prisma generate;
if [[ $DATABASE_URL == postgresql://* ]]; then
./node_modules/.bin/prisma generate --schema=packages/db/postgresql/schema.prisma;
else
./node_modules/.bin/prisma generate --schema=packages/db/mysql/schema.prisma;
fi
echo 'Waiting 5s for db to be ready...';
sleep 5;
./node_modules/.bin/prisma migrate deploy;
if [[ $DATABASE_URL == postgresql://* ]]; then
./node_modules/.bin/prisma migrate deploy --schema=packages/db/postgresql/schema.prisma;
fi
node apps/builder/server.js;

View File

@ -1,9 +1 @@
export * from '@prisma/client'
// Named export for enums to avoid vite barrel export bug (https://github.com/nrwl/nx/issues/13704)
export {
Plan,
WorkspaceRole,
GraphNavigation,
CollaborationType,
} from '@prisma/client'

View File

@ -0,0 +1,349 @@
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}
model Account {
id String @id @default(cuid())
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String? @db.Text
session_state String?
oauth_token_secret String?
oauth_token String?
refresh_token_expires_in Int?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
@@index([userId])
}
model Session {
id String @id @default(cuid())
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
}
model User {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
lastActivityAt DateTime @default(now())
name String? @db.VarChar(255)
email String? @unique
emailVerified DateTime?
image String? @db.VarChar(1000)
company String?
onboardingCategories Json
graphNavigation GraphNavigation?
preferredAppAppearance String?
accounts Account[]
apiTokens ApiToken[]
CollaboratorsOnTypebots CollaboratorsOnTypebots[]
workspaces MemberInWorkspace[]
sessions Session[]
}
model ApiToken {
id String @id @default(cuid())
createdAt DateTime @default(now())
token String @unique
name String
ownerId String
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
@@index([ownerId])
}
model Workspace {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
name String @db.VarChar(255)
icon String? @db.VarChar(1000)
plan Plan @default(FREE)
stripeId String? @unique
credentials Credentials[]
customDomains CustomDomain[]
folders DashboardFolder[]
members MemberInWorkspace[]
typebots Typebot[]
invitations WorkspaceInvitation[]
additionalChatsIndex Int @default(0)
additionalStorageIndex Int @default(0)
chatsLimitFirstEmailSentAt DateTime?
storageLimitFirstEmailSentAt DateTime?
chatsLimitSecondEmailSentAt DateTime?
storageLimitSecondEmailSentAt DateTime?
claimableCustomPlan ClaimableCustomPlan?
customChatsLimit Int?
customStorageLimit Int?
customSeatsLimit Int?
}
model MemberInWorkspace {
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
userId String
workspaceId String
role WorkspaceRole
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
workspace Workspace @relation(fields: [workspaceId], references: [id], onDelete: Cascade)
@@unique([userId, workspaceId])
@@index([workspaceId])
}
model WorkspaceInvitation {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
email String
workspaceId String
type WorkspaceRole
workspace Workspace @relation(fields: [workspaceId], references: [id], onDelete: Cascade)
@@index([workspaceId])
}
model CustomDomain {
name String @id @db.VarChar(255)
createdAt DateTime @default(now())
workspaceId String
workspace Workspace @relation(fields: [workspaceId], references: [id], onDelete: Cascade)
@@index([workspaceId])
}
model Credentials {
id String @id @default(cuid())
createdAt DateTime @default(now())
workspaceId String
data String
name String
type String
iv String
workspace Workspace @relation(fields: [workspaceId], references: [id], onDelete: Cascade)
@@index([workspaceId])
}
model VerificationToken {
identifier String
token String @unique
expires DateTime
@@unique([identifier, token])
}
model DashboardFolder {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
name String @db.VarChar(255)
parentFolderId String?
workspaceId String
parentFolder DashboardFolder? @relation("ParentChild", fields: [parentFolderId], references: [id], onDelete: NoAction, onUpdate: NoAction)
workspace Workspace @relation(fields: [workspaceId], references: [id], onDelete: Cascade)
childrenFolder DashboardFolder[] @relation("ParentChild")
typebots Typebot[]
@@index([workspaceId])
@@index([parentFolderId])
}
model Typebot {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
icon String? @db.VarChar(1000)
name String @db.VarChar(255)
folderId String?
groups Json
variables Json
edges Json
theme Json
settings Json
publicId String? @unique
customDomain String? @unique
workspaceId String
resultsTablePreferences Json?
folder DashboardFolder? @relation(fields: [folderId], references: [id])
workspace Workspace @relation(fields: [workspaceId], references: [id], onDelete: Cascade)
collaborators CollaboratorsOnTypebots[]
invitations Invitation[]
publishedTypebot PublicTypebot?
results Result[]
webhooks Webhook[]
isArchived Boolean @default(false)
isClosed Boolean @default(false)
@@index([workspaceId])
@@index([folderId])
}
model Invitation {
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
email String
typebotId String
type CollaborationType
typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
@@unique([email, typebotId])
@@index([typebotId])
}
model CollaboratorsOnTypebots {
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
userId String
typebotId String
type CollaborationType
typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([userId, typebotId])
@@index([typebotId])
}
model PublicTypebot {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
typebotId String @unique
groups Json
variables Json
edges Json
theme Json
settings Json
typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
}
model Result {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
typebotId String
variables Json
isCompleted Boolean
hasStarted Boolean?
isArchived Boolean? @default(false)
typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
answers Answer[]
logs Log[]
@@index([typebotId])
}
model Log {
id String @id @default(cuid())
createdAt DateTime @default(now())
resultId String
status String
description String @db.Text
details String? @db.Text
result Result @relation(fields: [resultId], references: [id], onDelete: Cascade)
@@index([resultId])
}
model Answer {
createdAt DateTime @default(now()) @updatedAt
resultId String
blockId String
groupId String
variableId String?
content String
storageUsed Int?
result Result @relation(fields: [resultId], references: [id], onDelete: Cascade)
@@unique([resultId, blockId, groupId])
@@index([groupId])
}
model Coupon {
userPropertiesToUpdate Json
code String @id @unique
dateRedeemed DateTime?
}
model Webhook {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
url String? @db.VarChar(2000)
method String
queryParams Json
headers Json
body String? @db.Text
typebotId String
typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
@@index([typebotId])
}
model ClaimableCustomPlan {
id String @id @default(cuid())
createdAt DateTime @default(now())
claimedAt DateTime?
name String
description String?
price Int
currency String
workspaceId String @unique
workspace Workspace @relation(fields: [workspaceId], references: [id], onDelete: Cascade)
chatsLimit Int
storageLimit Int
seatsLimit Int
}
model ChatSession {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
state Json
}
enum WorkspaceRole {
ADMIN
MEMBER
GUEST
}
enum GraphNavigation {
MOUSE
TRACKPAD
}
enum Plan {
FREE
STARTER
PRO
LIFETIME
OFFERED
CUSTOM
UNLIMITED
}
enum CollaborationType {
READ
WRITE
FULL_ACCESS
}

View File

@ -5,19 +5,22 @@
"main": "./index.ts",
"types": "./index.ts",
"scripts": {
"dev": "cross-env BROWSER=none prisma studio",
"db:generate": "prisma generate",
"db:push": "prisma db push --skip-generate",
"db:migrate": "prisma migrate deploy",
"create:migration": "prisma migrate dev"
"dev": "tsx scripts/studio.ts",
"db:generate": "tsx scripts/db-generate.ts",
"db:push": "tsx scripts/db-push.ts",
"migrate:deploy": "tsx scripts/migrate-deploy.ts",
"migrate:dev": "tsx scripts/migrate-dev.ts",
"db:migrate": "pnpm migrate:deploy"
},
"dependencies": {
"@prisma/client": "4.9.0"
},
"devDependencies": {
"dotenv-cli": "7.0.0",
"@types/node": "18.11.18",
"dotenv": "16.0.3",
"prisma": "4.9.0",
"tsconfig": "workspace:*",
"tsx": "3.12.2",
"typescript": "4.9.4"
}
}

View File

@ -0,0 +1,3 @@
import { executePrismaCommand } from './executeCommand'
executePrismaCommand('prisma generate')

View File

@ -0,0 +1,3 @@
import { executePrismaCommand } from './executeCommand'
executePrismaCommand('prisma db push --skip-generate')

View File

@ -0,0 +1,43 @@
import { exec } from 'child_process'
import { join } from 'path'
require('dotenv').config({
override: true,
path: join(__dirname, `../.env`),
})
const postgesqlSchemaPath = join(__dirname, '../postgresql/schema.prisma')
const mysqlSchemaPath = join(__dirname, '../mysql/schema.prisma')
export const executePrismaCommand = (command: string) => {
const databaseUrl = process.env.DATABASE_URL
if (!databaseUrl) {
console.error('Could not find DATABASE_URL in environment')
return
}
if (databaseUrl?.startsWith('mysql://')) {
console.log('Executing for MySQL schema')
executeCommand(`${command} --schema ${mysqlSchemaPath}`)
}
if (databaseUrl?.startsWith('postgresql://')) {
console.log('Executing for PostgreSQL schema')
executeCommand(`${command} --schema ${postgesqlSchemaPath}`)
}
}
const executeCommand = (command: string) => {
exec(command, (error, stdout, stderr) => {
if (error) {
console.log(error.message)
return
}
if (stderr) {
console.log(stderr)
return
}
console.log(stdout)
})
}

View File

@ -0,0 +1,4 @@
import { executePrismaCommand } from './executeCommand'
if (process.env.DATABASE_URL?.startsWith('postgresql://'))
executePrismaCommand('prisma migrate deploy')

View File

@ -0,0 +1,4 @@
import { executePrismaCommand } from './executeCommand'
if (process.env.DATABASE_URL?.startsWith('postgresql://'))
executePrismaCommand('prisma migrate dev --create-only')

View File

@ -0,0 +1,3 @@
import { executePrismaCommand } from './executeCommand'
executePrismaCommand('cross-env BROWSER=none prisma studio')

661
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,10 @@
ENVSH_ENV=./apps/viewer/.env.production ENVSH_OUTPUT=./apps/viewer/public/__env.js bash env.sh
./node_modules/.bin/prisma generate;
if [[ $DATABASE_URL == postgresql://* ]]; then
./node_modules/.bin/prisma generate --schema=packages/db/postgresql/schema.prisma;
else
./node_modules/.bin/prisma generate --schema=packages/db/mysql/schema.prisma;
fi
node apps/viewer/server.js;