npm.io
6.0.0 • Published 1 week ago

@varasto/multi-storage

Licence
MIT
Version
6.0.0
Deps
2
Size
13 kB
Vulns
0
Weekly
0

@varasto/multi-storage

npm

Storage implementation which routes namespaces to one or more other storage instances using picomatch patterns.

Installation

$ npm install --save @varasto/multi-storage

Usage

The package provides a function called createMultiStorage which takes a configuration object that maps namespace patterns to storage instances. Patterns are matched with picomatch; the first matching pattern wins.

import { createMemoryStorage } from '@varasto/memory-storage';
import { createMultiStorage } from '@varasto/multi-storage';

const usersStorage = createMemoryStorage();
const postsStorage = createMemoryStorage();
const postsReplica = createMemoryStorage();
const fallbackStorage = createMemoryStorage();

const multiStorage = createMultiStorage({
  users: usersStorage,
  'posts-*': [postsStorage, postsReplica],
  '*': fallbackStorage,
});

// Routed to `usersStorage`.
await multiStorage.set('users', '1', { name: 'Alice' });

// Routed to both `postsStorage` and `postsReplica`.
await multiStorage.set('posts-123', '1', { title: 'Hello' });

// Routed to `fallbackStorage`.
await multiStorage.set('other', '1', { value: 5 });

When a pattern maps to an array of storages, reads are merged across them and writes/updates/deletes are applied to all of them. If no pattern matches the namespace, the operation is a no-op (and update rejects with ItemDoesNotExistError).

Keywords