all repos — litestore @ cc412c41ac2d514e91af1b1daf4056bf67e303c2

A minimalist nosql document store.

Merge pull request #65 from tomidery/find-documents-by-id

Fabio Cevasco h3rald@h3rald.com
Tue, 06 Apr 2021 22:16:23 +0200
commit

cc412c41ac2d514e91af1b1daf4056bf67e303c2

parent

2eabe215b76a6d83b65a611a32eae0a693472a19

M src/admin/md/api_docs.mdsrc/admin/md/api_docs.md

@@ -195,6 +195,20 @@ > Tip

> > If **search** is specified, each result will contain a **highlight** property with a highlighted search snippet, and a **rank** property identified the rank of the result within the search. Results will also be automatically ordered by descending rank. +##### `like` option + +If this option is specified, retrieves a single document which id matches the specified pattern. The value give to `like` option is not used so the search pattern is specified as the regular document id as a part of the path. The regular SQL wildcards `%` and `_` can be used. To escape them use `\` encoded in the URL as `%5C`. + +Example: http://127.0.0.1:9500/docs/%/api%5C_%.md?like=1&raw=true&contents=false&search=API%20v7%Required + +If finds the first markdown file which name starts with _api\__ and which contains string _API v7 Required_. + +> %tip% +> Tip +> +> Only the first matching document is returned even if there was more than one which id matched the pattern. To get the subsequent documents use `offset` option and increase its value from _1_ up. + + ##### `tags` option Retrieve only documents with matching tag(s).
M src/litestorepkg/lib/config.nimsrc/litestorepkg/lib/config.nim

@@ -1,6 +1,6 @@

const pkgName* = "litestore" - pkgVersion* = "1.9.3" + pkgVersion* = "1.9.4" pkgAuthor* = "Fabio Cevasco" pkgDescription* = "Self-contained, lightweight, RESTful document store." pkgLicense* = "MIT"
M src/litestorepkg/lib/core.nimsrc/litestorepkg/lib/core.nim

@@ -450,6 +450,17 @@ except:

eWarn() store.rollback() +proc findDocumentId*(store: Datastore, pattern: string): string = + var select = "SELECT id FROM documents WHERE id LIKE ? ESCAPE '\\' " + var raw_document = store.db.getRow(select.sql, pattern) + LOG.debug("Retrieving document '$1'" % pattern) + if raw_document[0] == "": + LOG.debug("(No Such Document)") + result = "" + else: + result = raw_document[0] + LOG.debug("Found id: $1" % result) + proc retrieveDocument*(store: Datastore, id: string, options: QueryOptions = newQueryOptions()): tuple[data: string, contenttype: string] =
M src/litestorepkg/lib/utils.nimsrc/litestorepkg/lib/utils.nim

@@ -107,9 +107,12 @@ tables = options.tables & @[documents_table]

result = result & options.select.join(", ") result = result & " FROM "&tables.join(", ")&" WHERE 1=1 " if options.single: - result = result & "AND id = ?" + if options.like.len > 0: + options.limit = 1 + else: + result = result & "AND id = ?" var doc_id_col: string - if options.tags.len > 0 or options.folder.len > 0: + if options.tags.len > 0 or options.folder.len > 0 or options.like.len > 0: if options.jsonFilter.len > 0 or (options.search.len > 0 and options.select[0] != "COUNT(docid)"): doc_id_col = "$1.id" % documents_table else:

@@ -124,6 +127,8 @@ if options.modifiedBefore != "":

result = result & "AND modified < \"" & $options.modifiedBefore & "\" " if options.folder.len > 0: result = result & "AND " & doc_id_col & " BETWEEN ? and ? " + if options.like.len > 0: + result = result & "AND " & doc_id_col & " LIKE ? ESCAPE '\\' " if options.tags.len > 0: result = result & options.tags.selectDocumentsByTags(doc_id_col) if options.jsonFilter.len > 0: