115 lines
3.0 KiB
JavaScript
115 lines
3.0 KiB
JavaScript
const tools = require('../tools/general');
|
|
const Path = require('path')
|
|
|
|
module.exports = {
|
|
|
|
async insertIntoDb(tablename, table) {
|
|
|
|
try {
|
|
const pathfile = Path.join(__dirname, tablename + '.js');
|
|
if (tools.isFileExists(pathfile)) {
|
|
const mydbfile = require(pathfile);
|
|
|
|
if (mydbfile && mydbfile.list) {
|
|
return table.insertMany(mydbfile.list, {ordered: false}).
|
|
then((ris) => {
|
|
console.log('Populate table ', tablename);
|
|
return !!ris;
|
|
});
|
|
}
|
|
}
|
|
}catch (e){
|
|
console.log('error insertIntoDb', e);
|
|
}
|
|
|
|
},
|
|
|
|
async insertIntoDb_NoDuplicate(tablename, table, field) {
|
|
|
|
try {
|
|
const pathfile = Path.join(__dirname, tablename + '.js');
|
|
if (tools.isFileExists(pathfile)) {
|
|
const mydbfile = require(pathfile);
|
|
|
|
if (mydbfile && mydbfile.list) {
|
|
for (const rec of mydbfile.list) {
|
|
let obj = {}
|
|
obj[field] = rec[field];
|
|
|
|
var mynewrec = new table(rec);
|
|
|
|
const exist = await table.find(obj);
|
|
if (exist.length <= 0) {
|
|
try {
|
|
await mynewrec.save();
|
|
}catch (e){
|
|
console.log('error ', e);
|
|
}
|
|
|
|
//await table.insertMany(rec, {ordered: false});
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}catch (e){
|
|
console.log('error insertIntoDb', e);
|
|
}
|
|
|
|
},
|
|
|
|
async popolaTabelleNuove() {
|
|
const abilita = true;
|
|
const scrivi_citta = false;
|
|
|
|
let ris = null;
|
|
try {
|
|
|
|
if (abilita) {
|
|
// Sectors
|
|
const {Sector} = require('../models/sector');
|
|
await this.insertIntoDb_NoDuplicate('sectors', Sector, 'descr')
|
|
|
|
// CatGrps
|
|
const {CatGrp} = require('../models/catgrp');
|
|
await this.insertIntoDb_NoDuplicate('catgrps', CatGrp, 'descr')
|
|
|
|
// Skills (Competenze)
|
|
const {Skill} = require('../models/skill');
|
|
await this.insertIntoDb_NoDuplicate('skills', Skill, 'descr')
|
|
|
|
// SubSectors
|
|
const {SubSkill} = require('../models/subskill');
|
|
await this.insertIntoDb_NoDuplicate('subskills', SubSkill, 'descr')
|
|
|
|
// Levels
|
|
const {Level} = require('../models/level');
|
|
await this.insertIntoDb_NoDuplicate('levels', Level, 'descr')
|
|
|
|
// Status
|
|
const {StatusSkill} = require('../models/statusSkill');
|
|
await this.insertIntoDb_NoDuplicate('statusskills', StatusSkill, 'descr')
|
|
|
|
if (scrivi_citta) {
|
|
// Cities
|
|
const {City} = require('../models/city');
|
|
await this.insertIntoDb('cities', City)
|
|
|
|
// Province
|
|
const {Province} = require('../models/province');
|
|
await this.insertIntoDb('provinces', Province)
|
|
}
|
|
|
|
// Contribtypes
|
|
const {Contribtype} = require('../models/contribtype');
|
|
await this.insertIntoDb_NoDuplicate('contribtypes', Contribtype, 'label')
|
|
}
|
|
}catch (e) {
|
|
console.error('Err: ' + e);
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|