libj-db
v1.1.0
Published
Part of libj tools
Downloads
4
Readme
libj-db
Part of libj tools
Contains a wrapper around store library.
Usage (npm)
npm install libj-db
import { db } from 'libj-db'
db.set('key', 'value')
db.get('key') // returns 'value'
.
.
.
Test
- Run this in a separate command line to start node server
node server.js
- Run one of the following to re-create bundles
npm run dev
npm run dev:watch
- Navigate to http://localhost:3000
Build
npm run build
npm run build:watch
Make sure to test everything in all browsers (specially IE 10/11)
Source:
const store = require('store');
/**
* A wrapper around "store" npm package to work with database in browser
*/
export default class Database {
/**
* Sets a value in the database
* @param {String} key : key for entry
* @param {Object} value : value for entry
*/
set(key, value) {
return store.set(key, value);
}
/**
* Gets a value from the database
* @param {String} key : key of entry
* @param {Object} seed : a seed value for the entry's value if entry does not exist. Pass null or undefined to ignore seeding
*/
get(key, seed) {
if (seed !== undefined && seed !== null) {
var i = this.all().findIndex(function (item, index, array) {
return item.key === key;
});
if (i < 0) {
this.set(key, seed);
}
}
return store.get(key);
}
/**
* Removes an entry from database with the given key
* @param {String} key : key of entry
*/
remove(key) {
store.remove(key);
}
/**
* Clears the whole database
*/
clear() {
store.clearAll();
}
/**
* @returns array of {key, value} of all key-value pairs in database
*/
all() {
var res = [];
store.each((value, key) => {
res.push({
key: key,
value: value
});
});
return res;
}
}