Socket
Socket
Sign inDemoInstall

@andrewscwei/mongodb-odm

Package Overview
Dependencies
Maintainers
1
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@andrewscwei/mongodb-odm - npm Package Compare versions

Comparing version 0.16.0 to 0.17.0

102

build/core/Model.d.ts

@@ -62,5 +62,16 @@ /**

*/
static identifyOne(query: Query): Promise<ObjectID>;
static identifyOneStrict(query: Query): Promise<ObjectID>;
/**
* Finds one document of this collection using the aggregation framework. If
* Same as the strict identify one operation but this method swallows all
* errors and returns `null` if document canot be identified.
*
* @param query - Query used for the $match stage of the aggregation pipeline.
*
* @returns The matching ObjectID.
*
* @see Model.identifyOneStrict
*/
static identifyOne(query: Query): Promise<null | ObjectID>;
/**
* Finds one document from this collection using the aggregation framework. If
* no query is specified, a random document will be fetched.

@@ -72,3 +83,17 @@ *

* @returns The matching document as the fulfillment value.
*
* @throws {Error} No document found.
*/
static findOneStrict<T = {}, R = T>(query?: Query<T>, options?: ModelFindOneOptions): Promise<Document<R>>;
/**
* Same as the strict find one operation but this method swallows all errors
* and returns `null` when no document is found.
*
* @param query - Query used for the $match stage of the aggregation pipeline.
* @param options - @see module:mongodb.Collection#aggregate
*
* @returns The matching document as the fulfillment value.
*
* @see Model.findOneStrict
*/
static findOne<T = {}, R = T>(query?: Query<T>, options?: ModelFindOneOptions): Promise<null | Document<R>>;

@@ -101,2 +126,14 @@ /**

*/
static insertOneStrict<T>(doc?: DocumentFragment<T>, options?: ModelInsertOneOptions): Promise<Document<T>>;
/**
* Same as the strict insert one operation except this method swallows all
* errors and returns null if the document cannot be inserted.
*
* @param doc - Document to be inserted. @see module:mongodb.Collection#insertOne
* @param options - @see ModelInsertOneOptions
*
* @returns The inserted document.
*
* @see Model.insertOneStrict
*/
static insertOne<T>(doc?: DocumentFragment<T>, options?: ModelInsertOneOptions): Promise<null | Document<T>>;

@@ -131,4 +168,4 @@ /**

*
* @returns The updated doc if `returnDoc` is set to `true`, else `true` or
* `false` depending on whether or not the operation was successful.
* @returns The updated doc if `returnDoc` is set to `true`, else there is no
* fulfillment value.
*

@@ -146,4 +183,21 @@ * @see {@link https://docs.mongodb.com/manual/reference/operator/update-field/}

*/
static updateOne<T = {}>(query: Query<T>, update: DocumentFragment<T> | Update<T>, options?: ModelUpdateOneOptions): Promise<null | boolean | Document<T>>;
static updateOneStrict<T = {}>(query: Query<T>, update: DocumentFragment<T> | Update<T>, options?: ModelUpdateOneOptions): Promise<void | Document<T>>;
/**
* Same as the strict update one operation except this method swallows all
* errors and returns `null` if no document was updated (and that `returnDoc`
* is `true`) or `true`/`false` (if `returnDoc` is `false`).
*
* @param query - Query for the document to update.
* @param update - Either an object whose key/value pair represent the fields
* belonging to this model to update to, or an update query.
* @param options - @see ModelUpdateOneOptions
*
* @returns The updated doc if `returnDoc` is set to `true`, else either
* `true` or `false` depending on whether the operation was
* successful.
*
* @see Model.updateOneStrict
*/
static updateOne<T = {}>(query: Query<T>, update: DocumentFragment<T> | Update<T>, options?: ModelUpdateOneOptions): Promise<boolean | null | Document<T>>;
/**
* Updates multiple documents matched by `query` with `update` object.

@@ -175,4 +229,3 @@ *

*
* @returns The deleted doc if `returnDoc` is set to `true`, else `true` or
* `false` depending on whether or not the operation was successful.
* @returns The deleted doc if `returnDoc` is set to `true`.
*

@@ -185,5 +238,21 @@ * @see {@link http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#deleteOne}

* the schema.
* @throws {Error} Unable to return the deleted document when `returnDoc` is
* `true`.
* @throws {Error} Unable to delete document.
*/
static deleteOne<T = {}>(query: Query<T>, options?: ModelDeleteOneOptions): Promise<Document<T> | boolean | null>;
static deleteOneStrict<T = {}>(query: Query<T>, options?: ModelDeleteOneOptions): Promise<Document<T> | void>;
/**
* Same as the strict delete one operation except this method swallows all
* errors.
*
* @param query - Query for document to delete.
* @param options @see ModelDeleteOneOptions
*
* @returns The deleted doc if `returnDoc` is set to `true`, else `true` or
* `false` depending on whether or not the operation was successful.
*
* @see Model.deleteOneStrict
*/
static deleteOne<T = {}>(query: Query<T>, options?: ModelDeleteOneOptions): Promise<Document<T> | null | boolean>;
/**
* Deletes multiple documents matched by `query`.

@@ -214,3 +283,3 @@ *

* @returns The replaced document (by default) or the new document (depending
* on the `returnOriginal` option) if available, `null` otherwise.
* on the `returnOriginal` option).
*

@@ -220,4 +289,19 @@ * @see {@link http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#findOneAndReplace}

*
* @throws {Error} The old document cannot be returned.
* @throws {Error} The doc is replaced but it cannot be fetched.
*/
static findAndReplaceOneStrict<T = {}>(query: Query<T>, replacement?: DocumentFragment<T>, options?: ModelReplaceOneOptions): Promise<Document<T>>;
/**
* Same as the strict find and replace one operation except this method
* swallows all errors.
*
* @param query - Query for document to replace.
* @param replacement - The replacement document.
* @param options - @see ModelReplaceOneOptions
*
* @returns The replaced document (by default) or the new document (depending
* on the `returnOriginal` option) if available, `null` otherwise.
*
* @see Model.findAndReplaceOneStrict
*/
static findAndReplaceOne<T = {}>(query: Query<T>, replacement?: DocumentFragment<T>, options?: ModelReplaceOneOptions): Promise<null | Document<T>>;

@@ -224,0 +308,0 @@ /**

@@ -123,13 +123,30 @@ "use strict";

*/
static identifyOne(query) {
static identifyOneStrict(query) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.findOne(query);
if (is_1.default.nullOrUndefined(result)) {
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)}`);
}
else if (!mongodb_1.ObjectID.isValid(result._id)) {
if (!mongodb_1.ObjectID.isValid(result._id))
throw new Error(`ID of ${result} is not a valid ObjectID`);
return result._id;
});
}
/**
* Same as the strict identify one operation but this method swallows all
* errors and returns `null` if document canot be identified.
*
* @param query - Query used for the $match stage of the aggregation pipeline.
*
* @returns The matching ObjectID.
*
* @see Model.identifyOneStrict
*/
static identifyOne(query) {
return __awaiter(this, void 0, void 0, function* () {
try {
const o = yield this.identifyOne(query);
return o;
}
else {
return result._id;
catch (err) {
return null;
}

@@ -139,3 +156,3 @@ });

/**
* Finds one document of this collection using the aggregation framework. If
* Finds one document from this collection using the aggregation framework. If
* no query is specified, a random document will be fetched.

@@ -147,4 +164,6 @@ *

* @returns The matching document as the fulfillment value.
*
* @throws {Error} No document found.
*/
static findOne(query, options) {
static findOneStrict(query, options) {
return __awaiter(this, void 0, void 0, function* () {

@@ -154,6 +173,5 @@ if (is_1.default.nullOrUndefined(query)) {

const results = yield collection.aggregate(this.pipeline(query).concat([{ $sample: { size: 1 } }])).toArray();
assert_1.default(results.length <= 1, new Error('More than 1 random document found even though only 1 was supposed to be found.'));
if (results.length === 1)
return results[0];
return null;
if (results.length !== 1)
throw new Error('More or less than 1 random document found even though only 1 was supposed to be found.');
return results[0];
}

@@ -163,3 +181,3 @@ else {

if (results.length === 0)
return null;
throw new Error('No document found with provided query');
return results[0];

@@ -170,2 +188,24 @@ }

/**
* Same as the strict find one operation but this method swallows all errors
* and returns `null` when no document is found.
*
* @param query - Query used for the $match stage of the aggregation pipeline.
* @param options - @see module:mongodb.Collection#aggregate
*
* @returns The matching document as the fulfillment value.
*
* @see Model.findOneStrict
*/
static findOne(query, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const o = yield this.findOneStrict(query, options);
return o;
}
catch (err) {
return null;
}
});
}
/**
* Finds multiple documents of this collection using the aggregation

@@ -202,3 +242,3 @@ * framework. If no query is specified, all documents are fetched.

*/
static insertOne(doc, options) {
static insertOneStrict(doc, options) {
return __awaiter(this, void 0, void 0, function* () {

@@ -216,3 +256,3 @@ if (this.schema.noInserts === true)

if (results.ops.length < 1)
return null;
throw new Error('Unable to insert document');
const o = results.ops[0];

@@ -225,2 +265,24 @@ // Apply after insert handler.

/**
* Same as the strict insert one operation except this method swallows all
* errors and returns null if the document cannot be inserted.
*
* @param doc - Document to be inserted. @see module:mongodb.Collection#insertOne
* @param options - @see ModelInsertOneOptions
*
* @returns The inserted document.
*
* @see Model.insertOneStrict
*/
static insertOne(doc, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const o = yield this.insertOneStrict(doc, options);
return o;
}
catch (err) {
return null;
}
});
}
/**
* Inserts multiple documents into this model's collection.

@@ -275,4 +337,4 @@ *

*
* @returns The updated doc if `returnDoc` is set to `true`, else `true` or
* `false` depending on whether or not the operation was successful.
* @returns The updated doc if `returnDoc` is set to `true`, else there is no
* fulfillment value.
*

@@ -290,3 +352,3 @@ * @see {@link https://docs.mongodb.com/manual/reference/operator/update-field/}

*/
static updateOne(query, update, options = {}) {
static updateOneStrict(query, update, options = {}) {
return __awaiter(this, void 0, void 0, function* () {

@@ -312,3 +374,3 @@ if (this.schema.noUpdates === true)

if (is_1.default.nullOrUndefined(oldDoc))
return null;
throw new Error('Unable to return the old document before the update');
newDoc = yield this.findOne(oldDoc._id);

@@ -335,8 +397,36 @@ }

if (res.result.n <= 0)
return false;
throw new Error('Unable to update the document');
if (options.skipHooks !== true) {
yield this.afterUpdate();
}
}
});
}
/**
* Same as the strict update one operation except this method swallows all
* errors and returns `null` if no document was updated (and that `returnDoc`
* is `true`) or `true`/`false` (if `returnDoc` is `false`).
*
* @param query - Query for the document to update.
* @param update - Either an object whose key/value pair represent the fields
* belonging to this model to update to, or an update query.
* @param options - @see ModelUpdateOneOptions
*
* @returns The updated doc if `returnDoc` is set to `true`, else either
* `true` or `false` depending on whether the operation was
* successful.
*
* @see Model.updateOneStrict
*/
static updateOne(query, update, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
try {
const o = yield this.updateOneStrict(query, update, options);
if (o && options.returnDoc)
return o;
return true;
}
catch (err) {
return options.returnDoc ? null : false;
}
});

@@ -412,4 +502,3 @@ }

*
* @returns The deleted doc if `returnDoc` is set to `true`, else `true` or
* `false` depending on whether or not the operation was successful.
* @returns The deleted doc if `returnDoc` is set to `true`.
*

@@ -422,4 +511,7 @@ * @see {@link http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#deleteOne}

* the schema.
* @throws {Error} Unable to return the deleted document when `returnDoc` is
* `true`.
* @throws {Error} Unable to delete document.
*/
static deleteOne(query, options = {}) {
static deleteOneStrict(query, options = {}) {
return __awaiter(this, void 0, void 0, function* () {

@@ -435,5 +527,4 @@ if (this.schema.noDeletes === true)

assert_1.default(results.ok === 1);
if (!results.value) {
return null;
}
if (!results.value)
throw new Error('Unable to return deleted document');
yield this.afterDelete(results.value);

@@ -445,8 +536,6 @@ return results.value;

log(`${this.schema.model}.deleteOne results:`, JSON.stringify(results, null, 0));
assert_1.default(results.result.ok === 1);
if (!is_1.default.number(results.result.n) || (results.result.n <= 0)) {
return false;
}
assert_1.default(results.result.ok === 1, new Error('Unable to delete document'));
if (!is_1.default.number(results.result.n) || (results.result.n <= 0))
throw new Error('Unable to delete document');
yield this.afterDelete();
return true;
}

@@ -456,2 +545,30 @@ });

/**
* Same as the strict delete one operation except this method swallows all
* errors.
*
* @param query - Query for document to delete.
* @param options @see ModelDeleteOneOptions
*
* @returns The deleted doc if `returnDoc` is set to `true`, else `true` or
* `false` depending on whether or not the operation was successful.
*
* @see Model.deleteOneStrict
*/
static deleteOne(query, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
try {
const o = yield this.deleteOneStrict(query, options);
if (options.returnDoc && o) {
return o;
}
else {
return true;
}
}
catch (err) {
return options.returnDoc ? null : false;
}
});
}
/**
* Deletes multiple documents matched by `query`.

@@ -516,3 +633,3 @@ *

* @returns The replaced document (by default) or the new document (depending
* on the `returnOriginal` option) if available, `null` otherwise.
* on the `returnOriginal` option).
*

@@ -522,5 +639,6 @@ * @see {@link http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#findOneAndReplace}

*
* @throws {Error} The old document cannot be returned.
* @throws {Error} The doc is replaced but it cannot be fetched.
*/
static findAndReplaceOne(query, replacement, options = {}) {
static findAndReplaceOneStrict(query, replacement, options = {}) {
return __awaiter(this, void 0, void 0, function* () {

@@ -536,3 +654,3 @@ const q = yield this.beforeDelete(query, options);

if (is_1.default.nullOrUndefined(oldDoc))
return null;
throw new Error('Unable to return the old document');
const newDoc = yield this.findOne(r);

@@ -548,2 +666,26 @@ if (is_1.default.null_(newDoc)) {

/**
* Same as the strict find and replace one operation except this method
* swallows all errors.
*
* @param query - Query for document to replace.
* @param replacement - The replacement document.
* @param options - @see ModelReplaceOneOptions
*
* @returns The replaced document (by default) or the new document (depending
* on the `returnOriginal` option) if available, `null` otherwise.
*
* @see Model.findAndReplaceOneStrict
*/
static findAndReplaceOne(query, replacement, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
try {
const o = yield this.findAndReplaceOneStrict(query, replacement, options);
return o;
}
catch (err) {
return null;
}
});
}
/**
* Counts the documents that match the provided query.

@@ -550,0 +692,0 @@ *

2

build/tests/core/Model.spec.js

@@ -95,3 +95,3 @@ "use strict";

try {
yield Baz_1.default.insertOne({ aNumber: 6 }).catch(err => { throw err; });
yield Baz_1.default.insertOneStrict({ aNumber: 6 }).catch(err => { throw err; });
didThrow = false;

@@ -98,0 +98,0 @@ }

{
"name": "@andrewscwei/mongodb-odm",
"version": "0.16.0",
"version": "0.17.0",
"description": "ODM for MongoDB",

@@ -5,0 +5,0 @@ "main": "build/index.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc