table.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. const Schema = require('mongoose').Schema;
  3. const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
  4. const { ObjectId } = require('mongoose').Types;
  5. const column = new Schema(
  6. {
  7. title: { type: String, required: true }, // 字段名,必须是英文
  8. zh: { type: String }, //中文
  9. type: { type: String, default: 'String' }, // 字段类型,默认:String
  10. required: { type: Boolean, default: false }, // 是否必填,默认:否
  11. index: { type: Boolean, default: false },
  12. def: { type: String }, // 默认值
  13. ref: { type: String }, //关联表名
  14. getProp: { type: String }, //ref获取的属性; 使用分号(;)分隔
  15. remark: { type: String },
  16. },
  17. {
  18. _id: false,
  19. }
  20. );
  21. // 字段表
  22. const table = {
  23. name: { type: String, required: true }, // 表名
  24. name_zh: { type: String, required: true }, // 表中文名
  25. project: { type: ObjectId, required: true }, // 项目
  26. columns: [column], // 字段列表
  27. remark: { type: String },
  28. };
  29. const schema = new Schema(table, { toJSON: { virtuals: true } });
  30. schema.index({ id: 1 });
  31. schema.index({ name: 1 });
  32. schema.index({ project: 1 });
  33. schema.index({ 'meta.createdAt': 1 });
  34. schema.plugin(metaPlugin);
  35. module.exports = (app) => {
  36. const { mongoose } = app;
  37. return mongoose.model('Table', schema, 'table');
  38. };