fogine/couchbase-odm

View on GitHub
tutorials/5.hooks.md

Summary

Maintainability
Test Coverage
See {@link Hook} API for available hook types

### Defining hooks as part of Model definition

````javascript
    const User = couchbase.define('User', {
        type: 'object'
    }, {
        hooks: {
            beforeCreate: function(user) {
                //do something
            }
        }
    })
````

#### Or on Model object

````javascript
    User.beforeCreate(function(user) {
        //do something
    });
````

### Register global hook

#### As option

````javascript
    const couchbase = new couchbaseODM({
        bucket: bucket,
        hooks: {
            beforeCreate: function(instance) {
                //do something
            }
        }
    });
````

#### On CouchbaseODM instance
````javascript
    couchbase.beforeCreate(function(instance) {
        //do something
    }, 'optional-hook-name');
````

### Remove global hook

````javascript
    //it's possible to delete named hooks only
    couchbase.removeHook('beforeCreate','hook-name');
````