table.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. type: { type: String, default: 'String' }, // 字段类型,默认:String
  9. required: { type: Boolean, default: false }, // 是否必填,默认:否
  10. index: { type: Boolean, default: false },
  11. def: { type: String }, // 默认值
  12. remark: { type: String },
  13. },
  14. {
  15. _id: false,
  16. }
  17. );
  18. // 字段表
  19. const table = {
  20. name: { type: String, required: true }, // 表名
  21. name_zh: { type: String, required: true }, // 表中文名
  22. project: { type: ObjectId, required: true }, // 项目
  23. columns: [column], // 字段列表
  24. remark: { type: String },
  25. };
  26. const schema = new Schema(table, { toJSON: { virtuals: true } });
  27. schema.index({ id: 1 });
  28. schema.index({ name: 1 });
  29. schema.index({ project: 1 });
  30. schema.index({ 'meta.createdAt': 1 });
  31. schema.plugin(metaPlugin);
  32. module.exports = (app) => {
  33. const { mongoose } = app;
  34. return mongoose.model('Table', schema, 'table');
  35. };