table.js 1.1 KB

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