JSPM

resolve-readmodel-memory

0.4.1-0124080800.beta
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 564
  • Score
    100M100P100Q8212F
  • License MIT

This package serves as a resolve-query adapter for storing a read model in memory using NeDB

Package Exports

  • resolve-readmodel-memory

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (resolve-readmodel-memory) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

resolve-readmodel-memory npm version

This package is a resolve-query adapter for storing a read model in memory. The adapter is based on key/value ES6 Map interface and does not have binary dependencies.

The store interface is implemented using the resolve-readmodel-memory adapter and provides segregated read and write access:

  • Read access is used to retrieve data from a store (for example, by a graphql resolver). The package provides the hget(key, field) asynchronous method to get the value associated with the field from the hash stored at the key.
  • Projection functions use Write access to update data. The following asynchronous methods are available:
    • hset(key, field, value) - sets the value associated with the field in the hash stored at key;
    • del(key) - deletes the hash stored at the key. Does nothing if the key does not exist.

Usage

import createMemoryAdapter from 'resolve-readmodel-memory';
import { createReadModel } from 'resolve-query';

const adapter = createMemoryAdapter();

const testEventStore = {
    subscribeByEventType: (_, handler) => Promise.resolve(
        handler({ type: 'TestEvent', text: 'One' }),
        handler({ type: 'TestEvent', text: 'Two' })
    )
};

const executeReadModel = createReadModel(
    eventStore: testEventStore,
    adapter,
    projection: {
        Init: async (store) => {
            await store.hset('Test', 'myField', { changeCount: 0, text: 'Initial' })
        },
        TestEvent: async (store, event) => {
            const value = await store.hget('Test', 'myField');

            const newValue = { 
                changeCount: value.changeCount + 1,
                text: event.text
            };
            await store.hset('Test', 'myField', newValue);
        }
    }
)

const store = await executeReadModel();
const value = store.hget('Test', 'myField');
console.log(value)