@teamplay/server-aggregate
v0.3.24
Published
ShareDB middleware to allow defining aggregations only on the server
Downloads
1,834
Readme
@teamplay/server-aggregate
Racer server aggregate plugin. It allows only server-defined aggregate queries.
Install
yarn add @teamplay/server-aggregate
Setup
In the client code:
require('@teamplay/server-aggregate/client')
On the server:
const serverAggregate = require('@teamplay/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 typestring
.
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) => {
// ...
// access control or whatever
// ...
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