@andrewscwei/mongodb-odm
Advanced tools
Comparing version 0.45.0 to 0.46.0
@@ -75,3 +75,3 @@ "use strict"; | ||
if (!this.schema) | ||
throw new Error('This model has no schema, you must define this static property in the derived class'); | ||
throw new Error(`[${this.constructor.name}] This model has no schema, you must define this static property in the derived class`); | ||
return db.getCollection(this.schema.collection); | ||
@@ -104,3 +104,3 @@ }); | ||
if (!is_1.default.function_(func)) | ||
throw new Error(`Property "${key}" in randomProps must be a function`); | ||
throw new Error(`[${this.schema.model}] Property "${key}" in randomProps must be a function`); | ||
// Use provided random function if provided in the schema. | ||
@@ -131,3 +131,3 @@ o[key] = func(); | ||
if (!this.schema) | ||
throw new Error('This model has no schema, you must define this static proerty in the derived class'); | ||
throw new Error(`[${this.constructor.name}] This model has no schema, you must define this static proerty in the derived class`); | ||
// Check if the argument conforms to aggregation factory specs. | ||
@@ -157,5 +157,5 @@ if (queryOrSpecs && Object.keys(queryOrSpecs).some(val => val.startsWith('$'))) { | ||
if (is_1.default.nullOrUndefined(result)) | ||
throw new Error(`No results found while identifying this ${this.schema.model} using the query ${JSON.stringify(query, null, 0)}`); | ||
throw new Error(`[${this.schema.model}] No results found while identifying this ${this.schema.model} using the query ${JSON.stringify(query, null, 0)}`); | ||
if (!mongodb_1.ObjectID.isValid(result._id)) | ||
throw new Error(`ID of ${result} is not a valid ObjectID`); | ||
throw new Error(`[${this.schema.model}] ID of ${result} is not a valid ObjectID`); | ||
return result._id; | ||
@@ -226,3 +226,3 @@ }); | ||
if (results.length !== 1) | ||
throw new Error('More or less than 1 random document found even though only 1 was supposed to be found.'); | ||
throw new Error(`[${this.schema.model}] More or less than 1 random document found even though only 1 was supposed to be found.`); | ||
return results[0]; | ||
@@ -233,3 +233,3 @@ } | ||
if (results.length === 0) | ||
throw new Error('No document found with provided query'); | ||
throw new Error(`[${this.schema.model}] No document found with provided query`); | ||
return results[0]; | ||
@@ -296,3 +296,3 @@ } | ||
if (this.schema.noInserts === true) | ||
throw new Error('Insertions are disallowed for this model'); | ||
throw new Error(`[${this.schema.model}] Insertions are disallowed for this model`); | ||
// Apply before insert handler. | ||
@@ -305,7 +305,7 @@ const t = yield this.beforeInsert(doc || (yield this.randomFields()), Object.assign({ strict: true }, options)); | ||
if (results.result.ok !== 1) | ||
throw new Error('Unable to insert document'); | ||
throw new Error(`[${this.schema.model}] Unable to insert document`); | ||
if (results.ops.length > 1) | ||
throw new Error('Somehow insertOne() op inserted more than 1 document'); | ||
throw new Error(`[${this.schema.model}] Somehow insertOne() op inserted more than 1 document`); | ||
if (results.ops.length < 1) | ||
throw new Error('Unable to insert document'); | ||
throw new Error(`[${this.schema.model}] Unable to insert document`); | ||
const o = results.ops[0]; | ||
@@ -359,3 +359,3 @@ // Apply after insert handler. | ||
if ((this.schema.noInserts === true) || (this.schema.noInsertMany === true)) | ||
throw new Error('Multiple insertions are disallowed for this model'); | ||
throw new Error(`[${this.schema.model}] Multiple insertions are disallowed for this model`); | ||
const n = docs.length; | ||
@@ -372,3 +372,3 @@ const t = new Array(n); | ||
if (results.result.ok !== 1) | ||
throw new Error('Unable to insert many documents'); | ||
throw new Error(`[${this.schema.model}] Unable to insert many documents`); | ||
const o = results.ops; | ||
@@ -409,3 +409,3 @@ const m = o.length; | ||
if (this.schema.noUpdates === true) | ||
throw new Error('Updates are disallowed for this model'); | ||
throw new Error(`[${this.schema.model}] Updates are disallowed for this model`); | ||
const collection = yield this.getCollection(); | ||
@@ -416,3 +416,3 @@ const [q, u] = (options.skipHooks === true) ? [query, update] : yield this.beforeUpdate(query, update, options); | ||
if (!is_1.default.plainObject(q)) { | ||
throw new Error('Invalid query, maybe it is not sanitized? This could happen if you enabled skipHooks in the options, in which case you will need to sanitize the query yourself'); | ||
throw new Error(`[${this.schema.model}] Invalid query, maybe it is not sanitized? This could happen if you enabled skipHooks in the options, in which case you will need to sanitize the query yourself`); | ||
} | ||
@@ -423,3 +423,3 @@ // Need to keep the original doc for the didUpdateDocument() hook. | ||
if (res.ok !== 1) | ||
throw new Error('Update failed'); | ||
throw new Error(`[${this.schema.model}] Update failed`); | ||
let oldDoc; | ||
@@ -431,3 +431,3 @@ let newDoc; | ||
if (is_1.default.nullOrUndefined(oldDoc)) | ||
throw new Error('Unable to return the old document before the update'); | ||
throw new Error(`[${this.schema.model}] Unable to return the old document before the update`); | ||
newDoc = yield this.findOne(oldDoc._id); | ||
@@ -439,3 +439,3 @@ } | ||
if (is_1.default.nullOrUndefined(newDoc)) { | ||
throw new Error('Unable to find the updated doc'); | ||
throw new Error(`[${this.schema.model}] Unable to find the updated doc`); | ||
} | ||
@@ -449,3 +449,3 @@ if (options.skipHooks !== true) { | ||
if (!is_1.default.plainObject(q)) { | ||
throw new Error('Invalid query, maybe it is not sanitized? This could happen if you enabled skipHooks in the options, in which case you will need to sanitize the query yourself'); | ||
throw new Error(`[${this.schema.model}] Invalid query, maybe it is not sanitized? This could happen if you enabled skipHooks in the options, in which case you will need to sanitize the query yourself`); | ||
} | ||
@@ -455,5 +455,5 @@ const res = yield collection.updateOne(q, u, options); | ||
if (res.result.ok !== 1) | ||
throw new Error('Unable to update the document'); | ||
throw new Error(`[${this.schema.model}] Unable to update the document`); | ||
if (res.result.n <= 0) | ||
throw new Error('Unable to update the document'); | ||
throw new Error(`[${this.schema.model}] Unable to update the document`); | ||
if (options.skipHooks !== true) { | ||
@@ -517,3 +517,3 @@ yield this.afterUpdate(); | ||
if ((this.schema.noUpdates === true) || (this.schema.noUpdateMany === true)) | ||
throw new Error('Multiple updates are disallowed for this model'); | ||
throw new Error(`[${this.schema.model}] Multiple updates are disallowed for this model`); | ||
const [q, u] = yield this.beforeUpdate(query, update, options); | ||
@@ -529,3 +529,3 @@ const collection = yield this.getCollection(); | ||
if (is_1.default.boolean(res) || is_1.default.nullOrUndefined(res)) { | ||
throw new Error('Error upserting document during an updateMany operation'); | ||
throw new Error(`[${this.schema.model}] Error upserting document during an updateMany operation`); | ||
} | ||
@@ -539,5 +539,5 @@ results.push(res); | ||
if (result.ok !== 1) | ||
throw new Error('Unable to update many documents'); | ||
throw new Error(`[${this.schema.model}] Unable to update many documents`); | ||
if (!result.value) | ||
throw new Error('Unable to update many documents'); | ||
throw new Error(`[${this.schema.model}] Unable to update many documents`); | ||
results.push(result.value); | ||
@@ -554,3 +554,3 @@ } | ||
if (results.result.ok !== 1) | ||
throw new Error('Unable to update many documents'); | ||
throw new Error(`[${this.schema.model}] Unable to update many documents`); | ||
if (results.result.n <= 0) | ||
@@ -584,3 +584,3 @@ return false; | ||
if (this.schema.noDeletes === true) | ||
throw new Error('Deletions are disallowed for this model'); | ||
throw new Error(`[${this.schema.model}] Deletions are disallowed for this model`); | ||
const q = yield this.beforeDelete(query, options); | ||
@@ -593,5 +593,5 @@ log(`${this.schema.model}.deleteOne:`, JSON.stringify(query, null, 0)); | ||
if (results.ok !== 1) | ||
throw new Error('Unable to delete document'); | ||
throw new Error(`[${this.schema.model}] Unable to delete document`); | ||
if (!results.value) | ||
throw new Error('Unable to return deleted document'); | ||
throw new Error(`[${this.schema.model}] Unable to return deleted document`); | ||
yield this.afterDelete(results.value); | ||
@@ -604,5 +604,5 @@ return results.value; | ||
if (results.result.ok !== 1) | ||
throw new Error('Unable to delete document'); | ||
throw new Error(`[${this.schema.model}] Unable to delete document`); | ||
if (!is_1.default.number(results.result.n) || (results.result.n <= 0)) | ||
throw new Error('Unable to delete document'); | ||
throw new Error(`[${this.schema.model}] Unable to delete document`); | ||
yield this.afterDelete(); | ||
@@ -659,3 +659,3 @@ } | ||
if ((this.schema.noDeletes === true) || (this.schema.noDeleteMany === true)) | ||
throw new Error('Multiple deletions are disallowed for this model'); | ||
throw new Error(`[${this.schema.model}] Multiple deletions are disallowed for this model`); | ||
const q = yield this.beforeDelete(query, options); | ||
@@ -672,3 +672,3 @@ log(`${this.schema.model}.deleteMany:`, JSON.stringify(q, null, 0)); | ||
if (result.ok !== 1) | ||
throw new Error('Unable to delete documents'); | ||
throw new Error(`[${this.schema.model}] Unable to delete documents`); | ||
if (result.value) { | ||
@@ -687,3 +687,3 @@ results.push(result.value); | ||
if (results.result.ok !== 1) | ||
throw new Error('Unable to delete documents'); | ||
throw new Error(`[${this.schema.model}] Unable to delete documents`); | ||
if (!is_1.default.number(results.result.n) || (results.result.n <= 0)) | ||
@@ -722,9 +722,9 @@ return false; | ||
if (results.ok !== 1) | ||
throw new Error('Unable to find and replace document'); | ||
throw new Error(`[${this.schema.model}] Unable to find and replace document`); | ||
const oldDoc = results.value; | ||
if (is_1.default.nullOrUndefined(oldDoc)) | ||
throw new Error('Unable to return the old document'); | ||
throw new Error(`[${this.schema.model}] Unable to return the old document`); | ||
const newDoc = yield this.findOne(r); | ||
if (is_1.default.null_(newDoc)) { | ||
throw new Error('Document is replaced but unable to find the new document in the database'); | ||
throw new Error(`[${this.schema.model}] Document is replaced but unable to find the new document in the database`); | ||
} | ||
@@ -805,3 +805,3 @@ yield this.afterDelete(results.value); | ||
if (!fieldSpecs) | ||
throw new Error(`Field ${key} not found in schema`); | ||
throw new Error(`[${this.schema.model}] Field ${key} not found in schema`); | ||
// If the schema has a certain formatting function defined for this field, | ||
@@ -848,5 +848,5 @@ // apply it. | ||
if (!is_1.default.plainObject(doc)) | ||
throw new Error('Invalid document provided'); | ||
throw new Error(`[${this.schema.model}] Invalid document provided`); | ||
if (is_1.default.emptyObject(doc)) | ||
throw new Error('Empty objects are not permitted'); | ||
throw new Error(`[${this.schema.model}] Empty objects are not permitted`); | ||
const fields = this.schema.fields; | ||
@@ -865,3 +865,3 @@ for (const key in doc) { | ||
if (!this.schema.fields.hasOwnProperty(key)) { | ||
throw new Error(`The field '${key}' is not defined in the schema`); | ||
throw new Error(`[${this.schema.model}] The field '${key}' is not defined in the schema`); | ||
} | ||
@@ -888,3 +888,3 @@ // #2 Check if field value conforms to its defined specs. | ||
if (yield this.findOne(uniqueQuery)) | ||
throw new Error(`Another document already exists with ${JSON.stringify(uniqueQuery, null, 0)}`); | ||
throw new Error(`[${this.schema.model}] Another document already exists with ${JSON.stringify(uniqueQuery, null, 0)}`); | ||
} | ||
@@ -901,3 +901,3 @@ } | ||
if (!doc.hasOwnProperty(key)) | ||
throw new Error(`Missing required field '${key}'`); | ||
throw new Error(`[${this.schema.model}] Missing required field '${key}'`); | ||
} | ||
@@ -1048,3 +1048,3 @@ } | ||
if ((options.upsert === true) && (this.schema.allowUpserts !== true)) | ||
throw new Error('Attempting to upsert a document while upserting is disallowed in the schema'); | ||
throw new Error(`[${this.schema.model}] Attempting to upsert a document while upserting is disallowed in the schema`); | ||
const [q, u] = yield this.willUpdateDocument(query, update); | ||
@@ -1171,3 +1171,3 @@ // First sanitize the inputs. We want to be able to make sure the query is | ||
if (!is_1.default.array(cascadeModelNames)) | ||
throw new Error('Invalid definition of cascade in schema'); | ||
throw new Error(`[${this.schema.model}] Invalid definition of cascade in schema`); | ||
for (const modelName of cascadeModelNames) { | ||
@@ -1177,3 +1177,3 @@ const ModelClass = db.getModel(modelName); | ||
if (!ModelClass) | ||
throw new Error(`Trying to cascade delete from model ${modelName} but model is not found`); | ||
throw new Error(`[${this.schema.model}] Trying to cascade delete from model ${modelName} but model is not found`); | ||
for (const key in ModelClass.schema.fields) { | ||
@@ -1180,0 +1180,0 @@ if (!ModelClass.schema.fields.hasOwnProperty(key)) |
{ | ||
"name": "@andrewscwei/mongodb-odm", | ||
"version": "0.45.0", | ||
"version": "0.46.0", | ||
"description": "ODM for MongoDB", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
Sorry, the diff of this file is not supported yet
208381