1.全局安装nest
bash
pnpm add -g @nestjs/cli nodemon ts-node
2.创建nest项目
bash
nest new project-name
3.安装依赖
bash
pnpm add prisma-binding ts-node @prisma/client mockjs @nestjs/config class-validator class-transformer argon2 @nestjs/passport passport passport-local @nestjs/jwt passport-jwt lodash multer dayjs express redis @nestjs/throttler mockjs @nestjs/cache-manager cache-manager md5 @casl/prisma @casl/ability
bash
pnpm add -D prisma typescript @types/node @types/mockjs @nestjs/mapped-types @types/passport-local @types/passport-jwt @types/express @types/lodash @types/multer @types/cache-manager @types/md5
4.prisma初始化
bash
npx prisma init
5.配置package.json的prisma
package.json
bash
"prisma": {
"seed": "ts-node prisma/seed.ts"
},
顺便把scripts里的start:dev 改成 -> dev
6.配置prisma数据库
进入.env文件
bash
DATABASE_URL="mysql://root:111111@localhost:3306/nesttest"
7.配置prisma
prisma -> schema.prisma
TypeScript
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql" //这里改成自己的数据库
url = env("DATABASE_URL")
}
8.删一些不必要文件
src
app.module.ts
main.ts
src里只留这两个
9.配置项目别名
进入tsconfig.json文件
TypeScript
"paths":{
"@/*":["src/*"]
}
------------------------------------------------------分割线------------------------------------------------------
运行prisma
prisma
schema.prisma
seed.ts (创建一个seed.ts)
然后
bash
npx prisma migrate dev