How to share enum values in mongoose shema? How to share enum values in mongoose shema? mongoose mongoose

How to share enum values in mongoose shema?


I like to attach them with the model:

const ENUM = {  ONE: 1,  TWO: 2,  TEN: 10};const kindSchema = new Schema({  kind: { type: Number, enum: _.values(ENUM) }});kindSchema.statics.KINDS = ENUM;Model.create({ kind: Model.KINDS.TEN });


You can define const for each kind, and use it in both: schema and router:

// consts.jsconst KIND0 = 0;const KIND1 = 1;...const KIND10 = 1;const KINDS = [KIND0, KIND1,...,KIND10];// schema.jskind: {  type: Number,  enum: KINDS}// router.jsModel.create({ kind: KIND10 }).exec(callback);