Implementing dstore module.
h3rald h3rald@h3rald.com
Sun, 20 Dec 2020 20:58:50 +0100
6 files changed,
151 insertions(+),
0 deletions(-)
A
lib/min_dstore.nim
@@ -0,0 +1,124 @@
+import + json, + strutils, + oids +import + ../core/parser, + ../core/value, + ../core/interpreter, + ../core/utils + +proc dstore_module*(i: In)= + let def = i.define() + + def.symbol("dsinit") do (i: In): + let vals = i.expect("'sym") + let p = vals[0].getString + var j = %*{"collections": {}} + p.writeFile(j.pretty) + var d = newDict(i.scope) + i.dset(d, "data", i.fromJson(j)) + i.dset(d, "path", p.newVal) + d.objType = "datastore" + i.push d + + def.symbol("dsread") do (i: In): + let vals = i.expect("'sym") + let p = vals[0].getString + var j = p.readFile.parseJson + var d = newDict(i.scope) + i.dset(d, "data", i.fromJson(j)) + i.dset(d, "path", p.newVal) + d.objType = "datastore" + i.push d + + def.symbol("dswrite") do (i: In): + let vals = i.expect("dict:datastore") + let ds = vals[0] + let p = i.dget(ds, "path".newVal).getString + let data = i%(i.dget(ds, "data".newVal)) + p.writeFile(data.pretty) + + def.symbol("dshas?") do (i: In): + let vals = i.expect("'sym", "dict:datastore") + let s = vals[0].getString + let ds = vals[1] + let parts = s.split("/") + let collection = parts[0] + let id = parts[1] + let data = i.dget(ds, "data".newVal) + let cll = i.dget(data, collection.newVal) + if dhas(cll, id.newVal): + i.push true.newVal + else: + i.push false.newVal + + def.symbol("dsget") do (i: In): + let vals = i.expect("'sym", "dict:datastore") + let s = vals[0].getString + let ds = vals[1] + let parts = s.split("/") + let collection = parts[0] + let id = parts[1] + let data = i.dget(ds, "data".newVal) + let cll = i.dget(data, collection) + i.push i.dget(cll, id.newVal) + + def.symbol("dsquery") do (i: In): + let vals = i.expect("quot", "'sym", "dict:datastore") + var filter = vals[0] + let collection = vals[1] + let ds = vals[2] + let data = i.dget(ds, "data".newVal) + let cll = i.dget(data, collection) + var res = newSeq[MinValue](0) + for e in i.values(cll).qVal: + i.push e + i.dequote(filter) + var check = i.pop + if check.isBool and check.boolVal == true: + res.add e + i.push res.newVal + + def.symbol("dspost") do (i: In): + let vals = i.expect("dict", "'sym", "dict:datastore") + var d = vals[0] + let collection = vals[1] + var ds = vals[2] + let id = $genOid() + i.dset(d, "id", id.newVal) + var data = i.dget(ds, "data".newVal) + if not dhas(data, collection): + i.dset(data, collection, newDict(i.scope)) + var cll = i.dget(data, collection) + i.dset(cll, id, d) + #i.dset(ds, "data", data) + i.push ds + + def.symbol("dsput") do (i: In): + let vals = i.expect("dict", "'sym", "dict:datastore") + var d = vals[0] + let s = vals[1].getString + let ds = vals[2] + let parts = s.split("/") + let collection = parts[0] + let id = parts[1] + var data = i.dget(ds, "data".newVal) + var cll = i.dget(data, collection) + i.dset(cll, id, d) + #i.dset(ds, "data", data) + i.push ds + + def.symbol("dsdelete") do (i: In): + let vals = i.expect("'sym", "dict:datastore") + let s = vals[0].getString + let ds = vals[1] + let parts = s.split("/") + let collection = parts[0] + let id = parts[1] + var data = i.dget(ds, "data".newVal) + var cll = i.dget(data, collection) + i.ddel(cll, id) + i.push ds + + def.finalize("dstore")
M
min.nim
→
min.nim
@@ -38,6 +38,7 @@ import
packages/nimline/nimline, lib/min_sys, lib/min_io, + lib/min_dstore, lib/min_fs when not defined(lite) and not defined(mini):@@ -184,6 +185,7 @@ i.time_module
when not defined(mini): i.sys_module i.fs_module + i.dstore_module i.io_module when not defined(lite) and not defined(mini): i.crypto_module
M
prelude.min
→
prelude.min
@@ -11,6 +11,7 @@ (
'io import 'fs import 'sys import + 'dstore import ) ROOT with ) unless (lite? mini? or) (
M
tests/all.min
→
tests/all.min
@@ -17,6 +17,8 @@ 'num load
'str load 'sys load 'time load +'t load +'dstore load 'fs load 'crypto load 'math load
A
tests/dstore.min
@@ -0,0 +1,21 @@
+'test load +'test import + +"dstore" describe + + ("dstore.json" dsinit type "dict:datastore" ==) assert + + ( + "dstore.json" dsread :ds + ds "tests" + {} + 1 %test1 + 2 %test2 + dspost + "tests" (pop true) dsquery size 1 == + ) assert + + "dstore.json" rm + + report + clear-stack