Create REST APIs using Restify and Sequelize.
Example:
import query from 'restify-sequelize-query';
import cache from 'restify-sequelize-query/lib/RedisCache';
cache.configure({
'StuffModel': 'redis://localhost:7777',
'OtherStuff': 'redis://localhosT:7778',
'MoreStuff': 'redis://localhost:7779',
'raw': 'redis://localhost:77780' // exec/multiexec
});
server.get(
{ path: '/stuff/:id' },
query({
method: 'select',
model: StuffModel,
attributes: [ 'id', 'col1', 'col2' ],
order: [ 'id' ],
where: (params) => ({ id: params.id }),
limit: 1,
format: (results) => results.map(result => ({ column1: result.col1, column2: result.col2 })),
cache: cache
})
);
A Restify middleware function.
Specifies Sequelize query options. Options are passed directly to the Sequelize method selected by options.method
.
select
: Execute onefindAll
queryupdate
: Executeupdate
calls based onreq.body
contentsupsert
: Executeupsert
calls based onreq.body
contentsdelete
: Executedelete
calls based onreq.body
contentsexec
: Execute one raw SQL querymultiexec
: Execute raw SQL queries based onreq.body
contents
Specifies the Sequelize model to query.
A function with one parameter that returns either an object literal or a Promise
that resolves to an object literal describing the where clause. The parameter is either req.params
or req.body[idx]
depending on which query method is used. This option is only supported for select
, update
, and delete
query methods.
Use this to create custom where clauses based on either the request parameters or individual request body items.
A function with req.body
as it's parameter and returns either a Promise
or undefined. req.body
is forced to be an Array
if it isn't already. The parse function should iterate and alter each body item if necessary.
Use this to alter the request body if necessary.
A function that receives an array of query results and must return either an array of formatted results or a Promise
that resolves to the same.
Use this to format data results.
A SQL string used to specify the raw SQL to execute for exec
and multiexec
methods.
Set this to the sequelize
instance used to execute raw SQL queries when using the exec
and multiexec
query methods.
For exec
queries:
This can be set to a function accepting req
as it's parameter and returning a value or Promise
that describes the parameter replacements to be made in the raw SQL.
For multiexec
queries:
This can be set to a function accepting req.body[idx]
as it's parameter and returning a value or Promise
that describes the parameter replacements to be made in the raw SQL.
For exec
queries:
This can be set to a function accepting req
as it's parameter and returning a value or Promise
that describes the parameter binds to be made in the raw SQL.
For multiexec
queries:
This can be set to a function accepting req.body[idx]
as it's parameter and returning a value or Promise
that describes the parameter binds to be made in the raw SQL.
This can be set to a cache class that should have the following methods:
- set(shard, key, value): Return a
Promise
that sets a cache value and resolves to that same value. - get(shard, key): Return a
Promise
that resolves either to the cache value specified by the key orundefined
. - clear(shard): Return a
Promise
that removes all keys associated with the shard.
Two sample cache classes are included: MemoryCache
and RedisCache
.
This is an Array
of either model names or model references used to invalidate cache entries.