Files
habitica/website/server/models/iapPurchaseReceipt.js
Matteo Pagliazzi 79c0499672 Mongoose: use $type as the typeKey (#10789)
* use $type as the typeKey in mongoose

* fix and add tests
2018-10-28 15:42:48 -05:00

24 lines
796 B
JavaScript

import mongoose from 'mongoose';
import baseModel from '../libs/baseModel';
import validator from 'validator';
const Schema = mongoose.Schema;
export let schema = new Schema({
_id: {$type: String, required: true}, // Use a custom string as _id
consumed: {$type: Boolean, default: false, required: true},
userId: {$type: String, ref: 'User', required: true, validate: [v => validator.isUUID(v), 'Invalid uuid.']},
}, {
strict: true,
minimize: false, // So empty objects are returned
typeKey: '$type', // So that we can use fields named `type`
});
schema.plugin(baseModel, {
noSet: ['id', '_id', 'userId', 'consumed'], // Nothing can be set from the client
timestamps: true,
_id: false, // using custom _id
});
export let model = mongoose.model('IapPurchaseReceipt', schema);