-
-
Save Mattchewone/6a71b1926d1b601b101e169bf0f0195a to your computer and use it in GitHub Desktop.
Mongoose discriminators in feathers-cli generated service.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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