- record json operation to 4 words: cd | pr | set | rm
- for share very-big-json-env using (record append only)
- npm install nv-json-jstore
const x = require("nv-json-jstore");
/*
> x.JStore.prototype
{
impl_fwdcast: [Function (anonymous)], // your need to impl fwdcast:
// When server receive a <operation/ir> FROM one-client
// First: server will sync-to-<disk>
// Then: server will forward <operation/ir> TO all-other-clients
impl_broadcast: [Function (anonymous)], // your need to impl broadcast
// When server do <operation/ir> (for example : from cli-command OR repl)
// First: server will sync-to-<disk>
// Then: server will broadcast <operation/ir> TO all-clients
'#dispatch_one_srv_action': [Function (anonymous)],
'#dispatch_many_srv_action': [Function (anonymous)],
"#exec_and_dispatch_many_srv_action" : [Function (anonymous)],
'#dispath_srv_action': [Function (anonymous)], // no need, internal func
pr: [Function (anonymous)], // ir 1 goto parent
cd: [Function (anonymous)], // ir 2 goto direct child
set: [Function (anonymous)], // ir 3 update OR add
rm: [Function (anonymous)], // ir 4 delete
_exec_one_ir_and_sync_to_disk: [Function (anonymous)],
_recv_from_src_client: [Function (anonymous)], // this is for call in http/ws handle
// wrapped API for convinience:
'$set_then_cd': [Function (anonymous)],
'$flat_set': [Function (anonymous)]
}
*/
var jstore = x.creat();
jstore.disk.write = function (ir) {console.log("ir:",ir)} // your need to impl your-own <Disk> MUST have 2 methods: write | finish
// normally JUST use a <FileWriteStream> OR <Blob> is OK (you need impl the json-stream parser)
// in practice
// USING a database(postgres) OR a shared-mem(best) OR msg-queue
jstore.set("k1",100)
//ir: [ 'set', 'k1', 100 ]
jstore.set("k2",{})
//ir: [ 'set', 'k2', {} ]
/*
> jstore
JStore { ver: 3, rt: { k1: 100, k2: {} }, pl: [] }
>
*/
jstore.cd("k2")
/*
> jstore.cd("k2")
ir: [ 'cd', 'k2' ]
> jstore
JStore { ver: 4, rt: { k1: 100, k2: {} }, pl: [ 'k2' ] }
>
*/
jstore.$flat_set("k3",[1.1,"s",true,null,false,{a:"ss",b:"sss"}])
/*
> jstore.$flat_set("k3",[1.1,"s",true,null,false,{a:"ss",b:"sss"}])
ir: [ 'set', 'k3', [] ]
ir: [ 'cd', 'k3' ]
ir: [ 'set', 0, 1.1 ]
ir: [ 'set', 1, 's' ]
ir: [ 'set', 2, true ]
ir: [ 'set', 3, null ]
ir: [ 'set', 4, false ]
ir: [ 'set', 5, {} ]
ir: [ 'cd', 5 ]
ir: [ 'set', 'a', 'ss' ]
ir: [ 'set', 'b', 'sss' ]
ir: [ 'pr' ]
ir: [ 'pr' ]
> jstore
JStore { ver: 17, rt: { k1: 100, k2: { k3: [Array] } }, pl: [ 'k2' ] }
> jstore.rt.k2.k3
[ 1.1, 's', true, null, false, { a: 'ss', b: 'sss' } ]
>
>
/*
> jstore.cd("k3")
ir: [ 'cd', 'k3' ]
> jstore
JStore {
ver: 18,
rt: { k1: 100, k2: { k3: [Array] } },
pl: [ 'k2', 'k3' ]
}
> jstore.curr
[ 1.1, 's', true, null, false, { a: 'ss', b: 'sss' } ]
>
> jstore.cd(5)
ir: [ 'cd', 5 ]
> jstore.curr
{ a: 'ss', b: 'sss' }
>
> jstore.rm("a")
ir: [ 'rm', 'a' ]
> jstore.
...
> jstore
JStore {
ver: 19,
rt: { k1: 100, k2: { k3: [Array] } },
pl: [ 'k2', 'k3', 5 ]
}
> jstore.pr()
ir: [ 'pr' ]
> jstore
JStore {
ver: 20,
rt: { k1: 100, k2: { k3: [Array] } },
pl: [ 'k2', 'k3' ]
}
> jstore.pr()
ir: [ 'pr' ]
> jstore
JStore { ver: 21, rt: { k1: 100, k2: { k3: [Array] } }, pl: [ 'k2' ] }
>
> jstore.pr()
ir: [ 'pr' ]
> jstore
JStore { ver: 22, rt: { k1: 100, k2: { k3: [Array] } }, pl: [] }
>
>
*/
jstore.impl_broadcast jstore.impl_fwdcast
jstore.$flat_set
jstore.$set_then_cd
jstore._exec_one_ir_and_sync_to_disk jstore._recv_from_src_client
jstore.cd
jstore.pr
jstore.rm
jstore.set
// getters
jstore.clients jstore.curr jstore.disk jstore.pl
jstore.rt jstore.ver
{
DummyInitEnv: [class DummyInitEnv],
DummyDisk: [class DummyDisk],
JStore: [Function: JStore],
is_irary: [Function: is_irary],
creat_pr_ir: [Function: creat_pr_ir],
creat_cd_ir: [Function: creat_cd_ir],
creat_set_ir: [Function: creat_set_ir],
creat_rm_ir: [Function: creat_rm_ir],
creat_flat_set_irs: [Function: creat_flat_set_irs],
pr: [Function: pr],
cd: [Function: cd],
set: [Function: set],
rm: [Function: rm],
exec_one_ir: [Function: exec_one_ir],
exec_irs: [Function: exec_irs],
exec_irs_with_each_ircb: [Function: exec_irs_with_each_ircb],
creat: [Function: creat]
}