feat: add prisma migrate

This commit is contained in:
moonrailgun 2024-05-26 02:35:33 +08:00
parent 3cf3cfa427
commit 37757f6563
9 changed files with 180 additions and 2 deletions

View File

@ -1,8 +1,11 @@
import { AuthConfig } from '@auth/core';
import Nodemailer from '@auth/core/providers/nodemailer';
import { env } from '../utils/env';
import { PrismaAdapter } from '@auth/prisma-adapter';
import { prisma } from './_client';
export const authConfig: Omit<AuthConfig, 'raw'> = {
providers: [Nodemailer],
providers: [Nodemailer(env.auth.email)],
adapter: PrismaAdapter(prisma),
secret: env.auth.secret,
};

View File

@ -0,0 +1,51 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "email" TEXT,
ADD COLUMN "emailVerified" TIMESTAMP(3),
ADD COLUMN "image" TEXT,
ADD COLUMN "name" TEXT;
-- CreateTable
CREATE TABLE "Account" (
"userId" TEXT NOT NULL,
"type" TEXT NOT NULL,
"provider" TEXT NOT NULL,
"providerAccountId" TEXT NOT NULL,
"refresh_token" TEXT,
"access_token" TEXT,
"expires_at" INTEGER,
"token_type" TEXT,
"scope" TEXT,
"id_token" TEXT,
"session_state" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "Account_pkey" PRIMARY KEY ("provider","providerAccountId")
);
-- CreateTable
CREATE TABLE "Session" (
"sessionToken" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"expires" TIMESTAMP(3) NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL
);
-- CreateTable
CREATE TABLE "VerificationToken" (
"identifier" TEXT NOT NULL,
"token" TEXT NOT NULL,
"expires" TIMESTAMP(3) NOT NULL,
CONSTRAINT "VerificationToken_pkey" PRIMARY KEY ("identifier","token")
);
-- CreateIndex
CREATE UNIQUE INDEX "Session_sessionToken_key" ON "Session"("sessionToken");
-- AddForeignKey
ALTER TABLE "Account" ADD CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Session" ADD CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -19,6 +19,10 @@ generator zod {
model User {
id String @id @unique @default(cuid()) @db.VarChar(30)
name String?
email String?
emailVerified DateTime?
image String?
username String @unique @db.VarChar(255)
password String @db.VarChar(60)
role String @db.VarChar(50)
@ -29,9 +33,50 @@ model User {
currentWorkspace Workspace @relation(fields: [currentWorkspaceId], references: [id])
accounts Account[]
sessions Session[]
workspaces WorkspacesOnUsers[]
}
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])
}
model Workspace {
id String @id @unique @default(cuid()) @db.VarChar(30)
name String @db.VarChar(100)

View File

@ -0,0 +1,32 @@
import * as z from "zod"
import * as imports from "./schemas"
import { CompleteUser, RelatedUserModelSchema } from "./index"
export const AccountModelSchema = z.object({
userId: z.string(),
type: z.string(),
provider: z.string(),
providerAccountId: z.string(),
refresh_token: z.string().nullish(),
access_token: z.string().nullish(),
expires_at: z.number().int().nullish(),
token_type: z.string().nullish(),
scope: z.string().nullish(),
id_token: z.string().nullish(),
session_state: z.string().nullish(),
createdAt: z.date(),
updatedAt: z.date(),
})
export interface CompleteAccount extends z.infer<typeof AccountModelSchema> {
user: CompleteUser
}
/**
* RelatedAccountModelSchema contains all relations on your model in addition to the scalars
*
* NOTE: Lazy required in case of potential circular dependencies within schema
*/
export const RelatedAccountModelSchema: z.ZodSchema<CompleteAccount> = z.lazy(() => AccountModelSchema.extend({
user: RelatedUserModelSchema,
}))

View File

@ -1,4 +1,7 @@
export * from "./user"
export * from "./account"
export * from "./session"
export * from "./verificationtoken"
export * from "./workspace"
export * from "./workspacesonusers"
export * from "./website"

View File

@ -0,0 +1,24 @@
import * as z from "zod"
import * as imports from "./schemas"
import { CompleteUser, RelatedUserModelSchema } from "./index"
export const SessionModelSchema = z.object({
sessionToken: z.string(),
userId: z.string(),
expires: z.date(),
createdAt: z.date(),
updatedAt: z.date(),
})
export interface CompleteSession extends z.infer<typeof SessionModelSchema> {
user: CompleteUser
}
/**
* RelatedSessionModelSchema contains all relations on your model in addition to the scalars
*
* NOTE: Lazy required in case of potential circular dependencies within schema
*/
export const RelatedSessionModelSchema: z.ZodSchema<CompleteSession> = z.lazy(() => SessionModelSchema.extend({
user: RelatedUserModelSchema,
}))

View File

@ -1,9 +1,13 @@
import * as z from "zod"
import * as imports from "./schemas"
import { CompleteWorkspace, RelatedWorkspaceModelSchema, CompleteWorkspacesOnUsers, RelatedWorkspacesOnUsersModelSchema } from "./index"
import { CompleteWorkspace, RelatedWorkspaceModelSchema, CompleteAccount, RelatedAccountModelSchema, CompleteSession, RelatedSessionModelSchema, CompleteWorkspacesOnUsers, RelatedWorkspacesOnUsersModelSchema } from "./index"
export const UserModelSchema = z.object({
id: z.string(),
name: z.string().nullish(),
email: z.string().nullish(),
emailVerified: z.date().nullish(),
image: z.string().nullish(),
username: z.string(),
password: z.string(),
role: z.string(),
@ -15,6 +19,8 @@ export const UserModelSchema = z.object({
export interface CompleteUser extends z.infer<typeof UserModelSchema> {
currentWorkspace: CompleteWorkspace
accounts: CompleteAccount[]
sessions: CompleteSession[]
workspaces: CompleteWorkspacesOnUsers[]
}
@ -25,5 +31,7 @@ export interface CompleteUser extends z.infer<typeof UserModelSchema> {
*/
export const RelatedUserModelSchema: z.ZodSchema<CompleteUser> = z.lazy(() => UserModelSchema.extend({
currentWorkspace: RelatedWorkspaceModelSchema,
accounts: RelatedAccountModelSchema.array(),
sessions: RelatedSessionModelSchema.array(),
workspaces: RelatedWorkspacesOnUsersModelSchema.array(),
}))

View File

@ -0,0 +1,8 @@
import * as z from "zod"
import * as imports from "./schemas"
export const VerificationTokenModelSchema = z.object({
identifier: z.string(),
token: z.string(),
expires: z.date(),
})

View File

@ -14,6 +14,10 @@ export const env = {
port: Number(process.env.PORT || 12345),
auth: {
secret: process.env.AUTH_SECRET || md5(jwtSecret),
email: {
server: process.env.EMAIL_SERVER,
from: process.env.EMAIL_FROM,
},
},
allowRegister: checkEnvTrusty(process.env.ALLOW_REGISTER),
allowOpenapi: checkEnvTrusty(process.env.ALLOW_OPENAPI ?? 'true'),