race.js 1.1 KB

123456789101112131415161718192021222324252627282930
  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 source = 'race';
  6. // 表
  7. const race = {
  8. title: { type: String, zh: '比赛名称' },
  9. user: { type: String, zh: '用户', ref: 'base.User', getProp: [ 'name' ] },
  10. ground: { type: String, zh: '场地', ref: 'Ground', getProp: [ 'name' ] },
  11. player: { type: String, zh: '选手', refPath: 'player_from', getProp: [ 'name' ] },
  12. player_from: { type: String, zh: '选手数据来源' },
  13. remark: { type: String },
  14. };
  15. const schema = new Schema(race, { toJSON: { virtuals: true } });
  16. schema.index({ id: 1 });
  17. schema.index({ 'meta.createdAt': 1 });
  18. schema.plugin(metaPlugin);
  19. module.exports = app => {
  20. const is_multiple = app.mongooseDB.clients;
  21. let model;
  22. if (is_multiple) {
  23. const conn = app.mongooseDB.get(source);
  24. model = conn.model('Race', schema, 'race');
  25. } else {
  26. const { mongoose } = app;
  27. model = mongoose.model('Race', schema, 'race');
  28. }
  29. return model;
  30. };