xpress-mongo
Advanced tools
Comparing version 0.0.43 to 0.0.44
@@ -107,5 +107,12 @@ "use strict"; | ||
emptyData(replaceWith) { | ||
this.data = { | ||
_id: this.id() | ||
}; | ||
const _id = this.id(); | ||
/** | ||
* if _id exists then add it. | ||
*/ | ||
if (_id) { | ||
this.data = { _id }; | ||
} | ||
else { | ||
this.data = {}; | ||
} | ||
if (replaceWith && typeof replaceWith === 'object') | ||
@@ -393,3 +400,3 @@ this.data = Object.assign(Object.assign({}, this.data), replaceWith); | ||
if (!this.id()) | ||
throw "UPDATE_ERROR: Model does not have an _id, so we assume it is not from the database."; | ||
throw Error("UPDATE_ERROR: Model does not have an _id, so we assume it is not from the database."); | ||
return this.set(set).save(options); | ||
@@ -405,3 +412,3 @@ } | ||
if (!this.id()) | ||
throw "UPDATE_RAW_ERROR: Model does not have an _id, so we assume it is not from the database."; | ||
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) => { | ||
@@ -414,5 +421,6 @@ return this.constructor.native().updateOne({ _id: this.id() }, update, options, (error, res) => error ? reject(error) : resolve(res.connection)); | ||
* @param options | ||
* @param create | ||
* @return {Promise<UpdateWriteOpResult | InsertOneWriteOpResult<*>>} | ||
*/ | ||
save(options = {}) { | ||
save(options = {}, create = false) { | ||
return new Promise((resolve, reject) => { | ||
@@ -419,0 +427,0 @@ const id = this.id(); |
{ | ||
"name": "xpress-mongo", | ||
"version": "0.0.43", | ||
"version": "0.0.44", | ||
"description": "Light Weight ODM for mongoDb", | ||
@@ -5,0 +5,0 @@ "main": "js/index.js", |
@@ -152,6 +152,14 @@ import ObjectCollection = require('object-collection'); | ||
emptyData(replaceWith?: StringToAnyObject): this { | ||
this.data = { | ||
_id: this.id() | ||
}; | ||
const _id = this.id(); | ||
/** | ||
* if _id exists then add it. | ||
*/ | ||
if (_id) { | ||
this.data = {_id}; | ||
} else { | ||
this.data = {}; | ||
} | ||
if (replaceWith && typeof replaceWith === 'object') this.data = {...this.data, ...replaceWith}; | ||
@@ -480,3 +488,3 @@ | ||
update(set: StringToAnyObject, options?: UpdateOneOptions): Promise<UpdateWriteOpResult> { | ||
if (!this.id()) throw "UPDATE_ERROR: Model does not have an _id, so we assume it is not from the database."; | ||
if (!this.id()) throw Error("UPDATE_ERROR: Model does not have an _id, so we assume it is not from the database."); | ||
return <Promise<UpdateWriteOpResult>>this.set(set).save(options) | ||
@@ -492,3 +500,3 @@ } | ||
updateRaw(update: UpdateQuery<any> | Partial<any>, options?: UpdateOneOptions): Promise<UpdateWriteOpResult> { | ||
if (!this.id()) throw "UPDATE_RAW_ERROR: Model does not have an _id, so we assume it is not from the database."; | ||
if (!this.id()) 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) => { | ||
@@ -506,5 +514,6 @@ return (<typeof XMongoModel>this.constructor).native().updateOne( | ||
* @param options | ||
* @param create | ||
* @return {Promise<UpdateWriteOpResult | InsertOneWriteOpResult<*>>} | ||
*/ | ||
save(options: UpdateOneOptions | CollectionInsertOneOptions = {}): Promise<boolean | UpdateWriteOpResult | InsertOneWriteOpResult<any>> { | ||
save(options: UpdateOneOptions | CollectionInsertOneOptions = {}, create = false): Promise<boolean | UpdateWriteOpResult | InsertOneWriteOpResult<any>> { | ||
return new Promise((resolve, reject) => { | ||
@@ -778,3 +787,3 @@ const id = this.id(); | ||
static use(data: StringToAnyObject): XMongoModel { | ||
const model: typeof XMongoModel | StringToAnyObject = new this(); | ||
const model: XMongoModel | any = new this(); | ||
model.emptyData(); | ||
@@ -787,4 +796,4 @@ // Set Original Property | ||
for (const key of this.append) { | ||
if (typeof model[key] === "function") { | ||
model.set(key, model[key]()) | ||
if (typeof <StringToAnyObject>model[key] === "function") { | ||
model.set(key, <StringToAnyObject>model[key]()) | ||
} | ||
@@ -791,0 +800,0 @@ } |
const connection = require('./connection'); | ||
const Chance = require('chance'); | ||
const chance = new Chance(); | ||
@@ -9,6 +7,12 @@ async function run() { | ||
const user = await Users.findById('5e5acba088ebeef8a715ca43', { | ||
projection: {_id: 0} | ||
}); | ||
console.log(user); | ||
/** | ||
* Async Space | ||
*/ | ||
const guest = new Users().set({ | ||
/*const guest = new Users().set({ | ||
type: 'guest', | ||
@@ -22,3 +26,3 @@ first_name: 'Hello', | ||
// console.log(guest.data); | ||
console.log(guest.validate()); | ||
console.log(guest.validate());*/ | ||
@@ -25,0 +29,0 @@ |
@@ -200,5 +200,6 @@ import ObjectCollection = require('object-collection'); | ||
* @param options | ||
* @param create | ||
* @return {Promise<UpdateWriteOpResult | InsertOneWriteOpResult<*>>} | ||
*/ | ||
save(options?: UpdateOneOptions | CollectionInsertOneOptions): Promise<boolean | UpdateWriteOpResult | InsertOneWriteOpResult<any>>; | ||
save(options?: UpdateOneOptions | CollectionInsertOneOptions, create?: boolean): Promise<boolean | UpdateWriteOpResult | InsertOneWriteOpResult<any>>; | ||
/** | ||
@@ -205,0 +206,0 @@ * Unset a key or keys from this collection |
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
127720
3854