mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 06:07:21 +01:00
Mongoose: use $type as the typeKey (#10789)
* use $type as the typeKey in mongoose * fix and add tests
This commit is contained in:
@@ -3,10 +3,14 @@ import validator from 'validator';
|
||||
import _ from 'lodash';
|
||||
|
||||
module.exports = function baseModel (schema, options = {}) {
|
||||
if (schema.options.typeKey !== '$type') {
|
||||
throw new Error('Every schema must use $type as the typeKey, see https://mongoosejs.com/docs/guide.html#typeKey');
|
||||
}
|
||||
|
||||
if (options._id !== false) {
|
||||
schema.add({
|
||||
_id: {
|
||||
type: String,
|
||||
$type: String,
|
||||
default: uuid,
|
||||
validate: [v => validator.isUUID(v), 'Invalid uuid.'],
|
||||
},
|
||||
@@ -16,11 +20,11 @@ module.exports = function baseModel (schema, options = {}) {
|
||||
if (options.timestamps) {
|
||||
schema.add({
|
||||
createdAt: {
|
||||
type: Date,
|
||||
$type: Date,
|
||||
default: Date.now,
|
||||
},
|
||||
updatedAt: {
|
||||
type: Date,
|
||||
$type: Date,
|
||||
default: Date.now,
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user