12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- 'use strict';
- const Schema = require('mongoose').Schema;
- const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
- const { ObjectId } = require('mongoose').Types;
- const column = new Schema(
- {
- title: { type: String, required: true }, // 字段名,必须是英文
- zh: { type: String }, //中文
- type: { type: String, default: 'String' }, // 字段类型,默认:String
- required: { type: Boolean, default: false }, // 是否必填,默认:否
- index: { type: Boolean, default: false },
- def: { type: String }, // 默认值
- ref: { type: String }, //关联表名
- getProp: { type: String }, //ref获取的属性; 使用分号(;)分隔
- remark: { type: String },
- },
- {
- _id: false,
- }
- );
- // 字段表
- const table = {
- name: { type: String, required: true }, // 表名
- name_zh: { type: String, required: true }, // 表中文名
- dir: { type: String }, // 文件夹
- project: { type: ObjectId, required: true }, // 项目
- columns: [column], // 字段列表
- sort: { type: Number, default: 0 },
- remark: { type: String },
- };
- const schema = new Schema(table, { toJSON: { virtuals: true } });
- schema.index({ id: 1 });
- schema.index({ name: 1 });
- schema.index({ project: 1 });
- schema.index({ sort: 1 });
- schema.index({ 'meta.createdAt': 1 });
- schema.plugin(metaPlugin);
- module.exports = (app) => {
- const { mongoose } = app;
- return mongoose.model('Table', schema, 'table');
- };
|