node-simple-collectionmodel
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -87,3 +87,3 @@ 'use strict'; | ||
async close() { | ||
async closeAll() { | ||
await this.close(this.modelName); | ||
@@ -259,51 +259,57 @@ await this.close('sequence'); | ||
async save($dataObject) { | ||
let dataObject = { ...$dataObject }; | ||
return new Promise(async (resolve, reject) => { | ||
let dataObject = { ...$dataObject }; | ||
// If no data input is given, assume we are going to save this.document | ||
if (typeof dataObject === 'undefined') { | ||
dataObject = this.document; | ||
} | ||
// If we are saving the native mongoose object we can just call save() | ||
if (dataObject instanceof this.Model) { | ||
return dataObject.save(); | ||
} | ||
// If dataObject is still empty, bail out | ||
if (typeof dataObject === 'undefined') { | ||
return Promise.reject(new Error('No data to save!')); | ||
} | ||
if (!dataObject.id) { | ||
dataObject.id = await this.getNextSequence(); | ||
} | ||
// Set update time: | ||
dataObject.updatedDate = new Date(); | ||
// If no data input is given, assume we are going to save this.document | ||
if (typeof dataObject === 'undefined') { | ||
dataObject = this.document; | ||
} | ||
// If we are saving the native mongoose object we can just call save() | ||
if (dataObject instanceof this.Model) { | ||
return dataObject.save(); | ||
} | ||
// If dataObject is still empty, bail out | ||
if (typeof dataObject === 'undefined') { | ||
return reject(new Error('No data to save!')); | ||
} | ||
if (!dataObject.id) { | ||
dataObject.id = await this.getNextSequence(); | ||
} | ||
// Set update time: | ||
dataObject.updatedDate = new Date(); | ||
const opts = { | ||
new: true, | ||
upsert: true, | ||
runValidators: true, | ||
setDefaultsOnInsert: true, | ||
}; | ||
const query = util.cleanObject({ id: dataObject.id }); | ||
try { | ||
const document = await this.Model.findOneAndUpdate(query, dataObject, opts); | ||
if (typeof document === 'object') { | ||
this.document = document; | ||
this.dataObject = document.toObject(); | ||
} | ||
return this.dataObject; | ||
} catch (error) { | ||
const errorMessage = `${this.modelName}.findOneAndUpdate(${JSON.stringify(query, | ||
null, 4)}).error: ${error}`; | ||
console.error(errorMessage); | ||
return new Error(errorMessage); | ||
} | ||
const opts = { | ||
new: true, | ||
upsert: true, | ||
runValidators: true, | ||
setDefaultsOnInsert: true, | ||
}; | ||
const query = util.cleanObject({ id: dataObject.id }); | ||
return this.Model.findOneAndUpdate(query, dataObject, opts, (err, document) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
if (typeof document === 'object') { | ||
this.document = document; | ||
this.dataObject = document.toObject(); | ||
} | ||
return resolve(this.dataObject); | ||
}); | ||
}); | ||
} | ||
async update(obj, query) { | ||
const doc = await this.Model.findOne(query || { id: obj.id }); | ||
if (!doc) { | ||
throw new Error(`Updated failed; document ${JSON.stringify(query || obj.id, null, 4)} not found`); | ||
} | ||
doc.set(obj); | ||
return doc.save(); | ||
return new Promise((resolve, reject) => { | ||
this.Model.findOne(query || { id: obj.id }, (err, document) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
if (!document) { | ||
return reject(new Error(`Updated failed; document ${JSON.stringify(query | ||
|| obj.id, null, 4)} not found`)); | ||
} | ||
document.set(obj); | ||
return resolve(document.save()); | ||
}); | ||
}); | ||
} | ||
@@ -334,3 +340,3 @@ | ||
async getNextSequence() { | ||
try { | ||
return new Promise((resolve, reject) => { | ||
const opts = { | ||
@@ -344,24 +350,31 @@ new: true, | ||
}; | ||
const result = await this.ModelSequence.findOneAndUpdate({ name: this.modelName }, { $inc: { seq: 1 } }, opts); | ||
return result.value.seq + 1; | ||
} catch (err) { | ||
console.error(`mongo.getNextSequence-${this.modelName} ERROR:`, err); | ||
return Promise.reject(err); | ||
} | ||
this.ModelSequence.findOneAndUpdate({ name: this.modelName }, { $inc: { seq: 1 } }, opts, (err, result) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
resolve(result.value.seq + 1); | ||
}); | ||
}); | ||
} | ||
async delete(query) { | ||
const result = await this.Model.deleteOne(query); | ||
if (result.deletedCount === 1) { | ||
return true; | ||
} | ||
return false; | ||
return new Promise((resolve, reject) => { | ||
this.Model.deleteOne(query, (err, result) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
return resolve(result); | ||
}); | ||
}); | ||
} | ||
async deleteMany(query) { | ||
const result = await this.Model.deleteMany(query); | ||
if (result.deletedCount > 0) { | ||
return true; | ||
} | ||
return false; | ||
return new Promise((resolve, reject) => { | ||
this.Model.deleteMany(query, (err, result) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
return resolve(result); | ||
}); | ||
}); | ||
} | ||
@@ -368,0 +381,0 @@ } |
{ | ||
"name": "node-simple-collectionmodel", | ||
"description": "A simpel and nice collection model for mongoose stuff.", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"homepage": "https://github.com/5orenso/node-simple-collectionmodel", | ||
@@ -6,0 +6,0 @@ "repository": { |
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
27519
442