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 MySkillSchema = new Schema({ _id: { type: Number, }, idapp: { type: String, required: true, }, userId: {type: Schema.Types.ObjectId, ref: 'User'}, idSkill: { type: Number, default: 0, }, idSubSkill: [ { type: Number, default: 0, }], idStatusSkill: [ { type: Number, }], idContribType: [ { type: String, }], idCity: [ { type: Number, }], numLevel: { type: Number, default: 0, }, photos: [ { imagefile: { type: String, }, alt: { type: String, }, description: { type: String, }, }], note: { type: String, }, subTitle: { type: String, }, date_created: { type: Date, default: Date.now, }, date_updated: { type: Date, default: Date.now, }, }); MySkillSchema.pre('save', async function(next) { if (this.isNew) { const myrec = await MySkill.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; } this.date_created = new Date(); } next(); }); MySkillSchema.statics.findAllIdApp = async function(idapp) { const query = [ {$match: {idapp}}, {$sort: {descr: 1}}, ]; return MySkill.aggregate(query).then((arrrec) => { return arrrec; }); }; MySkillSchema.statics.getFieldsForSearch = function() { return [{field: 'idSkill', type: tools.FieldType.Number} ,{field: 'note', type: tools.FieldType.String} ,{field: 'subTitle', type: tools.FieldType.String} ]; }; MySkillSchema.statics.executeQueryTable = function(idapp, params) { params.fieldsearch = this.getFieldsForSearch(); const otherparams = { lookup1: { lk_tab: 'users', lk_LF: 'userId', lk_FF: '_id', lk_as: 'user', af_objId_tab: 'myId', lk_proj: { idSkill: 1, idStatusSkill: 1, idContribType: 1, idCity: 1, numLevel: 1, photos: 1, note: 1, subTitle: 1, date_created: 1, date_updated: 1, userId: 1, username: 1, name: 1, surname: 1, }, }, }; params = { ...params, ...otherparams }; return tools.executeQueryTable(this, idapp, params); }; const MySkill = mongoose.model('MySkill', MySkillSchema); module.exports = {MySkill};