zdatastorebase
v1.0.0
Published
A cross between a database and a data store.
Downloads
2
Readme
ZDataStoreBase
A cross between a database and a data store.
Install
npm install --save zdatastorebase
Or
yarn add zdatastorebase
Usage
// JavaScript
const Store = require('zdatastorebase').Store
const MultiStore = require('zdatastorebase').MultiStore
const Base = require('zdatastorebase').Base
// TypeScript
import {Store, MultiStore, Base} from 'zdatastorebase'
const store = new Store()
const multiStore = new MultiStore()
const base = new Base()
store.setData('key', {innerKey: 'value'})
store.getData('key') // {innerKey: 'value'}
multiStore.insertStore('storeName')
multiStore.insert('key', {innerKey: 'value'}, 'storeName')
multiStore.getStore('storeName') // {data: {key: {innerKey: 'value'}}, getData: Function, setData: Function}
multiStore.get('key', 'storeName') // {innerKey: 'value'}
multiStore.update('key', {innerKeyOne: 'value1', innerKeyTwo: 2}, 'storeName')
multiStore.drop('key', 'storeName')
multiStore.dropStore('storeName')
base.execute({
action: 'add',
store: 'storeName'
})
base.execute({
action: 'set',
store: 'storeName',
data: {key: {innerKey: 'value'}}
})
base.execute({
action: 'get',
store: 'storeName',
where: {
innerKey: 'value'
},
condition: '==='
}) // {innerKey: 'value'}
base.execute({
action: 'delete',
store: 'storeName',
where: {
innerKey: 1
},
condition: '!=='
})
base.execute({
action: 'drop',
store: 'storeName'
})
Types
Data
{
[key: string]: T
}
Query
export type Query = {
store: string
action: 'get' | 'set' | 'drop' | 'add' | 'delete'
data?: Data<any>
where?: Data<any>
key?: string
condition?: '===' | '!==' | '<' | '>' | '<=' | '>=' | '!exist' | 'exists'
}
actions
- get - if
key
is set then return thevalue
of key else returns the values instore
where the conditions are met - set - stores the value of
data
askey
- drop - deletes
store
- add - creates
store
- delete - if
key
is set then deletes the value ofkey
else deletes the values instore
where the conditions are met
conditions
where
and condition
are used together to perform logic.
When an action requires where
and condition
, where
is an object with keys and values, condition
is the condition that must be met for the action
to apply.
Every key/value pair in store
is checked against each key/value pair in where
with condition
.
The conditions are:
- '===' - equals
- '!==' - not equal
- '<' - less than
- '>' - greater than
- '<=' - less than or equal to
- '>=' - greater than or equal to
- '!exist' - key does not exist - value does not matter in this case
- 'exists' - key exists - value does not matter in this case
Logic
When using a query object in Base.execute(), the fuction will throw an error if the query does not meet the following logic:
if (action === 'get') {
key - if set then where and condition can't be defined
data must not be defined
where must be defined
condition must be defined
}
if (action === 'set') {
key must be set
data must not be defined
where must not be defined
condition must not be defined
}
if (action === 'drop') {
data must not be defined
where must not be defined
condition must not be defined
}
if (action === 'add') {
data must not be defined
where must not be defined
condition must not be defined
}
if (action === 'delete') {
key - if set then where and condition can't be defined
data must not be defined
where must be defined
condition must be defined
}
Classes
Store
Properties
data: Data<any>
Methods
getData(key: string): any
setData(key: string, value: any): void
MultiStore
Properties
stores: Data<Store>
Methods
drop(key: string, store: string): void
dropStore(store: string): void
get(key: string, store: string): any
getStore(store: string): Store
insert(key: string, value: Data<any>, store: string): void
insertStore(store: string): void
update(key: string, value: Data<any>, store: string): void
Base
Extends
MultiStore
Properties
stores: MultiStore
Methods
execute(query: Query): object
- returns nothing unlessquery
.action
equals 'get' then returns Data