mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 23:27:26 +01:00
54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
import _ from 'lodash';
|
|
import { uuid } from '../../../../common';
|
|
import validator from 'validator';
|
|
|
|
export default function baseModel (schema, options = {}) {
|
|
schema.add({
|
|
_id: {
|
|
type: String,
|
|
default: uuid.v4,
|
|
validate: [validator.isUUID, 'Invalid uuid.'], // TODO check for UUID version
|
|
},
|
|
});
|
|
|
|
if (options.timestamps) {
|
|
schema.add({
|
|
createdAt: {
|
|
type: Date,
|
|
default: Date.now,
|
|
},
|
|
updatedAt: {
|
|
type: Date,
|
|
default: Date.now,
|
|
},
|
|
});
|
|
}
|
|
|
|
if (options.timestamps) {
|
|
schema.pre('save', function updateUpdatedAt (next) {
|
|
if (!this.isNew) this.updatedAt = Date.now();
|
|
next();
|
|
});
|
|
}
|
|
|
|
let noSetFields = ['createdAt', 'updatedAt'];
|
|
let privateFields = ['__v'];
|
|
|
|
if (Array.isArray(options.noSet)) noSetFields.push(...options.noSet);
|
|
schema.statics.sanitize = function sanitize (objToSanitize = {}) {
|
|
noSetFields.forEach((fieldPath) => {
|
|
_.set(objToSanitize, fieldPath, undefined); // TODO decide wheter to use delete here
|
|
});
|
|
|
|
return objToSanitize;
|
|
};
|
|
|
|
if (Array.isArray(options.private)) privateFields.push(...options.private);
|
|
if (!schema.options.toObject) schema.options.toObject = {};
|
|
schema.options.toObject.transform = function transformToObject (doc, plainObj) {
|
|
privateFields.forEach((fieldPath) => {
|
|
_.set(plainObj, fieldPath, undefined); // TODO decide wheter to use delete here
|
|
});
|
|
};
|
|
}
|