all repos — litestore @ d1248e51f2ea65000eebb14aa53b66a16e58e03a

A minimalist nosql document store.

Implemented DELETE /stores/:id
h3rald h3rald@h3rald.com
Mon, 16 Mar 2020 15:34:28 +0100
commit

d1248e51f2ea65000eebb14aa53b66a16e58e03a

parent

365c896c2f5a75de9e30edfc5bf4ac9089f5a285

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

@@ -5,10 +5,12 @@ "port": 9200

}, "stores": { "test1": { - "file": "test1.db" + "file": "test1.db", + "config": null }, "test2": { - "file": "test2.db" + "file": "test2.db", + "config": null }, "test3": { "file": "test3.db",
M src/litestorepkg/lib/api_v7.nimsrc/litestorepkg/lib/api_v7.nim

@@ -568,7 +568,8 @@ 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) - let store = LS.addStore(id, id & ".db", config, true) + let store = LS.addStore(id, id & ".db", config) + LS.updateConfig() LSDICT[id] = store result = getStore(LS, id, newQueryOptions(), req) result.code = Http201

@@ -583,6 +584,25 @@ if (LS.store.retrieveIndex(id) == newJNull()):

return resError(Http404, "Index not found: $1" % id) try: LS.store.dropIndex(id) + result.headers = newHttpHeaders(TAB_HEADERS) + setOrigin(LS, req, result.headers) + result.headers["Content-Length"] = "0" + result.content = "" + result.code = Http204 + except: + eWarn() + result = resError(Http500, "Unable to delete index.") + +proc deleteStore*(LS: LiteStore, id: string, req: LSRequest): LSResponse = + if (not id.match(PEG_STORE)): + return resError(Http400, "invalid store ID: $1" % id) + if (not LSDICT.hasKey(id)): + return resError(Http404, "Store not found: $1" % id) + try: + LSDICT.del(id) + if LS.config.hasKey("stores") and LS.config["stores"].hasKey(id): + LS.config["stores"].delete(id) + LS.updateConfig() result.headers = newHttpHeaders(TAB_HEADERS) setOrigin(LS, req, result.headers) result.headers["Content-Length"] = "0"

@@ -931,6 +951,8 @@ proc delete*(req: LSRequest, LS: LiteStore, resource: string, id = ""): LSResponse =

if id != "": if resource == "indexes": return LS.deleteIndex(id, req) + elif resource == "stores": + return LS.deleteStore(id, req) else: # Assume docs return LS.deleteDocument(id, req) else:
M src/litestorepkg/lib/core.nimsrc/litestorepkg/lib/core.nim

@@ -733,7 +733,17 @@ if LS.execution.operation == "" and LS.operation == opExecute:

fail(111, "--operation option not specified") -proc addStore*(LS: LiteStore, id, file: string, config = newJNull(), updateConfig = false): LiteStore = +proc updateConfig*(LS: LiteStore) = + 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") + +proc addStore*(LS: LiteStore, id, file: string, config = newJNull()): LiteStore = result = initLiteStore() result.address = LS.address result.port = LS.port

@@ -746,18 +756,8 @@ 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")