@startupjs/server-aggregate
Racer server aggregate plugin. It allows only server-defined aggregate queries.
Install
yarn add @startupjs/server-aggregate
Setup
In the client code:
require('@startupjs/server-aggregate/client')
On the server:
const serverAggregate = require('@startupjs/server-aggregate')
serverAggregate(backend, customCheck)
where:
backend
: your ShareDB backend instancecustomCheck (optional)
: your personal check function. It should return an error message if there is an error. IMPORTANT The message must be of type string
.
How to add aggregation
On the server side to add aggregation use backend.addAggregate(collection, queryName, cb)
, where:
collection
: collection namequeryName
: query name (alias)cb(params, shareRequest)
: async function that returns a query object or throw an error
backend.addAggregate('items', 'main', async (params, shareRequest) => {
return [
{$match: {type: 'wooden'}}
]
})
Usage
model.query('items', {
$aggregationName: 'main',
$params: {
type: 'global'
}
})
When you setup the client side as described in the Setup
section you can use the aggregateQuery(collection, queryName, params)
function which is syntactic sugar over the model.query
, where:
model.aggregateQuery('items', 'main', { type: 'global' })
MIT License
Copyright (c) 2018 by Artur Zayats