快速开始
准备
nodejs 版本高于 v14.17.0
1 创建项目
# 创建文件夹
mkdir hello-prisma
cd hello-prisma
# 初始化
npm init -y
# 安装依赖
npm install typescript ts-node @types/node --save-dev
配置 typescript
touch tsconfig.json
{
"compilerOptions": {
"sourceMap": true,
"outDir": "dist",
"strict": true,
"lib": ["esnext"],
"esModuleInterop": true
}
}
安装依赖
npm install prisma --save-dev
初始化数据源
npx prisma init --datasource-provider sqlite
2 创建模型
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}