Need private packages and team management tools?Check out npm Teams »

@wireapp/store-engine

4.5.5 • Public • Published

Wire

This repository is part of the source code of Wire. You can find more information at wire.com or by contacting opensource@wire.com.

You can find the published source code at github.com/wireapp.

For licensing information, see the attached LICENSE file and the list of third-party licenses at wire.com/legal/licenses/.

Store Engine

The Store Engine is an interface which can be used to implement various storage technologies in a uniform manner. There is a MemoryEngine which serves as an example. Additional implementations can be found in separate packages.

Popular implementations

Store Engine Description
store-engine Implementation for in-memory.
store-engine-bro-fs Implementation for the browser's File and Directory Entries API.
store-engine-dexie Implementation for the browser's IndexedDB.
store-engine-fs Implementation for Node.js' File System.
store-engine-sqleet Implementation for SQLite 3 (WebAssembly) with encryption.
store-engine-web-storage Implementation for the browser's Web Storage API.

Motivation

Nowadays there are more and more storage possibilities and developers must be familiar with the characteristics of each individual solution to reliably store data. Because it can be sometimes hard to keep up with the highly dynamic world of data storages, we have developed a system which unifies the use of different storages / databases.

Quickstart

Engine instantiation

const {MemoryEngine} = require('@wireapp/store-engine');
const engine = new MemoryEngine();
await engine.init('my-database-name');

API

No matter which engine you use, they all support common CRUD operations.

Example

The following API calls we use this data:

const TABLE_NAME = 'the-simpsons';
const PRIMARY_KEY = 'lisa-simpson';
const ENTITY = {name: 'Lisa Simpson'};

create

const primaryKey = await engine.create(TABLE_NAME, PRIMARY_KEY, ENTITY);
console.log(`Saved record with primary key "${primaryKey}".`);

delete

const primaryKey = await engine.delete(TABLE_NAME, PRIMARY_KEY);
console.log(`Deleted record with primary key "${primaryKey}".`);

deleteAll

const wasDeleted = await engine.deleteAll(TABLE_NAME);
if (wasDeleted) {
  console.log('The Simpsons have been deleted. Poor Simpsons!');
}

purge

await engine.purge();
console.log('The Simpson Universe has been deleted. Doh!');

read

const record = await engine.read(TABLE_NAME, PRIMARY_KEY);
console.log(`Her name is "${record.name}".`);

readAll

const records = await engine.readAll(TABLE_NAME);
console.log(`There are "${records.length}" Simpsons in our database.`);

readAllPrimaryKeys

const primaryKeys = await engine.readAllPrimaryKeys(TABLE_NAME);
console.log(`Identifiers of our Simpsons: "${primaryKeys.join('')}"`);

update

const primaryKey = await engine.update(TABLE_NAME, PRIMARY_KEY, {brother: 'Bart Simpson'});
const updatedRecord = await engine.read(TABLE_NAME, PRIMARY_KEY);
console.log(`The brother of "${updatedRecord.name}" is "${updatedRecord.brother}".`);

Transient store

The Store Engine interface also provides a transient store which deletes data after a specified TTL.

Example

const {Store} = require('@wireapp/store-engine');
const {WebStorageEngine} = require('@wireapp/store-engine-web-storage');
 
const engine = new WebStorageEngine();
const store = new Store.TransientStore(engine);
 
(async () => {
  const ttl = 1000;
 
  await engine.init('my-database-name');
  await store.init('the-simpsons');
  const transientBundle = await store.set('bart', {name: 'Bart Simpson'}, ttl);
  console.log(`The record of "${transientBundle.payload.name}" will expire in "${transientBundle.expires}"ms.`);
})();

Install

npm i @wireapp/store-engine

DownloadsWeekly Downloads

888

Version

4.5.5

License

GPL-3.0

Unpacked Size

337 kB

Total Files

90

Last publish

Collaborators

  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar