1.0.0 • Published 8 years ago
zdatastorebase v1.0.0
ZDataStoreBase
A cross between a database and a data store.
Install
npm install --save zdatastorebaseOr
yarn add zdatastorebaseUsage
// 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
keyis set then return thevalueof key else returns the values instorewhere the conditions are met - set - stores the value of
dataaskey - drop - deletes
store - add - creates
store - delete - if
keyis set then deletes the value ofkeyelse deletes the values instorewhere 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): anysetData(key: string, value: any): void
MultiStore
Properties
stores: Data<Store>
Methods
drop(key: string, store: string): voiddropStore(store: string): voidget(key: string, store: string): anygetStore(store: string): Storeinsert(key: string, value: Data<any>, store: string): voidinsertStore(store: string): voidupdate(key: string, value: Data<any>, store: string): void
Base
Extends
MultiStore
Properties
stores: MultiStore
Methods
execute(query: Query): object- returns nothing unlessquery.actionequals 'get' then returns Data
1.0.0
8 years ago