All Projects → Meteor-Community-Packages → Meteor Collection Hooks

Meteor-Community-Packages / Meteor Collection Hooks

Licence: mit
Meteor Collection Hooks

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Meteor Collection Hooks

Meteor
Meteor, the JavaScript App Platform
Stars: ✭ 42,739 (+6567.55%)
Mutual labels:  hacktoberfest, mongodb, meteor
Meteor Collection2
A Meteor package that extends Mongo.Collection to provide support for specifying a schema and then validating against that schema when inserting and updating.
Stars: ✭ 1,020 (+59.13%)
Mutual labels:  hacktoberfest, mongodb, meteor
Meteor Publish Composite
Meteor.publishComposite provides a flexible way to publish a set of related documents from various collections using a reactive join
Stars: ✭ 546 (-14.82%)
Mutual labels:  hacktoberfest, mongodb, meteor
Meteor Partitioner
Transparently divide a single meteor app into several different instances shared between different groups of users.
Stars: ✭ 153 (-76.13%)
Mutual labels:  hacktoberfest, mongodb, meteor
React Fetch Hook
React hook for conveniently use Fetch API
Stars: ✭ 285 (-55.54%)
Mutual labels:  hacktoberfest, hooks
Drmongo
MongoDB admin app built on MeteorJs.
Stars: ✭ 283 (-55.85%)
Mutual labels:  mongodb, meteor
Yii2 Mongodb
Yii 2 MongoDB extension
Stars: ✭ 299 (-53.35%)
Mutual labels:  hacktoberfest, mongodb
Meteor Autocomplete
Client/server autocompletion designed for Meteor's collections and reactivity.
Stars: ✭ 352 (-45.09%)
Mutual labels:  hacktoberfest, meteor
Social Platform Donut Frontend
This is an Open Source social Platform where people can interact with Open Source expertise around the globe and work on different projects
Stars: ✭ 195 (-69.58%)
Mutual labels:  hacktoberfest, mongodb
Nosqlclient
Cross-platform and self hosted, easy to use, intuitive mongodb management tool - Formerly Mongoclient
Stars: ✭ 3,399 (+430.27%)
Mutual labels:  mongodb, meteor
Mongo Seeding
The ultimate solution for populating your MongoDB database.
Stars: ✭ 375 (-41.5%)
Mutual labels:  hacktoberfest, mongodb
meteor-method-hooks
atmospherejs.com/seba/method-hooks
Stars: ✭ 24 (-96.26%)
Mutual labels:  hooks, meteor
Bookmarks.dev
Bookmarks and Code Snippets Manager for Developers & Co
Stars: ✭ 218 (-65.99%)
Mutual labels:  hacktoberfest, mongodb
Mongo Sync
Sync Remote and Local MongoDB Databases 🔥
Stars: ✭ 293 (-54.29%)
Mutual labels:  hacktoberfest, mongodb
Chartbrew
Open-source web platform for creating charts out of different data sources (databases and APIs) 📈📊
Stars: ✭ 199 (-68.95%)
Mutual labels:  hacktoberfest, mongodb
Hexagon
Hexagon is a microservices toolkit written in Kotlin. Its purpose is to ease the building of services (Web applications, APIs or queue consumers) that run inside a cloud platform.
Stars: ✭ 336 (-47.58%)
Mutual labels:  hacktoberfest, mongodb
Mongo Express
Web-based MongoDB admin interface, written with Node.js and express
Stars: ✭ 4,403 (+586.9%)
Mutual labels:  hacktoberfest, mongodb
Django Dbbackup
Management commands to help backup and restore your project database and media files
Stars: ✭ 471 (-26.52%)
Mutual labels:  hacktoberfest, mongodb
Pup
The Ultimate Boilerplate for Products.
Stars: ✭ 563 (-12.17%)
Mutual labels:  mongodb, meteor
Nodebb
Node.js based forum software built for the modern web
Stars: ✭ 12,303 (+1819.34%)
Mutual labels:  hacktoberfest, mongodb

Meteor Collection Hooks Build Status

Extends Mongo.Collection with before/after hooks for insert, update, remove, find, and findOne.

Works across client, server or a mix. Also works when a client initiates a collection method and the server runs the hook, all while respecting the collection validators (allow/deny).

Please refer to History.md for a summary of recent changes.

Getting Started

Installation:

meteor add matb33:collection-hooks

.before.insert(userId, doc)

Fired before the doc is inserted.

Allows you to modify doc as needed, or run additional functionality

  • this.transform() obtains transformed version of document, if a transform was defined.
import { Mongo } from 'meteor/mongo';
const test = new Mongo.Collection("test");

test.before.insert(function (userId, doc) {
  doc.createdAt = Date.now();
});

.before.update(userId, doc, fieldNames, modifier, options)

Fired before the doc is updated.

Allows you to to change the modifier as needed, or run additional functionality.

  • this.transform() obtains transformed version of document, if a transform was defined.
test.before.update(function (userId, doc, fieldNames, modifier, options) {
  modifier.$set = modifier.$set || {};
  modifier.$set.modifiedAt = Date.now();
});

Important: Note that we are changing modifier, and not doc. Changing doc won't have any effect as the document is a copy and is not what ultimately gets sent down to the underlying update method.


.before.remove(userId, doc)

Fired just before the doc is removed.

Allows you to to affect your system while the document is still in existence -- useful for maintaining system integrity, such as cascading deletes.

  • this.transform() obtains transformed version of document, if a transform was defined.
test.before.remove(function (userId, doc) {
  // ...
});

.before.upsert(userId, selector, modifier, options)

Fired before the doc is upserted.

Allows you to to change the modifier as needed, or run additional functionality.

test.before.upsert(function (userId, selector, modifier, options) {
  modifier.$set = modifier.$set || {};
  modifier.$set.modifiedAt = Date.now();
});

Note that calling upsert will always fire .before.upsert hooks, but will call either .after.insert or .after.update hooks depending on the outcome of the upsert operation. There is no such thing as a .after.upsert hook at this time.


.after.insert(userId, doc)

Fired after the doc was inserted.

Allows you to run post-insert tasks, such as sending notifications of new document insertions.

  • this.transform() obtains transformed version of document, if a transform was defined;
  • this._id holds the newly inserted _id if available.
test.after.insert(function (userId, doc) {
  // ...
});

.after.update(userId, doc, fieldNames, modifier, options)

Fired after the doc was updated.

Allows you to to run post-update tasks, potentially comparing the previous and new documents to take further action.

  • this.previous contains the document before it was updated.
    • The optional fetchPrevious option, when set to false, will not fetch documents before running the hooks. this.previous will then not be available. The default behavior is to fetch the documents.
  • this.transform() obtains transformed version of document, if a transform was defined. Note that this function accepts an optional parameter to specify the document to transform — useful to transform previous: this.transform(this.previous).
test.after.update(function (userId, doc, fieldNames, modifier, options) {
  // ...
}, {fetchPrevious: true/false});

Important: If you have multiple hooks defined, and at least one of them does not specify fetchPrevious: false, then the documents will be fetched and provided as this.previous to all hook callbacks. All after-update hooks for the same collection must have fetchPrevious: false set in order to effectively disable the pre-fetching of documents.

It is instead recommended to use the collection-wide options (e.g. MyCollection.hookOptions.after.update = {fetchPrevious: false};).


.after.remove(userId, doc)

Fired after the doc was removed.

doc contains a copy of the document before it was removed.

Allows you to to run post-removal tasks that don't necessarily depend on the document being found in the database (external service clean-up for instance).

  • this.transform() obtains transformed version of document, if a transform was defined.
test.after.remove(function (userId, doc) {
  // ...
});

.before.find(userId, selector, options)

Fired before a find query.

Allows you to to adjust selector/options on-the-fly.

test.before.find(function (userId, selector, options) {
  // ...
});

.after.find(userId, selector, options, cursor)

Fired after a find query.

Allows you to to act on a given find query. The cursor resulting from the query is provided as the last argument for convenience.

test.after.find(function (userId, selector, options, cursor) {
  // ...
});

.before.findOne(userId, selector, options)

Fired before a findOne query.

Allows you to to adjust selector/options on-the-fly.

test.before.findOne(function (userId, selector, options) {
  // ...
});

.after.findOne(userId, selector, options, doc)

Fired after a findOne query.

Allows you to to act on a given findOne query. The document resulting from the query is provided as the last argument for convenience.

test.after.findOne(function (userId, selector, options, doc) {
  // ...
});

Direct access (circumventing hooks)

All compatible methods have a direct version that circumvent any defined hooks. For example:

collection.direct.insert({_id: "test", test: 1});
collection.direct.update({_id: "test"}, {$set: {test: 1}});
collection.direct.find({test: 1});
collection.direct.findOne({test: 1});
collection.direct.remove({_id: "test"});

Default options

As of version 0.7.0, options can be passed to hook definitions. Default options can be specified globally and on a per-collection basis for all or some hooks, with more specific ones having higher specificity.

Examples (in order of least specific to most specific):

import { CollectionHooks } from 'meteor/matb33:collection-hooks';

CollectionHooks.defaults.all.all = {exampleOption: 1};

CollectionHooks.defaults.before.all = {exampleOption: 2};
CollectionHooks.defaults.after.all = {exampleOption: 3};

CollectionHooks.defaults.all.update = {exampleOption: 4};
CollectionHooks.defaults.all.remove = {exampleOption: 5};

CollectionHooks.defaults.before.insert = {exampleOption: 6};
CollectionHooks.defaults.after.remove = {exampleOption: 7};

Similarly, collection-wide options can be defined (these have a higher specificity than the global defaults from above):

import { Mongo } from 'meteor/mongo';
const testCollection = new Mongo.Collection("test");

testCollection.hookOptions.all.all = {exampleOption: 1};

testCollection.hookOptions.before.all = {exampleOption: 2};
testCollection.hookOptions.after.all = {exampleOption: 3};

testCollection.hookOptions.all.update = {exampleOption: 4};
testCollection.hookOptions.all.remove = {exampleOption: 5};

testCollection.hookOptions.before.insert = {exampleOption: 6};
testCollection.hookOptions.after.remove = {exampleOption: 7};

Currently (as of 0.7.0), only fetchPrevious is implemented as an option, and is only relevant to after-update hooks.


Additional notes

  • Returning false in any before hook will prevent the underlying method (and subsequent after hooks) from executing. Note that all before hooks will still continue to run even if the first hook returns false.

  • If you wish to make userId available to a find query in a publish function, try the technique detailed in this comment userId is available to find and findOne queries that were invoked within a publish function.

  • All hook callbacks have this._super available to them (the underlying method) as well as this.context, the equivalent of this to the underlying method. Additionally, this.args contain the original arguments passed to the method and can be modified by reference (for example, modifying a selector in a before hook so that the underlying method uses this new selector).

  • It is quite normal for userId to sometimes be unavailable to hook callbacks in some circumstances. For example, if an update is fired from the server with no user context, the server certainly won't be able to provide any particular userId.

  • You can define a defaultUserId in case you want to pass an userId to the hooks but there is no context. For instance if you are executing and API endpoint where the userId is derived from a token. Just assign the userId to CollectionHooks.defaultUserId. It will be overriden by the userId of the context if it exists.

  • If, like me, you transform Meteor.users through a round-about way involving find and findOne, then you won't be able to use this.transform(). Instead, grab the transformed user with findOne.

  • When adding a hook, a handler object is returned with these methods:

    • remove(): will remove that particular hook;
    • replace(callback, options): will replace the hook callback and options.
  • If your hook is defined in common code (both server and client), it will run twice: once on the server and once on the client. If your intention is for the hook to run only once, make sure the hook is defined somewhere where only either the client or the server reads it. When in doubt, define your hooks on the server.

  • Both update and remove internally make use of find, so be aware that find/findOne hooks can fire for those methods.

  • find hooks are also fired when fetching documents for update, upsert and remove hooks.

  • If using the direct version to bypass a hook, any mongo operations done within nested callbacks of the direct operation will also by default run as direct. You can use the following line in a nested callback before the operation to unset the direct setting: CollectionHooks.directEnv = new Meteor.EnvironmentVariable(false)


Maintainers

Maintained by Meteor Community Packages and in particular by:

Contributors

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].