2022-09-19 17:13:11 -06:00
|
|
|
import pkg/upraises
|
2023-09-13 14:42:37 -06:00
|
|
|
import std/algorithm
|
2022-09-19 17:13:11 -06:00
|
|
|
import pkg/chronos
|
2022-09-20 16:41:54 -04:00
|
|
|
import pkg/questionable
|
|
|
|
|
import pkg/questionable/results
|
2022-09-19 17:13:11 -06:00
|
|
|
|
2022-07-15 15:28:42 -05:00
|
|
|
import ./key
|
2022-09-20 16:41:54 -04:00
|
|
|
import ./types
|
2023-09-25 19:54:22 -07:00
|
|
|
import ./backend
|
2023-09-19 18:43:00 -06:00
|
|
|
|
|
|
|
|
export types
|
2023-08-29 16:40:11 -07:00
|
|
|
export options, SortOrder
|
2022-07-15 15:28:42 -05:00
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
2023-09-25 19:54:22 -07:00
|
|
|
## Front end types
|
|
|
|
|
Query* = DbQuery[Key]
|
|
|
|
|
|
|
|
|
|
QueryResponse* = DbQueryResponse[Key, seq[byte]]
|
2022-09-19 17:13:11 -06:00
|
|
|
|
2023-09-13 14:42:37 -06:00
|
|
|
GetNext* = proc(): Future[?!QueryResponse] {.upraises: [], gcsafe.}
|
2022-09-20 16:41:54 -04:00
|
|
|
IterDispose* = proc(): Future[?!void] {.upraises: [], gcsafe.}
|
|
|
|
|
QueryIter* = ref object
|
|
|
|
|
finished*: bool
|
2022-09-19 17:13:11 -06:00
|
|
|
next*: GetNext
|
2022-09-20 16:41:54 -04:00
|
|
|
dispose*: IterDispose
|
2022-09-19 17:13:11 -06:00
|
|
|
|
2023-09-13 14:42:37 -06:00
|
|
|
iterator items*(q: QueryIter): Future[?!QueryResponse] =
|
|
|
|
|
while not q.finished:
|
|
|
|
|
yield q.next()
|
2023-08-29 20:49:43 -07:00
|
|
|
|
2022-09-20 20:18:33 -04:00
|
|
|
proc defaultDispose(): Future[?!void] {.upraises: [], gcsafe, async.} =
|
|
|
|
|
return success()
|
|
|
|
|
|
2023-09-14 18:19:14 -06:00
|
|
|
proc new*(T: type QueryIter, dispose = defaultDispose): T =
|
2022-11-22 15:23:23 -06:00
|
|
|
QueryIter(dispose: dispose)
|
2022-09-20 20:18:33 -04:00
|
|
|
|
2023-09-25 19:54:22 -07:00
|
|
|
proc init*(T: type Query,
|
|
|
|
|
key: Key,
|
2023-09-26 18:40:04 -07:00
|
|
|
value = true,
|
2023-09-25 19:54:22 -07:00
|
|
|
sort = SortOrder.Ascending,
|
|
|
|
|
offset = 0,
|
|
|
|
|
limit = -1): Query =
|
|
|
|
|
dbQuery[Key](key, value, sort, offset, limit)
|
|
|
|
|
|
|
|
|
|
proc toKey*(key: KeyId): Key {.inline, raises: [].} =
|
2023-09-26 17:01:21 -07:00
|
|
|
Key.init($key.data).expect("expected valid key here for but got `" & $key.data & "`")
|