xpress-mongo
Advanced tools
Comparing version 1.5.0 to 2.0.0
{ | ||
"name": "xpress-mongo", | ||
"version": "1.5.0", | ||
"version": "2.0.0", | ||
"description": "Light Weight ODM for mongoDb", | ||
@@ -31,3 +31,2 @@ "main": "index.js", | ||
"dependencies": { | ||
"@types/mongodb": "^3.6.20", | ||
"@types/node": "^16.6.1", | ||
@@ -37,3 +36,3 @@ "@types/uuid": "^8.3.1", | ||
"joi": "^17.4.2", | ||
"mongodb": "3.6.10", | ||
"mongodb": "4.1.0", | ||
"object-collection": "^2.0.0", | ||
@@ -51,3 +50,6 @@ "uuid": "^8.3.2" | ||
"typescript": "^4.3.5" | ||
}, | ||
"engines": { | ||
"node": ">=12.9" | ||
} | ||
} |
# Xpress Mongo | ||
Note: **Version 2, uses mongodb new v4 which includes breaking changes**. | ||
See mongodb change log for more details. [Mongodb Native Client 4.0 Change Log](https://github.com/mongodb/node-mongodb-native/blob/4.0/docs/CHANGES_4.0.0.md) | ||
A light mongodb model/helper library for nodejs, provides modeling for your documents while keeping you very close to | ||
@@ -4,0 +7,0 @@ mongodb native syntax which is always recommended. |
@@ -10,3 +10,3 @@ "use strict"; | ||
const isObject = (v) => v && typeof v === "object" && !Array.isArray(v); | ||
const isObjectId = (v) => mongodb_1.ObjectID.isValid(v); | ||
const isObjectId = (v) => mongodb_1.ObjectId.isValid(v); | ||
const isArray = (v) => Array.isArray(v); | ||
@@ -133,13 +133,13 @@ const isDate = (v) => { | ||
.cast((val, key) => { | ||
if (typeof val === "object" && mongodb_1.ObjectID.isValid(val)) { | ||
if (typeof val === "object" && mongodb_1.ObjectId.isValid(val)) { | ||
return val; | ||
} | ||
try { | ||
return new mongodb_1.ObjectID(val); | ||
return new mongodb_1.ObjectId(val); | ||
} | ||
catch (e) { | ||
throw TypeError(`(${key}) is not valid Mongodb-ObjectID`); | ||
throw TypeError(`(${key}) is not valid Mongodb-ObjectId`); | ||
} | ||
}) | ||
.validatorError((key) => `(${key}) is not a Mongodb-ObjectID`); | ||
.validatorError((key) => `(${key}) is not a Mongodb-ObjectId`); | ||
}, | ||
@@ -146,0 +146,0 @@ /** |
import ObjectCollection = require("object-collection"); | ||
import { ObjectID, Collection, UpdateWriteOpResult, InsertOneWriteOpResult, DeleteWriteOpResultObject, Cursor, FindOneOptions, UpdateOneOptions, CollectionInsertOneOptions, CollectionAggregationOptions, AggregationCursor, UpdateQuery, FilterQuery } from "mongodb"; | ||
import { ObjectId, Collection, InsertOneResult, DeleteResult, FindOptions, InsertOneOptions, AggregateOptions, AggregationCursor, Filter, UpdateFilter, UpdateOptions, UpdateResult, FindCursor } from "mongodb"; | ||
import { PaginationData, StringToAnyObject, XMongoSchema, XMongoSchemaFn, XMongoStrictConfig } from "./CustomTypes"; | ||
declare type FunctionWithRawArgument = { | ||
(raw: Collection): Cursor | AggregationCursor; | ||
}; | ||
declare type FunctionWithRawArgument = (raw: Collection) => FindCursor | AggregationCursor; | ||
/** | ||
@@ -187,5 +185,5 @@ * @class | ||
* Get id of current model instance | ||
* @returns {*|ObjectID|null} | ||
* @returns {*|ObjectId|null} | ||
*/ | ||
id(): any | ObjectID | null; | ||
id(): any | ObjectId | null; | ||
/** | ||
@@ -217,5 +215,5 @@ * Compare model id with a string or ObjectId type variable. | ||
* @param options | ||
* @return {Promise<UpdateWriteOpResult>} | ||
* @return {Promise<UpdateResult>} | ||
*/ | ||
update(set: StringToAnyObject, options?: UpdateOneOptions): Promise<UpdateWriteOpResult>; | ||
update(set: StringToAnyObject, options?: UpdateOptions): Promise<UpdateResult>; | ||
/** | ||
@@ -227,5 +225,4 @@ * Update model using raw updateQuery | ||
* @param options | ||
* @return {Promise<UpdateWriteOpResult>} | ||
*/ | ||
updateRaw(update: UpdateQuery<any> | Partial<any>, options?: UpdateOneOptions): Promise<UpdateWriteOpResult>; | ||
updateRaw(update: UpdateFilter<any>, options?: UpdateOptions): Promise<UpdateResult>; | ||
/** | ||
@@ -235,5 +232,4 @@ * Create Model if not id is missing or save document if id is found. | ||
* @param create | ||
* @return {Promise<UpdateWriteOpResult | InsertOneWriteOpResult<*>>} | ||
*/ | ||
save(options?: UpdateOneOptions | CollectionInsertOneOptions, create?: boolean): Promise<boolean | UpdateWriteOpResult | InsertOneWriteOpResult<any>>; | ||
save(options?: UpdateOptions | InsertOneOptions, create?: boolean): Promise<boolean | UpdateResult | InsertOneResult<any>>; | ||
/** | ||
@@ -243,3 +239,3 @@ * Save and return current | ||
*/ | ||
saveAndReturn(options?: UpdateOneOptions | CollectionInsertOneOptions): Promise<this>; | ||
saveAndReturn(options?: UpdateOptions | InsertOneOptions): Promise<this>; | ||
/** | ||
@@ -250,3 +246,3 @@ * Unset a key or keys from this collection | ||
*/ | ||
unset(keys: string | string[], options?: UpdateOneOptions): Promise<UpdateWriteOpResult>; | ||
unset(keys: string | string[], options?: UpdateOptions): Promise<UpdateResult>; | ||
/** | ||
@@ -264,3 +260,3 @@ * Validate | ||
*/ | ||
delete(): Promise<DeleteWriteOpResultObject>; | ||
delete(): Promise<DeleteResult>; | ||
/** | ||
@@ -304,3 +300,3 @@ * Sets data as an instance of ObjectCollection on this.$data | ||
/** | ||
* Alias to mongo.ObjectID | ||
* Alias to mongo.ObjectId | ||
* @param str {*} | ||
@@ -310,4 +306,4 @@ * @param returnObject | ||
*/ | ||
static id(str: any, returnObject?: boolean): (ObjectID | string) | { | ||
_id: ObjectID | string; | ||
static id(str: any, returnObject?: boolean): (ObjectId | string) | { | ||
_id: ObjectId | string; | ||
}; | ||
@@ -318,6 +314,5 @@ /** | ||
* @param options | ||
* @return {Promise<XMongoModel[]>} | ||
*/ | ||
static find(query?: StringToAnyObject | FilterQuery<any>, options?: FindOneOptions<any>): Promise<Record<string, any>[]>; | ||
static findRaw(query?: StringToAnyObject | FilterQuery<any>, options?: FindOneOptions<any>): Cursor; | ||
static find(query?: StringToAnyObject | Filter<any>, options?: FindOptions<any>): Promise<any[]>; | ||
static findRaw(query?: StringToAnyObject | Filter<any>, options?: FindOptions<any>): FindCursor; | ||
/** | ||
@@ -329,3 +324,3 @@ * Fetches the first document that matches the query | ||
*/ | ||
static findOne<T extends typeof XMongoModel>(this: T, query?: StringToAnyObject | FilterQuery<any>, options?: FindOneOptions<any> | boolean, raw?: boolean): Promise<InstanceType<T> | null>; | ||
static findOne<T extends typeof XMongoModel>(this: T, query?: StringToAnyObject | Filter<any>, options?: FindOptions<any> | boolean, raw?: boolean): Promise<InstanceType<T> | null>; | ||
/** | ||
@@ -338,3 +333,3 @@ * Fetches the first document that matches id provided. | ||
*/ | ||
static findById<T extends typeof XMongoModel>(this: T, _id: any, options?: FindOneOptions<any>, isTypeObjectId?: boolean): Promise<InstanceType<T> | null>; | ||
static findById<T extends typeof XMongoModel>(this: T, _id: any, options?: FindOptions<any>, isTypeObjectId?: boolean): Promise<InstanceType<T> | null>; | ||
/** | ||
@@ -346,3 +341,3 @@ * Count All the documents that match query. | ||
*/ | ||
static count(query?: StringToAnyObject | FilterQuery<any>, options?: FindOneOptions<any>): Promise<number>; | ||
static count(query?: StringToAnyObject | Filter<any>, options?: FindOptions<any>): Promise<number>; | ||
/** | ||
@@ -372,5 +367,4 @@ * Sum fields in this collection. | ||
* @param options | ||
* @returns {Promise<number|*>} | ||
*/ | ||
static countAggregate(query?: any[], options?: CollectionAggregationOptions): Promise<number>; | ||
static countAggregate(query?: any[], options?: AggregateOptions): Promise<number>; | ||
/** | ||
@@ -382,5 +376,4 @@ * Paginate Find. | ||
* @param perPage | ||
* @return {Promise<{total: *, perPage: number, lastPage: number, data: [], page: number}>} | ||
*/ | ||
static paginate(page?: number, perPage?: number, query?: {}, options?: FindOneOptions<any>): Promise<PaginationData>; | ||
static paginate(page?: number, perPage?: number, query?: {}, options?: FindOptions<any>): Promise<PaginationData>; | ||
/** | ||
@@ -394,3 +387,3 @@ * Paginate Aggregation. | ||
*/ | ||
static paginateAggregate(page?: number, perPage?: number, query?: any[], options?: CollectionAggregationOptions): Promise<PaginationData>; | ||
static paginateAggregate(page?: number, perPage?: number, query?: any[], options?: AggregateOptions): Promise<PaginationData>; | ||
/** | ||
@@ -456,3 +449,3 @@ * Turn array provided to model instances. | ||
*/ | ||
$refreshData(options?: FindOneOptions<any>): Promise<this>; | ||
$refreshData(options?: FindOptions<any>): Promise<this>; | ||
/** | ||
@@ -463,3 +456,3 @@ * Refresh Current Model Data using the specified fields value. | ||
*/ | ||
$refreshDataUsing(field: string, options?: FindOneOptions<any>): Promise<this>; | ||
$refreshDataUsing(field: string, options?: FindOptions<any>): Promise<this>; | ||
/** | ||
@@ -479,3 +472,2 @@ * Get Static Class from Instance | ||
* @param isStrict | ||
* @param schema | ||
* @private | ||
@@ -482,0 +474,0 @@ */ |
@@ -248,3 +248,3 @@ "use strict"; | ||
static isValidId(id) { | ||
const isMongoID = mongodb_1.ObjectID.isValid(id); | ||
const isMongoID = mongodb_1.ObjectId.isValid(id); | ||
/** | ||
@@ -254,3 +254,3 @@ * referring to this StackOverflow post | ||
* | ||
* ObjectID.isValid returns true on any 12 length string | ||
* ObjectId.isValid returns true on any 12 length string | ||
* | ||
@@ -261,3 +261,3 @@ * So converting to objectID and checking if the string value matches the original value | ||
if (isMongoID && typeof id === "string") { | ||
return new mongodb_1.ObjectID(id).toString() === id; | ||
return new mongodb_1.ObjectId(id).toString() === id; | ||
} | ||
@@ -370,3 +370,3 @@ return isMongoID; | ||
* Get id of current model instance | ||
* @returns {*|ObjectID|null} | ||
* @returns {*|ObjectId|null} | ||
*/ | ||
@@ -385,3 +385,3 @@ id() { | ||
* Get Value to be compared with | ||
* @type {ObjectID|string} | ||
* @type {ObjectId|string} | ||
*/ | ||
@@ -395,5 +395,5 @@ let compareWith = this.get(key, undefined); | ||
*/ | ||
if (mongodb_1.ObjectID.isValid(to)) | ||
if (mongodb_1.ObjectId.isValid(to)) | ||
to = to.toString(); | ||
if (mongodb_1.ObjectID.isValid(compareWith)) | ||
if (mongodb_1.ObjectId.isValid(compareWith)) | ||
compareWith = compareWith.toString(); | ||
@@ -447,3 +447,3 @@ // Compare Strings | ||
* @param options | ||
* @return {Promise<UpdateWriteOpResult>} | ||
* @return {Promise<UpdateResult>} | ||
*/ | ||
@@ -461,3 +461,2 @@ update(set, options) { | ||
* @param options | ||
* @return {Promise<UpdateWriteOpResult>} | ||
*/ | ||
@@ -467,7 +466,5 @@ updateRaw(update, options) { | ||
throw Error("UPDATE_RAW_ERROR: Model does not have an _id, so we assume it is not from the database."); | ||
return new Promise((resolve, reject) => { | ||
return this.$static() | ||
.native() | ||
.updateOne({ _id: this.id() }, update, options, (error, res) => error ? reject(error) : resolve(res.connection)); | ||
}); | ||
return this.$static() | ||
.native() | ||
.updateOne({ _id: this.id() }, update, options); | ||
} | ||
@@ -478,3 +475,2 @@ /** | ||
* @param create | ||
* @return {Promise<UpdateWriteOpResult | InsertOneWriteOpResult<*>>} | ||
*/ | ||
@@ -510,3 +506,3 @@ save(options = {}, create = false) { | ||
// Resolve | ||
resolve(res.connection); | ||
resolve(res); | ||
// Run Watch Event | ||
@@ -884,3 +880,3 @@ inbuilt_1.RunOnEvent("watch", this, changes); | ||
/** | ||
* Alias to mongo.ObjectID | ||
* Alias to mongo.ObjectId | ||
* @param str {*} | ||
@@ -894,3 +890,3 @@ * @param returnObject | ||
try { | ||
_id = new mongodb_1.ObjectID(str); | ||
_id = new mongodb_1.ObjectId(str); | ||
} | ||
@@ -912,9 +908,4 @@ catch (e) { | ||
* @param options | ||
* @return {Promise<XMongoModel[]>} | ||
*/ | ||
static find(query = {}, options = {}) { | ||
/** | ||
* options as any is used here because mongodb did not make its new | ||
* WithoutProjection type exportable so we can't make reference to it. | ||
*/ | ||
return new Promise((resolve, reject) => { | ||
@@ -1038,5 +1029,4 @@ return this.native() | ||
if (result.length) { | ||
result = result[0]; | ||
for (const field of fields) { | ||
$result[field] = result[field] || 0; | ||
$result[field] = result[0][field] || 0; | ||
} | ||
@@ -1050,3 +1040,2 @@ } | ||
* @param options | ||
* @returns {Promise<number|*>} | ||
*/ | ||
@@ -1068,3 +1057,2 @@ static async countAggregate(query = [], options) { | ||
* @param perPage | ||
* @return {Promise<{total: *, perPage: number, lastPage: number, data: [], page: number}>} | ||
*/ | ||
@@ -1079,3 +1067,3 @@ static async paginate(page = 1, perPage = 20, query = {}, options = {}) { | ||
* options as any is used here because mongodb did not make its new | ||
* WithoutProjection<FindOneOptions<TsSchema>> type exportable so we can't make reference to it. | ||
* WithoutProjection<FindOptions<TsSchema>> type exportable so we can't make reference to it. | ||
*/ | ||
@@ -1177,3 +1165,5 @@ const data = await this.native() | ||
*/ | ||
return resolve(typeof interceptor === "function" ? interceptor(lists) : this.fromArray(lists)); | ||
return resolve(typeof interceptor === "function" | ||
? interceptor(lists) | ||
: this.fromArray(lists)); | ||
}); | ||
@@ -1323,3 +1313,2 @@ }); | ||
* @param isStrict | ||
* @param schema | ||
* @private | ||
@@ -1379,3 +1368,5 @@ */ | ||
} | ||
const findField = await this.$static().native().findOne(findQuery); | ||
const findField = await this.$static() | ||
.native() | ||
.findOne(findQuery); | ||
if (findField) | ||
@@ -1382,0 +1373,0 @@ return reject(Error(`Field: (${field}) expects a unique value!`)); |
import XMongoModel from "./XMongoModel"; | ||
import { StringToAnyObject } from "./CustomTypes"; | ||
import { UpdateOneOptions, UpdateWriteOpResult } from "mongodb"; | ||
import { UpdateOptions, UpdateResult } from "mongodb"; | ||
declare class XMongoTypedModel<DT = Record<string, any>> extends XMongoModel { | ||
@@ -9,3 +9,3 @@ data: DT; | ||
has: (key: keyof DT | string, value: any) => boolean; | ||
update: (set: Record<keyof DT | string, any>, options?: UpdateOneOptions) => Promise<UpdateWriteOpResult>; | ||
update: (set: Record<keyof DT | string, any>, options?: UpdateOptions) => Promise<UpdateResult>; | ||
/** | ||
@@ -45,4 +45,4 @@ * ----------------------- | ||
*/ | ||
updateTyped(set: Record<keyof DT, any>, options?: UpdateOneOptions): Promise<UpdateWriteOpResult>; | ||
updateTyped(set: Record<keyof DT, any>, options?: UpdateOptions): Promise<UpdateResult>; | ||
} | ||
export = XMongoTypedModel; |
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
7
17
100002
3134
+ Added@types/webidl-conversions@7.0.3(transitive)
+ Added@types/whatwg-url@8.2.2(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbson@4.7.2(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedmongodb@4.1.0(transitive)
+ Addedmongodb-connection-string-url@1.1.2(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedtr46@2.1.0(transitive)
+ Addedwebidl-conversions@6.1.0(transitive)
+ Addedwhatwg-url@8.7.0(transitive)
- Removed@types/mongodb@^3.6.20
- Removed@types/bson@4.2.4(transitive)
- Removed@types/mongodb@3.6.20(transitive)
- Removedbl@2.2.1(transitive)
- Removedbson@1.1.66.10.0(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedinherits@2.0.4(transitive)
- Removedisarray@1.0.0(transitive)
- Removedmongodb@3.6.10(transitive)
- Removedoptional-require@1.1.8(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedrequire-at@1.0.6(transitive)
- Removedsafe-buffer@5.1.25.2.1(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedutil-deprecate@1.0.2(transitive)
Updatedmongodb@4.1.0