@andrewscwei/mongodb-odm
Advanced tools
Comparing version 0.22.0 to 0.23.0
@@ -187,3 +187,3 @@ /** | ||
*/ | ||
static updateOneStrict<T = {}>(query: Query<T>, update: DocumentFragment<T> | Update<T>, options?: ModelUpdateOneOptions): Promise<void | Document<T>>; | ||
static updateOneStrict<T = {}>(query: Query<T>, update: Update<T>, options?: ModelUpdateOneOptions): Promise<void | Document<T>>; | ||
/** | ||
@@ -205,3 +205,3 @@ * Same as the strict update one operation except this method swallows all | ||
*/ | ||
static updateOne<T = {}>(query: Query<T>, update: DocumentFragment<T> | Update<T>, options?: ModelUpdateOneOptions): Promise<boolean | null | Document<T>>; | ||
static updateOne<T = {}>(query: Query<T>, update: Update<T>, options?: ModelUpdateOneOptions): Promise<boolean | null | Document<T>>; | ||
/** | ||
@@ -227,3 +227,3 @@ * Updates multiple documents matched by `query` with `update` object. | ||
*/ | ||
static updateMany<T = {}>(query: Query<T>, update: DocumentFragment<T> | Update<T>, options?: ModelUpdateManyOptions): Promise<Document<T>[] | boolean>; | ||
static updateMany<T = {}>(query: Query<T>, update: Update<T>, options?: ModelUpdateManyOptions): Promise<Document<T>[] | boolean>; | ||
/** | ||
@@ -388,3 +388,3 @@ * Deletes one document matched by `query`. | ||
*/ | ||
static willUpdateDocument<T>(query: Query<T>, update: DocumentFragment<T> | Update<T>): Promise<[Query, DocumentFragment<T> | Update<T>]>; | ||
static willUpdateDocument<T>(query: Query<T>, update: Update<T>): Promise<[Query, Update<T>]>; | ||
/** | ||
@@ -391,0 +391,0 @@ * Handler called after a document or a set of documents have been |
@@ -16,2 +16,11 @@ "use strict"; | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) | ||
t[p[i]] = s[p[i]]; | ||
return t; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -978,19 +987,22 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
const sanitizedQuery = sanitizeQuery_1.default(this.schema, q); | ||
let sanitizedUpdate; | ||
if (types_1.typeIsUpdate(u)) { | ||
sanitizedUpdate = Object.assign({}, u); | ||
if (sanitizedUpdate.$set) | ||
sanitizedUpdate.$set = sanitizeDocument_1.default(this.schema, sanitizedUpdate.$set); | ||
if (sanitizedUpdate.$setOnInsert) | ||
sanitizedUpdate.$setOnInsert = sanitizeDocument_1.default(this.schema, sanitizedUpdate.$setOnInsert); | ||
if (sanitizedUpdate.$addToSet) | ||
sanitizedUpdate.$addToSet = sanitizeDocument_1.default(this.schema, sanitizedUpdate.$addToSet); | ||
if (sanitizedUpdate.$push) | ||
sanitizedUpdate.$push = sanitizeDocument_1.default(this.schema, sanitizedUpdate.$push); | ||
const sanitizedUpdate = types_1.typeIsUpdateQuery(u) ? Object.assign({}, u) : { $set: u }; | ||
// Sanitize all update queries. Remap `null` values to `$unset`. | ||
if (sanitizedUpdate.$set) { | ||
const obj = sanitizedUpdate.$set; | ||
const nulls = Object.keys(obj).filter(v => (obj[v] === null)); | ||
const n = nulls.length; | ||
sanitizedUpdate.$set = sanitizeDocument_1.default(this.schema, sanitizedUpdate.$set); | ||
if (n > 0) { | ||
sanitizedUpdate.$unset = {}; | ||
for (let i = 0; i < n; i++) { | ||
sanitizedUpdate.$unset[nulls[i]] = ''; | ||
} | ||
} | ||
} | ||
else { | ||
sanitizedUpdate = { | ||
$set: sanitizeDocument_1.default(this.schema, u), | ||
}; | ||
} | ||
if (sanitizedUpdate.$setOnInsert) | ||
sanitizedUpdate.$setOnInsert = sanitizeDocument_1.default(this.schema, sanitizedUpdate.$setOnInsert); | ||
if (sanitizedUpdate.$addToSet) | ||
sanitizedUpdate.$addToSet = sanitizeDocument_1.default(this.schema, sanitizedUpdate.$addToSet); | ||
if (sanitizedUpdate.$push) | ||
sanitizedUpdate.$push = sanitizeDocument_1.default(this.schema, sanitizedUpdate.$push); | ||
// Add updated timestamps if applicable. | ||
@@ -1014,3 +1026,6 @@ if ((this.schema.timestamps === true) && (options.ignoreTimestamps !== true)) { | ||
const beforeInsert = yield this.beforeInsert(lodash_1.default.cloneDeep(sanitizedQuery), Object.assign({}, options, { strict: false })); | ||
const setOnInsert = lodash_1.default.omit(Object.assign({}, sanitizedUpdate.$setOnInsert || {}, beforeInsert), Object.keys(sanitizedUpdate.$set || {})); | ||
const setOnInsert = lodash_1.default.omit(Object.assign({}, sanitizedUpdate.$setOnInsert || {}, beforeInsert), [ | ||
...Object.keys(sanitizedUpdate.$set || {}), | ||
...Object.keys(sanitizedUpdate.$unset || {}), | ||
]); | ||
if (!is_1.default.emptyObject(setOnInsert)) { | ||
@@ -1021,4 +1036,9 @@ sanitizedUpdate.$setOnInsert = setOnInsert; | ||
// Validate all fields in the update query. | ||
yield this.validateDocument(sanitizedUpdate.$set, Object.assign({ ignoreUniqueIndex: true }, options)); | ||
return [sanitizedQuery, sanitizedUpdate]; | ||
if (sanitizedUpdate.$set && !is_1.default.emptyObject(sanitizedUpdate.$set)) { | ||
yield this.validateDocument(sanitizedUpdate.$set, Object.assign({ ignoreUniqueIndex: true }, options)); | ||
} | ||
// Strip empty objects. | ||
const { $set, $setOnInsert, $addToSet, $push } = sanitizedUpdate, rest = __rest(sanitizedUpdate, ["$set", "$setOnInsert", "$addToSet", "$push"]); | ||
const finalizedUpdate = Object.assign({}, rest, (is_1.default.nullOrUndefined($set) || is_1.default.emptyObject($set)) ? {} : { $set }, (is_1.default.nullOrUndefined($setOnInsert) || is_1.default.emptyObject($setOnInsert)) ? {} : { $setOnInsert }, (is_1.default.nullOrUndefined($addToSet) || is_1.default.emptyObject($addToSet)) ? {} : { $addToSet }, (is_1.default.nullOrUndefined($push) || is_1.default.emptyObject($push)) ? {} : { $push }); | ||
return [sanitizedQuery, finalizedUpdate]; | ||
}); | ||
@@ -1025,0 +1045,0 @@ } |
@@ -235,8 +235,18 @@ "use strict"; | ||
})); | ||
mocha_1.it('foo', () => __awaiter(this, void 0, void 0, function* () { | ||
const s = faker_1.default.random.alphaNumeric(10); | ||
const baz = yield Baz_1.default.insertOneStrict({ aString: s }); | ||
yield Baz_1.default.updateOneStrict(baz._id, { aNumber: null, aBoolean: false }); | ||
mocha_1.it('can remove a property of a doc by updating it to `null`', () => __awaiter(this, void 0, void 0, function* () { | ||
const baz = yield Baz_1.default.insertOneStrict(); | ||
assert_1.default(baz.aNumber); | ||
yield Baz_1.default.updateOneStrict(baz._id, { aNumber: null }); | ||
const res = yield Baz_1.default.findOneStrict(baz._id); | ||
assert_1.default(res.aNumber === undefined); | ||
})); | ||
mocha_1.it('can upsert a document while setting an update field to `null`', () => __awaiter(this, void 0, void 0, function* () { | ||
const t = faker_1.default.random.alphaNumeric(10); | ||
const exists = yield Baz_1.default.exists({ aString: t }); | ||
assert_1.default(!exists); | ||
const baz = yield Baz_1.default.updateOneStrict({ aString: t }, { aNumber: null }, { upsert: true, returnDoc: true }); | ||
assert_1.default(baz); | ||
assert_1.default(baz.aNumber === undefined); | ||
})); | ||
}); | ||
//# sourceMappingURL=Model.spec.js.map |
@@ -21,5 +21,5 @@ import { CollectionAggregationOptions, CollectionInsertManyOptions, CollectionInsertOneOptions, CommonOptions, FilterQuery, FindOneAndReplaceOption, IndexOptions, ObjectID, ReplaceOneOptions, UpdateQuery } from 'mongodb'; | ||
/** | ||
* Document update descriptor. | ||
* Update document descriptor. | ||
*/ | ||
export declare type Update<T = {}> = UpdateQuery<DocumentFragment<T>>; | ||
export declare type Update<T = {}> = UpdateQuery<DocumentFragment<T>> | DocumentFragment<T>; | ||
/** | ||
@@ -335,3 +335,3 @@ * Data type for all field types. | ||
*/ | ||
declare type FieldBasicValue = null | ObjectID | string | number | boolean | Date; | ||
declare type FieldBasicValue = ObjectID | string | number | boolean | Date; | ||
/** | ||
@@ -387,3 +387,3 @@ * Function for formatting field values, in which the value to be formatted will | ||
/** | ||
* Checks if a value is an Update. | ||
* Checks if a value is an UpdateQuery. | ||
* | ||
@@ -394,9 +394,9 @@ * @param value - Value to check. | ||
*/ | ||
export declare function typeIsUpdate<T = {}>(value: any): value is Update<T>; | ||
export declare function typeIsUpdateQuery<T = {}>(value: any): value is UpdateQuery<DocumentFragment<T>>; | ||
/** | ||
* Checks if a value is an identifiable Document. | ||
* Checks if a value is an identifiable document. | ||
* | ||
* @param value - Value to check. | ||
* | ||
* @returns `true` if value is an identifiable Document, `false` otherwise. | ||
* @returns `true` if value is an identifiable document, `false` otherwise. | ||
*/ | ||
@@ -403,0 +403,0 @@ export declare function typeIsIdentifiableDocument(value: any): value is { |
@@ -9,3 +9,3 @@ "use strict"; | ||
/** | ||
* Checks if a value is an Update. | ||
* Checks if a value is an UpdateQuery. | ||
* | ||
@@ -16,3 +16,3 @@ * @param value - Value to check. | ||
*/ | ||
function typeIsUpdate(value) { | ||
function typeIsUpdateQuery(value) { | ||
if (!is_1.default.plainObject(value)) | ||
@@ -22,9 +22,9 @@ return false; | ||
} | ||
exports.typeIsUpdate = typeIsUpdate; | ||
exports.typeIsUpdateQuery = typeIsUpdateQuery; | ||
/** | ||
* Checks if a value is an identifiable Document. | ||
* Checks if a value is an identifiable document. | ||
* | ||
* @param value - Value to check. | ||
* | ||
* @returns `true` if value is an identifiable Document, `false` otherwise. | ||
* @returns `true` if value is an identifiable document, `false` otherwise. | ||
*/ | ||
@@ -31,0 +31,0 @@ function typeIsIdentifiableDocument(value) { |
@@ -1,2 +0,2 @@ | ||
import { DocumentFragment, FieldValue, Schema } from '../types'; | ||
import { DocumentFragment, Schema } from '../types'; | ||
/** | ||
@@ -15,4 +15,2 @@ * Sanitizes a partial document by removing all extraneous fields from it | ||
*/ | ||
export default function sanitizeDocument<T = {}>(schema: Schema, doc: { | ||
[field: string]: FieldValue; | ||
}): DocumentFragment<T>; | ||
export default function sanitizeDocument<T = {}>(schema: Schema, doc: DocumentFragment): DocumentFragment<T>; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const is_1 = __importDefault(require("@sindresorhus/is")); | ||
/** | ||
@@ -25,4 +29,4 @@ * Sanitizes a partial document by removing all extraneous fields from it | ||
continue; | ||
// Ignore undefined fields. | ||
if (doc[key] === undefined) | ||
// Ignore undefined and null fields. | ||
if (is_1.default.nullOrUndefined(doc[key])) | ||
continue; | ||
@@ -29,0 +33,0 @@ o[key] = doc[key]; |
{ | ||
"name": "@andrewscwei/mongodb-odm", | ||
"version": "0.22.0", | ||
"version": "0.23.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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
260612
49
4002