egg-mongoose 的写法怎么实现 Discriminators?

2019-04-22 15:01:37 +08:00
 vincentxue

正常的 Mongoose 里 不使用 Discriminators 的写法:

> models/book.js

// Define our Book schema
const BookSchema = new mongoose.Schema(
  {
    title: { type: String, required: true },
    author: { type: String, required: true },
    release_date: { type: Date, required: true },
  }
);

// Create a model from our schema
module.exports = mongoose.model('Book', BookSchema);
> models/movie.js

const MovieSchema = new mongoose.Schema(
  {
    title: { type: String, required: true },
    director: { type: String, required: true },
    release_date: { type: Date, required: true },
  }
);

module.exports = mongoose.model('Movie', MovieSchema);

使用 Discriminators 的写法:

> models/book.js

const baseOptions = {
  discriminatorKey: 'itemtype', // our discriminator key, could be anything
  collection: 'items', // the name of our collection
};

const Base = mongoose.model('Base', new mongoose.Schema({
      title: { type: String, required: true },
      date_added: { type: Date, required: true },
      redo: { type: Boolean, required: false },
    }, baseOptions,
  ),
);

module.exports = mongoose.model('Base');
> models/book.js

const Base = require('./base'); // we have to make sure our Book schema is aware of the Base schema

const Book = Base.discriminator('Book', new mongoose.Schema({
    author: { type: String, required: true },
  }),
);

module.exports = mongoose.model('Book');
> models/movie.js

const Base = require('./base');

const Movie = Base.discriminator('Movie', new mongoose.Schema({
    director: { type: String, required: true },
  }),
);

module.exports = mongoose.model('Movie');

上面的例子在 egg-mongoose 里这样写:

> {app_root}/app/model/base.js

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const baseOptions = {
  discriminatorKey: 'itemtype', // our discriminator key, could be anything
  collection: 'items', // the name of our collection
  };

const BaseScheme = new Schema({
      title: { type: String, required: true },
      date_added: { type: Date, required: true },
      redo: { type: Boolean, required: false },
    }, baseOptions);

  return mongoose.model('Base', BaseScheme);
};
> {app_root}/app/model/book.js

const Base = require('./base');

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  
  const BookSchema = new Schema({
    author: { type: String, required: true },
  });
  
  return Base.discriminator('Book', BookSchema);
};

这样做会报错,提示:

ERROR 30161 nodejs.TypeError: Base.discriminator is not a function

请问在 egg-mongoose 中在保持 model 分离的前提下如何实现上述功能?

其他

上面例子的代码源自 Getting started with Mongoose discriminators in Express.js

3634 次点击
所在节点    Node.js
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/557522

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX