xpress-mongo
Advanced tools
Comparing version 2.0.16 to 2.1.0
{ | ||
"name": "xpress-mongo", | ||
"version": "2.0.16", | ||
"version": "2.1.0", | ||
"description": "Light Weight ODM for mongoDb NodeJs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -476,3 +476,10 @@ import ObjectCollection = require("object-collection"); | ||
private $checkUniqueSchema; | ||
$updateFindOneQuery(query: string | string[] | Record<string, any>): this; | ||
/** | ||
* Get the find query | ||
* @private | ||
*/ | ||
private $findOneQuery; | ||
private $canTalkToDatabase; | ||
} | ||
export = XMongoModel; |
@@ -464,4 +464,3 @@ "use strict"; | ||
update(set, options) { | ||
if (!this.id()) | ||
throw Error("UPDATE_ERROR: Model does not have an _id, so we assume it is not from the database."); | ||
this.$canTalkToDatabase("UPDATE_ERROR"); | ||
return this.set(set).save(options); | ||
@@ -477,7 +476,6 @@ } | ||
updateRaw(update, options) { | ||
if (!this.id()) | ||
throw Error("UPDATE_RAW_ERROR: Model does not have an _id, so we assume it is not from the database."); | ||
this.$canTalkToDatabase("UPDATE_RAW_ERROR"); | ||
return this.$static() | ||
.native() | ||
.updateOne({ _id: this.id() }, update, options); | ||
.updateOne(this.$findOneQuery(), update, options); | ||
} | ||
@@ -491,4 +489,4 @@ /** | ||
return new Promise(async (resolve, reject) => { | ||
const id = this.id(); | ||
if (id && !this.meta.usingCustomId) { | ||
const findOneQuery = this.$findOneQuery(); | ||
if (findOneQuery && !this.meta.usingCustomId) { | ||
await (0, inbuilt_1.RunOnEvent)("update", this); | ||
@@ -515,5 +513,5 @@ let $set = {}; | ||
} | ||
return this.$static() | ||
this.$static() | ||
.native() | ||
.updateOne({ _id: this.id() }, { $set }, options, (error, res) => { | ||
.updateOne(findOneQuery, { $set }, options, (error, res) => { | ||
if (error) { | ||
@@ -572,2 +570,3 @@ return reject(error); | ||
unset(keys, options = {}) { | ||
this.$canTalkToDatabase("UNSET_ERROR"); | ||
// Throw Error if keys is undefined | ||
@@ -592,3 +591,3 @@ if (!keys) | ||
.native() | ||
.updateOne({ _id: this.id() }, { $unset }, options, (error, res) => { | ||
.updateOne(this.$findOneQuery(), { $unset }, options, (error, res) => { | ||
if (error) | ||
@@ -759,6 +758,6 @@ return reject(error); | ||
async delete() { | ||
const _id = this.id(); | ||
if (_id) { | ||
const findOneQuery = this.$findOneQuery(); | ||
if (findOneQuery) { | ||
// Delete Document | ||
const result = await this.$static().native().deleteOne({ _id }); | ||
const result = await this.$static().native().deleteOne(findOneQuery); | ||
(0, inbuilt_1.RunOnEvent)("deleted", this).finally(() => { }); | ||
@@ -965,3 +964,3 @@ return result; | ||
return resolve(data); | ||
return resolve(this.use(data)); | ||
return resolve(this.use(data).$updateFindOneQuery(query)); | ||
}); | ||
@@ -1280,6 +1279,7 @@ }); | ||
async $refreshData(options) { | ||
if (!this.id()) | ||
throw Error("Error refreshing data, _id not found in current model."); | ||
const findOneQuery = this.$findOneQuery(); | ||
if (!findOneQuery) | ||
throw Error("Error refreshing data, _id/findOneQuery not found in current model."); | ||
const Model = this.$static(); | ||
const value = await Model.findById(this.id(), options); | ||
const value = await Model.findOne(findOneQuery, options); | ||
if (!value) | ||
@@ -1395,3 +1395,43 @@ throw Error("Error refreshing data, Refresh result is null"); | ||
} | ||
$updateFindOneQuery(query) { | ||
// if string, set to object with value of field | ||
if (typeof query === "string") { | ||
this.meta.findQuery = { [query]: this.get(query) }; | ||
} | ||
// if array, populate object with fields and value. | ||
else if (Array.isArray(query)) { | ||
this.meta.findQuery = _.pick(this.data, query); | ||
} | ||
// if object, populate object with fields and value. | ||
else if (typeof query === "object") { | ||
this.meta.findQuery = query; | ||
} | ||
// Else throw error | ||
else { | ||
throw new Error("Invalid query type"); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Get the find query | ||
* @private | ||
*/ | ||
$findOneQuery() { | ||
const _id = this.id(); | ||
if (_id) { | ||
return { _id }; | ||
} | ||
else if (this.meta.findQuery) { | ||
return this.meta.findQuery; | ||
} | ||
else { | ||
return undefined; | ||
} | ||
} | ||
$canTalkToDatabase(name) { | ||
if (this.meta.usingCustomId || !this.$findOneQuery()) | ||
throw Error(`${name}: Model does not have an _id, so we assume it is not from the database.`); | ||
return this; | ||
} | ||
} | ||
module.exports = XMongoModel; |
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
100416
3129