@itentialopensource/adapter-utils
Advanced tools
Comparing version 4.23.2 to 4.24.0
## 4.24.0 [11-01-2019] | ||
* Resolve PH-46289 "Minor/" | ||
Closes PH-46289 | ||
See merge request itentialopensource/adapter-utils!135 | ||
--- | ||
## 4.23.2 [10-29-2019] | ||
@@ -3,0 +13,0 @@ |
@@ -303,2 +303,3 @@ /* Required libraries. */ | ||
this.dbpasswd = null; | ||
this.uri = null; | ||
@@ -311,9 +312,12 @@ if (properties.mongo) { | ||
this.dbpasswd = properties.mongo.password; | ||
this.uri = `mongodb://${this.dbhost}:${this.dbport}`; | ||
} | ||
// this.dbhost = 'localhost'; | ||
// this.dbport = 27017; | ||
// this.database = 'dbUtil'; | ||
this.uri = `mongodb://${this.dbhost}:${this.dbport}`; | ||
} | ||
/** | ||
* createCollection creates the provided collection in the file system or database. | ||
* | ||
* @function createCollection | ||
* @param {String} collectionName - the name of the collection to create | ||
*/ | ||
createCollection(collectionName, callback) { | ||
@@ -327,48 +331,46 @@ const origin = `${this.myid}-dbUtil-createCollection`; | ||
log.warn(`${origin}: Missing Collection Name or not string`); | ||
if (typeof collectionName === 'string') { | ||
fs.createFileSync(`${adapterDir}/storage/${collectionName}.json`); | ||
log.info(`File ${collectionName}.json created`); | ||
return callback(`${origin}: Missing Collection Name or not string`, null); | ||
} | ||
// if using file storage | ||
if (this.uri === null) { | ||
// if the collection already exists | ||
if (fs.existsSync(`${adapterDir}/storage/${collectionName}`)) { | ||
log.trace(`${origin}: storage file collection already exists`); | ||
return callback(null, collectionName); | ||
} | ||
return callback('Missing Collection Name or not string', null); | ||
// build the pieces that are needed for collection storage | ||
if (!fs.existsSync(`${adapterDir}/storage`)) { | ||
if (!fs.existsSync(`${adapterDir}`)) { | ||
fs.mkdirSync(`${adapterDir}`); | ||
} | ||
fs.mkdirSync(`${adapterDir}/storage`); | ||
} | ||
fs.mkdirSync(`${adapterDir}/storage/${collectionName}`); | ||
log.trace(`${origin}: storage file collection ${collectionName} created`); | ||
return callback(null, collectionName); | ||
} | ||
// work with the provided database | ||
return MongoClient.connect(this.uri, (error, db) => { | ||
if (error) { | ||
// No Database connection available | ||
log.error('database connection failed, making new file'); | ||
if (fs.existsSync(`${adapterDir}/storage/${collectionName}`)) { | ||
log.info('this collection already exists'); | ||
return callback(null, 'Duplication collection attempt'); | ||
} | ||
if (!fs.existsSync(`${adapterDir}/storage`)) { | ||
if (!fs.existsSync(`${adapterDir}`)) { | ||
fs.mkdirSync(`${adapterDir}`); | ||
} | ||
fs.mkdirSync(`${adapterDir}/storage`); | ||
} | ||
fs.mkdirSync(`${adapterDir}/storage/${collectionName}`); | ||
return callback(null, collectionName); | ||
log.warn(`${origin}: Error connecting to Mongo ${error}`); | ||
return callback(`${origin}: Error connecting to Mongo ${error}`, null); | ||
} | ||
// Add the collection to the database | ||
return db.db(this.database).createCollection(collectionName, (err, res) => { | ||
db.close(); | ||
if (err) { | ||
// error we get back if the collection already existed - not a true error | ||
if (err.codeName === 'NamespaceExists') { | ||
log.info('Duplicate collection attempt; new one was not created'); | ||
return callback(null, 'Duplication collection attempt'); | ||
log.trace(`${origin}: database collection already exists`); | ||
return callback(null, collectionName); | ||
} | ||
log.error(`${origin}: ${err}`); | ||
return callback(err, null); | ||
log.warn(`${origin}: Error creating collection ${error}`); | ||
return callback(`${origin}: Error creating collection ${error}`, null); | ||
} | ||
return db.db(this.database).listCollections().toArray((err1, result) => { | ||
if (err1) { | ||
log.error(`${origin}: ${err1}`); | ||
return callback(err1, null); | ||
} | ||
db.close(); | ||
result.forEach((thing) => { | ||
if (thing.name === collectionName); | ||
return callback(null, 'Duplication collection attempt'); | ||
}); | ||
log.info(`collection ${collectionName} created`); | ||
return callback(null, collectionName); | ||
}); | ||
log.trace(`${origin}: database collection ${collectionName} created`); | ||
return callback(null, collectionName); | ||
}); | ||
@@ -378,6 +380,12 @@ }); | ||
log.warn(`${origin}: Caught Exception - ${ex}`); | ||
return callback(`Caught Exception - ${ex}`, null); | ||
return callback(`${origin}: Caught Exception - ${ex}`, null); | ||
} | ||
} | ||
/** | ||
* removeCollection removes the provided collection from the file system or database. | ||
* | ||
* @function removeCollection | ||
* @param {String} collectionName - the name of the collection to remove | ||
*/ | ||
removeCollection(collectionName, callback) { | ||
@@ -389,28 +397,43 @@ const origin = `${this.myid}-dbUtil-removeCollection`; | ||
// verify the required data has been provided | ||
if (!collectionName) { | ||
log.warn(`${origin}: Missing Collection Name`); | ||
return callback('Missing Collection Name', null); | ||
if (!collectionName || (typeof collectionName !== 'string')) { | ||
log.warn(`${origin}: Missing Collection Name or not string`); | ||
return callback(`${origin}: Missing Collection Name or not string`, null); | ||
} | ||
return MongoClient.connect(this.uri, (err, db) => { | ||
if (err) { | ||
log.info('looking to remove a JSON'); | ||
const deld = deleteJSON(collectionName); | ||
if (deld) { | ||
return callback(null, deld); | ||
} | ||
return callback('unsuccessful JSON deletion', null); | ||
// if using file storage | ||
if (this.uri === null) { | ||
const deld = deleteJSON(collectionName); | ||
if (deld) { | ||
log.trace(`${origin}: storage file collection ${collectionName} removed`); | ||
return callback(null, deld); | ||
} | ||
db.db(this.database).listCollections().toArray((error, result) => { | ||
if (error) { | ||
log.error(`${origin}: ${error}`); | ||
return callback(error, null); | ||
log.trace(`${origin}: could not remove storage file collection`); | ||
return callback(`${origin}: could not remove storage file collection`, null); | ||
} | ||
// work with the provided database | ||
return MongoClient.connect(this.uri, (error, db) => { | ||
if (error) { | ||
// no Database connection available | ||
log.warn(`${origin}: Error connecting to Mongo ${error}`); | ||
return callback(`${origin}: Error connecting to Mongo ${error}`, null); | ||
} | ||
// get the list of collections from the database | ||
db.db(this.database).listCollections().toArray((err, result) => { | ||
if (err) { | ||
log.warn(`${origin}: Failed to get collections ${err}`); | ||
return callback(`${origin}: Failed to get collections ${err}`, null); | ||
} | ||
// go through the collections to get the correct one for removal | ||
result.forEach((elem) => { | ||
if (elem.name === collectionName) { | ||
// now that we found it, remove it | ||
return db.db(this.database).collection(collectionName).drop({}, (err1, res) => { | ||
db.close(); | ||
if (err1) { | ||
log.error(`${origin}: ${err1}`); | ||
return callback(err1, null); | ||
log.warn(`${origin}: Failed to remove collection ${err1}`); | ||
return callback(`${origin}: Failed to remove collection ${err1}`, null); | ||
} | ||
db.close(); | ||
log.trace(`${origin}: database collection ${collectionName} removed`); | ||
return callback(null, collectionName); | ||
@@ -426,3 +449,3 @@ }); | ||
log.warn(`${origin}: Caught Exception - ${ex}`); | ||
return callback(`Caught Exception - ${ex}`, null); | ||
return callback(`${origin}: Caught Exception - ${ex}`, null); | ||
} | ||
@@ -438,3 +461,3 @@ } | ||
*/ | ||
create(collectionName, data, callback) { // THIS WORKS PLEASE DO NOT EDIT | ||
create(collectionName, data, callback) { | ||
const origin = `${this.myid}-dbUtil-create`; | ||
@@ -447,8 +470,12 @@ log.trace(origin); | ||
log.warn(`${origin}: Missing Collection Name`); | ||
return callback('Missing Collection Name', null); | ||
return callback(`${origin}: Missing Collection Name`, null); | ||
} | ||
if (!data) { | ||
log.warn(`${origin}: Missing data to add`); | ||
return callback('Missing data to add', null); | ||
return callback(`${origin}: Missing data to add`, null); | ||
} | ||
if ((data.entity && !data.action) || (!data.entity && data.action)) { | ||
log.warn(`${origin}: Inconsistent entity/action set`); | ||
return callback(`${origin}: Inconsistent entity/action set`, null); | ||
} | ||
@@ -459,18 +486,21 @@ const dataInfo = data; | ||
} | ||
if ((data.entity && !data.action) || (!data.entity && data.action)) { | ||
log.warn(`${origin}: provided with one of entity/action, require both or neither`); | ||
return callback('Inconsistent entity/action set', null); | ||
// if using file storage | ||
if (this.uri === null) { | ||
// save it to file in the adapter storage directory | ||
const saved = saveAsJson(collectionName, data, data.entity, data.action); | ||
if (!saved) { | ||
log.warn(`${origin}: Data has not been saved to file storage`); | ||
return callback(`${origin}: Data has not been saved to file storage`, null); | ||
} | ||
log.trace(`${origin}: Data saved in file storage`); | ||
return callback(null, saved); | ||
} | ||
// work with the provided database | ||
return MongoClient.connect(this.uri, (error, db) => { | ||
if (error) { | ||
// No Database connection available | ||
log.error(`Saving ${collectionName} to JSON File`); | ||
// save it to file in the adapter storage directory | ||
const saved = saveAsJson(collectionName, data, data.entity, data.action); | ||
if (!saved) { | ||
log.error(`${origin}: Data has not been saved`); | ||
return callback('Data has not been saved', null); | ||
} | ||
return callback(null, saved); | ||
// no Database connection available | ||
log.warn(`${origin}: Error connecting to Mongo ${error}`); | ||
return callback(`${origin}: Error connecting to Mongo ${error}`, null); | ||
} | ||
@@ -483,5 +513,6 @@ | ||
if (err) { | ||
log.warn(`${origin}: ${error}`); | ||
return callback(error, null); | ||
log.warn(`${origin}: Failed to insert data in collection ${err}`); | ||
return callback(`${origin}: Failed to insert data in collection ${err}`, null); | ||
} | ||
log.trace(`${origin}: Data saved in database`); | ||
return callback(null, data); | ||
@@ -492,32 +523,51 @@ }); | ||
log.warn(`${origin}: Caught Exception - ${ex}`); | ||
return callback(`Caught Exception - ${ex}`, null); | ||
return callback(`${origin}: Caught Exception - ${ex}`, null); | ||
} | ||
} | ||
/** | ||
* Call to create an index in the database | ||
* | ||
* @function createIndex | ||
* @param {String} collectionName - the collection to index. (required) | ||
* @param {String} fieldOrSpec - what to index. (required) | ||
*/ | ||
createIndex(collectionName, fieldOrSpec, options, callback) { | ||
// callback(error, result) | ||
const origin = `${this.myid}-dbUtil-createIndex`; | ||
log.trace(origin); | ||
try { | ||
// verify the required data has been provided | ||
if (!collectionName) { | ||
log.error('Missing collection name'); | ||
return callback('Missing collection name', null); | ||
log.warn(`${origin}: Missing Collection Name`); | ||
return callback(`${origin}: Missing Collection Name`, null); | ||
} | ||
if (!fieldOrSpec) { | ||
log.error('Missing specs'); | ||
return callback('Missing specs', null); | ||
log.warn(`${origin}: Missing Specs`); | ||
return callback(`${origin}: Missing Specs`, null); | ||
} | ||
// if using file storage | ||
if (this.uri === null) { | ||
// no database - no index | ||
log.warn(`${origin}: No database - no index`); | ||
return callback(`${origin}: No database - no index`, null); | ||
} | ||
// work with the provided database | ||
return MongoClient.connect(this.uri, (error, db) => { | ||
if (error) { | ||
// No Database connection available | ||
log.error('No database connection, not making an index'); | ||
return callback(null, 'No database, no index creation'); | ||
// no Database connection available | ||
log.warn(`${origin}: Error connecting to Mongo ${error}`); | ||
return callback(`${origin}: Error connecting to Mongo ${error}`, null); | ||
} | ||
// create the index on the collection | ||
return db.db(this.database).collection(collectionName).createIndex(fieldOrSpec, options || {}, (err, res) => { | ||
db.close(); | ||
if (error) { | ||
log.error(`${origin}: ${err}`); | ||
return callback(err, null); | ||
if (err) { | ||
log.warn(`${origin}: Failed to index data in collection ${err}`); | ||
return callback(`${origin}: Failed to index data in collection ${err}`, null); | ||
} | ||
log.info('Successful index creation'); | ||
log.trace(`${origin}: Data in collection indexed`); | ||
return callback(null, res); | ||
@@ -527,32 +577,51 @@ }); | ||
} catch (ex) { | ||
log.error(`${origin}: ${ex}`); | ||
return callback(ex, null); | ||
log.warn(`${origin}: Caught Exception - ${ex}`); | ||
return callback(`${origin}: Caught Exception - ${ex}`, null); | ||
} | ||
} | ||
/** | ||
* Call to count the documents in a collection | ||
* | ||
* @function countDocuments | ||
* @param {String} collectionName - the collection to count documents in. (required) | ||
* @param {Object} query - the query to minimize documents you count. (required) | ||
*/ | ||
countDocuments(collectionName, query, options, callback) { | ||
// callback(error, result) | ||
const origin = `${this.myid}-dbUtil-countDocuments`; | ||
log.trace(origin); | ||
try { | ||
if (!collectionName) { | ||
log.error('Missing collection name'); | ||
return callback('Missing collection name', null); | ||
log.warn(`${origin}: Missing Collection Name`); | ||
return callback(`${origin}: Missing Collection Name`, null); | ||
} | ||
// if using file storage | ||
if (this.uri === null) { | ||
const data = countJSON(collectionName, null, null, query); | ||
if (!data || data === -1) { | ||
log.warn(`${origin}: Could not count data from file storage`); | ||
return callback(`${origin}: Could not count data from file storage`, null); | ||
} | ||
log.trace(`${origin}: Count from file storage ${data}`); | ||
return callback(null, data); | ||
} | ||
// work with the provided database | ||
return MongoClient.connect(this.uri, (error, db) => { | ||
if (error) { | ||
// No Database connection available | ||
// log.error('No database connection, not counting documents'); | ||
const data = countJSON(collectionName, null, null, query); | ||
if (!data || data === -1) { | ||
log.error('Could not count from JSON'); | ||
} | ||
return callback(null, data); | ||
// no Database connection available | ||
log.warn(`${origin}: Error connecting to Mongo ${error}`); | ||
return callback(`${origin}: Error connecting to Mongo ${error}`, null); | ||
} | ||
// get the count from mongo | ||
return db.db(this.database).collection(collectionName).count(query, options, (err, res) => { | ||
db.close(); | ||
if (err) { | ||
log.error(`${origin}: ${err}`); | ||
return callback(err, null); | ||
log.warn(`${origin}: Failed to count collection ${err}`); | ||
return callback(`${origin}: Failed to count collection ${err}`, null); | ||
} | ||
log.trace(`${origin}: Count from database ${res}`); | ||
return callback(null, res); | ||
@@ -562,54 +631,76 @@ }); | ||
} catch (ex) { | ||
log.error(`${origin}: ${ex}`); | ||
return callback(ex, null); | ||
log.warn(`${origin}: Caught Exception - ${ex}`); | ||
return callback(`${origin}: Caught Exception - ${ex}`, null); | ||
} | ||
} | ||
/** | ||
* Delete items from a collection | ||
* | ||
* @function delete | ||
* @param {String} collectionName - the collection to remove document from. (required) | ||
* @param {Object} filter - the filter for the document(s) to remove. (required) | ||
*/ | ||
delete(collectionName, filter, options, multiple, callback) { | ||
const origin = `${this.myid}-dbUtil-delete`; | ||
log.trace(origin); | ||
try { | ||
if (!collectionName) { | ||
log.error('Missing collection name'); | ||
return callback('Missing collection name', null); | ||
log.warn(`${origin}: Missing Collection Name`); | ||
return callback(`${origin}: Missing Collection Name`, null); | ||
} | ||
if (!filter) { | ||
log.error('Missing filter'); | ||
return callback('Missing filter', null); | ||
log.warn(`${origin}: Missing Filter`); | ||
return callback(`${origin}: Missing Filter`, null); | ||
} | ||
if (!multiple && multiple !== false) { | ||
log.error('Missing multiple boolean'); | ||
return callback('Missing multiple boolean', null); | ||
log.warn(`${origin}: Missing Multiple flag`); | ||
return callback(`${origin}: Missing Multiple flag`, null); | ||
} | ||
// if using file storage | ||
if (this.uri === null) { | ||
if (!fs.existsSync(`${adapterDir}/storage/${collectionName}.json`)) { | ||
log.warn(`${origin}: Collection ${collectionName} does not exist`); | ||
return callback(null, `${origin}: Collection ${collectionName} does not exist`); | ||
} | ||
const deld = removeFromJSON(collectionName, null, null, filter, multiple); | ||
if (!deld) { | ||
log.warn(`${origin}: Data has not been deleted from file storage`); | ||
return callback(`${origin}: Data has not been deleted from file storage`, null); | ||
} | ||
log.trace(`${origin}: Data has been deleted from file storage`); | ||
return callback(null, deld); | ||
} | ||
// work with the provided database | ||
return MongoClient.connect(this.uri, (error, db) => { | ||
if (error) { | ||
// No Database connection available | ||
log.error('No database connection, deleting from JSON.'); | ||
if (!fs.existsSync(`${adapterDir}/storage/${collectionName}.json`)) { | ||
log.error(`Collection ${collectionName} does not exist`); | ||
return callback(null, 'Nonexistent collection'); | ||
} | ||
const deld = removeFromJSON(collectionName, null, null, filter, multiple); | ||
if (!deld) { | ||
log.error(`${origin}: Data has not been deleted`); | ||
return callback('Data has not been deleted', null); | ||
} | ||
return callback(null, deld); | ||
// no Database connection available | ||
log.warn(`${origin}: Error connecting to Mongo ${error}`); | ||
return callback(`${origin}: Error connecting to Mongo ${error}`, null); | ||
} | ||
if (!multiple) { | ||
// delete the single item from mongo | ||
return db.db(this.database).collection(collectionName).deleteOne(filter, options, (err, res) => { | ||
db.close(); | ||
if (err) { | ||
log.error(`${origin}: ${err}`); | ||
return callback(err, null); | ||
log.warn(`${origin}: Failed to delete data from database ${err}`); | ||
return callback(`${origin}: Failed delete data from database ${err}`, null); | ||
} | ||
log.trace(`${origin}: Data has been deleted from database`); | ||
return callback(null, res); | ||
}); | ||
} | ||
// delete the multiple items from mongo | ||
return db.db(this.database).collection(collectionName).deleteMany(filter, options, (err, res) => { | ||
db.close(); | ||
if (err) { | ||
log.error(`${origin}: ${err}`); | ||
return callback(err, null); | ||
log.warn(`${origin}: Failed to delete data from database ${err}`); | ||
return callback(`${origin}: Failed delete data from database ${err}`, null); | ||
} | ||
log.trace(`${origin}: Data has been deleted from database`); | ||
return callback(null, res); | ||
@@ -619,43 +710,65 @@ }); | ||
} catch (ex) { | ||
log.error(`${origin}: ${ex}`); | ||
return callback(ex, null); | ||
log.warn(`${origin}: Caught Exception - ${ex}`); | ||
return callback(`${origin}: Caught Exception - ${ex}`, null); | ||
} | ||
} | ||
/** | ||
* Replace an item in a collection | ||
* | ||
* @function replaceOne | ||
* @param {String} collectionName - the collection to replace document in. (required) | ||
* @param {Object} filter - the filter for the document(s) to replace. (required) | ||
* @param {Object} doc - the filter for the document(s) to replace. (required) | ||
*/ | ||
replaceOne(collectionName, filter, doc, options, callback) { | ||
const origin = `${this.myid}-dbUtil-replaceOne`; | ||
log.trace(origin); | ||
try { | ||
if (!collectionName) { | ||
log.error('Missing collection name'); | ||
return callback('Missing collection name', null); | ||
log.warn(`${origin}: Missing Collection Name`); | ||
return callback(`${origin}: Missing Collection Name`, null); | ||
} | ||
if (!filter) { | ||
log.error('Missing filter'); | ||
return callback('Missing filter', null); | ||
log.warn(`${origin}: Missing Filter`); | ||
return callback(`${origin}: Missing Filter`, null); | ||
} | ||
if (!doc) { | ||
log.error('Missing doc'); | ||
return callback('Missing doc', null); | ||
log.warn(`${origin}: Missing Document`); | ||
return callback(`${origin}: Missing Document`, null); | ||
} | ||
// if using file storage | ||
if (this.uri === null) { | ||
const rem = removeFromJSON(collectionName, null, null, filter, false); | ||
if (rem) { | ||
const sav = saveAsJson(collectionName, doc, null, null); | ||
if (sav) { | ||
log.trace(`${origin}: Data replaced in file storage`); | ||
return callback(null, sav); | ||
} | ||
log.warn(`${origin}: Could not save doc into file storage`); | ||
return callback(`${origin}: Could not save doc into file storage`, null); | ||
} | ||
log.warn(`${origin}: Could not delete from file storage`); | ||
return callback(`${origin}: Could not delete from file storage`, null); | ||
} | ||
// work with the provided database | ||
return MongoClient.connect(this.uri, (error, db) => { | ||
if (error) { | ||
// No Database connection available | ||
log.error('No database connection, not replacing'); | ||
const rem = removeFromJSON(collectionName, null, null, filter, false); | ||
if (rem) { | ||
const sav = saveAsJson(collectionName, doc, null, null); | ||
if (sav) { | ||
return callback(null, sav); | ||
} | ||
return callback(`${origin}: Could not save doc into JSON`, null); | ||
} | ||
return callback(`${origin}: Could not delete from JSON`, null); | ||
// no Database connection available | ||
log.warn(`${origin}: Error connecting to Mongo ${error}`); | ||
return callback(`${origin}: Error connecting to Mongo ${error}`, null); | ||
} | ||
// replace an items in mongo | ||
return db.db(this.database).collection(collectionName).replaceOne(filter, doc, options, (err, res) => { | ||
db.close(); | ||
if (err) { | ||
log.error(`${origin}: ${err}`); | ||
return callback(err, null); | ||
log.warn(`${origin}: Failed to replace data in database ${err}`); | ||
return callback(`${origin}: Failed replace data in database ${err}`, null); | ||
} | ||
log.trace(`${origin}: Data replaced in file storage`); | ||
return callback(null, res); | ||
@@ -665,4 +778,4 @@ }); | ||
} catch (ex) { | ||
log.error(`${origin}: ${ex}`); | ||
return callback(ex, null); | ||
log.warn(`${origin}: Caught Exception - ${ex}`); | ||
return callback(`${origin}: Caught Exception - ${ex}`, null); | ||
} | ||
@@ -693,17 +806,9 @@ } | ||
if (!collectionName) { | ||
log.warn(`${origin}: Missing collection name`); | ||
return callback('Missing collection name', null); | ||
log.warn(`${origin}: Missing Collection Name`); | ||
return callback(`${origin}: Missing Collection Name`, null); | ||
} | ||
if ((entity && !action) || (!entity && action)) { | ||
log.warn(`${origin}: provided with one of entity/action, require both or neither`); | ||
return callback('Inconsistent entity/action set', null); | ||
log.warn(`${origin}: Inconsistent entity/action set`); | ||
return callback(`${origin}: Inconsistent entity/action set`, null); | ||
} | ||
// if (!entity) { | ||
// log.warn(`${origin}: Missing Entity`); | ||
// return callback('Missing Entity', null); | ||
// } | ||
// if (!action) { | ||
// log.warn(`${origin}: Missing Action`); | ||
// return callback('Missing Action', null); | ||
// } commented out for throttle.js functionality. | ||
@@ -743,20 +848,25 @@ // get the collection so we can run the remove on the collection | ||
// find the items in the collection | ||
return MongoClient.connect(this.uri, async (err, db) => { | ||
if (err) { | ||
// No Database connection available | ||
log.info(`looking for ${entity} in JSON File`); | ||
// Find it from file in the adapter | ||
let toReturn = getFromJson(collectionName, entity, action, filter); | ||
if (toReturn && toReturn.length > limit) { | ||
let curEnd = start + limit; | ||
if (curEnd < toReturn.length) { | ||
curEnd = toReturn.length; | ||
} | ||
toReturn = toReturn.slice(start, curEnd); | ||
// if using file storage | ||
if (this.uri === null) { | ||
// Find it from file in the adapter | ||
let toReturn = getFromJson(collectionName, entity, action, filter); | ||
if (toReturn && toReturn.length > limit) { | ||
let curEnd = start + limit; | ||
if (curEnd < toReturn.length) { | ||
curEnd = toReturn.length; | ||
} | ||
return callback(null, toReturn); | ||
toReturn = toReturn.slice(start, curEnd); | ||
} | ||
log.trace(`${origin}: Data retrieved from file storage`); | ||
return callback(null, toReturn); | ||
} | ||
// use the database | ||
return MongoClient.connect(this.uri, async (error, db) => { | ||
if (error) { | ||
// no Database connection available | ||
log.warn(`${origin}: Error connecting to Mongo ${error}`); | ||
return callback(`${origin}: Error connecting to Mongo ${error}`, null); | ||
} | ||
// Find the data in the database | ||
@@ -769,2 +879,3 @@ const col = entity || 'metrics'; | ||
db.close(); | ||
log.trace(`${origin}: Data retrieved from database`); | ||
return callback(null, value); | ||
@@ -775,3 +886,3 @@ }); | ||
log.warn(`${origin}: Caught Exception - ${ex}`); | ||
return callback(`Caught Exception - ${ex}`, null); | ||
return callback(`${origin}: Caught Exception - ${ex}`, null); | ||
} | ||
@@ -800,7 +911,7 @@ } | ||
log.warn(`${origin}: Missing Collection Name`); | ||
return callback('Missing Collection Name', null); | ||
return callback(`${origin}: Missing Collection Name`, null); | ||
} | ||
if (!data) { | ||
log.warn(`${origin}: Missing data for modification`); | ||
return callback('Missing data for modification', null); | ||
return callback(`${origin}: Missing data for modification`, null); | ||
} | ||
@@ -833,24 +944,32 @@ | ||
// get the collection so we can run the remove on the collection | ||
return MongoClient.connect(this.uri || '', (err, db) => { | ||
if (err) { | ||
// No Database connection available | ||
log.info(`Saving ${collectionName} to JSON File`); | ||
// save it to file in the adapter storage directory | ||
const saved = saveAsJson(collectionName, data, ent, act); | ||
if (!saved) { | ||
log.error(`${origin}: Data has not been saved`); | ||
return callback('Data has not been saved', null); | ||
} | ||
return callback(null, saved); | ||
// if using file storage | ||
if (this.uri === null) { | ||
// save it to file in the adapter storage directory | ||
const saved = saveAsJson(collectionName, data, ent, act); | ||
if (!saved) { | ||
log.error(`${origin}: Data has not been saved`); | ||
return callback(`${origin}: Data has not been saved`, null); | ||
} | ||
log.trace(`${origin}: Data modified in file storage`); | ||
return callback(null, saved); | ||
} | ||
// use the database | ||
return MongoClient.connect(this.uri || '', (error, db) => { | ||
if (error) { | ||
// no Database connection available | ||
log.warn(`${origin}: Error connecting to Mongo ${error}`); | ||
return callback(`${origin}: Error connecting to Mongo ${error}`, null); | ||
} | ||
// Modify the data in the database | ||
const coln = db.db(this.database).collection(collectionName); | ||
return coln.findOneAndUpdate((filter || {}), data, options, (error, result) => { | ||
return coln.findOneAndUpdate((filter || {}), data, options, (err, result) => { | ||
db.close(); | ||
if (error) { | ||
log.warn(`${origin}: ${error}`); | ||
return callback(error, null); | ||
if (err) { | ||
log.warn(`${origin}: Failed to modified data in database ${err}`); | ||
return callback(`${origin}: Failed modified data in database ${err}`, null); | ||
} | ||
log.trace(`${origin}: Data modified in database`); | ||
return callback(null, result); | ||
@@ -861,3 +980,3 @@ }); | ||
log.warn(`${origin}: Caught Exception - ${ex}`); | ||
return callback(`Caught Exception - ${ex}`, null); | ||
return callback(`${origin}: Caught Exception - ${ex}`, null); | ||
} | ||
@@ -1078,2 +1197,3 @@ } | ||
} | ||
module.exports = DBUtil; |
@@ -290,3 +290,3 @@ /* @copyright Itential, LLC 2018-9 */ | ||
if (entitySchema.requestDatatype.toUpperCase() !== 'JSON' && entitySchema.requestDatatype.toUpperCase() !== 'XML' | ||
&& entitySchema.requestDatatype.toUpperCase() !== 'URLENCODE') { | ||
&& entitySchema.requestDatatype.toUpperCase() !== 'URLENCODE' && entitySchema.requestDatatype.toUpperCase() !== 'FORM') { | ||
entitySchema.requestDatatype = 'PLAIN'; | ||
@@ -293,0 +293,0 @@ } |
{ | ||
"name": "@itentialopensource/adapter-utils", | ||
"version": "4.23.2", | ||
"version": "4.24.0", | ||
"description": "Itential Adapter Utility Libraries", | ||
@@ -32,2 +32,3 @@ "scripts": { | ||
"ejs": "^2.7.1", | ||
"form-data": "^2.5.1", | ||
"fs-extra": "^7.0.0", | ||
@@ -34,0 +35,0 @@ "https-proxy-agent": "^2.2.1", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1513759
13226
14
+ Addedform-data@^2.5.1
+ Addedasynckit@0.4.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedform-data@2.5.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)