Files
freeplanet_serverside/src/server/models/level.js
2023-12-09 11:55:58 +01:00

81 lines
1.5 KiB
JavaScript
Executable File

const mongoose = require('mongoose').set('debug', false);
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
mongoose.level = 'F';
const tools = require('../tools/general');
const {ObjectID} = require('mongodb');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true;
});
const LevelSchema = new Schema({
_id: {
type: Number,
},
descr: {
type: String,
},
years_of_exp: {
type: Number,
},
color: {
type: String,
},
theme: {
type: String,
},
});
LevelSchema.pre('save', async function (next) {
if (this.isNew) {
const myrec = await Level.findOne().limit(1).sort({_id:-1});
if (!!myrec) {
if (myrec._doc._id === 0)
this._id = 1;
else
this._id = myrec._doc._id + 1;
} else {
this._id = 1;
}
}
next();
});
LevelSchema.statics.findAllIdApp = async function(idapp) {
const Level = this;
const query = [
{$sort: {_id: 1}},
];
return await Level.aggregate(query).then((arrrec) => {
return arrrec;
});
};
LevelSchema.statics.getFieldsForSearch = function() {
return [
{field: 'descr', type: tools.FieldType.string}];
};
LevelSchema.statics.executeQueryTable = function(idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, 0, params);
};
const Level = mongoose.model('Level', LevelSchema);
Level.createIndexes((err) => {
if (err) throw err;
});
module.exports = {Level};