all repos — litestore @ 35fc8b5cfa1a104fe86db3e48016b89e08485070

A minimalist nosql document store.

Implemented PUT store/:id
h3rald h3rald@h3rald.com
Mon, 16 Mar 2020 15:13:13 +0100
commit

35fc8b5cfa1a104fe86db3e48016b89e08485070

parent

4728c4c8fe591c9637342255a107236c388d2871

M src/litestorepkg/examples/system/config.jsonsrc/litestorepkg/examples/system/config.json

@@ -9,35 +9,62 @@ "file": "test1.db"

}, "test2": { "file": "test2.db" + }, + "test3": { + "file": "test3.db", + "config": null } }, "resources": { "/docs/vehicles/*": { "GET": { - "middleware": ["validate", "log"] + "middleware": [ + "validate", + "log" + ] }, "HEAD": { - "middleware": ["validate", "log"] + "middleware": [ + "validate", + "log" + ] }, "POST": { "allowed": false }, "PATCH": { - "auth": ["admin:vehicles"], - "middleware": ["validate", "log"] + "auth": [ + "admin:vehicles" + ], + "middleware": [ + "validate", + "log" + ] }, "PUT": { - "auth": ["admin:vehicles"], - "middleware": ["validate", "log"] + "auth": [ + "admin:vehicles" + ], + "middleware": [ + "validate", + "log" + ] }, "DELETE": { - "auth": ["admin:vehicles"], - "middleware": ["validate", "log"] + "auth": [ + "admin:vehicles" + ], + "middleware": [ + "validate", + "log" + ] } }, "/docs/logs/*": { "GET": { - "auth": ["admin:server"] + "auth": [ + "admin:server" + ] }, "POST": { "allowed": false
M src/litestorepkg/lib/api_v7.nimsrc/litestorepkg/lib/api_v7.nim

@@ -562,18 +562,19 @@ except:

eWarn() result = resError(Http500, "Unable to create index.") -proc putStore*(LS: LiteStore, id: string, content: JsonNode, req: LSRequest): LSResponse = +proc putStore*(LS: LiteStore, id: string, config: JsonNode, req: LSRequest): LSResponse = try: - if (not id.match(PEG_STORE)): + if (not id.match(PEG_STORE) or id == "master"): return resError(Http400, "invalid store ID: $1" % id) if (LSDICT.hasKey(id)): return resError(Http409, "Store already exists: $1" % id) - # TODO: create store + let store = LS.addStore(id, id & ".db", config, true) + LSDICT[id] = store result = getStore(LS, id, newQueryOptions(), req) result.code = Http201 except: eWarn() - result = resError(Http500, "Unable to create index.") + result = resError(Http500, "Unable to create store.") proc deleteIndex*(LS: LiteStore, id: string, req: LSRequest): LSResponse = if (not id.match(PEG_INDEX)):

@@ -911,6 +912,13 @@ field = parseJson(req.body.strip)["field"].getStr

except: return resError(Http400, "Bad Request - Invalid JSON body - $1" % getCurrentExceptionMsg()) return LS.putIndex(id, field, req) + elif resource == "stores": + var config = newJNull() + try: + config = parseJson(req.body) + except: + return resError(Http400, "Bad Request - Invalid JSON body - $1" % getCurrentExceptionMsg()) + return LS.putStore(id, config, req) else: # Assume docs var ct = "text/plain" if req.headers.hasKey("Content-Type"):
M src/litestorepkg/lib/core.nimsrc/litestorepkg/lib/core.nim

@@ -355,6 +355,35 @@ store.rollback()

eWarn() raise +proc updateSystemDocument*(store: Datastore, id: string, rawdata: string, + contenttype = "text/plain", binary = -1): string = + let singleOp = not LS_TRANSACTION + var contenttype = contenttype.replace(peg"""\;(.+)$""", "") # Strip charset for now + var binary = checkIfBinary(binary, contenttype) + var data = rawdata + if contenttype == "application/json": + # Validate JSON data + try: + discard data.parseJson + except: + raise newException(JsonParsingError, "Invalid JSON content - " & + getCurrentExceptionMsg()) + try: + LOG.debug("Updating system document '$1'" % id) + store.begin() + var res = store.db.execAffectedRows(SQL_UPDATE_SYSTEM_DOCUMENT, data, contenttype, + binary, currentTime(), id) + if res > 0: + result = $store.retrieveRawDocument(id) + else: + result = "" + if singleOp: + store.commit() + except: + eWarn() + store.rollback() + raise + proc updateDocument*(store: Datastore, id: string, rawdata: string, contenttype = "text/plain", binary = -1, searchable = 1): string = let singleOp = not LS_TRANSACTION

@@ -704,18 +733,31 @@ if LS.execution.operation == "" and LS.operation == opExecute:

fail(111, "--operation option not specified") -proc addStore*(LS: LiteStore, id, file: string, config = newJNull()): LiteStore = +proc addStore*(LS: LiteStore, id, file: string, config = newJNull(), updateConfig = false): LiteStore = result = initLiteStore() result.address = LS.address result.port = LS.port result.appname = LS.appname result.appversion = LS.appversion result.favicon = LS.favicon - # TODO error handling result.file = file if config != newJNull(): result.config = config LOG.info("Initializing store '$1'" % id) result.setup(true) result.initStore() - + if not updateConfig: + return + if not LS.config.hasKey("stores"): + LS.config["stores"] = newJObject() + LS.config["stores"][id] = newJObject() + LS.config["stores"][id]["file"] = %file + LS.config["stores"][id]["config"] = config + let rawConfig = LS.config.pretty + if LS.configFile != "": + LS.configFile.writeFile(rawConfig) + else: + let options = newQueryOptions(true) + let configDoc = LS.store.retrieveRawDocument("config.json", options) + if configDoc != "": + discard LS.store.updateSystemDocument("config.json", rawConfig, "application/json")
M src/litestorepkg/lib/queries.nimsrc/litestorepkg/lib/queries.nim

@@ -121,6 +121,15 @@ modified = ?

WHERE id = ? """ +const SQL_UPDATE_SYSTEM_DOCUMENT* = sql""" +UPDATE system_documents +SET data = ?, +content_type = ?, +binary = ?, +modified = ? +WHERE id = ? +""" + const SQL_SET_DOCUMENT_MODIFIED* = sql""" UPDATE documents SET modified = ?