Files
freeplanet_serverside/src/server/models/sector.js
Paolo Arena 845e244470 Circuits OK
Accounts Ok
Movements OK
2022-09-14 11:32:04 +02:00

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