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

65 lines
1.3 KiB
JavaScript
Executable File

const mongoose = require('mongoose').set('debug', false)
const Schema = mongoose.Schema;
const tools = require('../tools/general');
mongoose.Promise = global.Promise;
mongoose.level = "F";
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
const PermissionSchema = new Schema({
_id: {
type: Number,
},
label: {
type: String,
default: ''
}
}, { _id: false });
PermissionSchema.pre('save', async function (next) {
if (this.isNew) {
const myrec = await Permission.findOne().limit(1).sort({ _id: -1 });
if (!!myrec) {
if (myrec._doc._id === 0)
this._id = 1;
else
this._id = myrec._doc._id * 2;
} else {
this._id = 1;
}
}
next();
});
PermissionSchema.statics.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, 0, params);
};
PermissionSchema.statics.findAllIdApp = async function () {
const Permission = this;
const myfind = {};
return await Permission.find(myfind, (err, arrrec) => {
return arrrec
});
};
const Permission = mongoose.model('Permission', PermissionSchema);
Permission.createIndexes((err) => {
if (err) throw err;
});
module.exports = { Permission };