# @startupjs/server-aggregate

> Module for describing aggregations on the server

Latest version **0.55.8** (published 2023-12-16) · 0 weekly downloads

## Install

```sh
npm install @startupjs/server-aggregate
pnpm add @startupjs/server-aggregate
yarn add @startupjs/server-aggregate
bun add @startupjs/server-aggregate
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.55.8 |
| Published | 2023-12-16 |
| First published | 2020-11-30 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 4.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | cray0000, yska, zag2art, fctsvirus, maestro.sc, byshock |

## Links

- npm: https://www.npmjs.com/package/@startupjs/server-aggregate
- npm.io page: https://npm.io/package/@startupjs/server-aggregate

## Dependencies (1)

- [racer](https://npm.io/package/racer.md) 1.0.1

## Recent versions

- 0.55.8 (latest) — 2023-12-16
- 0.57.0-canary.0 (canary) — 2024-05-25
- 0.56.0-alpha.53 (next) — 2024-03-15
- 0.56.0-alpha.0 — 2024-01-15
- 0.55.7 — 2023-12-16
- 0.55.0 — 2023-11-29
- 0.55.0-alpha.12 — 2023-11-17
- 0.55.0-alpha.1 — 2023-11-08
- 0.54.1 — 2023-11-01
- 0.54.0 — 2023-10-25
- 0.53.0 — 2023-08-17
- 0.52.1 — 2023-05-31
- 0.52.0 — 2023-05-26
- 0.51.0 — 2023-05-24
- 0.50.13 — 2023-02-27
- … 43 more at https://npm.io/package/@startupjs/server-aggregate/versions

## README

# @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:

```js
require('@startupjs/server-aggregate/client')
```

On the server:

```js
const serverAggregate = require('@startupjs/server-aggregate')
serverAggregate(backend, customCheck)
```

where:
* `backend`: your ShareDB backend instance
* `customCheck (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 name
* `queryName`: query name (alias)
* `cb(params, shareRequest)`: async function that returns a query object or throw an error

```js
backend.addAggregate('items', 'main', async (params, shareRequest) => {
  // ...
  // access control or whatever
  // ...

  return [
    {$match: {type: 'wooden'}}
  ]
})
```

## Usage

```js
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:

```js
model.aggregateQuery('items', 'main', { type: 'global' })
```

## MIT License

Copyright (c) 2018 by Artur Zayats

---
_Source: https://npm.io/package/@startupjs/server-aggregate · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
