# event-reduce-js

> javascript implementation of the event-reduce algorithm https://pubkey.github.io/event-reduce/

Latest version **6.0.0** (published 2026-03-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install event-reduce-js
pnpm add event-reduce-js
yarn add event-reduce-js
bun add event-reduce-js
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 6.0.0 |
| Published | 2026-03-17 |
| First published | 2020-03-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16 |
| Dependencies | 2 |
| Unpacked size | 443.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 755 |
| Author | pubkey |
| Maintainers | pubkey |
| Keywords | event-reduce, database, query, mongodb, nosql, no-sql, performance, optimization, bdd |

## Links

- npm: https://www.npmjs.com/package/event-reduce-js
- Repository: https://github.com/pubkey/event-reduce
- Homepage: https://github.com/pubkey/event-reduce#readme
- Issues: https://github.com/pubkey/event-reduce/issues
- npm.io page: https://npm.io/package/event-reduce-js

## Dependencies (2)

- [binary-decision-diagram](https://npm.io/package/binary-decision-diagram.md) 4.0.0
- [array-push-at-sort-position](https://npm.io/package/array-push-at-sort-position.md) 5.0.0

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 6.0.0 (latest) — 2026-03-17
- 5.2.7 — 2023-12-19
- 5.2.6 — 2023-12-18
- 5.2.5 — 2023-12-18
- 5.2.4 — 2023-12-18
- 5.2.3 — 2023-12-18
- 5.2.2 — 2023-12-18
- 5.2.1 — 2023-12-18
- 5.2.0 — 2023-12-18
- 5.1.0 — 2023-12-12
- 5.0.0 — 2023-11-25
- 4.0.1 — 2023-10-16
- 4.0.0 — 2023-10-09
- 3.1.2 — 2023-09-23
- 3.1.1 — 2023-09-23
- … 25 more at https://npm.io/package/event-reduce-js/versions

## README

# EventReduce JavaScript Implementation

This is the javascript version of the [EventReduce algorithm](https://github.com/pubkey/event-reduce).


## Installation

`npm run install event-reduce-js --save`

## Usage

In the following we will use EventReduce together with minimongo as an example. You can apply the code to any other database.

1. First you need some `QueryParams` that can be used by EventReduce to analyze result-event combinations.

```typescript
import {
    getSortFieldsOfQuery,
    ChangeEvent,
    calculateActionName,
    StateResolveFunctionInput,
    runAction
} from 'event-reduce-js';

// some stuff must not be coded by hand but is already in the minimongo library
import {
    compileDocumentSelector,
    compileSort
} from 'minimongo/src/selector';

// create this helper function that can be used for all queries
export function getQueryParamsByMongoQuery(query: MongoQuery): QueryParams<any> {
    const sort = query.sort ? query.sort : ['_id'];
    return {
        // primary key of the documents
        primaryKey: '_id',
        // a string[] with all fields that are used in the sorting
        sortFields: getSortFieldsOfQuery(query),
        skip: query.skip ? query.skip : undefined,
        limit: query.limit ? query.limit : undefined,
        // a function that returns true if the given document matches the query's selector
        queryMatcher: compileDocumentSelector(query.selector),
        // a function that can be used as comparator in Array.sort() (returns 1 or -1)
        sortComparator: compileSort(sort)
    };
}

const exampleQuery: MongoQuery = {
    selector: {
        age: {
            $gt: 18
        },
        gender: 'm'
    },
    limit: 10,
    sort: ['name', '_id']
};

const queryParams = getQueryParamsByMongoQuery(exampleQuery);

```


2. Now lets say you have an `changeEvent` from whatever changestream or observable your database provides. You also have the `currentResults` of the query.

```typescript

// build the input
const input: StateResolveFunctionInput<DocumentType> = {
    // the changeEvent
    changeEvent,
    // queryParams from above
    queryParams,
    // array with previous results documents
    previousResults: currentResults,
    // key->document map with previous results indexed by primary key
    // (optional) improves performance
    keyDocumentMap: currentDocMap
};

// calculate the correct action name
const action = calculateActionName(input);

if (action === 'runFullQueryAgain') {
    /**
     * when EventReduce could not optimize the input,
     * we get the actionName 'runFullQueryAgain'
     * and run the query over the database again
     */
    currentResults = await implementation.getRawResults(query);
    // also refresh the key-document map
    currentDocMap.clear();
    currentResults.forEach(doc => currentDocMap.set(doc._id, doc));
} else {
    // event-reduce could optimize the event, run the action function
    runAction(
        action,
        queryParams,
        changeEvent,
        currentResults,
        currentDocMap
    );
}

// show new results
// notice that for performance resons,
// the functions of event-reduce will mutate the input variables
console.dir(currentResults);
```

---
_Source: https://npm.io/package/event-reduce-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
