all repos — min @ f5c37b9b51e9a4784537995f0a9ac1ccbb73d02a

A small but practical concatenative programming language.

minpkg/lib/min_dstore.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
import
  std/[json,
  strutils,
  oids]
import
  ../core/parser,
  ../core/value,
  ../core/interpreter,
  ../core/utils

proc dstore_module*(i: In) =
  let def = i.define()

  def.symbol("init") do (i: In):
    let vals = i.expect("'sym")
    let p = vals[0].getString
    p.writeFile("{}")
    var d = newDict(i.scope)
    i.dset(d, "data", newDict(i.scope))
    i.dset(d, "path", p.newVal)
    d.objType = "datastore"
    i.push d

  def.symbol("read") 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("write") 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)
    i.push ds

  def.symbol("has?") 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)
    if not dhas(data, collection):
      i.push false.newVal
    else:
      let cll = i.dget(data, collection.newVal)
      if dhas(cll, id.newVal):
        i.push true.newVal
      else:
        i.push false.newVal

  def.symbol("get") 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)
    if not dhas(data, collection):
      raiseInvalid("Collection '$#' does not exist" % collection)
    let cll = i.dget(data, collection)
    i.push i.dget(cll, id.newVal)

  def.symbol("query") 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)
    var res = newSeq[MinValue](0)
    if not dhas(data, collection):
      i.push res.newVal
      return
    let cll = i.dget(data, collection)
    for e in i.values(cll).qVal:
      i.push e
      try:
        i.dequote(filter)
        var check = i.pop
        if check.isBool and check.boolVal == true:
          res.add e
      except CatchableError:
        discard
    i.push res.newVal

  def.symbol("post") 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.push ds

  def.symbol("put") 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]
    if parts.len < 2:
      raiseInvalid("collection/id not specified")
    let id = parts[1]
    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.push ds

  def.symbol("delete") do (i: In):
    let vals = i.expect("'sym", "dict:datastore")
    let s = vals[0].getString
    let ds = vals[1]
    let parts = s.split("/")
    if parts.len < 2:
      raiseInvalid("collection/id not specified")
    let collection = parts[0]
    let id = parts[1]
    var data = i.dget(ds, "data".newVal)
    if not dhas(data, collection):
      raiseInvalid("Collection '$#' does not exist" % collection)
    var cll = i.dget(data, collection)
    i.ddel(cll, id)
    i.push ds

  def.finalize("dstore")