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

86 lines
1.6 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 SkillSchema = new Schema({
_id: {
type: Number,
},
descr: {
type: String,
},
idSector: [{
type: Number
}],
icon: {
type: String,
},
img: {
type: String,
},
});
SkillSchema.statics.findAllIdApp = async function (idapp) {
const Skill = this;
const query = [
{ $sort: { descr: 1 } }
];
return await Skill
.aggregate(query)
.then((arrrec) => {
return arrrec
})
};
SkillSchema.pre('save', async function (next) {
if (this.isNew) {
const myrec = await Skill.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();
});
SkillSchema.statics.getFieldsForSearch = function () {
return [{ field: 'label', type: tools.FieldType.string },
{ field: 'descr', type: tools.FieldType.string }]
};
SkillSchema.statics.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, 0, params);
};
const Skill = mongoose.model('Skill', SkillSchema);
Skill.createIndexes((err) => {
if (err) throw err;
});
module.exports = { Skill };