all repos — litestore @ d1fb3abac771f07f6259e1f645242fef8dd69aba

A minimalist nosql document store.

Added support for creating documents under a folder; other fixes.
h3rald h3rald@h3rald.com
Sun, 17 Apr 2016 15:46:10 +0200
commit

d1fb3abac771f07f6259e1f645242fef8dd69aba

parent

e4718aa749b456ed1ef66d51abd5b37be42aceb0

4 files changed, 41 insertions(+), 17 deletions(-)

jump to
M admin/md/api_docs.mdadmin/md/api_docs.md

@@ -74,7 +74,7 @@ ```

$ curl -i -X OPTIONS 'http://127.0.0.1:9500/docs/test/' HTTP/1.1 200 OK Content-Length: 0 -Access-Control-Allow-Methods: HEAD,GET,OPTIONS +Access-Control-Allow-Methods: HEAD,GET,OPTIONS,POST Allow: HEAD,GET,OPTIONS Access-Control-Allow-Headers: Content-Type Access-Control-Allow-Origin: *

@@ -97,6 +97,24 @@ Access-Control-Allow-Origin: *

Server: LiteStore/1.0.3 {"id": "555f93e82190e77500000000", "data": "A document with a randomly-generated ID.", "created": "2015-05-22T08:39:04Z", "modified": null, "tags": ["$type:text", "$subtype:plain", "$format:text"]} +``` + +#### POST docs/:folder/ + +Creates a new document with a randomly-generated ID under the specified folder path. + +##### Example + +``` +$ curl -i -X POST -d 'A document with a randomly-generated ID.' 'http://127.0.0.1:9500/docs/test/' --header "Content-Type:text/plain" +HTTP/1.1 201 Created +Content-Length: 197 +Content-Type: application/json +Access-Control-Allow-Headers: Content-Type +Access-Control-Allow-Origin: * +Server: LiteStore/1.0.3 + +{"id": "test/555f93e82230f77500000000", "data": "A document with a randomly-generated ID.", "created": "2015-05-22T08:39:04Z", "modified": null, "tags": ["$type:text", "$subtype:plain", "$format:text"]} ``` #### HEAD docs
M lib/api_v2.nimlib/api_v2.nim

@@ -18,9 +18,6 @@

# Helper procs -proc isFolder(id: string): bool = - return (id.len > 0 and id[id.len-1] == '/') - proc orderByClause(clause: string): string = var matches = @["", ""] if clause.find(peg"{[-+ ]} {(id / created / modified)}", matches) != -1:

@@ -221,9 +218,11 @@ result.headers = ctJsonHeader()

result.content = content.pretty result.code = Http200 -proc postDocument(LS: LiteStore, body: string, ct: string): Response = +proc postDocument(LS: LiteStore, body: string, ct: string, folder=""): Response = + if not folder.isFolder: + return resError(Http400, "Invalid folder specified when creating document: $folder" % folder) try: - var doc = LS.store.createDocument("", body, ct) + var doc = LS.store.createDocument(folder, body, ct) if doc != "": result.headers = ctJsonHeader() result.content = doc

@@ -321,12 +320,17 @@ of "docs":

var folder: string if id.isFolder: folder = id - if folder != "": + if not folder.isNil: result.code = Http200 result.content = "" - result.headers = TAB_HEADERS.newStringTable - result.headers["Allow"] = "HEAD,GET,OPTIONS" - result.headers["Access-Control-Allow-Methods"] = "HEAD,GET,OPTIONS" + if LS.readonly: + result.headers = TAB_HEADERS.newStringTable + result.headers["Allow"] = "HEAD,GET,OPTIONS" + result.headers["Access-Control-Allow-Methods"] = "HEAD,GET,OPTIONS" + else: + result.headers = TAB_HEADERS.newStringTable + result.headers["Allow"] = "HEAD,GET,OPTIONS,POST,PUT" + result.headers["Access-Control-Allow-Methods"] = "HEAD,GET,OPTIONS,POST,PUT" elif id != "": result.code = Http200 result.content = ""

@@ -398,13 +402,10 @@ discard # never happens really.

proc post(req: Request, LS: LiteStore, resource: string, id = ""): Response = - if id == "": - var ct = "text/plain" - if req.headers.hasKey("Content-Type"): - ct = req.headers["Content-Type"] - return LS.postDocument(req.body.strip, ct) - else: - return resError(Http400, "Bad request: document ID cannot be specified in POST requests.") + var ct = "text/plain" + if req.headers.hasKey("Content-Type"): + ct = req.headers["Content-Type"] + return LS.postDocument(req.body.strip, ct, id) proc put(req: Request, LS: LiteStore, resource: string, id = ""): Response = if id != "":
M lib/core.nimlib/core.nim

@@ -176,6 +176,8 @@ searchable = 0

var data = rawdata if id == "": id = $genOid() + elif id.isFolder: + id = id & $genOid() # Store document try: LOG.debug("Creating document '$1'" % id)
M lib/utils.nimlib/utils.nim

@@ -15,6 +15,9 @@ queries,

contenttypes, logger +proc isFolder*(id: string): bool = + return (id.len > 0 and id[id.len-1] == '/') + proc dbQuote*(s: string): string = result = "'" for c in items(s):