express + ts
1. 配置prisma(读音:普锐斯吗)
1. 安装
            
            
              shell
              
              
            
          
          mkdir hello-prisma
cd hello-prisma
npm init -y
npm install prisma typescript ts-node @types/node --save-dev
        
            
            
              shell
              
              
            
          
          pnpm init
pnpm add prisma typescript ts-node @types/node --save-dev
        2. 创建tsconfig.json文件
        
            
            
              typescript
              
              
            
          
          {
  "compilerOptions": {
    "sourceMap": true,
    "outDir": "dist",
    "strict": true,
    "lib": ["esnext"],
    "esModuleInterop": true
  }
}
        3. 初始化prisma项目
            
            
              shell
              
              
            
          
          npx prisma
npx prisma init
# 创建了一个名为prisma的新目录,其中包含一个名为schema.prisma的文件和一个位于项目根目录中的.env文件
        - 此时的目录结构

 
4. 配置数据库
- prisma/schema.prisma
 
            
            
              typescript
              
              
            
          
          datasource db {
  provider = "mysql"	// 使用mysql的需要修改
  url      = env("DATABASE_URL")
}
        - .env
 
            
            
              typescript
              
              
            
          
          DATABASE_URL="mysql://johndoe:randompassword@localhost:3306/mydb"
DATABASE_URL="mysql://用户名:密码@数据库地址:3306/库名"
        5. vscode安装插件


2. 使用prisma
1. 数据模型映射到数据库
- prisma/schema.prisma 添加表数据
 
            
            
              javascript
              
              
            
          
          model Post {  // 著作
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  title     String   @db.VarChar(255)
  content   String?
  published Boolean  @default(false)
  author    User     @relation(fields: [authorId], references: [id])  // 外键
  authorId  Int // 关联id
}
model Profile { // 个人简介
  id     Int     @id @default(autoincrement())
  bio    String?
  user   User    @relation(fields: [userId], references: [id])
  userId Int     @unique
}
model User {  // 作者
  id      Int      @id @default(autoincrement())
  email   String   @unique
  name    String?
  posts   Post[]  // 一对多
  profile Profile?  // 一对一
}
        - 数据模型映射到数据库
 
            
            
              shell
              
              
            
          
          npx prisma migrate dev --name [init:自己起个名字]
        - 目录结构

 - 数据库生成对应的表

 
2. 使用express
- 安装express
 
            
            
              shell
              
              
            
          
          pnpm add express
# express报错,安装express类型声明
pnpm add @types/express
        - 安装@prisma/client
 
            
            
              shell
              
              
            
          
          pnpm add @prisma/client
# pnpm 安装 @prisma/client,好像不会自动调用 prisma generate 
npx prisma generate
        - 创建 src/app.ts文件
 
            
            
              ts
              
              
            
          
          import express from "express";
import { PrismaClient } from "@prisma/client";
const app = express();
const prisma = new PrismaClient();
app.use(express.json());
const port = 3000;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
        - 运行express
 
            
            
              powershell
              
              
            
          
          nodemon src/app.ts
        
            
            
              powershell
              
              
            
          
          # 如果没有nodemon,全局安装
pnpm i nodemon -g
        - 遇到的报错
 
            
            
              powershell
              
              
            
          
          # 如果报错:throw new Error('@prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.');
npx prisma generate
        - 遇到的错误(不要修改package.json中的type=model):
- 解决办法 : 链接
 
 
            
            
              powershell
              
              
            
          
          TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"