Files
freeplanet_serverside/src/server/models/city.js

137 lines
2.7 KiB
JavaScript
Raw Normal View History

const mongoose = require('mongoose').set('debug', false);
2021-10-08 00:38:35 +02:00
const Schema = mongoose.Schema;
const escapeStringRegexp = require('escape-string-regexp');
2021-10-08 00:38:35 +02:00
mongoose.Promise = global.Promise;
mongoose.level = 'F';
2021-10-08 00:38:35 +02:00
const tools = require('../tools/general');
const {ObjectID} = require('mongodb');
2021-10-08 00:38:35 +02:00
2022-01-23 23:25:34 +01:00
const shared_consts = require('../tools/shared_nodejs');
2021-10-08 00:38:35 +02:00
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true;
2021-10-08 00:38:35 +02:00
});
const CitySchema = new Schema({
_id: {
type: Number,
},
istat: {
type: String,
},
comune: {
type: String,
2021-10-08 00:38:35 +02:00
},
prov: {
type: String,
maxlength: 3,
},
reg: {
type: String,
maxlength: 3,
},
pref: {
type: String,
},
cap: {
type: String,
maxlength: 6,
},
abitanti: {
type: String,
2021-10-08 00:38:35 +02:00
},
country: {
type: String,
maxlength: 2,
},
});
CitySchema.pre('save', async function(next) {
2021-10-08 00:38:35 +02:00
if (this.isNew) {
const myrec = await City.findOne().limit(1).sort({_id: -1});
2021-10-08 00:38:35 +02:00
if (!!myrec) {
if (myrec._doc._id === 0)
this._id = 1;
else
this._id = myrec._doc._id + 1;
} else {
this._id = 1;
}
}
next();
});
CitySchema.statics.findByCity = function(mycity) {
2021-10-08 00:38:35 +02:00
let myregexp = new RegExp(mycity.trim().replace(' ', '|'), 'ig');
const query = [
{$match: {comune: {$regex: myregexp}}},
{$sort: {descr: 1}},
2021-10-08 00:38:35 +02:00
];
return City.aggregate(query).then((arrrec) => {
return arrrec;
});
2021-10-08 00:38:35 +02:00
};
CitySchema.statics.getFieldsForSearch = function() {
return [
{field: 'comune', type: tools.FieldType.string},
{field: 'prov', type: tools.FieldType.string},
{field: 'reg', type: tools.FieldType.string},
{field: 'pref', type: tools.FieldType.number},
{field: 'cap', type: tools.FieldType.number},
];
2021-10-08 00:38:35 +02:00
};
CitySchema.statics.executeQueryTable = function(idapp, params) {
2021-10-08 00:38:35 +02:00
params.fieldsearch = this.getFieldsForSearch();
2022-01-03 21:53:50 +01:00
const strfind = params.search;
if (strfind === '') {
return [];
}
2021-10-08 00:38:35 +02:00
return tools.executeQueryTable(this, 0, params);
};
CitySchema.statics.executeQueryPickup = async function(idapp, params) {
const strfind = params.search;
if (strfind === '') {
return [];
}
2022-01-23 23:25:34 +01:00
let filterfindexact = {comune: strfind};
const risexact = await City.find(filterfindexact, {comune: 1, prov: 1, reg: 1}).lean();
let filterfind = {comune: {$regex: '^' + strfind, $options: 'i'}};
const ris = await City.find(filterfind, {comune: 1, prov: 1, reg: 1}).lean().limit(10);
return [...risexact, ...ris];
};
CitySchema.statics.findAllIdApp = async function(idapp) {
2021-10-28 00:38:10 +02:00
const myfind = {};
return City.find(myfind);
};
2021-10-08 00:38:35 +02:00
const City = mongoose.model('City', CitySchema);
module.exports = {City};