Varasto 📦

Varasto is minimalistic namespaced key-value store written in TypeScript that can store JSON objects identified by namespace and key. Objects can be persisted on disk, database or remote server.

Installation

Choose storage backend that best suits your needs:

Package Description
@varasto/cache-storage Acts as an cache for another storage.
@varasto/fs-storage Persists data to hard disk.
@varasto/memory-storage Data is stored in memory. No persistence.
@varasto/multi-storage Data is stored to multiple storages.
@varasto/postgres-storage Data is stored to PostgreSQL database.
@varasto/remote-storage Data is stored to remote server.
@varasto/redis-storage Data is stored to Redis.
@varasto/single-file-storage Data is stored to single file.
@varasto/sqlite-storage Data is stored to SQLite database.
@varasto/validator-storage Acts as an validator for another storage.
@varasto/web-storage Data is stored to browser storage.

Varasto also has an HTTP interface and useful utilities for querying data and doing bulk operations and even an simple ORM.

Usage

Storage class provided by an backend has following methods:

Storing items

set<T extends JsonObject>(
  namespace: string,
  key: string,
  value: T
): Promise<void>

Attempts to store an item identified by namespace and key. Returned promise will fail if an I/O error occurs while storing the item.

Retrieving items

get<T extends JsonObject>(
  namespace: string,
  key: string
): Promise<T | undefined>

Attempts to retrieve an item identified by namespace and key. Returned promise will either resolve into the value, or undefined if item with the given identifier does not exist. The promise will fail if an I/O error occurs while retrieving the item.

Removing items

delete(
  namespace: string,
  key: string
): Promise<boolean>

Attempts to remove an item identified by namespace and key. Returned promise will resolve into a boolean value which tells whether an value with the given identifier existed or not. The promise will fail if an I/O error occurs while removing the item.

Updating already existing item

update<T extends JsonObject>(
  namespace: string,
  key: string,
  value: Partial<T>
): Promise<T>

Attempts to update an already existing item identified by namespace and key by shallowly merging with the given new data. Returned promise will resolve into the new value, or will fail if no such item exists.

Testing whether an item exists or not

has(
  namespace: string,
  key: string
): Promise<boolean>

Returns true if an item identified by namespace and key exists in the storage, or false if it doesn't. The promise will fail if an I/O error occurs while testing whether item exists or not.

Listing keys stored in a namespace

keys(
  namespace: string
): AsyncGenerator<string>

Returns keys of all items stored under an namespace. The promise will fail if an I/O error occurs while listing the keys.

Listing values stored in a namespace

values<T extends JsonObject>(
  namespace: string
): AsyncGenerator<T>

Returns all items stored under an namespace. The promise will fail if an I/O error occurs.

Listing entries stored in a namespace

entries<T extends JsonObject>(
  namespace: string
): AsyncGenerator<[string, T]>

Returns all items stored under an namespace, with the keys they are identified by. The promise will fail if an I/O error occurs.

Searching for entries stored in a namespace

find<T extends JsonObject>(
  namespace: string,
  callback: (value: T, key: string) => boolean
): Promise<[string, T] | undefined>

Returns the first entry from specified namespace to which given callback function returns true for, or undefined if the callback function does not return true for any entry in the namespace.

The promise will fail if an I/O error occurs.

Filtering entries stored in a namespace

filter<T extends JsonObject>(
  namespace: string,
  callback: (value: T, key: string) => boolean
): AsyncGenerator<[string, T]>

Goes through all entries from the given namespace, returning ones for which the given callback function returns true for.

The promise will fail if an I/O error occurs.

Map operation

map<T extends JsonObject, U extends JsonObject>(
  namespace: string,
  callback: (value: T, key: string) => U
): AsyncGenerator<[string, U]>

Goes through all entries from given namespace, passing them to the given callback function and returning whatever the callback function returned.

The promise will fail if an I/O error occurs.