# webrtc-server **Repository Path**: hlb8055/webrtc-server ## Basic Information - **Project Name**: webrtc-server - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-10-11 - **Last Updated**: 2026-05-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #### 一、运行环境 #### 1.安装 `npm install` #### 2. 配置 .env 文件 #### 3. 初始化 `npm run init` #### 4.启动 `npm run dev` #### 二、PM2生产环境部署 `npm run build:prod` #### 三、切换镜像源: 使用镜像源可能有助于解决一些网络问题。可以尝试切换到其他可用的镜像源。例如,registry 配置为淘宝镜像: `npm config set registry https://registry.npm.taobao.org` #### 四、数据库 #### 1.创建第一个模型(和迁移),如:创建一个名为User的模型 ` npx sequelize-cli model:generate --name User --attributes username:string,email:string ` 生成如 migrations/20231212095233-create-user.js迁移文件 ` ``` 'use strict'; /** @type {import('sequelize-cli').Migration} */ module.exports = { async up(queryInterface, Sequelize) { await queryInterface.createTable('Users', { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER }, username: { type: Sequelize.STRING }, email: { type: Sequelize.STRING }, createdAt: { allowNull: false, type: Sequelize.DATE }, updatedAt: { allowNull: false, type: Sequelize.DATE } }); }, async down(queryInterface, Sequelize) { await queryInterface.dropTable('Users'); } }; ``` 在 src/models下生成模型文件 user.js,重命名模型为大驼峰,继承BaseModel.js ``` 'use strict' import { DataTypes } from 'sequelize' import sequelize from '../utils/database.js' import BaseModel from './BaseModel.js' import { parseTime } from '../utils/helper.js' class User extends BaseModel { static associate(models) { // 定义一对一关联 this.hasOne(models.Sms, { as: 'sms', foreignKey: 'user_id' }) // 定义一对多关联 // this.hasMany(models.Sms, { as: 'bb', foreignKey: 'user_id' }) } } User.init({ id: { type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true, }, username: { type: DataTypes.STRING, allowNull: false }, email: { type: DataTypes.STRING, allowNull: false, unique: true }, created_at: { type: DataTypes.DATE, get() { const rawValue = this.getDataValue('created_at') const formattedDate = parseTime(rawValue, '{y}-{m}-{d} {h}:{i}:{s}'); return formattedDate } }, updated_at: { type: DataTypes.DATE, get() { const rawValue = this.getDataValue('created_at') const formattedDate = parseTime(rawValue, '{y}-{m}-{d} {h}:{i}:{s}'); return formattedDate } }, }, { sequelize, modelName: 'User', tableName: 'user' }) export default User ``` #### 2 生成迁移 `npx sequelize-cli db:migrate` #### 3、增删改查 BaseModel实现增删改查 以下where支持的查询条件,比较复杂的查询单独实现 列参数若获取全部字段传空数组 ``` id: 1, // id = 1 id: ['in', [1, 2]], // id in (1, 2) project_id: ['notIn', [1, 2]], // id NOT IN (1, 2) id: ['<>', 1], // id != 1 id: ['>', 1] // id > 1 id: ['>=', 1] // id >= 1 id: ['<', 1] // id < 1 id: ['<=', 1] // id <= 1 created_at: ['between', ['2023-01-13 13:51:05', '2023-04-09 13:51:05']], // created_at BETWEEN '2023-01-13 05:51:05' AND '2023-04-09 05:51:05' updated_at: ['notBetween', ['2023-01-13 13:51:05', '2023-04-09 13:51:05']], // updated_at NOT BETWEEN '2023-01-13 05:51:05' AND '2023-04-09 05:51:05' username: ['like', '%张三%'], // username LIKE '%张三%' aa: [ // (id = 1 OR username LIKE '%小夏%') 注:key可以自定义 'or', { id: 1 }, { username: ['like', '%张三%'] } ], aa: [ (id > 1 AND id < 7) 注:key可以自定义 'and', { id: ['>', 1] }, { id: ['<', 7] } ] id: ['isnull'] // id IS NULL age: ['or', [5, 6]] // (age = 5 OR age = 6) ``` 关联关系 ``` 1、一对一 this.hasOne(Sms, { as: 'sms', foreignKey: 'user_id' }) Sms.belongsTo(this, { foreignKey: 'user_id' }) ``` ``` 2、一对多 this.hasMany(Sms, { as: 'sms', foreignKey: 'user_id' }) Sms.belongsTo(this, { foreignKey: 'user_id' }) ``` include参数 ``` [ { where: { user_id: 1 }, // 和以上where结构一致 model: Sms, // 关联的模型 required: true, // 为false指定 LEFT OUTER JOIN as: 'sms' // 别名 } ] ``` 1、通过主键新增或更新一条数据,未传主键为新增 ``` await User.saveData({ username: '测试1', email: '123@qq.com' }) ``` 2、批量新增数据 ``` await User.createManyData([ { username: '测试1', email: '123@qq.com' }, { username: '测试2', email: '123@qq.com' } ]) ``` 3、根据条件更新一条数据 ``` await User.updateOneBy(where = {}, { username: '测试1', }) ``` 4、 根据条件更新多条数据 ``` await User.updateBy(where = {}, { username: '测试1', }) ``` 5、根据条件删除数据 ``` await User.deleteByWhere(where = {}) ``` 6、获取数据总数量 ``` await User.getCount({ where: {}, include: [] }) ``` 7、聚合sum,比如获取条件内年龄相加 ``` await User.getSum({ where: {}, include: [] }, 'age') ``` 8、通过主键获取一列数据 第一个参数为主键ID,第二个参数为获取的列 ``` await User.getById(4, ['id']) ``` 9、根据条件获取一条数据 第二个参数是获取的列,第三个参数是排序 ``` await User.findOneBy({ where: {}, include: [] }, ['id'], [['id', 'ASC'], ['username', 'DESC']]) ``` 10、获取多条数据 第二个参数为获取得列,第三个参数页码,第四个为每页条数,第五个排序,第六个分组,第七个为是否获取总数 ``` await User.getList({ where: {}, include: [] }, ['id', 'username'], 1, 5, [['id', 'DESC'], ['username', 'DESC']], ['id', 'username'], false) ``` #### 五、验证 在src/validators创建验证文件 user.js ``` import ValidationModule from '../utils/lin-validator-v2.js' const { Rule, LinValidator } = ValidationModule class UserLoginValidator extends LinValidator { constructor() { super() this.mobile = [ new Rule('isLength', '请输入手机号码', { min: 1 }), new Rule( 'matches', '手机号格式不正确', /^1[3456789]\d{9}$/ ) ] } } export default { UserLoginValidator } ``` 在控制器中使用 ``` 导入 import UserValidator from '../validators/user.js' const { UserLoginValidator } = UserValidator 使用 const v = await new UserLoginValidator().validate(ctx) ``` #### 六、Swagger使用 编写swagger ``` /** * @swagger * /api/login: * post: * tags: * - 用户 * summary: 用户登录 * description: 用户登录 * requestBody: * content: * application/json: * schema: * type: object * required: * - mobile * - type * properties: * mobile: * type: string * description: 手机号 * responses: * 200: * description: 成功响应 * content: * application/json: * schema: * type: object * properties: * errcode: * type: integer * description: 0成功 非0失败 * errmsg: * type: string * description: 描述 * data: * type: object * description: 信息返回 * required: * - access_token * - refresh_token * properties: * access_token: * type: string * description: 登录凭证 * refresh_token: * type: string * description: 刷新Token */ router.post('/login', (new UserController).login) ``` 生成swagger文档 ``` node script/generate-swagger.js ```