45 lines
2.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* NVR 录像机表模型(nvrs)
*
* 一台 NVR = 一个地址 + 一个登录账号。密码**只存密文**(`password_enc`,AES-256-GCM),
* 接口层永不返回;需要调用设备时由 service 解密。
*
* `serial_number` / `channel_count` / `last_sync_at` 是「最近一次拉取通道」的回填信息,
* 拉取动作由前端调用**本机软件**(/api/hik/channels)完成,后端只落库。
*/
module.exports = (app) => {
const { BIGINT, STRING, SMALLINT, INTEGER, TINYINT, DATE } = app.Sequelize;
const Nvr = app.model.define('nvr', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
subjectId: { type: BIGINT.UNSIGNED, comment: '所属主体 subjects.id' },
userId: { type: BIGINT.UNSIGNED, comment: '归属用户 users.id' },
name: { type: STRING(64), allowNull: false, defaultValue: '', comment: '名称 / 别名' },
host: { type: STRING(128), allowNull: false, comment: '设备地址(IP 或域名)' },
port: { type: SMALLINT.UNSIGNED, allowNull: false, defaultValue: 8000, comment: 'SDK 服务端口' },
username: { type: STRING(64), allowNull: false, defaultValue: 'admin', comment: '登录账号' },
passwordEnc: { type: STRING(512), comment: '密码密文(AES-256-GCM)' },
serialNumber: { type: STRING(64), comment: '设备序列号' },
// 属性名不用 model,避免和 Sequelize 的 model 概念混淆;字段名仍是 model
deviceModel: { type: STRING(64), field: 'model', comment: '设备型号' },
channelCount: { type: INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '最近同步到的通道数' },
lastSyncAt: { type: DATE, comment: '最近一次拉取通道时间' },
lastError: { type: STRING(255), comment: '最近一次失败原因' },
status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1正常 0停用' },
remark: { type: STRING(255), comment: '备注' },
}, {
tableName: 'nvrs',
paranoid: true,
comment: 'NVR 录像机表',
});
Nvr.associate = function() {
app.model.Nvr.belongsTo(app.model.User, { foreignKey: 'user_id', as: 'user' });
app.model.Nvr.hasMany(app.model.Camera, { foreignKey: 'nvr_id', as: 'cameras' });
};
return Nvr;
};