JSPM

resolve-readmodel-memory

0.2.2-alpha.1226074522
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 564
  • Score
    100M100P100Q8216F
  • 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 the NeDB memory database and does not have binary dependencies. API is a subset of the MongoDB query language. It provides high performance and full backward compatibility. The resolve-readmodel-memory package is useful for local development before deploying an application on a MongoDB instance.

The store interface, implemented using the resolve-readmodel-memory adapter, provides segregated read and write access to collections. Write access is used on the update side when projection functions fill collections with data. Read access is used on the read endpoint where the client side retrieves collection data (for example, in a graphql resolver).

A store has the following asynchronous methods:

  • collection - gets a collection by its name; if a collection does not exist, it is created in the write mode, and an error is thrown in the read mode.
  • getCollections - retrieves the list of actual collection names, in the read and write mode.

A collection in the read mode provides the following asynchronous methods:

  • find - selects documents in a collection, like the MongoDB collection's find method. The sort, skip, limit and projection methods support modifying selection behavior, like MongoDB's cursor methods. A query supports modifier operators.
  • findOne - returns one document that satisfies the specified query criteria in a collection, like the findOne method.
  • count - returns the number of documents that match a find() query in a collection, like the count method.

A collection in the write mode supports all read mode methods and additionally provides the following asynchronous methods:

  • insert - inserts a document or documents into a collection, like the insert method.
  • update - modifies an existing document or documents in a collection, like the update method.
  • remove - removes documents from a collection, like the remove method.
  • ensureIndex - creates indexes in collections, like the createIndex method.
  • removeIndex - drops or removes the specified index from a collection, like the dropIndex method.

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) => {
            const TestCollection = await store.collection('Test');
            await TestCollection.ensureIndex({ field: 'ID' });
            await TestCollection.insert({ id: 0, text: 'Initial' })
        },
        TestEvent: async (store, event) => {
            const TestCollection = await store.collection('Test');
            const lastId = (await TestCollection.find({}).sort({ id: -1 }).limit(1))[0].id;
            await TestCollection.insert({
                id: lastId + 1,
                text: event.text
            })
        }
    }
)

const store = await executeReadModel();
const TestCollection = await store.collection('Test');
const records = await store.find({ id: { $gt: 0 } });
console.log(records)