@based/db 0.0.70 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (263) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +105 -77
  3. package/dist/lib/darwin_aarch64/include/libdeflate.h +23 -0
  4. package/dist/lib/darwin_aarch64/include/selva/db.h +0 -41
  5. package/dist/lib/darwin_aarch64/include/selva/fast_linear_search.h +6 -2
  6. package/dist/lib/darwin_aarch64/include/selva/fields.h +52 -37
  7. package/dist/lib/darwin_aarch64/include/selva/gmtime.h +137 -0
  8. package/dist/lib/darwin_aarch64/include/selva/hll.h +1 -1
  9. package/dist/lib/darwin_aarch64/include/selva/mblen.h +40 -0
  10. package/dist/lib/darwin_aarch64/include/selva/selva_string.h +8 -5
  11. package/dist/lib/darwin_aarch64/include/selva/thread.h +37 -0
  12. package/dist/lib/darwin_aarch64/include/selva/types.h +18 -4
  13. package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
  14. package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
  15. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  16. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  17. package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
  18. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  19. package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
  20. package/dist/lib/linux_aarch64/include/libdeflate.h +23 -0
  21. package/dist/lib/linux_aarch64/include/selva/db.h +0 -41
  22. package/dist/lib/linux_aarch64/include/selva/fast_linear_search.h +6 -2
  23. package/dist/lib/linux_aarch64/include/selva/fields.h +52 -37
  24. package/dist/lib/linux_aarch64/include/selva/gmtime.h +137 -0
  25. package/dist/lib/linux_aarch64/include/selva/hll.h +1 -1
  26. package/dist/lib/linux_aarch64/include/selva/mblen.h +40 -0
  27. package/dist/lib/linux_aarch64/include/selva/selva_string.h +8 -5
  28. package/dist/lib/linux_aarch64/include/selva/thread.h +37 -0
  29. package/dist/lib/linux_aarch64/include/selva/types.h +18 -4
  30. package/dist/lib/linux_aarch64/libdeflate.so +0 -0
  31. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  32. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  33. package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
  34. package/dist/lib/linux_aarch64/libselva.so +0 -0
  35. package/dist/lib/linux_x86_64/include/libdeflate.h +23 -0
  36. package/dist/lib/linux_x86_64/include/selva/db.h +0 -41
  37. package/dist/lib/linux_x86_64/include/selva/fast_linear_search.h +6 -2
  38. package/dist/lib/linux_x86_64/include/selva/fields.h +52 -37
  39. package/dist/lib/linux_x86_64/include/selva/gmtime.h +137 -0
  40. package/dist/lib/linux_x86_64/include/selva/hll.h +1 -1
  41. package/dist/lib/linux_x86_64/include/selva/mblen.h +40 -0
  42. package/dist/lib/linux_x86_64/include/selva/selva_string.h +8 -5
  43. package/dist/lib/linux_x86_64/include/selva/thread.h +37 -0
  44. package/dist/lib/linux_x86_64/include/selva/types.h +18 -4
  45. package/dist/lib/linux_x86_64/libdeflate.so +0 -0
  46. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  47. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  48. package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
  49. package/dist/lib/linux_x86_64/libselva.so +0 -0
  50. package/dist/src/client/index.d.ts +16 -21
  51. package/dist/src/client/index.js +48 -59
  52. package/dist/src/client/modify/Ctx.d.ts +38 -0
  53. package/dist/src/client/modify/Ctx.js +30 -0
  54. package/dist/src/client/modify/Tmp.d.ts +19 -0
  55. package/dist/src/client/modify/Tmp.js +67 -0
  56. package/dist/src/client/modify/create/index.d.ts +6 -0
  57. package/dist/src/client/modify/create/index.js +191 -0
  58. package/dist/src/client/modify/create/mark.d.ts +7 -0
  59. package/dist/src/client/modify/create/mark.js +42 -0
  60. package/dist/src/client/modify/cursor.d.ts +10 -0
  61. package/dist/src/client/modify/cursor.js +48 -0
  62. package/dist/src/client/modify/delete/index.d.ts +2 -0
  63. package/dist/src/client/modify/delete/index.js +39 -0
  64. package/dist/src/client/modify/drain.d.ts +7 -0
  65. package/dist/src/client/modify/drain.js +77 -0
  66. package/dist/src/client/modify/edges/binary.d.ts +3 -0
  67. package/dist/src/client/modify/edges/binary.js +28 -0
  68. package/dist/src/client/modify/edges/cardinality.d.ts +3 -0
  69. package/dist/src/client/modify/edges/cardinality.js +15 -0
  70. package/dist/src/client/modify/edges/header.d.ts +5 -0
  71. package/dist/src/client/modify/edges/header.js +21 -0
  72. package/dist/src/client/modify/edges/index.d.ts +3 -0
  73. package/dist/src/client/modify/edges/index.js +128 -0
  74. package/dist/src/client/modify/edges/reference.d.ts +3 -0
  75. package/dist/src/client/modify/edges/reference.js +31 -0
  76. package/dist/src/client/modify/edges/references.d.ts +3 -0
  77. package/dist/src/client/modify/edges/references.js +38 -0
  78. package/dist/src/client/modify/edges/separate.d.ts +3 -0
  79. package/dist/src/client/modify/edges/separate.js +24 -0
  80. package/dist/src/client/modify/edges/string.d.ts +3 -0
  81. package/dist/src/client/modify/edges/string.js +30 -0
  82. package/dist/src/client/modify/error.d.ts +7 -0
  83. package/dist/src/client/modify/error.js +68 -0
  84. package/dist/src/client/modify/expire/index.d.ts +2 -0
  85. package/dist/src/client/modify/expire/index.js +31 -0
  86. package/dist/src/client/modify/props/alias.d.ts +3 -0
  87. package/dist/src/client/modify/props/alias.js +26 -0
  88. package/dist/src/client/modify/props/binary.d.ts +5 -0
  89. package/dist/src/client/modify/props/binary.js +52 -0
  90. package/dist/src/client/modify/props/cardinality.d.ts +4 -0
  91. package/dist/src/client/modify/props/cardinality.js +46 -0
  92. package/dist/src/client/modify/props/delete.d.ts +3 -0
  93. package/dist/src/client/modify/props/delete.js +13 -0
  94. package/dist/src/client/modify/props/fixed.d.ts +4 -0
  95. package/dist/src/client/modify/props/fixed.js +105 -0
  96. package/dist/src/client/modify/props/increment.d.ts +3 -0
  97. package/dist/src/client/modify/props/increment.js +28 -0
  98. package/dist/src/client/modify/props/json.d.ts +3 -0
  99. package/dist/src/client/modify/props/json.js +5 -0
  100. package/dist/src/client/modify/props/main.d.ts +4 -0
  101. package/dist/src/client/modify/props/main.js +22 -0
  102. package/dist/src/client/modify/props/object.d.ts +3 -0
  103. package/dist/src/client/modify/props/object.js +37 -0
  104. package/dist/src/client/modify/props/reference.d.ts +6 -0
  105. package/dist/src/client/modify/props/reference.js +92 -0
  106. package/dist/src/client/modify/props/references.d.ts +3 -0
  107. package/dist/src/client/modify/props/references.js +231 -0
  108. package/dist/src/client/modify/props/separate.d.ts +3 -0
  109. package/dist/src/client/modify/props/separate.js +43 -0
  110. package/dist/src/client/modify/props/string.d.ts +5 -0
  111. package/dist/src/client/modify/props/string.js +51 -0
  112. package/dist/src/client/modify/props/text.d.ts +3 -0
  113. package/dist/src/client/modify/props/text.js +35 -0
  114. package/dist/src/client/modify/props/vector.d.ts +3 -0
  115. package/dist/src/client/modify/props/vector.js +30 -0
  116. package/dist/src/client/modify/resize.d.ts +3 -0
  117. package/dist/src/client/modify/resize.js +20 -0
  118. package/dist/src/client/modify/types.d.ts +3 -4
  119. package/dist/src/client/modify/types.js +2 -1
  120. package/dist/src/client/modify/uint.d.ts +7 -0
  121. package/dist/src/client/modify/uint.js +27 -0
  122. package/dist/src/client/modify/update/index.d.ts +3 -0
  123. package/dist/src/client/modify/update/index.js +75 -0
  124. package/dist/src/client/modify/upsert/index.d.ts +3 -0
  125. package/dist/src/client/modify/{upsert.js → upsert/index.js} +4 -4
  126. package/dist/src/client/modify/validate.d.ts +6 -0
  127. package/dist/src/client/modify/validate.js +24 -0
  128. package/dist/src/client/query/BasedDbQuery.d.ts +18 -15
  129. package/dist/src/client/query/BasedDbQuery.js +93 -96
  130. package/dist/src/client/query/{BasedIterable.d.ts → BasedQueryResponse.d.ts} +5 -4
  131. package/dist/src/client/query/{BasedIterable.js → BasedQueryResponse.js} +15 -10
  132. package/dist/src/client/query/aggregates/aggregation.d.ts +5 -3
  133. package/dist/src/client/query/aggregates/aggregation.js +105 -48
  134. package/dist/src/client/query/aggregates/types.d.ts +34 -12
  135. package/dist/src/client/query/aggregates/types.js +30 -12
  136. package/dist/src/client/query/debug.js +1 -1
  137. package/dist/src/client/query/display.d.ts +1 -1
  138. package/dist/src/client/query/display.js +21 -4
  139. package/dist/src/client/query/filter/FilterBranch.js +2 -2
  140. package/dist/src/client/query/filter/convertFilter.d.ts +3 -2
  141. package/dist/src/client/query/filter/convertFilter.js +13 -1
  142. package/dist/src/client/query/filter/createFixedFilterBuffer.js +16 -30
  143. package/dist/src/client/query/filter/createReferenceFilter.js +6 -20
  144. package/dist/src/client/query/filter/createVariableFilterBuffer.js +15 -32
  145. package/dist/src/client/query/filter/parseFilterValue.js +3 -11
  146. package/dist/src/client/query/filter/primitiveFilter.js +1 -1
  147. package/dist/src/client/query/filter/toBuffer.js +9 -15
  148. package/dist/src/client/query/filter/types.d.ts +6 -6
  149. package/dist/src/client/query/filter/types.js +8 -8
  150. package/dist/src/client/query/include/include.d.ts +3 -0
  151. package/dist/src/client/query/include/include.js +58 -0
  152. package/dist/src/client/query/include/props.d.ts +7 -7
  153. package/dist/src/client/query/include/props.js +26 -21
  154. package/dist/src/client/query/include/toByteCode.js +188 -0
  155. package/dist/src/client/query/include/utils.d.ts +3 -1
  156. package/dist/src/client/query/include/utils.js +13 -0
  157. package/dist/src/client/query/include/walk.d.ts +2 -2
  158. package/dist/src/client/query/include/walk.js +38 -23
  159. package/dist/src/client/query/query.d.ts +2 -3
  160. package/dist/src/client/query/query.js +2 -3
  161. package/dist/src/client/query/queryDef.d.ts +4 -2
  162. package/dist/src/client/query/queryDef.js +3 -5
  163. package/dist/src/client/query/queryDefToReadSchema.d.ts +3 -0
  164. package/dist/src/client/query/queryDefToReadSchema.js +126 -0
  165. package/dist/src/client/query/registerQuery.js +15 -4
  166. package/dist/src/client/query/search/index.d.ts +2 -1
  167. package/dist/src/client/query/search/index.js +9 -2
  168. package/dist/src/client/query/sort.js +3 -0
  169. package/dist/src/client/query/subscription/index.d.ts +1 -1
  170. package/dist/src/client/query/subscription/index.js +2 -2
  171. package/dist/src/client/query/subscription/types.d.ts +1 -1
  172. package/dist/src/client/query/thresholds.d.ts +1 -1
  173. package/dist/src/client/query/thresholds.js +1 -1
  174. package/dist/src/client/query/toByteCode/{toBuffer.js → toByteCode.js} +8 -8
  175. package/dist/src/client/query/types.d.ts +40 -25
  176. package/dist/src/client/query/types.js +2 -6
  177. package/dist/src/client/query/validation.d.ts +6 -0
  178. package/dist/src/client/query/validation.js +32 -8
  179. package/dist/src/client/setLocalClientSchema.js +6 -10
  180. package/dist/src/client/string.d.ts +3 -7
  181. package/dist/src/client/string.js +18 -47
  182. package/dist/src/hooks.d.ts +2 -3
  183. package/dist/src/hooks.js +14 -9
  184. package/dist/src/index.d.ts +3 -5
  185. package/dist/src/index.js +4 -6
  186. package/dist/src/native.d.ts +3 -2
  187. package/dist/src/native.js +9 -8
  188. package/dist/src/server/IoWorker.js +2 -3
  189. package/dist/src/server/QueryWorker.js +1 -1
  190. package/dist/src/server/blocks.js +5 -4
  191. package/dist/src/server/dbHash.js +1 -1
  192. package/dist/src/server/index.d.ts +4 -6
  193. package/dist/src/server/index.js +63 -96
  194. package/dist/src/server/migrate/index.d.ts +2 -4
  195. package/dist/src/server/migrate/index.js +31 -16
  196. package/dist/src/server/migrate/worker.js +2 -27
  197. package/dist/src/server/save.js +10 -8
  198. package/dist/src/server/schema.js +17 -25
  199. package/dist/src/server/start.d.ts +1 -0
  200. package/dist/src/server/start.js +18 -10
  201. package/dist/src/server/tree.d.ts +20 -0
  202. package/dist/src/server/tree.js +17 -2
  203. package/dist/src/server/workers/DbWorker.d.ts +1 -0
  204. package/dist/src/server/workers/DbWorker.js +8 -1
  205. package/dist/src/server/workers/io_worker.js +1 -1
  206. package/dist/src/server/workers/worker.js +9 -4
  207. package/dist/src/table.d.ts +4 -0
  208. package/dist/src/table.js +97 -0
  209. package/dist/src/types.d.ts +1 -1
  210. package/package.json +18 -17
  211. package/dist/lib/darwin_aarch64/include/selva/worker_ctx.h +0 -29
  212. package/dist/lib/linux_aarch64/include/selva/worker_ctx.h +0 -29
  213. package/dist/lib/linux_x86_64/include/selva/worker_ctx.h +0 -29
  214. package/dist/src/client/flushModify.d.ts +0 -36
  215. package/dist/src/client/flushModify.js +0 -197
  216. package/dist/src/client/modify/ModifyRes.d.ts +0 -23
  217. package/dist/src/client/modify/ModifyRes.js +0 -101
  218. package/dist/src/client/modify/alias.d.ts +0 -4
  219. package/dist/src/client/modify/alias.js +0 -57
  220. package/dist/src/client/modify/binary.d.ts +0 -6
  221. package/dist/src/client/modify/binary.js +0 -80
  222. package/dist/src/client/modify/cardinality.d.ts +0 -6
  223. package/dist/src/client/modify/cardinality.js +0 -63
  224. package/dist/src/client/modify/create.d.ts +0 -5
  225. package/dist/src/client/modify/create.js +0 -228
  226. package/dist/src/client/modify/delete.d.ts +0 -4
  227. package/dist/src/client/modify/delete.js +0 -47
  228. package/dist/src/client/modify/expire.d.ts +0 -3
  229. package/dist/src/client/modify/expire.js +0 -25
  230. package/dist/src/client/modify/fixed.d.ts +0 -5
  231. package/dist/src/client/modify/fixed.js +0 -235
  232. package/dist/src/client/modify/index.d.ts +0 -4
  233. package/dist/src/client/modify/index.js +0 -4
  234. package/dist/src/client/modify/json.d.ts +0 -4
  235. package/dist/src/client/modify/json.js +0 -26
  236. package/dist/src/client/modify/modify.d.ts +0 -5
  237. package/dist/src/client/modify/modify.js +0 -134
  238. package/dist/src/client/modify/references/appendEdgeRefs.d.ts +0 -4
  239. package/dist/src/client/modify/references/appendEdgeRefs.js +0 -33
  240. package/dist/src/client/modify/references/edge.d.ts +0 -5
  241. package/dist/src/client/modify/references/edge.js +0 -322
  242. package/dist/src/client/modify/references/getEdgeSize.d.ts +0 -3
  243. package/dist/src/client/modify/references/getEdgeSize.js +0 -27
  244. package/dist/src/client/modify/references/reference.d.ts +0 -11
  245. package/dist/src/client/modify/references/reference.js +0 -112
  246. package/dist/src/client/modify/references/references.d.ts +0 -16
  247. package/dist/src/client/modify/references/references.js +0 -308
  248. package/dist/src/client/modify/setCursor.d.ts +0 -5
  249. package/dist/src/client/modify/setCursor.js +0 -33
  250. package/dist/src/client/modify/string.d.ts +0 -5
  251. package/dist/src/client/modify/string.js +0 -71
  252. package/dist/src/client/modify/text.d.ts +0 -8
  253. package/dist/src/client/modify/text.js +0 -69
  254. package/dist/src/client/modify/update.d.ts +0 -6
  255. package/dist/src/client/modify/update.js +0 -90
  256. package/dist/src/client/modify/upsert.d.ts +0 -3
  257. package/dist/src/client/modify/vector.d.ts +0 -4
  258. package/dist/src/client/modify/vector.js +0 -53
  259. package/dist/src/client/query/include/toBuffer.js +0 -123
  260. package/dist/src/client/query/read/read.d.ts +0 -9
  261. package/dist/src/client/query/read/read.js +0 -483
  262. /package/dist/src/client/query/include/{toBuffer.d.ts → toByteCode.d.ts} +0 -0
  263. /package/dist/src/client/query/toByteCode/{toBuffer.d.ts → toByteCode.d.ts} +0 -0
@@ -1,19 +1,18 @@
1
- import { filter, sort, filterOr, isAlias, includeField, includeFields, addAggregate, groupBy, } from './query.js';
2
- import { BasedQueryResponse } from './BasedIterable.js';
3
- import { createOrGetEdgeRefQueryDef, createOrGetRefQueryDef, } from './include/utils.js';
1
+ import { filter, sort, filterOr, isAlias, addAggregate, groupBy, } from './query.js';
2
+ import { BasedQueryResponse } from './BasedQueryResponse.js';
4
3
  import { FilterBranch } from './filter/FilterBranch.js';
5
4
  import { search, vectorSearch } from './search/index.js';
6
5
  import native from '../../native.js';
7
- import { REFERENCE, REFERENCES } from '@based/schema/def';
8
6
  import { subscribe } from './subscription/index.js';
9
7
  import { registerQuery } from './registerQuery.js';
10
8
  import { langCodesMap } from '@based/schema';
11
9
  import { convertFilter } from './filter/convertFilter.js';
12
10
  import { validateLocale, validateRange } from './validation.js';
13
11
  import { DEF_RANGE_PROP_LIMIT } from './thresholds.js';
14
- import { AggregateType } from './aggregates/types.js';
15
12
  import { displayTarget } from './display.js';
16
13
  import picocolors from 'picocolors';
14
+ import { include } from './include/include.js';
15
+ import { AggregateType } from '@based/protocol/db-read';
17
16
  export class QueryBranch {
18
17
  db;
19
18
  def;
@@ -43,7 +42,7 @@ export class QueryBranch {
43
42
  });
44
43
  }
45
44
  else {
46
- const f = convertFilter(this.def, field, operator, value, opts);
45
+ const f = convertFilter(this, field, operator, value, opts);
47
46
  if (!f) {
48
47
  // @ts-ignore
49
48
  return this;
@@ -53,19 +52,6 @@ export class QueryBranch {
53
52
  // @ts-ignore
54
53
  return this;
55
54
  }
56
- filterBatch(f) {
57
- if (this.queryCommands) {
58
- this.queryCommands.push({
59
- method: 'filterBatch',
60
- args: [f],
61
- });
62
- }
63
- else {
64
- filter(this.db, this.def, f, this.def.filter);
65
- }
66
- // @ts-ignore
67
- return this;
68
- }
69
55
  search(query, field, opts, ...fields) {
70
56
  if (this.queryCommands) {
71
57
  this.queryCommands.push({
@@ -102,7 +88,7 @@ export class QueryBranch {
102
88
  }
103
89
  if (fields.length) {
104
90
  if (fields.length === 1) {
105
- search(this.def, query, fields[0]);
91
+ search(this, query, fields[0]);
106
92
  }
107
93
  else {
108
94
  const s = {};
@@ -119,31 +105,37 @@ export class QueryBranch {
119
105
  Object.assign(s, f);
120
106
  }
121
107
  }
122
- search(this.def, query, s);
108
+ search(this, query, s);
123
109
  }
124
110
  }
125
111
  else {
126
- search(this.def, query);
112
+ search(this, query);
127
113
  }
128
114
  }
129
115
  // @ts-ignore
130
116
  return this;
131
117
  }
132
- groupBy(field) {
118
+ groupBy(field, step) {
133
119
  if (this.queryCommands) {
134
120
  this.queryCommands.push({
135
121
  method: 'groupBy',
136
- args: [field],
122
+ args: [field, step],
137
123
  });
138
124
  }
139
125
  else {
140
- groupBy(this.def, field);
126
+ const groupByHook = this.def.schema.hooks?.groupBy;
127
+ if (groupByHook) {
128
+ this.def.schema.hooks.groupBy = null;
129
+ groupByHook(this, field);
130
+ this.def.schema.hooks.groupBy = groupByHook;
131
+ }
132
+ groupBy(this.def, field, step);
141
133
  }
142
134
  // only works with aggregates for now
143
135
  // @ts-ignore
144
136
  return this;
145
137
  }
146
- count(field = '$count') {
138
+ count(field = 'count') {
147
139
  if (this.queryCommands) {
148
140
  this.queryCommands.push({
149
141
  method: 'count',
@@ -152,7 +144,7 @@ export class QueryBranch {
152
144
  }
153
145
  else {
154
146
  const p = field.split('.');
155
- addAggregate(AggregateType.COUNT, this.def, p);
147
+ addAggregate(this, AggregateType.COUNT, p);
156
148
  }
157
149
  // @ts-ignore
158
150
  return this;
@@ -168,55 +160,77 @@ export class QueryBranch {
168
160
  });
169
161
  }
170
162
  else {
171
- addAggregate(AggregateType.SUM, this.def, fields);
163
+ addAggregate(this, AggregateType.SUM, fields);
172
164
  }
173
165
  // @ts-ignore
174
166
  return this;
175
167
  }
176
- cardinality(...fields) {
177
- if (fields.length === 0) {
168
+ cardinality(field) {
169
+ if (field.length === 0) {
178
170
  throw new Error('Empty cardinality() called');
179
171
  }
180
172
  if (this.queryCommands) {
181
173
  this.queryCommands.push({
182
174
  method: 'cardinality',
183
- args: fields,
175
+ args: [field],
184
176
  });
185
177
  }
186
178
  else {
187
- addAggregate(AggregateType.CARDINALITY, this.def, fields);
179
+ addAggregate(this, AggregateType.CARDINALITY, [field]);
188
180
  }
189
181
  // @ts-ignore
190
182
  return this;
191
183
  }
192
- stddev(...fields) {
193
- if (fields.length === 0) {
194
- throw new Error('Empty standard deviation function called');
195
- }
184
+ stddev(...args) {
196
185
  if (this.queryCommands) {
197
186
  this.queryCommands.push({
198
187
  method: 'stddev',
199
- args: fields,
188
+ args,
200
189
  });
201
190
  }
202
191
  else {
203
- addAggregate(AggregateType.STDDEV, this.def, fields);
192
+ let option = {};
193
+ let fields;
194
+ const lastArg = args[args.length - 1];
195
+ const lastArgIsOptions = typeof lastArg === 'object' && lastArg !== null;
196
+ if (lastArgIsOptions) {
197
+ option = lastArg;
198
+ fields = args.slice(0, -1);
199
+ }
200
+ else {
201
+ fields = args;
202
+ }
203
+ if (fields.length === 0) {
204
+ throw new Error('Empty standard deviation function called');
205
+ }
206
+ addAggregate(this, AggregateType.STDDEV, fields, option);
204
207
  }
205
208
  // @ts-ignore
206
209
  return this;
207
210
  }
208
- var(...fields) {
209
- if (fields.length === 0) {
210
- throw new Error('Empty variance function called');
211
- }
211
+ var(...args) {
212
212
  if (this.queryCommands) {
213
213
  this.queryCommands.push({
214
214
  method: 'var',
215
- args: fields,
215
+ args,
216
216
  });
217
217
  }
218
218
  else {
219
- addAggregate(AggregateType.VARIANCE, this.def, fields);
219
+ let option = {};
220
+ let fields = [];
221
+ const lastArg = args[args.length - 1];
222
+ const lastArgIsOptions = typeof lastArg === 'object' && lastArg !== null;
223
+ if (lastArgIsOptions) {
224
+ option = lastArg;
225
+ fields = args.slice(0, -1);
226
+ }
227
+ else {
228
+ fields = args;
229
+ }
230
+ if (fields.length === 0) {
231
+ throw new Error('Empty variance called');
232
+ }
233
+ addAggregate(this, AggregateType.VARIANCE, fields, option);
220
234
  }
221
235
  // @ts-ignore
222
236
  return this;
@@ -232,7 +246,23 @@ export class QueryBranch {
232
246
  });
233
247
  }
234
248
  else {
235
- addAggregate(AggregateType.AVERAGE, this.def, fields);
249
+ addAggregate(this, AggregateType.AVERAGE, fields);
250
+ }
251
+ // @ts-ignore
252
+ return this;
253
+ }
254
+ harmonicMean(...fields) {
255
+ if (fields.length === 0) {
256
+ throw new Error('Empty harmonic mean function called');
257
+ }
258
+ if (this.queryCommands) {
259
+ this.queryCommands.push({
260
+ method: 'harmonicMean',
261
+ args: fields,
262
+ });
263
+ }
264
+ else {
265
+ addAggregate(this, AggregateType.HMEAN, fields);
236
266
  }
237
267
  // @ts-ignore
238
268
  return this;
@@ -248,7 +278,7 @@ export class QueryBranch {
248
278
  });
249
279
  }
250
280
  else {
251
- addAggregate(AggregateType.MAX, this.def, fields);
281
+ addAggregate(this, AggregateType.MAX, fields);
252
282
  }
253
283
  // @ts-ignore
254
284
  return this;
@@ -264,7 +294,7 @@ export class QueryBranch {
264
294
  });
265
295
  }
266
296
  else {
267
- addAggregate(AggregateType.MIN, this.def, fields);
297
+ addAggregate(this, AggregateType.MIN, fields);
268
298
  }
269
299
  // @ts-ignore
270
300
  return this;
@@ -283,7 +313,7 @@ export class QueryBranch {
283
313
  this.def.filter.size += f.filterBranch.size;
284
314
  }
285
315
  else {
286
- const f = convertFilter(this.def, field, operator, value, opts);
316
+ const f = convertFilter(this, field, operator, value, opts);
287
317
  if (f) {
288
318
  filterOr(this.db, this.def, f, this.def.filter);
289
319
  }
@@ -316,49 +346,12 @@ export class QueryBranch {
316
346
  this.queryCommands.push({ method: 'include', args: fields });
317
347
  }
318
348
  else {
319
- for (const f of fields) {
320
- if (typeof f === 'string') {
321
- includeField(this.def, f);
322
- }
323
- else if (typeof f === 'function') {
324
- f((field) => {
325
- if (field[0] == '$') {
326
- // @ts-ignore
327
- const prop = this.def.target?.propDef?.edges[field];
328
- if (prop &&
329
- (prop.typeIndex === REFERENCE || prop.typeIndex === REFERENCES)) {
330
- const refDef = createOrGetEdgeRefQueryDef(this.db, this.def, prop);
331
- // @ts-ignore
332
- return new QueryBranch(this.db, refDef);
333
- }
334
- throw new Error(`No edge reference or edge references field named "${field}"`);
335
- }
336
- else {
337
- const prop = field[0] == '$'
338
- ? // @ts-ignore
339
- this.def.target?.propDef?.edges[field]
340
- : this.def.props[field];
341
- if (prop &&
342
- (prop.typeIndex === REFERENCE || prop.typeIndex === REFERENCES)) {
343
- const refDef = createOrGetRefQueryDef(this.db, this.def, prop);
344
- // @ts-ignore
345
- return new QueryBranch(this.db, refDef);
346
- }
347
- throw new Error(`No reference or references field named "${field}"`);
348
- }
349
- });
350
- }
351
- else if (Array.isArray(f)) {
352
- if (f.length === 0) {
353
- includeFields(this.def, ['id']);
354
- }
355
- else {
356
- includeFields(this.def, f);
357
- }
358
- }
359
- else if (f !== undefined) {
360
- throw new Error('Invalid include statement: expected props, refs and edges (string or array) or function');
361
- }
349
+ include(this, fields);
350
+ const includeHook = this.def.schema.hooks?.include;
351
+ if (includeHook) {
352
+ this.def.schema.hooks.include = null;
353
+ includeHook(this, this.def.include.stringFields);
354
+ this.def.schema.hooks.include = includeHook;
362
355
  }
363
356
  }
364
357
  // @ts-ignore
@@ -367,14 +360,14 @@ export class QueryBranch {
367
360
  }
368
361
  export class BasedDbReferenceQuery extends QueryBranch {
369
362
  }
370
- const resToJSON = (res) => res.toJSON();
363
+ const resToJSON = (res, replacer, space) => res.toJSON(replacer, space);
371
364
  const resToObject = (res) => res.toObject();
372
365
  class GetPromise extends Promise {
373
366
  toObject() {
374
367
  return this.then(resToObject);
375
368
  }
376
- toJSON() {
377
- return this.then(resToJSON);
369
+ toJSON(replacer, space) {
370
+ return this.then((res) => resToJSON(res, replacer, space));
378
371
  }
379
372
  inspect(depth, raw) {
380
373
  return this.then((res) => new GetPromise((resolve) => resolve(res.inspect(depth, raw))));
@@ -383,6 +376,7 @@ class GetPromise extends Promise {
383
376
  export class BasedDbQuery extends QueryBranch {
384
377
  skipValidation = false;
385
378
  target;
379
+ readSchema;
386
380
  constructor(db, type, rawTarget, skipValidation) {
387
381
  const target = {
388
382
  type,
@@ -419,6 +413,9 @@ export class BasedDbQuery extends QueryBranch {
419
413
  if (!this.db.schema) {
420
414
  await this.db.once('schema');
421
415
  }
416
+ if ('id' in this.target) {
417
+ this.target.id = await this.target.id;
418
+ }
422
419
  buf = registerQuery(this);
423
420
  }
424
421
  catch (err) {
@@ -456,7 +453,7 @@ export class BasedDbQuery extends QueryBranch {
456
453
  reject(res);
457
454
  }
458
455
  else {
459
- resolve(new BasedQueryResponse(this.id, this.def, res, performance.now() - d));
456
+ resolve(new BasedQueryResponse(this.def, res, performance.now() - d));
460
457
  }
461
458
  };
462
459
  // if !id not initialized yet
@@ -514,7 +511,7 @@ export class BasedDbQuery extends QueryBranch {
514
511
  const buf = registerQuery(this);
515
512
  const d = performance.now();
516
513
  const res = native.getQueryBuf(buf, dbCtxExternal);
517
- return new BasedQueryResponse(this.id, this.def, new Uint8Array(res), performance.now() - d);
514
+ return new BasedQueryResponse(this.def, new Uint8Array(res), performance.now() - d);
518
515
  }
519
516
  }
520
517
  //# sourceMappingURL=BasedDbQuery.js.map
@@ -1,15 +1,16 @@
1
1
  import { inspect } from 'node:util';
2
2
  import { QueryDef } from './types.js';
3
- import { Item } from './query.js';
4
3
  import { size, time, inspectData } from './display.js';
4
+ import { Item } from '@based/protocol/db-read';
5
5
  export { time, size, inspectData };
6
6
  export declare class BasedQueryResponse {
7
7
  result: Uint8Array;
8
8
  def: QueryDef;
9
9
  execTime: number;
10
10
  end: number;
11
- id: number;
12
- constructor(id: number, def: QueryDef, result: Uint8Array, execTime: number, end?: number);
11
+ constructor(def: QueryDef, result: Uint8Array, execTime: number, end?: number);
12
+ get id(): number;
13
+ get queryId(): number;
13
14
  get version(): number;
14
15
  get size(): number;
15
16
  [inspect.custom](depth: number): string;
@@ -22,6 +23,6 @@ export declare class BasedQueryResponse {
22
23
  get checksum(): number;
23
24
  get length(): number;
24
25
  toObject(): any;
25
- toJSON(): string;
26
+ toJSON(replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;
26
27
  toString(): string;
27
28
  }
@@ -1,8 +1,9 @@
1
1
  import { inspect } from 'node:util';
2
2
  import picocolors from 'picocolors';
3
- import { debug, resultToObject, readAllFields } from './query.js';
3
+ import { debug } from './query.js';
4
4
  import { size, time, inspectData, defHasId, displayTarget } from './display.js';
5
- import { readFloatLE, readUint32 } from '@saulx/utils';
5
+ import { readFloatLE, readUint32 } from '@based/utils';
6
+ import { resultToObject, readProps, readId, } from '@based/protocol/db-read';
6
7
  export { time, size, inspectData };
7
8
  const BITS_FOR_BYTE_LEN = 21;
8
9
  const FACTOR = 2 ** BITS_FOR_BYTE_LEN;
@@ -12,14 +13,18 @@ export class BasedQueryResponse {
12
13
  def;
13
14
  execTime;
14
15
  end;
15
- id;
16
- constructor(id, def, result, execTime, end = result.length) {
17
- this.id = id;
16
+ constructor(def, result, execTime, end = result.length) {
18
17
  this.def = def;
19
18
  this.result = result;
20
19
  this.execTime = execTime;
21
20
  this.end = end;
22
21
  }
22
+ get id() {
23
+ return readId(this.def.readSchema, this.result);
24
+ }
25
+ get queryId() {
26
+ return this.def.queryId;
27
+ }
23
28
  get version() {
24
29
  return (this.checksum >>> 0) * FACTOR + (this.result.byteLength & MASK_B);
25
30
  }
@@ -72,7 +77,7 @@ export class BasedQueryResponse {
72
77
  item.$searchScore = readFloatLE(result, i);
73
78
  i += 4;
74
79
  }
75
- const l = readAllFields(this.def, result, i, result.byteLength - 4, item, id);
80
+ const l = readProps(this.def.readSchema, result, i, result.byteLength - 4, item);
76
81
  i += l;
77
82
  yield item;
78
83
  }
@@ -110,14 +115,14 @@ export class BasedQueryResponse {
110
115
  return l;
111
116
  }
112
117
  toObject() {
113
- return resultToObject(this.def, this.result, this.end - 4, 0);
118
+ return resultToObject(this.def.readSchema, this.result, this.end - 4, 0);
114
119
  }
115
- toJSON() {
120
+ toJSON(replacer, space) {
116
121
  // TODO: optimize
117
- return JSON.stringify(this.toObject());
122
+ return JSON.stringify(this.toObject(), replacer, space);
118
123
  }
119
124
  toString() {
120
125
  return this.toJSON();
121
126
  }
122
127
  }
123
- //# sourceMappingURL=BasedIterable.js.map
128
+ //# sourceMappingURL=BasedQueryResponse.js.map
@@ -1,6 +1,8 @@
1
1
  import { QueryDef, QueryDefAggregation } from '../types.js';
2
- import { AggregateType } from './types.js';
2
+ import { StepInput, aggFnOptions } from './types.js';
3
+ import { QueryBranch } from '../BasedDbQuery.js';
4
+ import { AggregateType } from '@based/protocol/db-read';
3
5
  export declare const aggregateToBuffer: (aggregates: QueryDefAggregation) => Uint8Array;
4
- export declare const groupBy: (def: QueryDef, field: string) => void;
5
- export declare const addAggregate: (type: AggregateType, def: QueryDef, fields: (string | string[])[]) => void;
6
+ export declare const groupBy: (def: QueryDef, field: string, StepInput: StepInput) => void;
7
+ export declare const addAggregate: (query: QueryBranch<any>, type: AggregateType, fields: string[], option?: aggFnOptions) => void;
6
8
  export declare const isRootCountOnly: (def: QueryDef, filterSize: number) => boolean;
@@ -1,9 +1,10 @@
1
- import { writeUint16 } from '@saulx/utils';
1
+ import { writeUint16, writeInt16, writeUint32 } from '@based/utils';
2
2
  import { QueryDefType } from '../types.js';
3
- import { AggregateType } from './types.js';
3
+ import { setMode } from './types.js';
4
4
  import { UINT32 } from '@based/schema/def';
5
- import { aggregationFieldDoesNotExist } from '../validation.js';
6
- import { aggregateTypeMap } from '../aggregates/types.js';
5
+ import { aggregationFieldDoesNotExist, validateStepRange, } from '../validation.js';
6
+ import { aggregateTypeMap, Interval, } from '../aggregates/types.js';
7
+ import { AggregateType } from '@based/protocol/db-read';
7
8
  export const aggregateToBuffer = (aggregates) => {
8
9
  const aggBuffer = new Uint8Array(aggregates.size);
9
10
  let i = 0;
@@ -18,6 +19,12 @@ export const aggregateToBuffer = (aggregates) => {
18
19
  i += 2;
19
20
  writeUint16(aggBuffer, aggregates.groupBy.len, i);
20
21
  i += 2;
22
+ aggBuffer[i] = aggregates.groupBy.stepType || 0;
23
+ i += 1;
24
+ writeUint32(aggBuffer, aggregates.groupBy.stepRange || 0, i);
25
+ i += 4;
26
+ writeInt16(aggBuffer, aggregates.groupBy.tz || 0, i);
27
+ i += 2;
21
28
  }
22
29
  else {
23
30
  aggBuffer[i] = 0 /* GroupBy.NONE */;
@@ -27,6 +34,8 @@ export const aggregateToBuffer = (aggregates) => {
27
34
  i += 2;
28
35
  writeUint16(aggBuffer, aggregates.totalAccumulatorSize, i);
29
36
  i += 2;
37
+ aggBuffer[i] = setMode[aggregates?.option?.mode] || 0;
38
+ i += 1;
30
39
  for (const [prop, aggregatesArray] of aggregates.aggregates.entries()) {
31
40
  aggBuffer[i] = prop;
32
41
  i += 1;
@@ -54,71 +63,100 @@ export const aggregateToBuffer = (aggregates) => {
54
63
  const ensureAggregate = (def) => {
55
64
  if (!def.aggregate) {
56
65
  def.aggregate = {
57
- size: 5,
66
+ size: 7,
58
67
  aggregates: new Map(),
59
68
  totalResultsSize: 0,
60
69
  totalAccumulatorSize: 0,
61
70
  };
62
71
  }
63
72
  };
64
- export const groupBy = (def, field) => {
73
+ export const groupBy = (def, field, StepInput) => {
65
74
  const fieldDef = def.schema.props[field];
66
75
  if (!fieldDef) {
67
76
  aggregationFieldDoesNotExist(def, field);
68
77
  }
69
78
  ensureAggregate(def);
70
79
  if (!def.aggregate.groupBy) {
71
- def.aggregate.size += 6;
80
+ def.aggregate.size += 12;
72
81
  }
73
82
  def.aggregate.groupBy = fieldDef;
83
+ def.aggregate.groupBy.stepRange = undefined;
84
+ def.aggregate.groupBy.stepType = undefined;
85
+ def.aggregate.groupBy.tz = undefined;
86
+ def.aggregate.groupBy.display = undefined;
87
+ if (typeof StepInput === 'object' &&
88
+ StepInput !== null &&
89
+ 'step' in StepInput) {
90
+ if (typeof StepInput.timeZone == 'string') {
91
+ def.aggregate.groupBy.tz = getTimeZoneOffsetInMinutes(StepInput.timeZone);
92
+ }
93
+ if (typeof StepInput?.step == 'string') {
94
+ const intervalEnumKey = StepInput.step;
95
+ def.aggregate.groupBy.stepType = Interval[intervalEnumKey];
96
+ }
97
+ else {
98
+ validateStepRange(def, StepInput?.step);
99
+ def.aggregate.groupBy.stepRange = StepInput.step;
100
+ }
101
+ }
102
+ else if (typeof StepInput == 'number') {
103
+ validateStepRange(def, StepInput);
104
+ def.aggregate.groupBy.stepRange = StepInput;
105
+ }
106
+ else {
107
+ const intervalEnumKey = StepInput;
108
+ def.aggregate.groupBy.stepType = Interval[intervalEnumKey];
109
+ }
110
+ if (typeof StepInput === 'object' && StepInput?.display) {
111
+ def.aggregate.groupBy.display = StepInput?.display;
112
+ }
74
113
  };
75
- export const addAggregate = (type, def, fields) => {
114
+ export const addAggregate = (query, type, fields, option) => {
115
+ const def = query.def;
116
+ def.schema.hooks?.aggregate?.(def, new Set(fields));
76
117
  ensureAggregate(def);
118
+ if (option?.mode)
119
+ def.aggregate.option = option;
77
120
  const aggregates = def.aggregate.aggregates;
78
121
  for (const field of fields) {
79
- if (Array.isArray(field)) {
80
- addAggregate(type, def, field);
122
+ const fieldDef = type === AggregateType.COUNT
123
+ ? {
124
+ prop: 255,
125
+ path: [field],
126
+ __isPropDef: true,
127
+ len: 4,
128
+ start: 0,
129
+ typeIndex: UINT32,
130
+ separate: true,
131
+ validation: () => true,
132
+ default: 0,
133
+ }
134
+ : def.schema.props[field];
135
+ if (!fieldDef) {
136
+ aggregationFieldDoesNotExist(def, field);
137
+ }
138
+ if (!aggregates.get(fieldDef.prop)) {
139
+ aggregates.set(fieldDef.prop, []);
140
+ def.aggregate.size += 3;
141
+ }
142
+ const aggregateField = aggregates.get(fieldDef.prop);
143
+ aggregateField.push({
144
+ propDef: fieldDef,
145
+ type,
146
+ resultPos: def.aggregate.totalResultsSize,
147
+ accumulatorPos: def.aggregate.totalAccumulatorSize,
148
+ });
149
+ const specificSizes = aggregateTypeMap.get(type);
150
+ if (specificSizes) {
151
+ def.aggregate.totalResultsSize += specificSizes.resultsSize;
152
+ def.aggregate.totalAccumulatorSize += specificSizes.accumulatorSize;
81
153
  }
82
154
  else {
83
- const fieldDef = type === AggregateType.COUNT
84
- ? {
85
- prop: 255,
86
- path: [field],
87
- __isPropDef: true,
88
- len: 4,
89
- start: 0,
90
- typeIndex: UINT32,
91
- separate: true,
92
- validation: () => true,
93
- default: 0,
94
- }
95
- : def.schema.props[field];
96
- if (!fieldDef) {
97
- aggregationFieldDoesNotExist(def, field);
98
- }
99
- if (!aggregates.get(fieldDef.prop)) {
100
- aggregates.set(fieldDef.prop, []);
101
- def.aggregate.size += 3;
102
- }
103
- const aggregateField = aggregates.get(fieldDef.prop);
104
- aggregateField.push({
105
- propDef: fieldDef,
106
- type,
107
- resultPos: def.aggregate.totalResultsSize,
108
- accumulatorPos: def.aggregate.totalAccumulatorSize,
109
- });
110
- const specificSizes = aggregateTypeMap.get(type);
111
- if (specificSizes) {
112
- def.aggregate.totalResultsSize += specificSizes.resultsSize;
113
- def.aggregate.totalAccumulatorSize += specificSizes.accumulatorSize;
114
- }
115
- else {
116
- def.aggregate.totalResultsSize += 8;
117
- def.aggregate.totalAccumulatorSize += 8;
118
- }
119
- // needs to add an extra field WRITE TO
120
- def.aggregate.size += 8;
155
+ def.aggregate.totalResultsSize += 8;
156
+ def.aggregate.totalAccumulatorSize += 8;
121
157
  }
158
+ // needs to add an extra field WRITE TO
159
+ def.aggregate.size += 8;
122
160
  }
123
161
  };
124
162
  export const isRootCountOnly = (def, filterSize) => {
@@ -149,4 +187,23 @@ export const isRootCountOnly = (def, filterSize) => {
149
187
  }
150
188
  return true;
151
189
  };
190
+ function getTimeZoneOffsetInMinutes(timeZone, date = new Date()) {
191
+ const formatter = new Intl.DateTimeFormat('en-US', {
192
+ timeZone,
193
+ year: 'numeric',
194
+ month: 'numeric',
195
+ day: 'numeric',
196
+ hour: 'numeric',
197
+ minute: 'numeric',
198
+ second: 'numeric',
199
+ hour12: false,
200
+ });
201
+ const parts = formatter.formatToParts(date);
202
+ const getPart = (partName) => parseInt(parts.find((p) => p.type === partName)?.value || '0', 10);
203
+ const targetTimeAsUTC = Date.UTC(getPart('year'), getPart('month') - 1, getPart('day'), getPart('hour'), getPart('minute'), getPart('second'));
204
+ const originalUTCTime = date.getTime();
205
+ const offsetInMilliseconds = targetTimeAsUTC - originalUTCTime;
206
+ const offsetInMinutes = offsetInMilliseconds / (1000 * 60);
207
+ return Math.round(offsetInMinutes);
208
+ }
152
209
  //# sourceMappingURL=aggregation.js.map