init commit
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "public"."User" (
|
||||
"id" TEXT NOT NULL,
|
||||
"username" TEXT NOT NULL,
|
||||
"password" TEXT NOT NULL,
|
||||
"avatar" BYTEA,
|
||||
"avatarMime" TEXT,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "public"."CircuitModel" (
|
||||
"id" TEXT NOT NULL,
|
||||
"title" TEXT NOT NULL,
|
||||
"authorId" TEXT NOT NULL,
|
||||
"desc" TEXT NOT NULL,
|
||||
"model" JSONB NOT NULL,
|
||||
"preview" BYTEA,
|
||||
"previewMime" TEXT,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "CircuitModel_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "User_username_key" ON "public"."User"("username");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "CircuitModel_authorId_idx" ON "public"."CircuitModel"("authorId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "CircuitModel_createdAt_idx" ON "public"."CircuitModel"("createdAt");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "public"."CircuitModel" ADD CONSTRAINT "CircuitModel_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "public"."User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (e.g., Git)
|
||||
provider = "postgresql"
|
||||
@@ -0,0 +1,38 @@
|
||||
// Prisma schema for circuit-virtual-lab-server
|
||||
// PostgreSQL provider, use DATABASE_URL from .env
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(cuid())
|
||||
username String @unique
|
||||
password String // bcrypt hash
|
||||
avatar Bytes? // store binary avatar
|
||||
avatarMime String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
models CircuitModel[]
|
||||
}
|
||||
|
||||
model CircuitModel {
|
||||
id String @id @default(cuid())
|
||||
title String
|
||||
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
|
||||
authorId String
|
||||
desc String // markdown
|
||||
model Json // JSON model data
|
||||
preview Bytes? // binary preview image
|
||||
previewMime String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index([authorId])
|
||||
@@index([createdAt])
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import 'dotenv/config';
|
||||
import { prisma } from '../src/lib/prisma';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
async function main() {
|
||||
// Ensure an initial user
|
||||
const username = 'admin';
|
||||
const password = 'admin123';
|
||||
const hash = await bcrypt.hash(password, 10);
|
||||
const user = await prisma.user.upsert({
|
||||
where: { username },
|
||||
create: { username, password: hash },
|
||||
update: {},
|
||||
});
|
||||
|
||||
// Insert sample models
|
||||
const tinyPng = Buffer.from(
|
||||
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMBA7P6sGQAAAAASUVORK5CYII=',
|
||||
'base64'
|
||||
);
|
||||
|
||||
const samples = [
|
||||
{
|
||||
title: '直流电桥测电阻(单臂)',
|
||||
desc: '使用直流电桥进行未知电阻测量的经典实验。',
|
||||
model: { type: 'dc_bridge_single_arm', version: 1 },
|
||||
},
|
||||
{
|
||||
title: 'RC 充放电实验',
|
||||
desc: '研究 RC 电路的充放电过程与时间常数。',
|
||||
model: { type: 'rc_charge_discharge', version: 1 },
|
||||
},
|
||||
];
|
||||
|
||||
for (const s of samples) {
|
||||
await prisma.circuitModel.create({
|
||||
data: {
|
||||
title: s.title,
|
||||
desc: s.desc,
|
||||
model: s.model as any,
|
||||
preview: tinyPng,
|
||||
previewMime: 'image/png',
|
||||
authorId: user.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
console.log('Seed completed. Admin user:', username, 'password:', password);
|
||||
}
|
||||
|
||||
main().catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user