Lista Città (prese dal server: pickup)

This commit is contained in:
paoloar77
2022-01-20 00:39:06 +01:00
parent f463f88495
commit 66eaca6d41
11 changed files with 97289 additions and 56 deletions

View File

@@ -1,16 +1,18 @@
const mongoose = require('mongoose').set('debug', false)
const mongoose = require('mongoose').set('debug', false);
const Schema = mongoose.Schema;
const escapeStringRegexp = require('escape-string-regexp');
mongoose.Promise = global.Promise;
mongoose.level = "F";
mongoose.level = 'F';
const tools = require('../tools/general');
const { ObjectID } = require('mongodb');
const {ObjectID} = require('mongodb');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
schema.options.usePushEach = true;
});
const CitySchema = new Schema({
@@ -21,7 +23,7 @@ const CitySchema = new Schema({
type: String,
},
comune: {
type: String
type: String,
},
prov: {
type: String,
@@ -39,7 +41,7 @@ const CitySchema = new Schema({
maxlength: 6,
},
abitanti: {
type: Number,
type: String,
},
country: {
type: String,
@@ -47,9 +49,9 @@ const CitySchema = new Schema({
},
});
CitySchema.pre('save', async function (next) {
CitySchema.pre('save', async function(next) {
if (this.isNew) {
const myrec = await City.findOne().limit(1).sort({_id:-1});
const myrec = await City.findOne().limit(1).sort({_id: -1});
if (!!myrec) {
if (myrec._doc._id === 0)
this._id = 1;
@@ -64,34 +66,32 @@ CitySchema.pre('save', async function (next) {
next();
});
CitySchema.statics.findByCity = function (mycity) {
CitySchema.statics.findByCity = function(mycity) {
let myregexp = new RegExp(mycity.trim().replace(' ', '|'), 'ig');
const query = [
{ $match: {comune: { $regex: myregexp } } },
{ $sort: { descr: 1 } }
{$match: {comune: {$regex: myregexp}}},
{$sort: {descr: 1}},
];
return City
.aggregate(query)
.then((arrrec) => {
return arrrec
})
return City.aggregate(query).then((arrrec) => {
return arrrec;
});
};
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 },
]
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},
];
};
CitySchema.statics.executeQueryTable = function (idapp, params) {
CitySchema.statics.executeQueryTable = function(idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
const strfind = params.search;
@@ -103,13 +103,25 @@ CitySchema.statics.executeQueryTable = function (idapp, params) {
return tools.executeQueryTable(this, 0, params);
};
CitySchema.statics.findAllIdApp = async function (idapp) {
CitySchema.statics.executeQueryPickup = async function(idapp, params) {
const strfind = params.search;
if (strfind === '') {
return [];
}
const ris = await City.find({ comune: { $regex : '^' + strfind, $options: 'i' } }, {comune: 1, prov: 1, reg: 1}).lean();
return ris;
};
CitySchema.statics.findAllIdApp = async function(idapp) {
const myfind = {};
return City.find(myfind);
};
const City = mongoose.model('City', CitySchema);
module.exports = { City };
module.exports = {City};