Comparing version 1.1.4 to 1.1.5
{ | ||
"name": "mongot", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "MongoT is a modern ODM library for MongoDb.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -6,2 +6,10 @@ import * as MongoDb from 'mongodb'; | ||
import { InsertResult, DeleteResult, UpdateResult, FindAndModifyResult } from "./collection/helpers"; | ||
export declare namespace Events { | ||
const beforeInsert = "beforeInsert"; | ||
const beforeUpdate = "beforeUpdate"; | ||
const beforeDelete = "beforeDelete"; | ||
const afterInsert = "afterInsert"; | ||
const afterUpdate = "afterUpdate"; | ||
const afterDelete = "afterDelete"; | ||
} | ||
export declare type Partial<T> = Object; | ||
@@ -11,3 +19,3 @@ declare class Collection<TDocument extends SchemaDocument> { | ||
private readonly state; | ||
private readonly name; | ||
readonly name: string; | ||
readonly connection: Promise<Connection>; | ||
@@ -36,3 +44,3 @@ constructor(connection: Promise<Connection>, name?: string, options?: MongoDb.CollectionOptions | MongoDb.CollectionCreateOptions, construct?: typeof SchemaDocument); | ||
* @param options | ||
* @returns {undefined} | ||
* @returns {any} | ||
*/ | ||
@@ -43,3 +51,3 @@ aggregate(pipeline: Object[], options?: MongoDb.CollectionAggregationOptions): Promise<MongoDb.AggregationCursor>; | ||
* @param options | ||
* @returns {undefined} | ||
* @returns {any} | ||
*/ | ||
@@ -50,3 +58,3 @@ bulkWrite<TResult extends MongoDb.BulkWriteResult>(operations: Object[], options: MongoDb.CollectionBluckWriteOptions): Promise<TResult>; | ||
* @param options | ||
* @returns {undefined} | ||
* @returns {Promise<number>} | ||
*/ | ||
@@ -53,0 +61,0 @@ count(query: Object, options: MongoDb.MongoCountPreferences): Promise<number>; |
@@ -34,3 +34,3 @@ "use strict"; | ||
Events.afterDelete = 'afterDelete'; | ||
})(Events || (Events = {})); | ||
})(Events = exports.Events || (exports.Events = {})); | ||
class Collection { | ||
@@ -128,3 +128,3 @@ constructor(connection, name, options, construct) { | ||
* @param options | ||
* @returns {undefined} | ||
* @returns {any} | ||
*/ | ||
@@ -137,3 +137,3 @@ aggregate(pipeline, options) { | ||
* @param options | ||
* @returns {undefined} | ||
* @returns {any} | ||
*/ | ||
@@ -146,3 +146,3 @@ bulkWrite(operations, options) { | ||
* @param options | ||
* @returns {undefined} | ||
* @returns {Promise<number>} | ||
*/ | ||
@@ -181,6 +181,5 @@ count(query, options) { | ||
if (filter instanceof document_1.SchemaDocument) { | ||
const listener = filter.getEventListener(); | ||
listener.emit(Events.beforeDelete); | ||
yield filter.call(Events.beforeDelete, this); | ||
const deleteResult = new helpers_1.DeleteResult(yield collection.deleteOne(this.filter(filter), options)); | ||
listener.emit(Events.afterDelete); | ||
yield filter.call(Events.afterDelete, this); | ||
return deleteResult; | ||
@@ -347,10 +346,9 @@ } | ||
const documents = docs.map(doc => doc instanceof document_1.SchemaDocument ? doc : this.factory(doc)); | ||
const listeners = documents.map(doc => doc.getEventListener()); | ||
listeners.forEach(listener => listener.emit(Events.beforeInsert)); | ||
yield Promise.all(documents.map(document => document.call(Events.beforeInsert, this))); | ||
const result = yield collection.insertMany(documents.map(doc => this.createObjectReference(doc)), options); | ||
return result.ops.map(res => { | ||
const inertResult = new helpers_1.InsertResult({ insertedId: res._id }, res.unref()); | ||
listeners.map(listener => listener.emit(Events.afterInsert)); | ||
return inertResult; | ||
const insertResultList = result.ops.map(res => { | ||
return new helpers_1.InsertResult({ insertedId: res._id }, res.unref()); | ||
}); | ||
yield Promise.all(insertResultList.map(({ ref }) => ref.call(Events.afterInsert, this))); | ||
return insertResultList; | ||
})); | ||
@@ -365,13 +363,12 @@ } | ||
return this.queue((collection) => __awaiter(this, void 0, void 0, function* () { | ||
let prepared; | ||
let formalized; | ||
if (document instanceof document_1.SchemaDocument) { | ||
prepared = document; | ||
formalized = document; | ||
} | ||
else { | ||
prepared = this.factory(document); | ||
formalized = this.factory(document); | ||
} | ||
const listener = prepared.getEventListener(); | ||
listener.emit(Events.beforeInsert); | ||
const insertResult = new helpers_1.InsertResult(yield collection.insertOne(prepared.toObject(), options), prepared); | ||
listener.emit(Events.afterInsert); | ||
yield formalized.call(Events.beforeInsert, this); | ||
const insertResult = new helpers_1.InsertResult(yield collection.insertOne(formalized.toObject(), options), formalized); | ||
yield formalized.call(Events.afterInsert, this); | ||
return insertResult; | ||
@@ -462,12 +459,10 @@ })); | ||
if (filter instanceof document_1.SchemaDocument) { | ||
const listener = filter.getEventListener(); | ||
beforeUpdate = () => listener.emit(Events.beforeUpdate); | ||
afterUpdate = () => listener.emit(Events.afterUpdate); | ||
beforeUpdate = () => filter.call(Events.beforeUpdate, this); | ||
afterUpdate = () => filter.call(Events.afterUpdate, this); | ||
} | ||
if (update instanceof document_1.SchemaDocument) { | ||
const listener = update.getEventListener(); | ||
beforeUpdate = () => listener.emit(Events.beforeUpdate); | ||
afterUpdate = () => listener.emit(Events.afterUpdate); | ||
beforeUpdate = () => update.call(Events.beforeUpdate, this); | ||
afterUpdate = () => update.call(Events.afterUpdate, this); | ||
} | ||
beforeUpdate(); | ||
yield beforeUpdate(); | ||
if (update instanceof document_1.SchemaDocument) { | ||
@@ -478,3 +473,3 @@ const _a = update.toObject(), { _id } = _a, document = __rest(_a, ["_id"]); | ||
const updateResult = new helpers_1.UpdateResult(yield collection.updateOne(this.filter(filter), updateSchema, options)); | ||
afterUpdate(); | ||
yield afterUpdate(); | ||
return updateResult; | ||
@@ -481,0 +476,0 @@ })); |
import * as MongoDb from 'mongodb'; | ||
import { SchemaDocument } from "../document"; | ||
import { Collection } from "../collection"; | ||
export declare class UpdateResult { | ||
@@ -34,1 +35,2 @@ readonly matched: number; | ||
} | ||
export declare const createNextAutoIncrementNumber: <T extends SchemaDocument>(collection: Collection<T>) => Promise<number>; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
const document_1 = require("../document"); | ||
@@ -44,2 +52,14 @@ class UpdateResult { | ||
exports.FindAndModifyResult = FindAndModifyResult; | ||
exports.createNextAutoIncrementNumber = (collection) => __awaiter(this, void 0, void 0, function* () { | ||
const { db } = yield collection.connection; | ||
const res = yield db.collection('mongot.counter').findOneAndUpdate({ | ||
_id: collection.name | ||
}, { | ||
$inc: { seq: 1 } | ||
}, { | ||
upsert: true, | ||
returnOriginal: false | ||
}); | ||
return res.value.seq; | ||
}); | ||
//# sourceMappingURL=helpers.js.map |
@@ -1,5 +0,4 @@ | ||
/// <reference types="node" /> | ||
import { ObjectID } from "mongodb"; | ||
import { SchemaMutate } from './metadata/mutation'; | ||
import { EventEmitter } from "events"; | ||
import { Collection } from "./collection"; | ||
export declare const PRIMARY_KEY_NAME = "_id"; | ||
@@ -26,3 +25,3 @@ export declare class TypeCast { | ||
}>; | ||
protected getDefinedHooks(): string[]; | ||
protected getDefinedHooks(): Map<string, string[]>; | ||
toObject(): any; | ||
@@ -32,11 +31,5 @@ toJSON(): any; | ||
} | ||
export declare class SchemaDocument extends SchemaMetadata { | ||
export declare abstract class SchemaDocument extends SchemaMetadata { | ||
readonly _id: ObjectID; | ||
getEventListener(): EventEmitter; | ||
protected beforeInsert(): void; | ||
protected beforeUpdate(): void; | ||
protected beforeDelete(): void; | ||
protected afterInsert(): void; | ||
protected afterUpdate(): void; | ||
protected afterDelete(): void; | ||
call(hook: string, collection: Collection<this>): Promise<any[]>; | ||
} | ||
@@ -43,0 +36,0 @@ export declare class SchemaFragment extends SchemaMetadata { |
@@ -15,3 +15,2 @@ "use strict"; | ||
const mutation_1 = require("./metadata/mutation"); | ||
const events_1 = require("events"); | ||
const identifiers = new WeakMap(); | ||
@@ -214,17 +213,9 @@ const values = new WeakMap(); | ||
class SchemaDocument extends SchemaMetadata { | ||
getEventListener() { | ||
const emitter = new events_1.EventEmitter(); | ||
this.getDefinedHooks().forEach(hook => { | ||
if (typeof this[hook] === 'function') { | ||
emitter.on(hook, () => this[hook]()); | ||
} | ||
}); | ||
return emitter; | ||
call(hook, collection) { | ||
const definedHooks = this.getDefinedHooks(); | ||
if (definedHooks && definedHooks.has(hook)) { | ||
return Promise.all(definedHooks.get(hook).map(property => this[property](collection))); | ||
} | ||
return Promise.resolve([]); | ||
} | ||
beforeInsert() { } | ||
beforeUpdate() { } | ||
beforeDelete() { } | ||
afterInsert() { } | ||
afterUpdate() { } | ||
afterDelete() { } | ||
} | ||
@@ -231,0 +222,0 @@ exports.SchemaDocument = SchemaDocument; |
@@ -33,4 +33,4 @@ import { SchemaMetadata } from "../document"; | ||
}; | ||
static setSchemaHookMetadata(target: typeof SchemaMetadata, method: string): void; | ||
static getSchemaHookMetadata(target: typeof SchemaMetadata): string[]; | ||
static setSchemaHookMetadata(target: typeof SchemaMetadata, hook: string, property?: string): void; | ||
static getSchemaHookMetadata(target: typeof SchemaMetadata): Map<string, string[]>; | ||
} |
@@ -49,7 +49,13 @@ "use strict"; | ||
} | ||
static setSchemaHookMetadata(target, method) { | ||
StoreHooks.set(target, (StoreHooks.get(target) || []).concat([method])); | ||
static setSchemaHookMetadata(target, hook, property) { | ||
if (false === StoreHooks.has(target)) { | ||
StoreHooks.set(target, new Map()); | ||
} | ||
if (false === StoreHooks.get(target).has(hook)) { | ||
StoreHooks.get(target).set(hook, []); | ||
} | ||
StoreHooks.get(target).get(hook).push(property || hook); | ||
} | ||
static getSchemaHookMetadata(target) { | ||
return StoreHooks.get(target) || []; | ||
return StoreHooks.get(target); | ||
} | ||
@@ -56,0 +62,0 @@ } |
@@ -35,2 +35,3 @@ import "./reflect"; | ||
export declare const preq: PropRequiredDecorator; | ||
export declare const hook: (target: any, propertyKey: string) => void; | ||
export declare const hook: (...args: any[]) => (target: any, propertyKey: string) => void; | ||
export declare const auto: (fn: Function) => (target: any, propertyKey: string) => void; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
require("./reflect"); | ||
@@ -6,2 +14,3 @@ const assert_1 = require("assert"); | ||
const document_1 = require("./document"); | ||
const collection_1 = require("./collection"); | ||
exports.collection = (...args) => { | ||
@@ -84,5 +93,22 @@ assert_1.ok(args.length > 0 && args.length < 4, 'Mapper @collection has invalid number of arguments: ' + args.length); | ||
}; | ||
exports.hook = (target, propertyKey) => { | ||
exports.hook = (...args) => { | ||
if (typeof args[0] === 'string') { | ||
return (target, propertyKey) => { | ||
store_1.MetadataStore.setSchemaHookMetadata(target.constructor, args[0], propertyKey); | ||
}; | ||
} | ||
const [target, propertyKey] = args; | ||
store_1.MetadataStore.setSchemaHookMetadata(target.constructor, propertyKey); | ||
}; | ||
exports.auto = (fn) => { | ||
return (target, propertyKey) => { | ||
const property = `$_generated_auto_before_insert_${propertyKey}$`; | ||
target[property] = function (collection) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this[propertyKey] = yield fn(collection); | ||
}); | ||
}; | ||
store_1.MetadataStore.setSchemaHookMetadata(target.constructor, collection_1.Events.beforeInsert, property); | ||
}; | ||
}; | ||
//# sourceMappingURL=schema.js.map |
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
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
127822
1849