table.js 1.1 KB

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