@itentialopensource/adapter-utils 4.44.7 → 4.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +50 -0
- package/lib/connectorRest.js +16 -2
- package/lib/dbUtil.js +409 -477
- package/lib/propertyUtil.js +2 -1
- package/lib/restHandler.js +21 -2
- package/package.json +4 -4
- package/propertiesSchema.json +5 -0
package/lib/dbUtil.js
CHANGED
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
|
|
7
7
|
const fs = require('fs-extra');
|
|
8
8
|
const uuid = require('uuid');
|
|
9
|
-
const path = require('path');
|
|
10
9
|
|
|
11
10
|
/* Fetch in the other needed components for the this Adaptor */
|
|
12
11
|
const { MongoClient } = require('mongodb');
|
|
@@ -15,8 +14,18 @@ const { MongoClient } = require('mongodb');
|
|
|
15
14
|
let adapterDir = '';
|
|
16
15
|
let saveFS = false;
|
|
17
16
|
let storDir = `${adapterDir}/storage`;
|
|
17
|
+
let entityDir = `${adapterDir}/entities`;
|
|
18
18
|
let id = null;
|
|
19
19
|
|
|
20
|
+
const Storage = {
|
|
21
|
+
UNDEFINED: 0,
|
|
22
|
+
DBINFO: 1,
|
|
23
|
+
ADAPTERDB: 2,
|
|
24
|
+
FILESYSTEM: 3,
|
|
25
|
+
IAPDB: 4
|
|
26
|
+
};
|
|
27
|
+
Object.freeze(Storage);
|
|
28
|
+
|
|
20
29
|
/* DB UTILS INTERNAL FUNCTIONS */
|
|
21
30
|
/** getFromJson
|
|
22
31
|
* @summary returns information from a json file on the file system
|
|
@@ -52,6 +61,55 @@ function getFromJson(fileName, filter) {
|
|
|
52
61
|
}
|
|
53
62
|
|
|
54
63
|
try {
|
|
64
|
+
if (fileName === 'adapter_configs') {
|
|
65
|
+
const retEntity = filter.entity;
|
|
66
|
+
if (!retEntity || !fs.existsSync(`${entityDir}`) || !fs.existsSync(`${entityDir}/${retEntity}`)) {
|
|
67
|
+
log.warn(`${origin}: Could not find adapter entity directory ${retEntity} - nothing to retrieve`);
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
if (!fs.existsSync(`${entityDir}/${retEntity}/action.json`)) {
|
|
71
|
+
log.warn(`${origin}: Could not find action.json for entity ${retEntity} - nothing to retrieve`);
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// load the mockdatafiles
|
|
76
|
+
let files = fs.readdirSync(`${entityDir}/${retEntity}`);
|
|
77
|
+
const mockdatafiles = {};
|
|
78
|
+
if (files.includes('mockdatafiles') && fs.lstatSync(`${entityDir}/${retEntity}/mockdatafiles`).isDirectory()) {
|
|
79
|
+
fs.readdirSync(`${entityDir}/${retEntity}/mockdatafiles`).forEach((file) => {
|
|
80
|
+
if (file.split('.').pop() === 'json') {
|
|
81
|
+
const mockpath = `${entityDir}/${retEntity}/mockdatafiles/${file}`;
|
|
82
|
+
const data = JSON.parse(fs.readFileSync(mockpath));
|
|
83
|
+
mockdatafiles[mockpath.split('/').pop()] = data;
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// load the action data
|
|
89
|
+
let actions;
|
|
90
|
+
if (files.includes('action.json')) {
|
|
91
|
+
actions = JSON.parse(fs.readFileSync(`${entityDir}/${retEntity}/action.json`));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Load schema.json and other schemas in remaining json files
|
|
95
|
+
files = files.filter((f) => (f !== 'action.json') && f.endsWith('.json'));
|
|
96
|
+
const schema = [];
|
|
97
|
+
files.forEach((file) => {
|
|
98
|
+
const data = JSON.parse(fs.readFileSync(`${entityDir}/${retEntity}/${file}`));
|
|
99
|
+
schema.push({
|
|
100
|
+
name: file,
|
|
101
|
+
schema: data
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// return the data
|
|
106
|
+
return {
|
|
107
|
+
actions: actions.actions,
|
|
108
|
+
schema,
|
|
109
|
+
mockdatafiles
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
55
113
|
// make sure what we need exists and has been provided
|
|
56
114
|
if (!fs.existsSync(`${storDir}`)) {
|
|
57
115
|
log.warn(`${origin}: Could not find adapter storage directory - nothing to retrieve`);
|
|
@@ -72,21 +130,29 @@ function getFromJson(fileName, filter) {
|
|
|
72
130
|
});
|
|
73
131
|
} else {
|
|
74
132
|
// if not metric must match the items in the filter
|
|
75
|
-
|
|
133
|
+
// if no filter, match everything
|
|
134
|
+
let key = [];
|
|
135
|
+
if (useFilter && useFilter.length > 0) {
|
|
136
|
+
key = Object.keys(useFilter);
|
|
137
|
+
}
|
|
76
138
|
content.table.forEach((item) => {
|
|
77
139
|
let push = true;
|
|
140
|
+
// check each key - if it does not match a key do not add to return data
|
|
78
141
|
key.forEach((fil) => {
|
|
79
142
|
if (useFilter[fil] !== item[fil]) push = false;
|
|
80
143
|
});
|
|
81
144
|
if (push) toReturn.push(item);
|
|
82
145
|
});
|
|
83
146
|
}
|
|
147
|
+
// not sure why we do this - a little different than above.
|
|
84
148
|
const filtered = content.table.filter((el) => {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
149
|
+
if (useFilter) {
|
|
150
|
+
Object.keys(useFilter).forEach((obj) => {
|
|
151
|
+
if (el[obj] !== useFilter[obj]) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
90
156
|
return true;
|
|
91
157
|
});
|
|
92
158
|
return toReturn;
|
|
@@ -393,7 +459,9 @@ class DBUtil {
|
|
|
393
459
|
this.baseDir = directory;
|
|
394
460
|
adapterDir = this.baseDir;
|
|
395
461
|
storDir = `${adapterDir}/storage`;
|
|
462
|
+
entityDir = `${adapterDir}/entities`;
|
|
396
463
|
this.props = properties;
|
|
464
|
+
this.adapterMongoClient = null;
|
|
397
465
|
|
|
398
466
|
// set up the properties I care about
|
|
399
467
|
this.refreshProperties(properties);
|
|
@@ -509,6 +577,43 @@ class DBUtil {
|
|
|
509
577
|
}
|
|
510
578
|
}
|
|
511
579
|
|
|
580
|
+
/**
|
|
581
|
+
* Call to determine the storage target. If the storage is a Mongo database, then
|
|
582
|
+
* the client connection and database is returned.
|
|
583
|
+
*
|
|
584
|
+
* @function determineStorage
|
|
585
|
+
* @param {object} dbInfo - the url for the database to connect to (dburl, dboptions, database)
|
|
586
|
+
* @param {callback} callback - the error, the storage, the Mongo client connection, the Mongo database
|
|
587
|
+
*/
|
|
588
|
+
determineStorage(dbInfo, callback) {
|
|
589
|
+
const origin = `${this.myid}-dbUtil-determineStorage`;
|
|
590
|
+
|
|
591
|
+
if (dbInfo) {
|
|
592
|
+
// priority 1 - use the dbInfo passed in
|
|
593
|
+
if (dbInfo.dburl && dbInfo.database) {
|
|
594
|
+
MongoClient.connect(dbInfo.dburl, dbInfo.dboptions, (err, mongoClient) => {
|
|
595
|
+
if (err) {
|
|
596
|
+
log.error(`${origin}: Error! Failed to connect to database: ${err}`);
|
|
597
|
+
return callback(err, null, null, null);
|
|
598
|
+
}
|
|
599
|
+
log.debug('using dbinfo');
|
|
600
|
+
return callback(null, Storage.DBINFO, mongoClient, dbInfo.database);
|
|
601
|
+
});
|
|
602
|
+
} else {
|
|
603
|
+
const err = 'Error! Marlformed dbInfo';
|
|
604
|
+
return callback(err, null, null, Storage.DBINFO);
|
|
605
|
+
}
|
|
606
|
+
} else if (this.adapterMongoClient) {
|
|
607
|
+
// priority 2 - use the adapter database
|
|
608
|
+
log.debug('using adapter db');
|
|
609
|
+
return callback(null, Storage.ADAPTERDB, this.adapterMongoClient, this.database);
|
|
610
|
+
} else if (this.dburl === null && dbInfo === null) {
|
|
611
|
+
// priority 3 - use the filesystem
|
|
612
|
+
log.debug('using filesystem');
|
|
613
|
+
return callback(null, Storage.FILESYSTEM, null, null);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
512
617
|
/**
|
|
513
618
|
* Call to connect and authenticate to the adapter database
|
|
514
619
|
*
|
|
@@ -544,6 +649,7 @@ class DBUtil {
|
|
|
544
649
|
} else {
|
|
545
650
|
log.info(`${origin}: MONGO CONNECTION BACK...`);
|
|
546
651
|
this.alive = true;
|
|
652
|
+
this.adapterMongoClient = mongoClient;
|
|
547
653
|
}
|
|
548
654
|
});
|
|
549
655
|
});
|
|
@@ -567,6 +673,7 @@ class DBUtil {
|
|
|
567
673
|
|
|
568
674
|
log.info(`${origin}: mongo running @${this.dburl}/${this.database}`);
|
|
569
675
|
this.clientDB = mongoClient.db(this.database);
|
|
676
|
+
this.adapterMongoClient = mongoClient;
|
|
570
677
|
|
|
571
678
|
// we don't have authentication defined but we still need to check if Mongo does not
|
|
572
679
|
// require one, so we just list collections to test if it's doable.
|
|
@@ -577,12 +684,24 @@ class DBUtil {
|
|
|
577
684
|
} else {
|
|
578
685
|
log.info(`${origin}: MongoDB connection has been established`);
|
|
579
686
|
this.alive = true;
|
|
687
|
+
this.adapterMongoClient = mongoClient;
|
|
580
688
|
}
|
|
581
689
|
return callback(this.alive, mongoClient);
|
|
582
690
|
});
|
|
583
691
|
});
|
|
584
692
|
}
|
|
585
693
|
|
|
694
|
+
/**
|
|
695
|
+
* Call to disconnect from the adapter database
|
|
696
|
+
*
|
|
697
|
+
* @function disconnect
|
|
698
|
+
*/
|
|
699
|
+
disconnect() {
|
|
700
|
+
if (this.adapterMongoClient) {
|
|
701
|
+
this.adapterMongoClient.close();
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
586
705
|
/**
|
|
587
706
|
* createCollection creates the provided collection in the file system or database.
|
|
588
707
|
*
|
|
@@ -595,11 +714,6 @@ class DBUtil {
|
|
|
595
714
|
const origin = `${this.myid}-dbUtil-createCollection`;
|
|
596
715
|
log.trace(origin);
|
|
597
716
|
|
|
598
|
-
// set the default adapter database
|
|
599
|
-
let usedburl = this.dburl;
|
|
600
|
-
let usedbopt = this.dboptions;
|
|
601
|
-
let usedb = this.database;
|
|
602
|
-
|
|
603
717
|
try {
|
|
604
718
|
// verify the required data has been provided
|
|
605
719
|
if (!collectionName || (typeof collectionName !== 'string')) {
|
|
@@ -607,68 +721,51 @@ class DBUtil {
|
|
|
607
721
|
return callback(`${origin}: Missing Collection Name or not string`, null);
|
|
608
722
|
}
|
|
609
723
|
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
if (!fs.existsSync(`${this.baseDir}`)) {
|
|
614
|
-
log.warn(`${origin}: Not able to create storage - missing base directory!`);
|
|
615
|
-
return callback(`${origin}: Not able to create storage - missing base directory!`, null);
|
|
616
|
-
}
|
|
617
|
-
// if there is no storage directory - create it
|
|
618
|
-
if (!fs.existsSync(`${this.baseDir}/storage`)) {
|
|
619
|
-
fs.mkdirSync(`${this.baseDir}/storage`);
|
|
724
|
+
return this.determineStorage(dbInfo, (storageError, storage, mongoClient, database) => {
|
|
725
|
+
if (storageError) {
|
|
726
|
+
return callback(`${origin}: ${storageError}`, null);
|
|
620
727
|
}
|
|
621
|
-
// if the collection already exists - no need to create it
|
|
622
|
-
if (fs.existsSync(`${this.baseDir}/storage/${collectionName}`)) {
|
|
623
|
-
log.debug(`${origin}: storage file collection already exists`);
|
|
624
|
-
return callback(null, collectionName);
|
|
625
|
-
}
|
|
626
|
-
// create the new collection on the file system
|
|
627
|
-
fs.mkdirSync(`${this.baseDir}/storage/${collectionName}`);
|
|
628
|
-
log.debug(`${origin}: storage file collection ${collectionName} created`);
|
|
629
|
-
return callback(null, collectionName);
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
// if we were passed in dbInfo, verify it contains all the pieces we want
|
|
633
|
-
if (!dbInfo || !dbInfo.dburl || !dbInfo.database) {
|
|
634
|
-
log.warn(`${origin}: Improper dbInfo provided should have dburl and database! Using default database`);
|
|
635
|
-
}
|
|
636
|
-
// if we were passed in dbInfo, that should supercede the default database
|
|
637
|
-
if (dbInfo && dbInfo.dburl && dbInfo.database) {
|
|
638
|
-
usedburl = dbInfo.dburl;
|
|
639
|
-
usedbopt = dbInfo.dboptions;
|
|
640
|
-
usedb = dbInfo.database;
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
if (!usedburl || !usedb) {
|
|
644
|
-
log.warn(`${origin}: Did not find database to connect to`);
|
|
645
|
-
return callback(`${origin}: Did not find database to connect to`, null);
|
|
646
|
-
}
|
|
647
728
|
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
729
|
+
// if using file storage
|
|
730
|
+
if (storage === Storage.FILESYSTEM) {
|
|
731
|
+
// if there is no adapter directory - can not do anything so error
|
|
732
|
+
if (!fs.existsSync(`${this.baseDir}`)) {
|
|
733
|
+
log.warn(`${origin}: Not able to create storage - missing base directory!`);
|
|
734
|
+
return callback(`${origin}: Not able to create storage - missing base directory!`, null);
|
|
735
|
+
}
|
|
736
|
+
// if there is no storage directory - create it
|
|
737
|
+
if (!fs.existsSync(`${this.baseDir}/storage`)) {
|
|
738
|
+
fs.mkdirSync(`${this.baseDir}/storage`);
|
|
739
|
+
}
|
|
740
|
+
// if the collection already exists - no need to create it
|
|
741
|
+
if (fs.existsSync(`${this.baseDir}/storage/${collectionName}`)) {
|
|
742
|
+
log.debug(`${origin}: storage file collection already exists`);
|
|
743
|
+
return callback(null, collectionName);
|
|
744
|
+
}
|
|
745
|
+
// create the new collection on the file system
|
|
746
|
+
fs.mkdirSync(`${this.baseDir}/storage/${collectionName}`);
|
|
747
|
+
log.debug(`${origin}: storage file collection ${collectionName} created`);
|
|
748
|
+
return callback(null, collectionName);
|
|
654
749
|
}
|
|
655
750
|
|
|
656
|
-
//
|
|
657
|
-
|
|
658
|
-
db.
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
751
|
+
// if using MongoDB storage
|
|
752
|
+
if (storage === Storage.DBINFO || storage === Storage.ADAPTERDB) {
|
|
753
|
+
return mongoClient.db(database).createCollection(collectionName, (err, res) => {
|
|
754
|
+
if (storage === Storage.DBINFO && mongoClient) mongoClient.close();
|
|
755
|
+
if (err) {
|
|
756
|
+
// error we get back if the collection already existed - not a true error
|
|
757
|
+
if (err.codeName === 'NamespaceExists') {
|
|
758
|
+
log.debug(`${origin}: database collection already exists`);
|
|
759
|
+
return callback(null, collectionName);
|
|
760
|
+
}
|
|
761
|
+
log.warn(`${origin}: Error creating collection ${err}`);
|
|
762
|
+
return callback(`${origin}: Error creating collection ${err}`, null);
|
|
664
763
|
}
|
|
665
|
-
log.
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
return callback(null, collectionName);
|
|
671
|
-
});
|
|
764
|
+
log.spam(`${origin}: db response ${res}`);
|
|
765
|
+
log.debug(`${origin}: database collection ${collectionName} created`);
|
|
766
|
+
return callback(null, collectionName);
|
|
767
|
+
});
|
|
768
|
+
}
|
|
672
769
|
});
|
|
673
770
|
} catch (ex) {
|
|
674
771
|
log.warn(`${origin}: Caught Exception - ${ex}`);
|
|
@@ -688,11 +785,6 @@ class DBUtil {
|
|
|
688
785
|
const origin = `${this.myid}-dbUtil-removeCollection`;
|
|
689
786
|
log.trace(origin);
|
|
690
787
|
|
|
691
|
-
// set the default database
|
|
692
|
-
let usedburl = this.dburl;
|
|
693
|
-
let usedbopt = this.dboptions;
|
|
694
|
-
let usedb = this.database;
|
|
695
|
-
|
|
696
788
|
try {
|
|
697
789
|
// verify the required data has been provided
|
|
698
790
|
if (!collectionName || (typeof collectionName !== 'string')) {
|
|
@@ -700,66 +792,49 @@ class DBUtil {
|
|
|
700
792
|
return callback(`${origin}: Missing Collection Name or not string`, null);
|
|
701
793
|
}
|
|
702
794
|
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
if (deld) {
|
|
707
|
-
log.debug(`${origin}: storage file collection ${collectionName} removed`);
|
|
708
|
-
return callback(null, deld);
|
|
795
|
+
return this.determineStorage(dbInfo, (storageError, storage, mongoClient, database) => {
|
|
796
|
+
if (storageError) {
|
|
797
|
+
return callback(`${origin}: ${storageError}`, null);
|
|
709
798
|
}
|
|
710
|
-
log.debug(`${origin}: could not remove storage file collection`);
|
|
711
|
-
return callback(`${origin}: could not remove storage file collection`, null);
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
// if we were passed in dbInfo, verify it contains all the pieces we want
|
|
715
|
-
if (!dbInfo || !dbInfo.dburl || !dbInfo.database) {
|
|
716
|
-
log.warn(`${origin}: Improper dbInfo provided should have dburl and database! Using default database`);
|
|
717
|
-
}
|
|
718
|
-
// if we were passed in dbInfo, that should supercede the default database
|
|
719
|
-
if (dbInfo && dbInfo.dburl && dbInfo.database) {
|
|
720
|
-
usedburl = dbInfo.dburl;
|
|
721
|
-
usedbopt = dbInfo.dboptions;
|
|
722
|
-
usedb = dbInfo.database;
|
|
723
|
-
}
|
|
724
799
|
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
log.warn(`${origin}: Error connecting to Mongo ${error}`);
|
|
735
|
-
return callback(`${origin}: Error connecting to Mongo ${error}`, null);
|
|
800
|
+
// if using file storage
|
|
801
|
+
if (storage === Storage.FILESYSTEM) {
|
|
802
|
+
const deld = deleteJSON(collectionName);
|
|
803
|
+
if (deld) {
|
|
804
|
+
log.debug(`${origin}: storage file collection ${collectionName} removed`);
|
|
805
|
+
return callback(null, deld);
|
|
806
|
+
}
|
|
807
|
+
log.debug(`${origin}: could not remove storage file collection`);
|
|
808
|
+
return callback(`${origin}: could not remove storage file collection`, null);
|
|
736
809
|
}
|
|
737
810
|
|
|
738
|
-
//
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
for (let e = 0; e < result.length; e += 1) {
|
|
746
|
-
if (result[e].name === collectionName) {
|
|
747
|
-
// now that we found it, remove it
|
|
748
|
-
return db.db(usedb).collection(collectionName).drop({}, (err1, res) => {
|
|
749
|
-
db.close();
|
|
750
|
-
if (err1) {
|
|
751
|
-
log.warn(`${origin}: Failed to remove collection ${err1}`);
|
|
752
|
-
return callback(`${origin}: Failed to remove collection ${err1}`, null);
|
|
753
|
-
}
|
|
754
|
-
log.spam(`${origin}: db response ${res}`);
|
|
755
|
-
log.debug(`${origin}: database collection ${collectionName} removed`);
|
|
756
|
-
return callback(null, collectionName);
|
|
757
|
-
});
|
|
811
|
+
// if using MongoDB storage
|
|
812
|
+
if (storage === Storage.DBINFO || storage === Storage.ADAPTERDB) {
|
|
813
|
+
// get the list of collections from the database - can we just drop?
|
|
814
|
+
return mongoClient.db(database).listCollections().toArray((err, result) => {
|
|
815
|
+
if (err) {
|
|
816
|
+
log.warn(`${origin}: Failed to get collections ${err}`);
|
|
817
|
+
return callback(`${origin}: Failed to get collections ${err}`, null);
|
|
758
818
|
}
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
819
|
+
// go through the collections to get the correct one for removal
|
|
820
|
+
for (let e = 0; e < result.length; e += 1) {
|
|
821
|
+
if (result[e].name === collectionName) {
|
|
822
|
+
// now that we found it, remove it
|
|
823
|
+
return mongoClient.db(database).collection(collectionName).drop({}, (err1, res) => {
|
|
824
|
+
if (storage === Storage.DBINFO && mongoClient) mongoClient.close();
|
|
825
|
+
if (err1) {
|
|
826
|
+
log.warn(`${origin}: Failed to remove collection ${err1}`);
|
|
827
|
+
return callback(`${origin}: Failed to remove collection ${err1}`, null);
|
|
828
|
+
}
|
|
829
|
+
log.spam(`${origin}: db response ${res}`);
|
|
830
|
+
log.debug(`${origin}: database collection ${collectionName} removed`);
|
|
831
|
+
return callback(null, collectionName);
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
return callback(null, collectionName);
|
|
836
|
+
});
|
|
837
|
+
}
|
|
763
838
|
});
|
|
764
839
|
} catch (ex) {
|
|
765
840
|
log.warn(`${origin}: Caught Exception - ${ex}`);
|
|
@@ -780,11 +855,6 @@ class DBUtil {
|
|
|
780
855
|
const origin = `${this.myid}-dbUtil-create`;
|
|
781
856
|
log.trace(origin);
|
|
782
857
|
|
|
783
|
-
// set the default database
|
|
784
|
-
let usedburl = this.dburl;
|
|
785
|
-
let usedbopt = this.dboptions;
|
|
786
|
-
let usedb = this.database;
|
|
787
|
-
|
|
788
858
|
try {
|
|
789
859
|
// verify the required data has been provided
|
|
790
860
|
if (!collectionName) {
|
|
@@ -806,54 +876,38 @@ class DBUtil {
|
|
|
806
876
|
dataInfo._id = uuid.v4();
|
|
807
877
|
}
|
|
808
878
|
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
const saved = saveAsJson(collectionName, data);
|
|
813
|
-
if (!saved) {
|
|
814
|
-
log.warn(`${origin}: Data has not been saved to file storage`);
|
|
815
|
-
return callback(`${origin}: Data has not been saved to file storage`, null);
|
|
879
|
+
return this.determineStorage(dbInfo, (storageError, storage, mongoClient, database) => {
|
|
880
|
+
if (storageError) {
|
|
881
|
+
return callback(`${origin}: ${storageError}`, null);
|
|
816
882
|
}
|
|
817
|
-
log.debug(`${origin}: Data saved in file storage`);
|
|
818
|
-
return callback(null, saved);
|
|
819
|
-
}
|
|
820
883
|
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
if (!usedburl || !usedb) {
|
|
833
|
-
log.warn(`${origin}: Did not find database to connect to`);
|
|
834
|
-
return callback(`${origin}: Did not find database to connect to`, null);
|
|
835
|
-
}
|
|
836
|
-
|
|
837
|
-
// work with the provided database
|
|
838
|
-
return MongoClient.connect(usedburl, usedbopt, (error, db) => {
|
|
839
|
-
if (error) {
|
|
840
|
-
// no Database connection available
|
|
841
|
-
log.warn(`${origin}: Error connecting to Mongo ${error}`);
|
|
842
|
-
return callback(`${origin}: Error connecting to Mongo ${error}`, null);
|
|
884
|
+
// if using file storage
|
|
885
|
+
if (storage === Storage.FILESYSTEM) {
|
|
886
|
+
// save it to file in the adapter storage directory
|
|
887
|
+
const saved = saveAsJson(collectionName, data);
|
|
888
|
+
if (!saved) {
|
|
889
|
+
log.warn(`${origin}: Data has not been saved to file storage`);
|
|
890
|
+
return callback(`${origin}: Data has not been saved to file storage`, null);
|
|
891
|
+
}
|
|
892
|
+
log.debug(`${origin}: Data saved in file storage`);
|
|
893
|
+
return callback(null, saved);
|
|
843
894
|
}
|
|
844
895
|
|
|
845
|
-
//
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
896
|
+
// if using MongoDB storage
|
|
897
|
+
if (storage === Storage.DBINFO || storage === Storage.ADAPTERDB) {
|
|
898
|
+
// Add the data to the database
|
|
899
|
+
// insertOne has only 2 parameters: the data to be added & callback. Not an identifier.
|
|
900
|
+
return mongoClient.db(database).collection(collectionName).insertOne(dataInfo, (err, result) => {
|
|
901
|
+
if (storage === Storage.DBINFO && mongoClient) mongoClient.close();
|
|
902
|
+
if (err) {
|
|
903
|
+
log.warn(`${origin}: Failed to insert data in collection ${err}`);
|
|
904
|
+
return callback(`${origin}: Failed to insert data in collection ${err}`, null);
|
|
905
|
+
}
|
|
906
|
+
log.spam(`${origin}: db response ${result}`);
|
|
907
|
+
log.debug(`${origin}: Data saved in database`);
|
|
908
|
+
return callback(null, data);
|
|
909
|
+
});
|
|
910
|
+
}
|
|
857
911
|
});
|
|
858
912
|
} catch (ex) {
|
|
859
913
|
log.warn(`${origin}: Caught Exception - ${ex}`);
|
|
@@ -873,11 +927,6 @@ class DBUtil {
|
|
|
873
927
|
const origin = `${this.myid}-dbUtil-createIndex`;
|
|
874
928
|
log.trace(origin);
|
|
875
929
|
|
|
876
|
-
// set the default database
|
|
877
|
-
let usedburl = this.dburl;
|
|
878
|
-
let usedbopt = this.dboptions;
|
|
879
|
-
let usedb = this.database;
|
|
880
|
-
|
|
881
930
|
try {
|
|
882
931
|
// verify the required data has been provided
|
|
883
932
|
if (!collectionName) {
|
|
@@ -889,47 +938,31 @@ class DBUtil {
|
|
|
889
938
|
return callback(`${origin}: Missing Specs`, null);
|
|
890
939
|
}
|
|
891
940
|
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
return callback(`${origin}: No database - no index`, null);
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
// if we were passed in dbInfo, verify it contains all the pieces we want
|
|
900
|
-
if (!dbInfo || !dbInfo.dburl || !dbInfo.database) {
|
|
901
|
-
log.warn(`${origin}: Improper dbInfo provided should have dburl and database! Using default database`);
|
|
902
|
-
}
|
|
903
|
-
// if we were passed in dbInfo, that should supercede the default database
|
|
904
|
-
if (dbInfo && dbInfo.dburl && dbInfo.database) {
|
|
905
|
-
usedburl = dbInfo.dburl;
|
|
906
|
-
usedbopt = dbInfo.dboptions;
|
|
907
|
-
usedb = dbInfo.database;
|
|
908
|
-
}
|
|
909
|
-
|
|
910
|
-
if (!usedburl || !usedb) {
|
|
911
|
-
log.warn(`${origin}: Did not find database to connect to`);
|
|
912
|
-
return callback(`${origin}: Did not find database to connect to`, null);
|
|
913
|
-
}
|
|
941
|
+
return this.determineStorage(dbInfo, (storageError, storage, mongoClient, database) => {
|
|
942
|
+
if (storageError) {
|
|
943
|
+
return callback(`${origin}: ${storageError}`, null);
|
|
944
|
+
}
|
|
914
945
|
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
return callback(`${origin}: Error connecting to Mongo ${error}`, null);
|
|
946
|
+
// if using file storage
|
|
947
|
+
if (storage === Storage.FILESYSTEM) {
|
|
948
|
+
// no database - no index
|
|
949
|
+
log.warn(`${origin}: No database - no index`);
|
|
950
|
+
return callback(`${origin}: No database - no index`, null);
|
|
921
951
|
}
|
|
922
952
|
|
|
923
|
-
//
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
953
|
+
// if using MongoDB storage
|
|
954
|
+
if (storage === Storage.DBINFO || storage === Storage.ADAPTERDB) {
|
|
955
|
+
// create the index on the collection
|
|
956
|
+
return mongoClient.db(database).collection(collectionName).createIndex(fieldOrSpec, options || {}, (err, res) => {
|
|
957
|
+
if (storage === Storage.DBINFO && mongoClient) mongoClient.close();
|
|
958
|
+
if (err) {
|
|
959
|
+
log.warn(`${origin}: Failed to index data in collection ${err}`);
|
|
960
|
+
return callback(`${origin}: Failed to index data in collection ${err}`, null);
|
|
961
|
+
}
|
|
962
|
+
log.debug(`${origin}: Data in collection indexed`);
|
|
963
|
+
return callback(null, res);
|
|
964
|
+
});
|
|
965
|
+
}
|
|
933
966
|
});
|
|
934
967
|
} catch (ex) {
|
|
935
968
|
log.warn(`${origin}: Caught Exception - ${ex}`);
|
|
@@ -950,11 +983,6 @@ class DBUtil {
|
|
|
950
983
|
const origin = `${this.myid}-dbUtil-countDocuments`;
|
|
951
984
|
log.trace(origin);
|
|
952
985
|
|
|
953
|
-
// set the default database
|
|
954
|
-
let usedburl = this.dburl;
|
|
955
|
-
let usedbopt = this.dboptions;
|
|
956
|
-
let usedb = this.database;
|
|
957
|
-
|
|
958
986
|
try {
|
|
959
987
|
// verify the required data has been provided
|
|
960
988
|
if (!collectionName) {
|
|
@@ -962,52 +990,36 @@ class DBUtil {
|
|
|
962
990
|
return callback(`${origin}: Missing Collection Name`, null);
|
|
963
991
|
}
|
|
964
992
|
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
const data = countJSON(collectionName, query);
|
|
969
|
-
if (!data || data === -1) {
|
|
970
|
-
log.warn(`${origin}: Could not count data from file storage`);
|
|
971
|
-
return callback(`${origin}: Could not count data from file storage`, null);
|
|
993
|
+
return this.determineStorage(dbInfo, (storageError, storage, mongoClient, database) => {
|
|
994
|
+
if (storageError) {
|
|
995
|
+
return callback(`${origin}: ${storageError}`, null);
|
|
972
996
|
}
|
|
973
|
-
log.debug(`${origin}: Count from file storage ${data}`);
|
|
974
|
-
return callback(null, data);
|
|
975
|
-
}
|
|
976
997
|
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
if (!usedburl || !usedb) {
|
|
989
|
-
log.warn(`${origin}: Did not find database to connect to`);
|
|
990
|
-
return callback(`${origin}: Did not find database to connect to`, null);
|
|
991
|
-
}
|
|
992
|
-
|
|
993
|
-
// work with the provided database
|
|
994
|
-
return MongoClient.connect(usedburl, usedbopt, (error, db) => {
|
|
995
|
-
if (error) {
|
|
996
|
-
// no Database connection available
|
|
997
|
-
log.warn(`${origin}: Error connecting to Mongo ${error}`);
|
|
998
|
-
return callback(`${origin}: Error connecting to Mongo ${error}`, null);
|
|
998
|
+
// if using file storage
|
|
999
|
+
if (storage === Storage.FILESYSTEM) {
|
|
1000
|
+
// get a count from the JSON
|
|
1001
|
+
const data = countJSON(collectionName, query);
|
|
1002
|
+
if (!data || data === -1) {
|
|
1003
|
+
log.warn(`${origin}: Could not count data from file storage`);
|
|
1004
|
+
return callback(`${origin}: Could not count data from file storage`, null);
|
|
1005
|
+
}
|
|
1006
|
+
log.debug(`${origin}: Count from file storage ${data}`);
|
|
1007
|
+
return callback(null, data);
|
|
999
1008
|
}
|
|
1000
1009
|
|
|
1001
|
-
//
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1010
|
+
// if using MongoDB storage
|
|
1011
|
+
if (storage === Storage.DBINFO || storage === Storage.ADAPTERDB) {
|
|
1012
|
+
// get the count from mongo
|
|
1013
|
+
return mongoClient.db(database).collection(collectionName).count(query, options, (err, res) => {
|
|
1014
|
+
if (storage === Storage.DBINFO && mongoClient) mongoClient.close();
|
|
1015
|
+
if (err) {
|
|
1016
|
+
log.warn(`${origin}: Failed to count collection ${err}`);
|
|
1017
|
+
return callback(`${origin}: Failed to count collection ${err}`, null);
|
|
1018
|
+
}
|
|
1019
|
+
log.debug(`${origin}: Count from database ${res}`);
|
|
1020
|
+
return callback(null, res);
|
|
1021
|
+
});
|
|
1022
|
+
}
|
|
1011
1023
|
});
|
|
1012
1024
|
} catch (ex) {
|
|
1013
1025
|
log.warn(`${origin}: Caught Exception - ${ex}`);
|
|
@@ -1028,11 +1040,6 @@ class DBUtil {
|
|
|
1028
1040
|
const origin = `${this.myid}-dbUtil-delete`;
|
|
1029
1041
|
log.trace(origin);
|
|
1030
1042
|
|
|
1031
|
-
// set the default database
|
|
1032
|
-
let usedburl = this.dburl;
|
|
1033
|
-
let usedbopt = this.dboptions;
|
|
1034
|
-
let usedb = this.database;
|
|
1035
|
-
|
|
1036
1043
|
try {
|
|
1037
1044
|
// verify the required data has been provided
|
|
1038
1045
|
if (!collectionName) {
|
|
@@ -1048,51 +1055,46 @@ class DBUtil {
|
|
|
1048
1055
|
return callback(`${origin}: Missing Multiple flag`, null);
|
|
1049
1056
|
}
|
|
1050
1057
|
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
if (!fs.existsSync(`${adapterDir}/storage/${collectionName}.json`)) {
|
|
1055
|
-
log.warn(`${origin}: Collection ${collectionName} does not exist`);
|
|
1056
|
-
return callback(null, `${origin}: Collection ${collectionName} does not exist`);
|
|
1057
|
-
}
|
|
1058
|
-
// remove the item from the collection
|
|
1059
|
-
const deld = removeFromJSON(collectionName, filter, multiple);
|
|
1060
|
-
if (!deld) {
|
|
1061
|
-
log.warn(`${origin}: Data has not been deleted from file storage`);
|
|
1062
|
-
return callback(`${origin}: Data has not been deleted from file storage`, null);
|
|
1058
|
+
return this.determineStorage(dbInfo, (storageError, storage, mongoClient, database) => {
|
|
1059
|
+
if (storageError) {
|
|
1060
|
+
return callback(`${origin}: ${storageError}`, null);
|
|
1063
1061
|
}
|
|
1064
|
-
log.debug(`${origin}: Data has been deleted from file storage`);
|
|
1065
|
-
return callback(null, deld);
|
|
1066
|
-
}
|
|
1067
1062
|
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
// work with the provided database
|
|
1085
|
-
return MongoClient.connect(usedburl, usedbopt, (error, db) => {
|
|
1086
|
-
if (error) {
|
|
1087
|
-
// no Database connection available
|
|
1088
|
-
log.warn(`${origin}: Error connecting to Mongo ${error}`);
|
|
1089
|
-
return callback(`${origin}: Error connecting to Mongo ${error}`, null);
|
|
1063
|
+
// if using file storage
|
|
1064
|
+
if (storage === Storage.FILESYSTEM) {
|
|
1065
|
+
// verify the collection exists
|
|
1066
|
+
if (!fs.existsSync(`${adapterDir}/storage/${collectionName}.json`)) {
|
|
1067
|
+
log.warn(`${origin}: Collection ${collectionName} does not exist`);
|
|
1068
|
+
return callback(null, `${origin}: Collection ${collectionName} does not exist`);
|
|
1069
|
+
}
|
|
1070
|
+
// remove the item from the collection
|
|
1071
|
+
const deld = removeFromJSON(collectionName, filter, multiple);
|
|
1072
|
+
if (!deld) {
|
|
1073
|
+
log.warn(`${origin}: Data has not been deleted from file storage`);
|
|
1074
|
+
return callback(`${origin}: Data has not been deleted from file storage`, null);
|
|
1075
|
+
}
|
|
1076
|
+
log.debug(`${origin}: Data has been deleted from file storage`);
|
|
1077
|
+
return callback(null, deld);
|
|
1090
1078
|
}
|
|
1091
1079
|
|
|
1092
|
-
if
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1080
|
+
// if using MongoDB storage
|
|
1081
|
+
if (storage === Storage.DBINFO || storage === Storage.ADAPTERDB) {
|
|
1082
|
+
if (!multiple) {
|
|
1083
|
+
// delete the single item from mongo
|
|
1084
|
+
return mongoClient.db(database).collection(collectionName).deleteOne(filter, options, (err, res) => {
|
|
1085
|
+
if (storage === Storage.DBINFO && mongoClient) mongoClient.close();
|
|
1086
|
+
if (err) {
|
|
1087
|
+
log.warn(`${origin}: Failed to delete data from database ${err}`);
|
|
1088
|
+
return callback(`${origin}: Failed delete data from database ${err}`, null);
|
|
1089
|
+
}
|
|
1090
|
+
log.debug(`${origin}: Data has been deleted from database`);
|
|
1091
|
+
return callback(null, res);
|
|
1092
|
+
});
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
// delete the multiple items from mongo
|
|
1096
|
+
return mongoClient.db(database).collection(collectionName).deleteMany(filter, options, (err, res) => {
|
|
1097
|
+
if (storage === Storage.DBINFO && mongoClient) mongoClient.close();
|
|
1096
1098
|
if (err) {
|
|
1097
1099
|
log.warn(`${origin}: Failed to delete data from database ${err}`);
|
|
1098
1100
|
return callback(`${origin}: Failed delete data from database ${err}`, null);
|
|
@@ -1101,17 +1103,6 @@ class DBUtil {
|
|
|
1101
1103
|
return callback(null, res);
|
|
1102
1104
|
});
|
|
1103
1105
|
}
|
|
1104
|
-
|
|
1105
|
-
// delete the multiple items from mongo
|
|
1106
|
-
return db.db(usedb).collection(collectionName).deleteMany(filter, options, (err, res) => {
|
|
1107
|
-
db.close();
|
|
1108
|
-
if (err) {
|
|
1109
|
-
log.warn(`${origin}: Failed to delete data from database ${err}`);
|
|
1110
|
-
return callback(`${origin}: Failed delete data from database ${err}`, null);
|
|
1111
|
-
}
|
|
1112
|
-
log.debug(`${origin}: Data has been deleted from database`);
|
|
1113
|
-
return callback(null, res);
|
|
1114
|
-
});
|
|
1115
1106
|
});
|
|
1116
1107
|
} catch (ex) {
|
|
1117
1108
|
log.warn(`${origin}: Caught Exception - ${ex}`);
|
|
@@ -1133,11 +1124,6 @@ class DBUtil {
|
|
|
1133
1124
|
const origin = `${this.myid}-dbUtil-replaceOne`;
|
|
1134
1125
|
log.trace(origin);
|
|
1135
1126
|
|
|
1136
|
-
// set the default database
|
|
1137
|
-
let usedburl = this.dburl;
|
|
1138
|
-
let usedbopt = this.dboptions;
|
|
1139
|
-
let usedb = this.database;
|
|
1140
|
-
|
|
1141
1127
|
try {
|
|
1142
1128
|
// verify the required data has been provided
|
|
1143
1129
|
if (!collectionName) {
|
|
@@ -1153,58 +1139,42 @@ class DBUtil {
|
|
|
1153
1139
|
return callback(`${origin}: Missing Document`, null);
|
|
1154
1140
|
}
|
|
1155
1141
|
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
const rem = removeFromJSON(collectionName, filter, false);
|
|
1160
|
-
if (rem) {
|
|
1161
|
-
// add the data into the collection
|
|
1162
|
-
const sav = saveAsJson(collectionName, doc);
|
|
1163
|
-
if (sav) {
|
|
1164
|
-
log.debug(`${origin}: Data replaced in file storage`);
|
|
1165
|
-
return callback(null, sav);
|
|
1166
|
-
}
|
|
1167
|
-
log.warn(`${origin}: Could not save doc into file storage`);
|
|
1168
|
-
return callback(`${origin}: Could not save doc into file storage`, null);
|
|
1142
|
+
return this.determineStorage(dbInfo, (storageError, storage, mongoClient, database) => {
|
|
1143
|
+
if (storageError) {
|
|
1144
|
+
return callback(`${origin}: ${storageError}`, null);
|
|
1169
1145
|
}
|
|
1170
|
-
log.warn(`${origin}: Could not delete from file storage`);
|
|
1171
|
-
return callback(`${origin}: Could not delete from file storage`, null);
|
|
1172
|
-
}
|
|
1173
|
-
|
|
1174
|
-
// if we were passed in dbInfo, verify it contains all the pieces we want
|
|
1175
|
-
if (!dbInfo || !dbInfo.dburl || !dbInfo.database) {
|
|
1176
|
-
log.warn(`${origin}: Improper dbInfo provided should have dburl and database! Using default database`);
|
|
1177
|
-
}
|
|
1178
|
-
// if we were passed in dbInfo, that should supercede the default database
|
|
1179
|
-
if (dbInfo && dbInfo.dburl && dbInfo.database) {
|
|
1180
|
-
usedburl = dbInfo.dburl;
|
|
1181
|
-
usedbopt = dbInfo.dboptions;
|
|
1182
|
-
usedb = dbInfo.database;
|
|
1183
|
-
}
|
|
1184
|
-
|
|
1185
|
-
if (!usedburl || !usedb) {
|
|
1186
|
-
log.warn(`${origin}: Did not find database to connect to`);
|
|
1187
|
-
return callback(`${origin}: Did not find database to connect to`, null);
|
|
1188
|
-
}
|
|
1189
1146
|
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1147
|
+
// if using file storage
|
|
1148
|
+
if (storage === Storage.FILESYSTEM) {
|
|
1149
|
+
// remove the data from the collection
|
|
1150
|
+
const rem = removeFromJSON(collectionName, filter, false);
|
|
1151
|
+
if (rem) {
|
|
1152
|
+
// add the data into the collection
|
|
1153
|
+
const sav = saveAsJson(collectionName, doc);
|
|
1154
|
+
if (sav) {
|
|
1155
|
+
log.debug(`${origin}: Data replaced in file storage`);
|
|
1156
|
+
return callback(null, sav);
|
|
1157
|
+
}
|
|
1158
|
+
log.warn(`${origin}: Could not save doc into file storage`);
|
|
1159
|
+
return callback(`${origin}: Could not save doc into file storage`, null);
|
|
1160
|
+
}
|
|
1161
|
+
log.warn(`${origin}: Could not delete from file storage`);
|
|
1162
|
+
return callback(`${origin}: Could not delete from file storage`, null);
|
|
1196
1163
|
}
|
|
1197
1164
|
|
|
1198
|
-
//
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1165
|
+
// if using MongoDB storage
|
|
1166
|
+
if (storage === Storage.DBINFO || storage === Storage.ADAPTERDB) {
|
|
1167
|
+
// replace an items in mongo
|
|
1168
|
+
return mongoClient.db(database).collection(collectionName).replaceOne(filter, doc, options, (err, res) => {
|
|
1169
|
+
if (storage === Storage.DBINFO && mongoClient) mongoClient.close();
|
|
1170
|
+
if (err) {
|
|
1171
|
+
log.warn(`${origin}: Failed to replace data in database ${err}`);
|
|
1172
|
+
return callback(`${origin}: Failed replace data in database ${err}`, null);
|
|
1173
|
+
}
|
|
1174
|
+
log.debug(`${origin}: Data replaced in file storage`);
|
|
1175
|
+
return callback(null, res);
|
|
1176
|
+
});
|
|
1177
|
+
}
|
|
1208
1178
|
});
|
|
1209
1179
|
} catch (ex) {
|
|
1210
1180
|
log.warn(`${origin}: Caught Exception - ${ex}`);
|
|
@@ -1231,11 +1201,6 @@ class DBUtil {
|
|
|
1231
1201
|
const origin = `${this.myid}-dbUtil-find`;
|
|
1232
1202
|
log.trace(origin);
|
|
1233
1203
|
|
|
1234
|
-
// set the default database
|
|
1235
|
-
let usedburl = this.dburl;
|
|
1236
|
-
let usedbopt = this.dboptions;
|
|
1237
|
-
let usedb = this.database;
|
|
1238
|
-
|
|
1239
1204
|
try {
|
|
1240
1205
|
// verify the required data has been provided
|
|
1241
1206
|
if (!collectionName) {
|
|
@@ -1276,55 +1241,45 @@ class DBUtil {
|
|
|
1276
1241
|
return key;
|
|
1277
1242
|
});
|
|
1278
1243
|
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
let toReturn = getFromJson(collectionName, filter);
|
|
1283
|
-
if (toReturn && toReturn.length > limit) {
|
|
1284
|
-
let curEnd = start + limit;
|
|
1285
|
-
if (curEnd < toReturn.length) {
|
|
1286
|
-
curEnd = toReturn.length;
|
|
1287
|
-
}
|
|
1288
|
-
toReturn = toReturn.slice(start, curEnd);
|
|
1244
|
+
return this.determineStorage(dbInfo, (storageError, storage, mongoClient, database) => {
|
|
1245
|
+
if (storageError) {
|
|
1246
|
+
return callback(`${origin}: ${storageError}`, null);
|
|
1289
1247
|
}
|
|
1290
|
-
log.trace(`${origin}: Data retrieved from file storage`);
|
|
1291
|
-
return callback(null, toReturn);
|
|
1292
|
-
}
|
|
1293
|
-
|
|
1294
|
-
// if we were passed in dbInfo, verify it contains all the pieces we want
|
|
1295
|
-
if (!dbInfo || !dbInfo.dburl || !dbInfo.database) {
|
|
1296
|
-
log.warn(`${origin}: Improper dbInfo provided should have dburl and database! Using default database`);
|
|
1297
|
-
}
|
|
1298
|
-
// if we were passed in dbInfo, that should supercede the default database
|
|
1299
|
-
if (dbInfo && dbInfo.dburl && dbInfo.database) {
|
|
1300
|
-
usedburl = dbInfo.dburl;
|
|
1301
|
-
usedbopt = dbInfo.dboptions;
|
|
1302
|
-
usedb = dbInfo.database;
|
|
1303
|
-
}
|
|
1304
|
-
|
|
1305
|
-
if (!usedburl || !usedb) {
|
|
1306
|
-
log.warn(`${origin}: Did not find database to connect to`);
|
|
1307
|
-
return callback(`${origin}: Did not find database to connect to`, null);
|
|
1308
|
-
}
|
|
1309
1248
|
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1249
|
+
// if using file storage
|
|
1250
|
+
if (storage === Storage.FILESYSTEM) {
|
|
1251
|
+
// Find it from file in the adapter
|
|
1252
|
+
let toReturn = getFromJson(collectionName, options);
|
|
1253
|
+
if (collectionName === 'adapter_configs') {
|
|
1254
|
+
log.debug(`${origin}: Data retrieved from file storage`);
|
|
1255
|
+
return callback(null, [toReturn]);
|
|
1256
|
+
}
|
|
1257
|
+
if (toReturn && toReturn.length > limit) {
|
|
1258
|
+
let curEnd = start + limit;
|
|
1259
|
+
if (curEnd < toReturn.length) {
|
|
1260
|
+
curEnd = toReturn.length;
|
|
1261
|
+
}
|
|
1262
|
+
toReturn = toReturn.slice(start, curEnd);
|
|
1263
|
+
}
|
|
1264
|
+
log.debug(`${origin}: Data retrieved from file storage`);
|
|
1265
|
+
return callback(null, toReturn);
|
|
1316
1266
|
}
|
|
1317
1267
|
|
|
1318
|
-
//
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
.
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1268
|
+
// if using MongoDB storage
|
|
1269
|
+
if (storage === Storage.DBINFO || storage === Storage.ADAPTERDB) {
|
|
1270
|
+
// Find the data in the database
|
|
1271
|
+
return mongoClient.db(database).collection(collectionName).find(filter).sort(sort)
|
|
1272
|
+
.skip(start)
|
|
1273
|
+
.limit(limit)
|
|
1274
|
+
.toArray()
|
|
1275
|
+
.then((value) => {
|
|
1276
|
+
log.debug(`${origin}: Data retrieved from database`);
|
|
1277
|
+
return callback(null, value);
|
|
1278
|
+
})
|
|
1279
|
+
.finally(() => {
|
|
1280
|
+
if (storage === Storage.DBINFO && mongoClient) mongoClient.close();
|
|
1281
|
+
});
|
|
1282
|
+
}
|
|
1328
1283
|
});
|
|
1329
1284
|
} catch (ex) {
|
|
1330
1285
|
log.warn(`${origin}: Caught Exception - ${ex}`);
|
|
@@ -1350,11 +1305,6 @@ class DBUtil {
|
|
|
1350
1305
|
const origin = `${this.myid}-dbUtil-findAndModify`;
|
|
1351
1306
|
log.trace(origin);
|
|
1352
1307
|
|
|
1353
|
-
// set the default database
|
|
1354
|
-
let usedburl = this.dburl;
|
|
1355
|
-
let usedbopt = this.dboptions;
|
|
1356
|
-
let usedb = this.database;
|
|
1357
|
-
|
|
1358
1308
|
try {
|
|
1359
1309
|
// verify the required data has been provided
|
|
1360
1310
|
if (collectionName === undefined || collectionName === null || collectionName === '') {
|
|
@@ -1372,54 +1322,36 @@ class DBUtil {
|
|
|
1372
1322
|
returnOriginal: false
|
|
1373
1323
|
};
|
|
1374
1324
|
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
const saved = saveAsJson(collectionName, data);
|
|
1379
|
-
if (!saved) {
|
|
1380
|
-
log.error(`${origin}: Data has not been saved`);
|
|
1381
|
-
return callback(`${origin}: Data has not been saved`, null);
|
|
1325
|
+
return this.determineStorage(dbInfo, (storageError, storage, mongoClient, database) => {
|
|
1326
|
+
if (storageError) {
|
|
1327
|
+
return callback(`${origin}: ${storageError}`, null);
|
|
1382
1328
|
}
|
|
1383
|
-
log.debug(`${origin}: Data modified in file storage`);
|
|
1384
|
-
return callback(null, saved);
|
|
1385
|
-
}
|
|
1386
1329
|
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
if (!usedburl || !usedb) {
|
|
1399
|
-
log.warn(`${origin}: Did not find database to connect to`);
|
|
1400
|
-
return callback(`${origin}: Did not find database to connect to`, null);
|
|
1401
|
-
}
|
|
1402
|
-
|
|
1403
|
-
// connect to the provided database
|
|
1404
|
-
return MongoClient.connect(usedburl, usedbopt, (error, db) => {
|
|
1405
|
-
if (error) {
|
|
1406
|
-
// no Database connection available
|
|
1407
|
-
log.warn(`${origin}: Error connecting to Mongo ${error}`);
|
|
1408
|
-
return callback(`${origin}: Error connecting to Mongo ${error}`, null);
|
|
1330
|
+
// if using file storage
|
|
1331
|
+
if (storage === Storage.FILESYSTEM) {
|
|
1332
|
+
// save it to file in the adapter storage directory
|
|
1333
|
+
const saved = saveAsJson(collectionName, data);
|
|
1334
|
+
if (!saved) {
|
|
1335
|
+
log.error(`${origin}: Data has not been saved`);
|
|
1336
|
+
return callback(`${origin}: Data has not been saved`, null);
|
|
1337
|
+
}
|
|
1338
|
+
log.debug(`${origin}: Data modified in file storage`);
|
|
1339
|
+
return callback(null, saved);
|
|
1409
1340
|
}
|
|
1410
1341
|
|
|
1411
|
-
//
|
|
1412
|
-
|
|
1413
|
-
//
|
|
1414
|
-
db.
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1342
|
+
// if using MongoDB storage
|
|
1343
|
+
if (storage === Storage.DBINFO || storage === Storage.ADAPTERDB) {
|
|
1344
|
+
// find and modify the data in the database
|
|
1345
|
+
return mongoClient.db(database).collection(collectionName).findOneAndUpdate((filter || {}), data, options, (err, result) => {
|
|
1346
|
+
if (storage === Storage.DBINFO && mongoClient) mongoClient.close();
|
|
1347
|
+
if (err) {
|
|
1348
|
+
log.warn(`${origin}: Failed to modified data in database ${err}`);
|
|
1349
|
+
return callback(`${origin}: Failed modified data in database ${err}`, null);
|
|
1350
|
+
}
|
|
1351
|
+
log.debug(`${origin}: Data modified in database`);
|
|
1352
|
+
return callback(null, result);
|
|
1353
|
+
});
|
|
1354
|
+
}
|
|
1423
1355
|
});
|
|
1424
1356
|
} catch (ex) {
|
|
1425
1357
|
log.warn(`${origin}: Caught Exception - ${ex}`);
|