Implemented folder support.
h3rald h3rald@h3rald.com
Sat, 16 Apr 2016 15:43:34 +0200
5 files changed,
31 insertions(+),
6 deletions(-)
M
lib/api_v1.nim
→
lib/api_v1.nim
@@ -18,6 +18,9 @@
# 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:@@ -173,6 +176,8 @@ if docs.len == 0:
result = resError(Http404, "No documents found.") else: var content = newJObject() + if options.folder != "": + content["folder"] = %(options.folder) if options.search != "": content["search"] = %(options.search.decodeURL) if options.tags != "":@@ -311,7 +316,16 @@ result.headers = TAB_HEADERS.newStringTable
result.headers["Allow"] = "GET,OPTIONS" result.headers["Access-Control-Allow-Methods"] = "GET,OPTIONS" of "docs": - if id != "": + var folder: string + if id.isFolder: + folder = id + if folder != "": + 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" + elif id != "": result.code = Http200 result.content = "" if LS.readonly:@@ -340,9 +354,11 @@
proc head(req: Request, LS: LiteStore, resource: string, id = ""): Response = var options = newQueryOptions() options.select = @["documents.id AS id", "created", "modified"] + if id.isFolder: + options.folder = id try: parseQueryOptions(req.url.query, options); - if id != "": + if id != "" and options.folder == "": result = LS.getRawDocument(id, options) result.content = "" else:@@ -356,11 +372,13 @@ let id = id.decodeURL
case resource: of "docs": var options = newQueryOptions() + if id.isFolder: + options.folder = id if req.url.query.contains("contents=false"): options.select = @["documents.id AS id", "created", "modified"] try: parseQueryOptions(req.url.query, options); - if id != "": + if id != "" and options.folder == "": if req.url.query.contains("raw=true") or req.headers["Accept"] == "application/json": return LS.getRawDocument(id, options) else:
M
lib/core.nim
→
lib/core.nim
@@ -277,7 +277,11 @@ return (data: raw_document[1], contenttype: raw_document[2])
proc retrieveRawDocuments*(store: Datastore, options: var QueryOptions = newQueryOptions()): JsonNode = var select = prepareSelectDocumentsQuery(options) - var raw_documents = store.db.getAllRows(select.sql) + var raw_documents: seq[TRow] + if options.folder != "": + raw_documents = store.db.getAllRows(select.sql, options.folder & "%") + else: + raw_documents = store.db.getAllRows(select.sql) var documents = newSeq[JsonNode](0) for doc in raw_documents: documents.add store.prepareJsonDocument(doc, options.select)
M
lib/types.nim
→
lib/types.nim
@@ -27,6 +27,7 @@ limit*: int
offset*: int orderby*: string tags*: string + folder*: string search*: string TagExpression* = object tag*: string@@ -96,4 +97,4 @@ "Server": LS.appname & "/" & LS.appversion
} proc newQueryOptions*(): QueryOptions = - return QueryOptions(select: @["documents.id AS id", "documents.data AS data", "content_type", "binary", "searchable", "created", "modified"], single: false, limit: 0, offset: 0, orderby: "", tags: "", search: "") + return QueryOptions(select: @["documents.id AS id", "documents.data AS data", "content_type", "binary", "searchable", "created", "modified"], single: false, limit: 0, offset: 0, orderby: "", tags: "", search: "", folder: "")
M
lib/utils.nim
→
lib/utils.nim
@@ -61,6 +61,8 @@ result = result & options.select.join(", ")
result = result & " FROM documents WHERE 1=1 " if options.single: result = result & "AND id = ?" + if options.folder.len > 0: + result = result & "AND id LIKE ?" if options.tags.len > 0: var doc_id_col: string if options.search.len > 0 and options.select[0] != "COUNT(docid)":
M
litestore.nimble
→
litestore.nimble
@@ -1,6 +1,6 @@
[Package] name = "litestore" -version = "1.0.5" +version = "1.1.0" author = "Fabio Cevasco" description = "Self-contained, lightweight, RESTful document store." license = "MIT"