Connector hooks are triggered by actions of connectors.
Page Contents

Overview

Connectors are responsible for interacting with the backend systems on behalf of model methods. Connector hooks enable applications to intercept the connector execution.

Hooks

before execute

The ‘before execute’ hook is invoked before a connector sends a request to the backend.

var connector = MyModel.getDataSource().connector;
connector.observe('before execute', function(ctx, next) {
  // ...
  next();
});

To terminate the invocation, call ctx.end(err, result), for example:

var connector = MyModel.getDataSource().connector;
connector.observe('before execute', function(ctx, next) {
  // ...
  ctx.end(null, cachedResponse);
});

after execute

The ‘after execute’ hook is invoked after the connector receives a response from the backend.

connector.observe('after execute', function(ctx, next) {
  // ...
  next();
});

Context

The context object contains information for the hooks to act on. It varies based on the type of connectors. 

SQL based connectors (MySQL, PostgreSQL, SQL Server, Oracle)

before: {req: {sql: 'SELECT ...', params: [1, 2]}, end: ...}
after: {req: {sql: 'SELECT ...', params: [1, 2]}, res: ..., end: ...}

MongoDB connector

before: {req: {command: ..., params: ...}, end: ...}
after: {req: {...}, res: {...}, end: ...}

req.command is the command for the mongodb collection.

req.params is the parameters passing to the mongodb driver.

res is the object received from the mongodb driver.

REST connector

before: {req: {...}, end: ...}
after: {req: {...}, res: {...}, end: ...}

req is the object passing to request module.

res is the object received from request module.

SOAP connector

before: {req: {...}, end: ...}
after: {req: {...}, res: {...}, end: ...}

req is the object passing to request module.

res is the object received from request module.