All Projects → azu → kvs

azu / kvs

Licence: MIT license
Lightweight key-value storage library for Browser, Node.js, and In-Memory.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to kvs

indexeddb-orm
Indexed DB ORM
Stars: ✭ 53 (-57.94%)
Mutual labels:  indexeddb, webworker
total
Ruby Gem to get total memory size in the system
Stars: ✭ 15 (-88.1%)
Mutual labels:  memory
mindav
A self-hosted file backup server which bridges WebDAV protocol with @minio written in @totoval. Webdav ❤️ Minio
Stars: ✭ 64 (-49.21%)
Mutual labels:  memory
RazorSharp
Low-level utilities and tools for working with the CLR and memory.
Stars: ✭ 31 (-75.4%)
Mutual labels:  memory
facilejdbc
FacileJDBC - Fast, simple and lightweight JDBC wrapper
Stars: ✭ 34 (-73.02%)
Mutual labels:  db
AloeDB
Light, Embeddable, NoSQL database for Deno 🦕
Stars: ✭ 111 (-11.9%)
Mutual labels:  db
mongoose-api-generator
Autogenerate a REST API from your Mongoose models
Stars: ✭ 19 (-84.92%)
Mutual labels:  db
CSJsonDB
A C# package that performs basic CRUD ( Create, Read, Update, Delete ) operations on a Json file, used for sample minimalistic DBs.
Stars: ✭ 75 (-40.48%)
Mutual labels:  db
derivejs
DeriveJS is a reactive ODM - Object Document Mapper - framework, a "wrapper" around a database, that removes all the hassle of data-persistence by handling it transparently in the background, in a DRY manner.
Stars: ✭ 54 (-57.14%)
Mutual labels:  db
hardware
Get CPU, Memory and Network informations of the running OS and its processes
Stars: ✭ 70 (-44.44%)
Mutual labels:  memory
memtester
Simple memory tester mirror from http://pyropus.ca/software/memtester/. Please note that I am not the author of Memtester
Stars: ✭ 84 (-33.33%)
Mutual labels:  memory
zap-db
An easy to use JSON database written with ease of setup and memory management of slack bots in mind.
Stars: ✭ 103 (-18.25%)
Mutual labels:  db
nodejs
Node.js in-process collectors for Instana
Stars: ✭ 66 (-47.62%)
Mutual labels:  memory
dualnback
In n-back task you need to remember n previous spatial or auditory stimuli. N-back is a memory test where n refers on how many previous stimuli must be remembered. Dual means that verbal auditory stimulus and spatial visual stimulus are presented at the same time and must be remembered separately.
Stars: ✭ 22 (-82.54%)
Mutual labels:  memory
moneta
Moneta is a live usermode memory analysis tool for Windows with the capability to detect malware IOCs
Stars: ✭ 384 (+204.76%)
Mutual labels:  memory
sqlt
SqlT golang实现的类似MyBaits的Sql 工具
Stars: ✭ 28 (-77.78%)
Mutual labels:  db
hasor
Hasor是一套基于 Java 语言的开发框架,区别于其它框架的是 Hasor 有着自己一套完整的体系,同时还可以和先有技术体系做到完美融合。它包含:IoC/Aop容器框架、Web框架、Jdbc框架、RSF分布式RPC框架、DataQL引擎,等几块。
Stars: ✭ 938 (+644.44%)
Mutual labels:  db
simpledb
No description or website provided.
Stars: ✭ 50 (-60.32%)
Mutual labels:  db
Android-ORM-Benchmarks
No description or website provided.
Stars: ✭ 25 (-80.16%)
Mutual labels:  db
human-memory
Course materials for Dartmouth course: Human Memory (PSYC 51.09)
Stars: ✭ 239 (+89.68%)
Mutual labels:  memory

KVS Actions Status: test

Key Value storage for Browser, Node.js, and In-Memory.

It is a monorepo for key-value storage.

Motivation

I want to get universal storage library that works on Browser and Node.js.

Previously, I've created localstorage-ponyfill for this purpose. However, Window.localStorage does not work on Web Workers or Service Worker

@kvs/* packages provide async storage API using IndexedDB etc and resolve this issue.

Common Features

KVS libraries provide following common features.

  • Key-Value Storage
  • Async Read, and Write API
    • provide get, set, has, delete, and clear API
  • Migration API
    • Upgrade storage data via version and upgrade method
  • Tiny packages
    • Almost package size is 1kb(gzip)
  • TypeScript
    • All packages are written by TypeScript

Support Browsers

  • A browser that support AsyncIterator
    • It requires ES2018+ supports
  • Chromium-based(Google Chrome and MSEdge), Firefox, and macOS Safari

Packages

If you want to custom implementation, please see @kvs/storage and test it with @kvs/common-test-case.

Usage

@kvs/env support Browser and Node.js. In fact, browser use @kvs/indexeddb and Node.js use @kvs/node-localstorage.

import { KVSIndexedDB, kvsIndexedDB } from "@kvs/env";
(async () => {
    const storage = await kvsEnvStorage({
        name: "database-name",
        version: 1
    });
    await storage.set("a1", "string"); 
    const a1 = await storage.get("a1");
    console.log(a1); // => "string"
})();

API

@kvs/types define common interface.

Each constructor function like kvsEnvStorage return KVS object that has following methods. Also, KVS object define Symbol.asyncIterator, and you can iterate the storage by for await...of.

export type KVS<Schema extends StorageSchema> = {
    /**
     * Returns the value associated to the key.
     * If the key does not exist, returns `undefined`.
     */
    get<K extends StoreNames<Schema>>(key: K): Promise<StoreValue<Schema, K> | undefined>;
    /**
     * Sets the value for the key in the storage. Returns the storage.
     */
    set<K extends StoreNames<Schema>>(key: K, value: StoreValue<Schema, K> | undefined): Promise<KVS<Schema>>;
    /**
     * Returns a boolean asserting whether a value has been associated to the key in the storage.
     */
    has(key: StoreNames<Schema>): Promise<boolean>;
    /**
     * Returns true if an key in the storage existed and has been removed.
     * Returns false if the key does not exist.
     */
    delete(key: StoreNames<Schema>): Promise<boolean>;
    /**
     * Removes all key-value pairs from the storage.
     * Note: clear method does not delete the storage.
     * In other words, after clear(), the storage still has internal metadata like version.
     */
    clear(): Promise<void>;
    /**
     * Drop the storage.
     * It delete all data that includes metadata completely.
     */
    dropInstance(): Promise<void>;
    /*
     * Close the KVS connection
     * DB-like KVS close the connection via this method
     * Of course, localStorage-like KVS implement do nothing. It is just noop function
     */
    close(): Promise<void>;
} & AsyncIterable<[StoreNames<Schema>, StoreValue<Schema, StoreNames<Schema>>]>;

Basic Usage

import assert from "assert";
import { kvsEnvStorage } from "@kvs/env";
(async () => {
    type StorageSchema = {
        a1: string;
        b2: number;
        c3: boolean;
    };
    // open database and initialize it
    const storage = await kvsEnvStorage<StorageSchema>({
        name: "database-name",
        version: 1
    });
    // set
    await storage.set("a1", "string"); // type check
    await storage.set("b2", 42);
    await storage.set("c3", false);
    // has
    console.log(await storage.has("a1")); // => true
    // get
    const a1 = await storage.get("a1"); // a1 will be string type
    const b2 = await storage.get("b2");
    const c3 = await storage.get("c3");
    assert.strictEqual(a1, "string");
    assert.strictEqual(b2, 42);
    assert.strictEqual(c3, false);
    // iterate
    for await (const [key, value] of storage) {
        console.log([key, value]);
    }
    // delete
    await storage.delete("a1");
    // clear all data
    await storage.clear();
})();

Migration

KVS support migration feature. You can define upgrade and use it as migration function.

import { kvsEnvStorage } from "@kvs/env";
(async () => {
    // Defaut version: 1 
    // when update version 1 → 2, call upgrace function
    const storage = await kvsEnvStorage({
        name: "database-name",
        version: 2,
        async upgrade({ kvs, oldVersion }) {
            if (oldVersion < 2) {
                await kvs.set("v1", "v1-migrated-value"); // modify storage as migration
            }
            return;
        }
    });
    assert.strictEqual(await storage.get("v1"), "v1-migrated-value");
})();

First Initializing

When open database at first time, this library also call upgrade function with { oldVersion: 0, newVersion: 1 }. So, You can implement 0 to 1 migration as initializing database.

import { KVSIndexedDB, kvsIndexedDB } from "@kvs/env";
(async () => {
    const storage = await kvsEnvStorage({
        name: "database-name",
        version: 1,
        async upgrade({ kvs, oldVersion, newVersion }) {
            console.log(oldVersion); // => 0
            console.log(newVersion); // => 1
        }
    });
})();

TypeScript

KVS packages support Schema type. It helps you to define a schema of the storage.

import { KVSIndexedDB, kvsIndexedDB } from "@kvs/env";
(async () => {
    type StorageSchema = {
        a1: string;
        b2: number;
        c3: boolean;
    };
    const storage = await kvsEnvStorage<StorageSchema>({
        name: "database-name",
        version: 1
    });
    await storage.set("a1", "string"); // type check
    await storage.set("b2", 42);
    await storage.set("c3", false);
    const a1 = await storage.get("a1"); // a1 will be string type
    const b2 = await storage.get("b2");
    const c3 = await storage.get("c3");
    assert.strictEqual(a1, "string");
    assert.strictEqual(b2, 42);
    assert.strictEqual(c3, false);
})();

Tips: Initial Data

You can also set up initial data using upgrade function. This approach help you to improve Scheme typing.

(async () => {
    type UnixTimeStamp = number;
    type Scheme = {
        timeStamp: UnixTimeStamp
    };
    const storage = await kvsEnvStorage<Scheme>({
        name: "test-data",
        version: 1,
        async upgrade({ kvs, oldVersion, newVersion }) {
            // Initialize data
            // oldVersion is 0 and newVersion is 1 at first time
            if (oldVersion < 1) {
                await kvs.set("timeStamp", Date.now());
            }
        }
    });
    const timeStamp = await storage.get("timeStamp");
    console.log(timeStamp); // => timestamp
})()

Related

Changelog

See Releases page.

Development

This repository use Yarn. You need to build before editing each packages.

# install and link
yarn install
# build all package
yarn run build

Running tests

Running test via yarn tesst command.

yarn test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].