From bbf4b2f6ebbe586f09960474cad7ebec0e8ccb92 Mon Sep 17 00:00:00 2001 From: Urigo Date: Thu, 26 Jan 2017 04:54:11 -0200 Subject: [PATCH] Add 'matb33:collection-hooks' type definitions --- meteor-collection-hooks/README.md | 4 + meteor-collection-hooks/index.d.ts | 184 ++++++++++++++++++ .../meteor-collection-hooks-tests.ts | 79 ++++++++ meteor-collection-hooks/package.json | 5 + meteor-collection-hooks/tsconfig.json | 23 +++ 5 files changed, 295 insertions(+) create mode 100644 meteor-collection-hooks/README.md create mode 100644 meteor-collection-hooks/index.d.ts create mode 100644 meteor-collection-hooks/meteor-collection-hooks-tests.ts create mode 100644 meteor-collection-hooks/package.json create mode 100644 meteor-collection-hooks/tsconfig.json diff --git a/meteor-collection-hooks/README.md b/meteor-collection-hooks/README.md new file mode 100644 index 0000000000..158fb76e7c --- /dev/null +++ b/meteor-collection-hooks/README.md @@ -0,0 +1,4 @@ +# Meteor Collection Hooks Definitions + +Type definitions for Meteor package `matb33:collection-hooks`. +Be sure to reference any sort of basic Meteor type definitions file before use. \ No newline at end of file diff --git a/meteor-collection-hooks/index.d.ts b/meteor-collection-hooks/index.d.ts new file mode 100644 index 0000000000..49e923658d --- /dev/null +++ b/meteor-collection-hooks/index.d.ts @@ -0,0 +1,184 @@ +// Type definitions for Meteor package matb33:collection-hooks +// Project: https://github.com/matb33/meteor-collection-hooks +// Definitions by: Trygve Wastvedt +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare module 'meteor/mongo' { + namespace Mongo { + interface Selector {} + interface Modifier {} + interface ObjectID {} + interface SortSpecifier {} + interface FieldSpecifier {} + interface Cursor {} + + interface Collection { + before: { + find(hook: {(userId: string, selector: Mongo.Selector, options: { multi?: boolean; upsert?: boolean; }): void}): void; + findOne(hook: {(userId: string, selector: Mongo.Selector, options: { multi?: boolean; upsert?: boolean; }): void}): void; + insert(hook: {(userId: string, doc: T): void}): void; + remove(hook: {(userId: string, doc: T): void}): void; + update(hook: {(userId: string, doc: T, fieldNames: string[], modifier: Mongo.Modifier, options: { multi?: boolean; upsert?: boolean; }): void}): void; + upsert(hook: {(userId: string, doc: T, selector: Mongo.Selector, modifier: Mongo.Modifier, options: { multi?: boolean; upsert?: boolean; }): void}): void; + }; + after: { + find(hook: {(userId: string, selector: Mongo.Selector, options: { multi?: boolean; upsert?: boolean; }, cursor: Mongo.Cursor): void}): void; + findOne(hook: {(userId: string, selector: Mongo.Selector, options: { multi?: boolean; upsert?: boolean; }, doc: T): void}): void; + insert(hook: {(userId: string, doc: T): void}): void; + remove(hook: {(userId: string, doc: T): void}): void; + update(hook: {(userId: string, doc: T, fieldNames: string[], modifier: Mongo.Modifier, options: { multi?: boolean; upsert?: boolean; }): void}, options?: HookOptions): void; + upsert(hook: {(userId: string, doc: T, selector: Mongo.Selector, modifier: Mongo.Modifier, options: { multi?: boolean; upsert?: boolean; }): void}): void; + }; + direct: { + find(selector?: Mongo.Selector | Mongo.ObjectID | string, options?: { + sort?: Mongo.SortSpecifier; + skip?: number; + limit?: number; + fields?: Mongo.FieldSpecifier; + reactive?: boolean; + transform?: Function; + }): Mongo.Cursor; + findOne(selector?: Mongo.Selector | Mongo.ObjectID | string, options?: { + sort?: Mongo.SortSpecifier; + skip?: number; + fields?: Mongo.FieldSpecifier; + reactive?: boolean; + transform?: Function; + }): T; + insert(doc: T, callback?: Function): string; + remove(selector: Mongo.Selector | Mongo.ObjectID | string, callback?: Function): number; + update(selector: Mongo.Selector | Mongo.ObjectID | string, modifier: Mongo.Modifier, options?: { + multi?: boolean; + upsert?: boolean; + }, callback?: Function): number; + upsert(selector: Mongo.Selector | Mongo.ObjectID | string, modifier: Mongo.Modifier, options?: { + multi?: boolean; + }, callback?: Function): {numberAffected?: number; insertedId?: string;}; + }; + hookOptions: CollectionOptions; + } + } + + interface HookOptions { + fetchPrevious?: boolean; + } + + interface CollectionOptions { + before?: { + all?: HookOptions; + find?: HookOptions; + findOne?: HookOptions; + insert?: HookOptions; + remove?: HookOptions; + update?: HookOptions; + upsert?: HookOptions; + }; + after?: { + all?: HookOptions; + find?: HookOptions; + findOne?: HookOptions; + insert?: HookOptions; + remove?: HookOptions; + update?: HookOptions; + upsert?: HookOptions; + }; + all?: { + all?: HookOptions; + find?: HookOptions; + findOne?: HookOptions; + insert?: HookOptions; + remove?: HookOptions; + update?: HookOptions; + upsert?: HookOptions; + }; + } +} + +declare namespace Mongo { + interface Selector {} + interface Modifier {} + interface ObjectID {} + interface SortSpecifier {} + interface FieldSpecifier {} + interface Cursor {} + + interface Collection { + before: { + find(hook: {(userId: string, selector: Mongo.Selector, options: { multi?: boolean; upsert?: boolean; }): void}): void; + findOne(hook: {(userId: string, selector: Mongo.Selector, options: { multi?: boolean; upsert?: boolean; }): void}): void; + insert(hook: {(userId: string, doc: T): void}): void; + remove(hook: {(userId: string, doc: T): void}): void; + update(hook: {(userId: string, doc: T, fieldNames: string[], modifier: Mongo.Modifier, options: { multi?: boolean; upsert?: boolean; }): void}): void; + upsert(hook: {(userId: string, doc: T, selector: Mongo.Selector, modifier: Mongo.Modifier, options: { multi?: boolean; upsert?: boolean; }): void}): void; + }; + after: { + find(hook: {(userId: string, selector: Mongo.Selector, options: { multi?: boolean; upsert?: boolean; }, cursor: Mongo.Cursor): void}): void; + findOne(hook: {(userId: string, selector: Mongo.Selector, options: { multi?: boolean; upsert?: boolean; }, doc: T): void}): void; + insert(hook: {(userId: string, doc: T): void}): void; + remove(hook: {(userId: string, doc: T): void}): void; + update(hook: {(userId: string, doc: T, fieldNames: string[], modifier: Mongo.Modifier, options: { multi?: boolean; upsert?: boolean; }): void}, options?: HookOptions): void; + upsert(hook: {(userId: string, doc: T, selector: Mongo.Selector, modifier: Mongo.Modifier, options: { multi?: boolean; upsert?: boolean; }): void}): void; + }; + direct: { + find(selector?: Mongo.Selector | Mongo.ObjectID | string, options?: { + sort?: Mongo.SortSpecifier; + skip?: number; + limit?: number; + fields?: Mongo.FieldSpecifier; + reactive?: boolean; + transform?: Function; + }): Mongo.Cursor; + findOne(selector?: Mongo.Selector | Mongo.ObjectID | string, options?: { + sort?: Mongo.SortSpecifier; + skip?: number; + fields?: Mongo.FieldSpecifier; + reactive?: boolean; + transform?: Function; + }): T; + insert(doc: T, callback?: Function): string; + remove(selector: Mongo.Selector | Mongo.ObjectID | string, callback?: Function): number; + update(selector: Mongo.Selector | Mongo.ObjectID | string, modifier: Mongo.Modifier, options?: { + multi?: boolean; + upsert?: boolean; + }, callback?: Function): number; + upsert(selector: Mongo.Selector | Mongo.ObjectID | string, modifier: Mongo.Modifier, options?: { + multi?: boolean; + }, callback?: Function): {numberAffected?: number; insertedId?: string;}; + }; + hookOptions: CollectionOptions; + } +} + +declare interface HookOptions { + fetchPrevious?: boolean; +} + +declare interface CollectionOptions { + before?: { + all?: HookOptions; + find?: HookOptions; + findOne?: HookOptions; + insert?: HookOptions; + remove?: HookOptions; + update?: HookOptions; + upsert?: HookOptions; + }; + after?: { + all?: HookOptions; + find?: HookOptions; + findOne?: HookOptions; + insert?: HookOptions; + remove?: HookOptions; + update?: HookOptions; + upsert?: HookOptions; + }; + all?: { + all?: HookOptions; + find?: HookOptions; + findOne?: HookOptions; + insert?: HookOptions; + remove?: HookOptions; + update?: HookOptions; + upsert?: HookOptions; + }; +} \ No newline at end of file diff --git a/meteor-collection-hooks/meteor-collection-hooks-tests.ts b/meteor-collection-hooks/meteor-collection-hooks-tests.ts new file mode 100644 index 0000000000..43efa16e3e --- /dev/null +++ b/meteor-collection-hooks/meteor-collection-hooks-tests.ts @@ -0,0 +1,79 @@ +/// + +interface Model { + _id: string; + value: number; +} + +const Collection = new Mongo.Collection("test_collection"); +let cursor: Mongo.Cursor; +let doc: Model; + +const selector = { + sort: { createdAt: -1 }, + skip: 5, + limit: 5, + fields: { createdAt: 1 }, + reacrive: true, + transform: function () {} +}; + +Collection.before.find(function (userId: string, selector: Mongo.Selector, options: { multi?: boolean; upsert?: boolean; }): void {}); +Collection.before.findOne(function (userId: string, selector: Mongo.Selector, options: { multi?: boolean; upsert?: boolean; }): void {}); +Collection.before.insert(function (userId: string, doc: Model): void {}); +Collection.before.remove(function (userId: string, doc: Model): void {}); +Collection.before.update(function (userId: string, doc: Model, fieldNames: string[], modifier: Mongo.Modifier, options: { multi?: boolean; upsert?: boolean; }): void {}); +Collection.before.upsert(function (userId: string, doc: Model, selector: Mongo.Selector, modifier: Mongo.Modifier, options: { multi?: boolean; upsert?: boolean; }): void {}); + +Collection.after.find(function (userId: string, selector: Mongo.Selector, options: { multi?: boolean; upsert?: boolean; }, cursor: Mongo.Cursor): void {}); +Collection.after.findOne(function (userId: string, selector: Mongo.Selector, options: { multi?: boolean; upsert?: boolean; }, doc: Model): void {}); +Collection.after.insert(function (userId: string, doc: Model): void {}); +Collection.after.remove(function (userId: string, doc: Model): void {}); +Collection.after.update(function (userId: string, doc: Model, fieldNames: string[], modifier: Mongo.Modifier, options: { multi?: boolean; upsert?: boolean; }): void {}, { fetchPrevious: true }); +Collection.after.upsert(function (userId: string, doc: Model, selector: Mongo.Selector, modifier: Mongo.Modifier, options: { multi?: boolean; upsert?: boolean; }): void {}); + +cursor = Collection.direct.find({ _id: doc._id }, { sort: { createdAt: -1 }, skip: 5, limit: 5, fields: { createdAt: 1 }, reactive: true, transform: function () {} }); +cursor = Collection.direct.find(doc._id, { sort: { createdAt: -1 }, skip: 5, limit: 5, fields: { createdAt: 1 }, reactive: true, transform: function () {} }); + +doc = Collection.direct.findOne({ _id: doc._id }, { sort: { createdAt: -1 }, skip: 5, fields: { createdAt: 1 }, reactive: true, transform: function () {} }); +doc = Collection.direct.findOne(doc._id, { sort: { createdAt: -1 }, skip: 5, fields: { createdAt: 1 }, reactive: true, transform: function () {} }); + +doc._id = Collection.direct.insert(doc, function () {}); + +doc.value = Collection.direct.remove({ _id: doc._id }, function () {}); +doc.value = Collection.direct.remove(doc._id, function () {}); + +doc.value = Collection.direct.update({ _id: doc._id }, { $inc: { votes: 2 } }, { multi: true, upsert: true }, function () {}); +doc.value = Collection.direct.update(doc._id, { $inc: { votes: 2 } }, { multi: true, upsert: true }, function () {}); + +let { numberAffected, insertedId }: { numberAffected?: number; insertedId?: string; } = Collection.direct.upsert(doc._id, { $inc: { votes: 2 } }, { multi: true }, function () {}); + +Collection.hookOptions = { + before: { + all: { fetchPrevious: true }, + find: { fetchPrevious: true }, + findOne: { fetchPrevious: true }, + insert: { fetchPrevious: true }, + remove: { fetchPrevious: true }, + update: { fetchPrevious: true }, + upsert: { fetchPrevious: true } + }, + after: { + all: { fetchPrevious: true }, + find: { fetchPrevious: true }, + findOne: { fetchPrevious: true }, + insert: { fetchPrevious: true }, + remove: { fetchPrevious: true }, + update: { fetchPrevious: true }, + upsert: { fetchPrevious: true } + }, + all: { + all: { fetchPrevious: true }, + find: { fetchPrevious: true }, + findOne: { fetchPrevious: true }, + insert: { fetchPrevious: true }, + remove: { fetchPrevious: true }, + update: { fetchPrevious: true }, + upsert: { fetchPrevious: true } + } +}; \ No newline at end of file diff --git a/meteor-collection-hooks/package.json b/meteor-collection-hooks/package.json new file mode 100644 index 0000000000..39dfad28b6 --- /dev/null +++ b/meteor-collection-hooks/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "meteor-typings": "^1.3.1" + } +} diff --git a/meteor-collection-hooks/tsconfig.json b/meteor-collection-hooks/tsconfig.json new file mode 100644 index 0000000000..90fa93f2b5 --- /dev/null +++ b/meteor-collection-hooks/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": false, + "strictNullChecks": false, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "meteor-collection-hooks-tests.ts" + ] +} \ No newline at end of file