用于浏览器的轻量级密钥-值存储库 Node.js和In-Memory.
Key Value storage for Browser, Node.js, and In-Memory.
It is a monorepo for key-value storage.
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.
KVS libraries provide following common features.
get, set, has, delete, and clear APIversion and upgrade methodIf you want to custom implementation, please see @kvs/storage and test it with @kvs/common-test-case.
@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"
})();@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.
……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 {
const storage = await kvsEnvStorage({
name: "database-name",
version: 1,
async upgrade({ kvs, oldVersion, newVersion }) {
console.log(oldVersion); // => 0
console.log(newVersion); // => 1
}
});
})();KVS packages support Schema type.
It helps you to define a schema of the storage.
…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({
name: "test-data",
version: 1,
async upgrade({ kvs, oldVersion, newVersion }) {
// Initialize data
// oldVersion is 0 and newVersion is 1 at first time
if (oldVersion timestamp
})()~8.8kB(gzipped)See Releases page.
This repository use Yarn. You need to build before editing each packages.
# install and link
yarn install
# build all package
yarn run buildRunning test via yarn test command.
yarn testPull requests and stars are always welcome.
For bugs and feature requests, please create an issue.
git checkout -b my-new-featuregit commit -am 'Add some feature'git push origin my-new-featureMIT © azu
暂无开放 Issues,或尚未同步最近议题。