all repos — litestore @ f5eb1555870df89f737a3d9e2f86e2aaa6e155c3

A minimalist nosql document store.

src/admin/md/nim-api.md

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
## Nim API Reference

Besides exposing an HTTP API, LiteStore also provides a basic Nim API to use it as a library within other Nim projects.

### Data Types

The following data types are used by the LiteStore Nim API [proc](class:kwd)s

#### LSResponse

An HTTP Response.

```
LSResponse* = tuple[
  code: HttpCode,
  content: string,
  headers: HttpHeaders]
```

#### QueryOptions

The set of options SQL-like to be used to compose a LiteStore query.

```
QueryOptions* = object
  select*: seq[string]
  single*:bool         
  limit*: int           
  offset*: int           
  orderby*: string      
  tags*: string
  folder*: string
  search*: string
```


### Example: Jester Web Framework Integration

The following code example shows how to use the [proc](class:kwd)s provided by LiteStore Nim API to expose LiteStore HTTP routes using the [Jester](https://github.com/dom96/jester) web framework for Nim:

```
import 
  jester,
  litestore, 
  asyncdispatch, 
  re, 
  strtabs, 
  asyncnet

litestore.setup()

routes:

  # Just a simple, unrelated Jester route
  get "/": 
    resp "Hello, World!"
  
  # Remapping LiteStore routes on Jester
  get re"^\/litestore\/(docs|info)\/?(.*)":
    let r = get(request.matches[0], 
                request.matches[1], 
                request.params, 
                request.headers)
    resp(r.code, r.content, r.headers["Content-Type"]) 

  post re"^\/litestore\/docs\/?(.*)":
    let r = post("docs", 
                 request.matches[0], 
                 request.body, 
                 request.headers)
    resp(r.code, r.content, r.headers["Content-Type"]) 

  put re"^\/litestore\/docs\/?(.*)":
    let r = put("docs", 
                request.matches[0], 
                request.body, 
                request.headers)
    resp(r.code, r.content, r.headers["Content-Type"]) 

  patch re"^\/litestore\/docs\/?(.*)":
    let r = patch("docs", 
                  request.matches[0], 
                  request.body, 
                  request.headers)
    resp(r.code, r.content, r.headers["Content-Type"]) 

  delete re"^\/litestore\/docs\/?(.*)":
    let r = delete("docs", 
                   request.matches[0], 
                   request.headers)
    resp(r.code, r.content) 

  head re"^\/litestore\/docs\/?(.*)":
    let r = head("docs", 
                 request.matches[0], 
                 request.headers)
    headers = newStringTable()
    for key, value in r.headers.pairs:
      headers[key] = value
    await response.sendHeaders(r.code, headers)
    response.client.close()

  options re"^\/litestore\/docs\/?(.*)":
    let r = options("docs", 
                    request.matches[0], 
                    request.headers)
    headers = newStringTable()
    for key, value in r.headers.pairs:
      headers[key] = value
    await response.sendHeaders(r.code, headers)
    response.client.close()

runForever()

```