xpress-mongo
Advanced tools
Comparing version 0.0.24 to 0.0.25
11
index.ts
@@ -1,2 +0,2 @@ | ||
import {MongoClient} from "mongodb"; | ||
import {MongoClient, MongoClientOptions} from "mongodb"; | ||
import {is, XMongoSchemaBuilder} from './src/XMongoSchemaBuilder'; | ||
@@ -9,10 +9,9 @@ import * as Projectors from './fn/projection'; | ||
/** | ||
* | ||
* @param url | ||
* @param options | ||
* @param errorCallback | ||
* Get connected to a client | ||
* @param {string|MongoClient} url | ||
* @param {MongoClientOptions} options | ||
* @return {XMongoClient} | ||
* @constructor | ||
*/ | ||
function Client(url: string | MongoClient, options = undefined, errorCallback = undefined): XMongoClient { | ||
function Client(url: string | MongoClient, options: MongoClientOptions = {}): XMongoClient { | ||
/** | ||
@@ -19,0 +18,0 @@ * If first argument i.e url is an instance of MongoClient |
@@ -21,10 +21,9 @@ "use strict"; | ||
/** | ||
* | ||
* @param url | ||
* @param options | ||
* @param errorCallback | ||
* Get connected to a client | ||
* @param {string|MongoClient} url | ||
* @param {MongoClientOptions} options | ||
* @return {XMongoClient} | ||
* @constructor | ||
*/ | ||
function Client(url, options = undefined, errorCallback = undefined) { | ||
function Client(url, options = {}) { | ||
/** | ||
@@ -31,0 +30,0 @@ * If first argument i.e url is an instance of MongoClient |
@@ -78,3 +78,3 @@ "use strict"; | ||
}, | ||
_a.raw = connection, | ||
_a.thisCollection = () => connection, | ||
_a; | ||
@@ -81,0 +81,0 @@ } |
@@ -80,2 +80,5 @@ "use strict"; | ||
} | ||
// @ts-ignore | ||
static thisCollection() { return null; } | ||
; | ||
/** | ||
@@ -104,3 +107,2 @@ * Empties data in current model. | ||
} | ||
; | ||
/** | ||
@@ -123,3 +125,2 @@ * Set data in model | ||
} | ||
; | ||
/** | ||
@@ -139,3 +140,2 @@ * Insert new record and return instance. | ||
} | ||
; | ||
/** | ||
@@ -149,3 +149,2 @@ * Check if id is a valid id | ||
} | ||
; | ||
/** | ||
@@ -165,3 +164,2 @@ * Set Original result gotten from db | ||
} | ||
; | ||
/** | ||
@@ -178,3 +176,2 @@ * Set multiple schemas and use them at anytime using `.setSchema` | ||
} | ||
; | ||
/** | ||
@@ -193,3 +190,2 @@ * Set Model Schema | ||
} | ||
; | ||
/** | ||
@@ -257,3 +253,2 @@ * Set Model Schema | ||
} | ||
; | ||
/** | ||
@@ -319,3 +314,2 @@ * Get id of current model instance | ||
} | ||
; | ||
/** | ||
@@ -340,3 +334,3 @@ * Create Model if not id is missing or save document if id is found. | ||
} | ||
return this.constructor.raw.updateOne({ _id: this.id() }, { $set }, options, (error, res) => error ? reject(error) : resolve(res.connection)); | ||
return this.constructor.thisCollection().updateOne({ _id: this.id() }, { $set }, options, (error, res) => error ? reject(error) : resolve(res.connection)); | ||
} | ||
@@ -351,3 +345,3 @@ else { | ||
} | ||
return this.constructor.raw.insertOne(this.data, options, (error, res) => { | ||
return this.constructor.thisCollection().insertOne(this.data, options, (error, res) => { | ||
if (error) | ||
@@ -386,3 +380,3 @@ return reject(error); | ||
return new Promise((resolve, reject) => { | ||
return this.constructor.raw.updateOne({ _id: this.id() }, { $unset }, options, (error, res) => { | ||
return this.constructor.thisCollection().updateOne({ _id: this.id() }, { $unset }, options, (error, res) => { | ||
if (error) | ||
@@ -524,3 +518,3 @@ return reject(error); | ||
this.emptyData(); | ||
return this.constructor.raw.deleteOne({ _id }); | ||
return this.constructor.thisCollection().deleteOne({ _id }); | ||
} | ||
@@ -546,3 +540,2 @@ else { | ||
} | ||
; | ||
/** | ||
@@ -568,3 +561,2 @@ * Turn data provided in query function to model instances. | ||
} | ||
; | ||
/** | ||
@@ -622,3 +614,3 @@ * Has One relationship | ||
model = model[0](); | ||
let relatedData = yield model.raw.findOne(where, options); | ||
let relatedData = yield model.thisCollection().findOne(where, options); | ||
if (cast && relatedData) | ||
@@ -658,3 +650,2 @@ relatedData = model.use(relatedData); | ||
} | ||
; | ||
/** | ||
@@ -691,3 +682,3 @@ * Alias to mongo.ObjectID | ||
static find(query, options = {}, raw = false) { | ||
const result = this.raw.find(query, options); | ||
const result = this.thisCollection().find(query, options); | ||
if (raw) | ||
@@ -703,3 +694,2 @@ return result; | ||
} | ||
; | ||
/** | ||
@@ -721,3 +711,3 @@ * Turn array provided to model instances. | ||
* | ||
* Model.raw.find().limit(10).toArray((err, lists) => { | ||
* Model.thisCollection().find().limit(10).toArray((err, lists) => { | ||
* Model.fromArray(lists); | ||
@@ -737,3 +727,3 @@ * }) | ||
return new Promise((resolve, reject) => { | ||
return query(this.raw).toArray((error, lists) => { | ||
return query(this.thisCollection()).toArray((error, lists) => { | ||
if (error) | ||
@@ -768,3 +758,3 @@ return reject(error); | ||
return reject(Error('.toArray expects a function as argument')); | ||
query(this.raw).toArray((error, data) => { | ||
query(this.thisCollection()).toArray((error, data) => { | ||
if (error) | ||
@@ -789,3 +779,3 @@ return reject(error); | ||
return new Promise((resolve, reject) => { | ||
return this.raw.findOne(query, options, (error, data) => { | ||
return this.thisCollection().findOne(query, options, (error, data) => { | ||
if (error) | ||
@@ -826,3 +816,3 @@ return reject(error); | ||
static count(query, options) { | ||
return this.raw.find(query, options).count(); | ||
return this.thisCollection().find(query, options).count(); | ||
} | ||
@@ -839,3 +829,3 @@ /** | ||
query.push({ $count: "count_aggregate" }); | ||
const data = yield this.raw.aggregate(query, options).toArray(); | ||
const data = yield this.thisCollection().aggregate(query, options).toArray(); | ||
if (data.length) { | ||
@@ -862,3 +852,3 @@ return data[0]['count_aggregate']; | ||
const skips = perPage * (page - 1); | ||
const data = yield this.raw.find(query, options).skip(skips).limit(perPage).toArray(); | ||
const data = yield this.thisCollection().find(query, options).skip(skips).limit(perPage).toArray(); | ||
return { | ||
@@ -873,3 +863,2 @@ total, | ||
} | ||
; | ||
/** | ||
@@ -893,3 +882,3 @@ * Paginate Aggregation. | ||
query.push({ $limit: perPage }); | ||
const data = yield this.raw.aggregate(query, options).toArray(); | ||
const data = yield this.thisCollection().aggregate(query, options).toArray(); | ||
return { | ||
@@ -904,3 +893,2 @@ total, | ||
} | ||
; | ||
} | ||
@@ -907,0 +895,0 @@ /** |
{ | ||
"name": "xpress-mongo", | ||
"version": "0.0.24", | ||
"version": "0.0.25", | ||
"description": "Light Weight ODM for mongoDb", | ||
@@ -34,3 +34,2 @@ "main": "js/index.js", | ||
"@types/lodash": "^4.14.149", | ||
"@types/lodash-es": "^4.17.3", | ||
"@types/mongodb": "^3.5.4", | ||
@@ -37,0 +36,0 @@ "@types/node": "^13.11.0", |
@@ -96,3 +96,3 @@ import XMongoModel = require('./XMongoModel'); | ||
return <typeof XMongoModel><unknown>class extends XMongoModel { | ||
static raw = connection; | ||
static thisCollection = () => connection; | ||
} | ||
@@ -99,0 +99,0 @@ } |
@@ -82,2 +82,3 @@ import ObjectCollection = require('object-collection'); | ||
* @type {Collection|null} | ||
* @deprecated - use thisCollection() | ||
*/ | ||
@@ -123,2 +124,5 @@ static raw: Collection; | ||
// @ts-ignore | ||
static thisCollection(): Collection { return null }; | ||
/** | ||
@@ -148,3 +152,3 @@ * Empties data in current model. | ||
return _.get(this.data, key, $default); | ||
}; | ||
} | ||
@@ -166,3 +170,3 @@ /** | ||
return this; | ||
}; | ||
} | ||
@@ -180,3 +184,3 @@ | ||
return record; | ||
}; | ||
} | ||
@@ -190,3 +194,3 @@ /** | ||
return ObjectID.isValid(objectId) | ||
}; | ||
} | ||
@@ -210,3 +214,3 @@ | ||
return this; | ||
}; | ||
} | ||
@@ -223,3 +227,3 @@ /** | ||
return this; | ||
}; | ||
} | ||
@@ -238,3 +242,3 @@ /** | ||
return this.useSchema(schema); | ||
}; | ||
} | ||
@@ -317,3 +321,3 @@ /** | ||
return this; | ||
}; | ||
} | ||
@@ -386,3 +390,3 @@ /** | ||
return <Promise<UpdateWriteOpResult>>this.set(set).save(options) | ||
}; | ||
} | ||
@@ -410,3 +414,3 @@ /** | ||
return (<typeof XMongoModel>this.constructor).raw.updateOne( | ||
return (<typeof XMongoModel>this.constructor).thisCollection().updateOne( | ||
{_id: this.id()}, | ||
@@ -424,3 +428,3 @@ {$set}, | ||
return (<typeof XMongoModel>this.constructor).raw.insertOne( | ||
return (<typeof XMongoModel>this.constructor).thisCollection().insertOne( | ||
this.data, | ||
@@ -469,3 +473,3 @@ <CollectionInsertOneOptions>options, | ||
return new Promise((resolve, reject) => { | ||
return (<typeof XMongoModel>this.constructor).raw.updateOne( | ||
return (<typeof XMongoModel>this.constructor).thisCollection().updateOne( | ||
{_id: this.id()}, | ||
@@ -633,3 +637,3 @@ {$unset}, | ||
this.emptyData(); | ||
return (<typeof XMongoModel>this.constructor).raw.deleteOne({_id}) | ||
return (<typeof XMongoModel>this.constructor).thisCollection().deleteOne({_id}) | ||
} else { | ||
@@ -657,3 +661,3 @@ throw "DELETE_ERROR: Model does not have an _id, so we assume it is not from the database."; | ||
return <ObjectCollection>this.$data; | ||
}; | ||
} | ||
@@ -681,3 +685,3 @@ /** | ||
return <XMongoModel>model; | ||
}; | ||
} | ||
@@ -743,3 +747,3 @@ /** | ||
let relatedData: StringToAnyObject | void = await (<typeof XMongoModel>model).raw.findOne(where, options); | ||
let relatedData: StringToAnyObject | void = await (<typeof XMongoModel>model).thisCollection().findOne(where, options); | ||
@@ -780,3 +784,3 @@ if (cast && relatedData) relatedData = (<typeof XMongoModel>model).use(relatedData); | ||
return JSON.stringify(this.data, replacer, space); | ||
}; | ||
} | ||
@@ -815,3 +819,3 @@ /** | ||
static find(query: StringToAnyObject, options: FindOneOptions = {}, raw = false): Promise<XMongoModel[]> | Cursor { | ||
const result = this.raw.find(query, options); | ||
const result = this.thisCollection().find(query, options); | ||
if (raw) return result; | ||
@@ -825,3 +829,3 @@ | ||
}); | ||
}; | ||
} | ||
@@ -844,3 +848,3 @@ /** | ||
* | ||
* Model.raw.find().limit(10).toArray((err, lists) => { | ||
* Model.thisCollection().find().limit(10).toArray((err, lists) => { | ||
* Model.fromArray(lists); | ||
@@ -857,6 +861,6 @@ * }) | ||
*/ | ||
static fromArray(query: FunctionWithRawArgument | StringToAnyObject[], interceptor: boolean | { (lists: Array<any>): any } = false): XMongoModel[] | Promise<any[]> { | ||
static fromArray(query: FunctionWithRawArgument | any[], interceptor: boolean | { (lists: Array<any>): any } = false): XMongoModel[] | Promise<any[]> { | ||
if (typeof query === "function") { | ||
return new Promise((resolve, reject) => { | ||
return (<Cursor>query(this.raw)).toArray((error, lists) => { | ||
return (<Cursor>query(this.thisCollection())).toArray((error, lists) => { | ||
if (error) return reject(error); | ||
@@ -891,3 +895,3 @@ | ||
(<Cursor>query(this.raw)).toArray((error, data) => { | ||
(<Cursor>query(this.thisCollection())).toArray((error, data) => { | ||
if (error) return reject(error); | ||
@@ -915,3 +919,3 @@ return resolve(data); | ||
return new Promise((resolve, reject) => { | ||
return this.raw.findOne(query, <FindOneOptions>options, (error, data) => { | ||
return this.thisCollection().findOne(query, <FindOneOptions>options, (error, data) => { | ||
if (error) return reject(error); | ||
@@ -954,3 +958,3 @@ // Return new instance of Model | ||
static count(query: StringToAnyObject, options?: FindOneOptions): Promise<number> { | ||
return this.raw.find(query, options).count() | ||
return this.thisCollection().find(query, options).count() | ||
} | ||
@@ -968,3 +972,3 @@ | ||
const data = await this.raw.aggregate(query, options).toArray(); | ||
const data = await this.thisCollection().aggregate(query, options).toArray(); | ||
@@ -993,3 +997,3 @@ if (data.length) { | ||
const skips = perPage * (page - 1); | ||
const data = await this.raw.find(query, options).skip(skips).limit(perPage).toArray(); | ||
const data = await this.thisCollection().find(query, options).skip(skips).limit(perPage).toArray(); | ||
@@ -1003,3 +1007,3 @@ return { | ||
} | ||
}; | ||
} | ||
@@ -1027,3 +1031,3 @@ /** | ||
const data = await this.raw.aggregate(query, options).toArray(); | ||
const data = await this.thisCollection().aggregate(query, options).toArray(); | ||
@@ -1037,3 +1041,3 @@ return { | ||
} | ||
}; | ||
} | ||
} | ||
@@ -1040,0 +1044,0 @@ |
@@ -59,3 +59,5 @@ { | ||
// "typeRoots": [], /* List of folders to include type definitions from. */ | ||
// "types": [], /* Type declaration files to be included in compilation. */ | ||
"types": [ | ||
"node", "lodash", "mongodb" | ||
], /* Type declaration files to be included in compilation. */ | ||
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ | ||
@@ -62,0 +64,0 @@ "esModuleInterop": true, |
@@ -1,2 +0,2 @@ | ||
import { MongoClient } from "mongodb"; | ||
import { MongoClient, MongoClientOptions } from "mongodb"; | ||
import { is, XMongoSchemaBuilder } from './src/XMongoSchemaBuilder'; | ||
@@ -8,10 +8,9 @@ import * as Projectors from './fn/projection'; | ||
/** | ||
* | ||
* @param url | ||
* @param options | ||
* @param errorCallback | ||
* Get connected to a client | ||
* @param {string|MongoClient} url | ||
* @param {MongoClientOptions} options | ||
* @return {XMongoClient} | ||
* @constructor | ||
*/ | ||
declare function Client(url: string | MongoClient, options?: undefined, errorCallback?: undefined): XMongoClient; | ||
declare function Client(url: string | MongoClient, options?: MongoClientOptions): XMongoClient; | ||
export { is, Client, Projectors, XMongoModel, XMongoDataType, XMongoSchemaBuilder }; |
@@ -52,2 +52,3 @@ import ObjectCollection = require('object-collection'); | ||
* @type {Collection|null} | ||
* @deprecated - use thisCollection() | ||
*/ | ||
@@ -69,2 +70,3 @@ static raw: Collection; | ||
constructor(); | ||
static thisCollection(): Collection; | ||
/** | ||
@@ -246,3 +248,3 @@ * Empties data in current model. | ||
* | ||
* Model.raw.find().limit(10).toArray((err, lists) => { | ||
* Model.thisCollection().find().limit(10).toArray((err, lists) => { | ||
* Model.fromArray(lists); | ||
@@ -259,3 +261,3 @@ * }) | ||
*/ | ||
static fromArray(query: FunctionWithRawArgument | StringToAnyObject[], interceptor?: boolean | { | ||
static fromArray(query: FunctionWithRawArgument | any[], interceptor?: boolean | { | ||
(lists: Array<any>): any; | ||
@@ -262,0 +264,0 @@ }): XMongoModel[] | Promise<any[]>; |
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
121707
4
3268