xpress-mongo
Advanced tools
Comparing version 0.0.49 to 0.0.50
import {FunctionReturnsBoolean, SchemaPropertiesType, StringToAnyObject, ValidatorType} from "../src/CustomTypes"; | ||
import XMongoModel from "../src/XMongoModel"; | ||
import _ from "../src/Lodash"; | ||
import {watch} from "fs"; | ||
@@ -65,6 +67,18 @@ /** | ||
export async function RunOnEvent(event: string, modelInstance: XMongoModel, changes: string[] = []): Promise<any> { | ||
// function eventKeys(events: StringToAnyObject){ | ||
// const keys = Object.keys(events); | ||
// const keysList = []; | ||
// for (const key of keys){ | ||
// if(typeof events[key] === "function"){ | ||
// keysList.push(key) | ||
// } | ||
// } | ||
// } | ||
export async function RunOnEvent(event: string, modelInstance: XMongoModel, changes?: StringToAnyObject): Promise<any> { | ||
const Model = (modelInstance.constructor as typeof XMongoModel); | ||
if (!Model.events) return false; | ||
let events = Model.events; | ||
@@ -74,3 +88,3 @@ if (!events[event]) return false; | ||
events = events[event]; | ||
if (event === 'watch' && !changes.length) return false | ||
if (event === 'watch' && changes && !Object.keys(changes).length) return false | ||
@@ -84,3 +98,3 @@ if (typeof events === "function") { | ||
if (event === 'watch') { | ||
if (changes.includes(field)) { | ||
if (_.has(changes, field)) { | ||
Promise.all([ | ||
@@ -87,0 +101,0 @@ events[field](modelInstance) |
@@ -11,4 +11,8 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RunOnEvent = exports.runAndValidation = exports.runOrValidation = exports.defaultValue = void 0; | ||
const Lodash_1 = __importDefault(require("../src/Lodash")); | ||
/** | ||
@@ -74,3 +78,12 @@ * Get Default value. | ||
exports.runAndValidation = runAndValidation; | ||
function RunOnEvent(event, modelInstance, changes = []) { | ||
// function eventKeys(events: StringToAnyObject){ | ||
// const keys = Object.keys(events); | ||
// const keysList = []; | ||
// for (const key of keys){ | ||
// if(typeof events[key] === "function"){ | ||
// keysList.push(key) | ||
// } | ||
// } | ||
// } | ||
function RunOnEvent(event, modelInstance, changes) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -84,3 +97,3 @@ const Model = modelInstance.constructor; | ||
events = events[event]; | ||
if (event === 'watch' && !changes.length) | ||
if (event === 'watch' && changes && !Object.keys(changes).length) | ||
return false; | ||
@@ -94,3 +107,3 @@ if (typeof events === "function") { | ||
if (event === 'watch') { | ||
if (changes.includes(field)) { | ||
if (Lodash_1.default.has(changes, field)) { | ||
Promise.all([ | ||
@@ -97,0 +110,0 @@ events[field](modelInstance) |
@@ -68,29 +68,38 @@ "use strict"; | ||
* @param collection | ||
* @param model | ||
* @return {typeof XMongoModel} | ||
*/ | ||
model(collection) { | ||
model(collection, model) { | ||
const connection = this.collection(collection); | ||
/** | ||
* Extend XMongoModel | ||
*/ | ||
return class extends XMongoModel { | ||
/** | ||
* Use `.native()` instead | ||
* @deprecated (v 0.0.40) | ||
*/ | ||
static thisCollection() { | ||
console.error('Model.thisCollection() is deprecated, use .native() instead.'); | ||
if (model) { | ||
model.native = function () { | ||
return connection; | ||
} | ||
; | ||
}; | ||
return model; | ||
} | ||
else { | ||
/** | ||
* Returns native mongodb instance to run native queries | ||
* Extend XMongoModel | ||
*/ | ||
static native() { | ||
return connection; | ||
} | ||
; | ||
}; | ||
return class extends XMongoModel { | ||
/** | ||
* Use `.native()` instead | ||
* @deprecated (v 0.0.40) | ||
*/ | ||
static thisCollection() { | ||
console.error('Model.thisCollection() is deprecated, use .native() instead.'); | ||
return connection; | ||
} | ||
; | ||
/** | ||
* Returns native mongodb instance to run native queries | ||
*/ | ||
static native() { | ||
return connection; | ||
} | ||
; | ||
}; | ||
} | ||
} | ||
} | ||
module.exports = XMongoClient; |
@@ -399,2 +399,9 @@ "use strict"; | ||
/** | ||
* Checks of current instance has changes. | ||
* @return {boolean} | ||
*/ | ||
hasChanges() { | ||
return Object.keys(this.changes()).length > 0; | ||
} | ||
/** | ||
* Update model | ||
@@ -434,4 +441,4 @@ * @param set | ||
yield inbuilt_1.RunOnEvent('update', this); | ||
let $set = this.changes(); | ||
let $setKeys = Object.keys($set); | ||
let $set, changes = this.changes(); | ||
let $setKeys = Object.keys(changes); | ||
if (!$setKeys.length) | ||
@@ -441,3 +448,3 @@ return resolve(false); | ||
try { | ||
$set = Object.assign(Object.assign({}, $set), this.validate($set)); | ||
$set = Object.assign(Object.assign({}, changes), this.validate(changes)); | ||
} | ||
@@ -452,4 +459,4 @@ catch (e) { | ||
else { | ||
inbuilt_1.RunOnEvent('watch', this, $setKeys); | ||
resolve(res.connection); | ||
inbuilt_1.RunOnEvent('watch', this, changes); | ||
} | ||
@@ -1066,3 +1073,16 @@ }); | ||
} | ||
_.set(this.events, event, functionOrFunctions); | ||
const dots = event.split('.'); | ||
if (dots.length > 2) { | ||
let [main, ...others] = dots; | ||
others = others.join('.'); | ||
if (this.events[main]) { | ||
this.events[main][others] = functionOrFunctions; | ||
} | ||
else { | ||
this.events[main] = { [others]: functionOrFunctions }; | ||
} | ||
} | ||
else { | ||
_.set(this.events, event, functionOrFunctions); | ||
} | ||
} | ||
@@ -1069,0 +1089,0 @@ } |
{ | ||
"name": "xpress-mongo", | ||
"version": "0.0.49", | ||
"version": "0.0.50", | ||
"description": "Light Weight ODM for mongoDb", | ||
@@ -29,2 +29,3 @@ "main": "js/index.js", | ||
"@types/mongodb": "^3.5.27", | ||
"axios": "^0.20.0", | ||
"deep-object-diff": "^1.1.0", | ||
@@ -31,0 +32,0 @@ "mongodb": "^3.6.2", |
@@ -87,27 +87,37 @@ import XMongoModel = require('./XMongoModel'); | ||
* @param collection | ||
* @param model | ||
* @return {typeof XMongoModel} | ||
*/ | ||
model(collection: string): typeof XMongoModel { | ||
model(collection: string, model?: typeof XMongoModel): typeof XMongoModel { | ||
const connection: Collection = this.collection(collection); | ||
/** | ||
* Extend XMongoModel | ||
*/ | ||
return <typeof XMongoModel><unknown>class extends XMongoModel { | ||
if (model) { | ||
model.native = function (): Collection { | ||
return connection | ||
} | ||
return model; | ||
} else { | ||
/** | ||
* Use `.native()` instead | ||
* @deprecated (v 0.0.40) | ||
* Extend XMongoModel | ||
*/ | ||
static thisCollection(): Collection { | ||
console.error('Model.thisCollection() is deprecated, use .native() instead.') | ||
return connection; | ||
}; | ||
return <typeof XMongoModel><unknown>class extends XMongoModel { | ||
/** | ||
* Use `.native()` instead | ||
* @deprecated (v 0.0.40) | ||
*/ | ||
static thisCollection(): Collection { | ||
console.error('Model.thisCollection() is deprecated, use .native() instead.') | ||
return connection; | ||
}; | ||
/** | ||
* Returns native mongodb instance to run native queries | ||
*/ | ||
static native(): Collection { | ||
return connection; | ||
}; | ||
/** | ||
* Returns native mongodb instance to run native queries | ||
*/ | ||
static native(): Collection { | ||
return connection; | ||
}; | ||
} | ||
} | ||
} | ||
@@ -114,0 +124,0 @@ } |
@@ -503,2 +503,11 @@ import ObjectCollection = require('object-collection'); | ||
/** | ||
* Checks of current instance has changes. | ||
* @return {boolean} | ||
*/ | ||
hasChanges(): boolean { | ||
return Object.keys(this.changes()).length > 0; | ||
} | ||
/** | ||
* Update model | ||
@@ -544,4 +553,4 @@ * @param set | ||
let $set = this.changes(); | ||
let $setKeys = Object.keys($set); | ||
let $set, changes = this.changes(); | ||
let $setKeys = Object.keys(changes); | ||
if (!$setKeys.length) return resolve(false); | ||
@@ -551,3 +560,3 @@ | ||
try { | ||
$set = {...$set, ...this.validate($set)}; | ||
$set = {...changes, ...this.validate(changes)}; | ||
} catch (e) { | ||
@@ -565,4 +574,4 @@ return reject(e) | ||
} else { | ||
RunOnEvent('watch', this, $setKeys); | ||
resolve(res.connection) | ||
RunOnEvent('watch', this, changes); | ||
} | ||
@@ -1255,3 +1264,16 @@ }) | ||
_.set(this.events, event, functionOrFunctions); | ||
const dots: string[] = event.split('.'); | ||
if (dots.length > 2) { | ||
let [main, ...others]: any = dots; | ||
others = others.join('.'); | ||
if (this.events[main]) { | ||
this.events[main][others] = functionOrFunctions; | ||
} else { | ||
this.events[main] = {[others as string]: functionOrFunctions}; | ||
} | ||
} else { | ||
_.set(this.events, event, functionOrFunctions); | ||
} | ||
} | ||
@@ -1258,0 +1280,0 @@ } |
@@ -9,4 +9,6 @@ const {Client} = require('../'); | ||
}; | ||
const connection = Client(db, db_options); | ||
/** | ||
@@ -16,4 +18,3 @@ * Export Client | ||
*/ | ||
module.exports = async (runAfterConnection = () => false) => { | ||
const connection = Client(db, db_options); | ||
module.exports = {connection, connector: async (runAfterConnection = () => false) => { | ||
let client; | ||
@@ -29,2 +30,2 @@ try { | ||
} | ||
}; | ||
}}; |
@@ -1,5 +0,4 @@ | ||
const {is, RefreshDateOnUpdate} = require('../'); | ||
const {is, RefreshDateOnUpdate, XMongoModel} = require('../'); | ||
const Database = global['Database']; | ||
const ContactSchema = { | ||
@@ -36,2 +35,6 @@ user_id: is.ObjectId().required(), | ||
last_name: is.String(), | ||
contact: is.Object(() => ({ | ||
addy: 'Astro World', | ||
number: '0816762374' | ||
})).required(), | ||
guestId: is.Types([ | ||
@@ -38,0 +41,0 @@ is.Number(), |
@@ -1,5 +0,5 @@ | ||
const connection = require('./connection'); | ||
const {connector, connection} = require('./connection'); | ||
async function run() { | ||
await connection(); | ||
await connector(); | ||
const {Users, Contacts} = require('./models'); | ||
@@ -13,5 +13,3 @@ | ||
await user.update({ | ||
// created_at: new Date() | ||
}); | ||
await user.set('contact.date', new Date()).save(); | ||
@@ -38,7 +36,4 @@ console.log(user); | ||
run().then(() => { | ||
process.exit(); | ||
}).catch(e => { | ||
console.log(e); | ||
process.exit(); | ||
run().catch(console.error).finally(() => { | ||
connection.client.close().catch(console.error); | ||
}); |
@@ -1,2 +0,2 @@ | ||
import { FunctionReturnsBoolean, SchemaPropertiesType, ValidatorType } from "../src/CustomTypes"; | ||
import { FunctionReturnsBoolean, SchemaPropertiesType, StringToAnyObject, ValidatorType } from "../src/CustomTypes"; | ||
import XMongoModel from "../src/XMongoModel"; | ||
@@ -33,2 +33,2 @@ /** | ||
export declare function runAndValidation(value: any, validators: ValidatorType[] | FunctionReturnsBoolean[]): boolean; | ||
export declare function RunOnEvent(event: string, modelInstance: XMongoModel, changes?: string[]): Promise<any>; | ||
export declare function RunOnEvent(event: string, modelInstance: XMongoModel, changes?: StringToAnyObject): Promise<any>; |
@@ -32,6 +32,7 @@ import XMongoModel = require('./XMongoModel'); | ||
* @param collection | ||
* @param model | ||
* @return {typeof XMongoModel} | ||
*/ | ||
model(collection: string): typeof XMongoModel; | ||
model(collection: string, model?: typeof XMongoModel): typeof XMongoModel; | ||
} | ||
export = XMongoClient; |
@@ -199,2 +199,7 @@ import ObjectCollection = require('object-collection'); | ||
/** | ||
* Checks of current instance has changes. | ||
* @return {boolean} | ||
*/ | ||
hasChanges(): boolean; | ||
/** | ||
* Update model | ||
@@ -201,0 +206,0 @@ * @param set |
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
142047
34
4240
6
+ Addedaxios@^0.20.0
+ Addedaxios@0.20.0(transitive)
+ Addedfollow-redirects@1.15.9(transitive)