### Installing Meteor Collection Hooks Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This snippet shows the command to add the `matb33:collection-hooks` package to a Meteor project. This is the first step to enable collection hooks functionality. ```Shell meteor add matb33:collection-hooks ``` -------------------------------- ### Implementing before.update Hook in Meteor Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This hook is triggered before a document is updated. It allows modification of the `modifier` object, for example, adding a `modifiedAt` timestamp to the `$set` operator. It's crucial to modify `modifier` and not `doc`, as `doc` is a copy and changes to it won't persist. ```JavaScript test.before.update(function (userId, doc, fieldNames, modifier, options) { modifier.$set = modifier.$set || {}; modifier.$set.modifiedAt = Date.now(); }); ``` -------------------------------- ### Defining Before FindOne Hook in JavaScript Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This hook fires before a `findOne` query is executed. It allows dynamic adjustment of the `selector` and `options` used in the query, similar to `before.find`. This can be used to enforce access controls or modify query criteria before a single document is retrieved. ```JavaScript test.before.findOne(function (userId, selector, options) { // ... }); ``` -------------------------------- ### Implementing before.insert Hook in Meteor Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This hook fires before a document is inserted into the `test` collection. It allows modification of the `doc` object, such as adding a `createdAt` timestamp, before it is saved to the database. The `userId` parameter represents the ID of the user performing the operation. ```JavaScript import { Mongo } from 'meteor/mongo'; const test = new Mongo.Collection("test"); test.before.insert(function (userId, doc) { doc.createdAt = Date.now(); }); ``` -------------------------------- ### Setting Global Default Options for Collection Hooks in JavaScript Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This snippet demonstrates how to set global default options for `meteor-collection-hooks`. Options can be applied universally to all hooks and operations, or more specifically to certain hook types (e.g., `before`, `after`) or operations (e.g., `update`, `remove`). These global settings serve as the least specific defaults. ```JavaScript 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}; ``` -------------------------------- ### Defining Before Find Hook in JavaScript Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This hook fires before a `find` query is executed. It allows dynamic adjustment of the `selector` and `options` used in the query, enabling real-time modification of query parameters. It's important to note that the function used as a `before.find` hook cannot be asynchronous. ```JavaScript test.before.find(function (userId, selector, options) { // ... }); ``` -------------------------------- ### Defining After FindOne Hook in JavaScript Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This hook fires after a `findOne` query is executed. It provides access to the `userId`, `selector`, `options`, and the resulting `doc`, allowing actions to be performed on the retrieved document. This is useful for post-retrieval processing or data enrichment. ```JavaScript test.after.findOne(function (userId, selector, options, doc) { // ... }); ``` -------------------------------- ### Setting Collection-Wide Default Options for Collection Hooks in JavaScript Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This snippet illustrates how to define default options for hooks on a specific `Mongo.Collection` instance. These collection-wide options override global defaults and can be configured for all hooks/operations on that collection, or for specific hook types or operations, providing higher specificity. ```JavaScript 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}; ``` -------------------------------- ### Bypassing Hooks with Direct Access Methods in JavaScript Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This snippet demonstrates how to use the `direct` property on a collection to bypass all defined hooks for database operations. This is useful for operations that should not trigger any custom logic defined in `before` or `after` hooks, ensuring raw database interaction. ```JavaScript collection.direct.insert({_id: "test", test: 1}); collection.direct.insertAsync({_id: "test", test: 1}); collection.direct.upsert({_id: "test", test: 1}); collection.direct.upsertAsync({_id: "test", test: 1}); collection.direct.update({_id: "test"}, {$set: {test: 1}}); collection.direct.updateAsync({_id: "test"}, {$set: {test: 1}}); collection.direct.find({test: 1}); collection.direct.findOne({test: 1}); collection.direct.findOneAsync({test: 1}); collection.direct.remove({_id: "test"}); collection.direct.removeAsync({_id: "test"}); ``` -------------------------------- ### Implementing before.remove Hook in Meteor Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This hook fires just before a document is removed from the collection. It's useful for performing actions that depend on the document's existence, such as cascading deletes or maintaining system integrity, before the document is permanently deleted. The `userId` and `doc` parameters provide context about the operation. ```JavaScript test.before.remove(function (userId, doc) { // ... }); ``` -------------------------------- ### Defining After Find Hook in JavaScript Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This hook fires after a `find` query is executed. It provides access to the `userId`, `selector`, `options`, and the resulting `cursor`, allowing actions to be performed on the query's outcome. This is useful for post-query processing or logging. ```JavaScript test.after.find(function (userId, selector, options, cursor) { // ... }); ``` -------------------------------- ### Defining After Update Hook in JavaScript Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This hook fires after a document has been updated. It allows running post-update tasks, providing access to the document's state before (`this.previous`) and after the update. The `fetchPrevious` option controls whether the previous document is fetched; setting it to `false` will prevent `this.previous` from being available. ```JavaScript test.after.update(function (userId, doc, fieldNames, modifier, options) { // ... }, {fetchPrevious: true/false}); ``` -------------------------------- ### Defining After Remove Hook in JavaScript Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This hook fires after a document has been removed. It provides a copy of the document (`doc`) before its removal, enabling post-removal tasks that don't rely on the document's presence in the database, such as external service clean-up. The `this.transform()` method can be used to obtain a transformed version of the document. ```JavaScript test.after.remove(function (userId, doc) { // ... }); ``` -------------------------------- ### Implementing after.insert Hook in Meteor Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This hook fires after a document has been successfully inserted into the collection. It's suitable for post-insertion tasks like sending notifications or logging. The `this._id` property holds the `_id` of the newly inserted document, and `doc` contains the inserted document's data. ```JavaScript test.after.insert(function (userId, doc) { // ... }); ``` -------------------------------- ### Implementing before.upsert Hook in Meteor Source: https://github.com/meteor-community-packages/meteor-collection-hooks/blob/master/README.md This hook is triggered before an upsert operation. Similar to `before.update`, it allows modification of the `modifier` object. It always fires for an upsert, but the subsequent `after` hook will be either `after.insert` or `after.update` depending on whether a new document was inserted or an existing one updated. ```JavaScript test.before.upsert(function (userId, selector, modifier, options) { modifier.$set = modifier.$set || {}; modifier.$set.modifiedAt = Date.now(); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.