Skip to content

Instantly share code, notes, and snippets.

@Mattchewone
Forked from RubyRubenstahl/media.model.js
Created February 22, 2018 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mattchewone/6a71b1926d1b601b101e169bf0f0195a to your computer and use it in GitHub Desktop.
Save Mattchewone/6a71b1926d1b601b101e169bf0f0195a to your computer and use it in GitHub Desktop.
Mongoose discriminators in feathers-cli generated service.
// media-model.js - A mongoose model
//
// See http://mongoosejs.com/docs/models.html
// for more of what you can do here.
module.exports = function (app) {
const mongooseClient = app.get('mongooseClient');
const { Schema } = mongooseClient;
const options = {
discriminatorKey: 'type',
timestamps: true
}
const Media = new Schema({
name: {type: String, default: 'New Media File'},
type: {type: String, enum: ['file', 'folder']},
parent: {type: Schema.Types.ObjectId},
uid: String,
}, options);
const Model = mongooseClient.model('media', Media);
const MediaFile = Model.discriminator('file',
new Schema({
systemFilename: String,
mimetype: {type: String},
size: Number, // size in bytes
meta: { type: Schema.Types.Mixed},
}, options)
);
const MediaFolder = Model.discriminator('folder',
new Schema({
children: {type: [Schema.Types.ObjectId]},
}, options)
);
return {Model, discriminators: [MediaFile, MediaFolder]};
};
// Initializes the `media` service on path `/media`
const createService = require('feathers-mongoose');
const createModel = require('../../models/media.model');
const hooks = require('./media.hooks');
module.exports = function (app) {
const {Model, discriminators} = createModel(app);
const paginate = app.get('paginate');
const options = {
name: 'media',
Model,
paginate,
discriminators
};
// Initialize our service with any options it requires
app.use('/media', createService(options));
// Get our initialized service so that we can register hooks and filters
const service = app.service('media');
service.hooks(hooks);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment