vano
v0.1.4
Published
small and flexible collection storage for nodejs
Downloads
150
Readme
small and flexible collection storage library suitable for small projects.
Installation
$ npm i vano
Usage
import vano, {file} from 'vano';
(async () => {
const db = vano({adapter: file('./storage')});
const characters = db.collection('characters', {
name: '',
age: 0,
});
characters.add({name: "john doe", age: 30});
await characters.write();
})()
API
- vano(config)
- file(base)
- memory()
- db.collection(name[, schema])
- col.read()
- col.write()
- col.add(item)
- col.all()
- col.count()
- col.get(id)
- col.remove(id)
- col.update(id, update)
- col.query()
- col.reset()
- col.on(event, listener)
- col.off(event, listener)
- query.eq(key, value)
- query.neq(key, value)
- query.gt(key, value)
- query.gte(key, value)
- query.lt(key, value)
- query.lte(key, value)
- query.skip(n)
- query.limit(n)
- query.get(n)
Library
vano(config: Config)
Config
| | required | default | description |
|:----------|:--------:|:--------|:--------------------------------------------------------------------------------------------------------------------------------------------------|
| adapter
| ✓ | | interface for the communication between vano and io system. it can be file
, memory
or a custom function. adapter
|
Creates a new vano
instance.
import vano, {memory} from 'vano';
const db = vano({adapter: memory()});
Adapter
file(base)
Reads/writes the collections as JSON file to the provided base
directory.
import vano, {file} from 'vano';
const db = vano({adapter: file('./collections')});
memory()
Keeps all collections in the memory.
import vano, {memory} from 'vano';
const db = vano({adapter: memory()});
Custom
Custom adapters can be used to read and write collections from and in different formats.
These have to adhere the Adapter
interface and return a function with four of the following sub-functions:
| function | parameter | return | description |
|:--------------|:---------------------------|:----------------|:----------------------------------------------------------------------------------|
| read
| key: string
| Promise<any>
| resource reading logic. should return anything deserialize
is comfortable with. |
| write
| key: string
, data: any
| void
| resource saving logic. data
is previously by serialize
processed. |
| serialize
| data: any
| Promise<any>
| transform data into a processable form for write
. |
| deserialize
| data: any
| Promise<any>
| transform data into a processable form for read
. |
Example:
const adapterFactory = (someLibrary, someSerializer) => {
const read = (key) => {
return someLibrary.loadByKey(key);
}
const write = (key, value) => {
someLibrary.saveByKey(key, value);
}
const serialize = (data) => {
return someSerializer.serialize(data);
}
const deserialize = (data) => {
return someSerializer.deserialize(data);
}
}
Collection
db.collection(name[, schema])
Creates a collection with:
name
(only alphanumeric)- optional
schema
(rough shape of every collection item)
vano uses schema
for autocompletion and can be omitted if not necessary.
const col = db.collection('users', {name: "", age: 0});
col.read()
Asynchronously loads a collection into the memory via adapter
, given a previously written collection exists.
col.read();
col.write()
Asynchronously saves a collection from the memory via adapter
.
col.write();
col.add(item)
Adds an item to the collection and returns the unique id
of the item inside the collection.
const id = col.add({name: "john doe", "age": 30});
col.all()
Returns all items from the collection.
const items = col.all();
col.count()
Returns the count of all items in the collection.
const count = col.count();
col.get(id)
Returns the item from the collection via id.
const item = col.get('ID');
col.remove(id)
Removes the item from the collection via id.
col.remove('ID');
col.update(id, update)
Updates the values of an item by an id and update object.
col.update('ID', {name: "max mustermann"});
col.reset()
Removes all items from the collection.
col.reset();
col.on(event, listener[, {once: bool])
Binds an event listener to one of the following actions add
| update
| remove
.
const onAdd = (data) => console.log(data);
col.on('add', onAdd);
col.off(event, listener)
Unbinds the previously bound event listener.
const onAdd = (data) => console.log(data);
col.off('add', onAdd);
col.query()
Returns a query function for basic querying. See query
.
const query = col.query();
Query
query.eq(key, value)
Selects items where item[key]
is equal value and returns a new query
function. Value can also be a regular expression.
const query = query.eq('name', 'doe');
query.neq(key, value)
Selects items where item[key]
is not equal value and returns a new query
function. Value can also be a regular expression.
const query = query.neq('name', 'doe');
query.gt(key, value)
Selects items where item[key]
is greater than value and returns a new query
function.
const query = query.gt('age', 20);
query.gte(key, value)
Selects items where item[key]
is greater than or equal value and returns a new query
function.
const query = query.gte('age', 30);
query.lt(key, value)
Selects items where item[key]
is lesser than value and returns a new query
function.
const query = query.lt('age', 20);
query.lte(key, value)
Selects items where item[key]
is lesser than or equal value and returns a new query
function.
const query = query.lte('age', 30);
query.skip(n)
Sets an offset of n and returns a new query
function.
const query = query.skip(2);
query.limit(n)
Sets a limit of n items and returns a new query
function.
const query = query.limit(10);
query.get()
Returns the queried items as an array.
const items = query.get();
ToDo
- autocompletion via schema in collection.query() and collection.update()
Licence
MIT License, see LICENSE