@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
@@ -0,0 +1,51 @@
1
+ import { DELETE, DELETE_TEXT_FIELD, RANGE_ERR, UPDATE, } from '../types.js';
2
+ import { FULL_CURSOR_SIZE, PROP_CURSOR_SIZE, writePropCursor, } from '../cursor.js';
3
+ import { reserve } from '../resize.js';
4
+ import { ENCODER, writeUint32 } from '@based/utils';
5
+ import { write } from '../../string.js';
6
+ import { writeU8, writeU8Array } from '../uint.js';
7
+ import { markString } from '../create/mark.js';
8
+ import { validate } from '../validate.js';
9
+ export const deleteString = (ctx, def, lang) => {
10
+ if (ctx.operation !== UPDATE) {
11
+ return;
12
+ }
13
+ if (!lang) {
14
+ reserve(ctx, PROP_CURSOR_SIZE + 1);
15
+ writePropCursor(ctx, def);
16
+ writeU8(ctx, DELETE);
17
+ }
18
+ else {
19
+ reserve(ctx, PROP_CURSOR_SIZE + 2);
20
+ writePropCursor(ctx, def);
21
+ writeU8(ctx, DELETE_TEXT_FIELD);
22
+ writeU8(ctx, lang);
23
+ }
24
+ };
25
+ export const writeString = (ctx, def, val, lang) => {
26
+ const isUint8 = val instanceof Uint8Array;
27
+ if (val === null || val === '' || (isUint8 && val.byteLength === 0)) {
28
+ deleteString(ctx, def, lang);
29
+ return;
30
+ }
31
+ validate(def, val);
32
+ let size = isUint8 ? val.byteLength : ENCODER.encode(val).byteLength + 6;
33
+ reserve(ctx, FULL_CURSOR_SIZE + 11 + size);
34
+ writePropCursor(ctx, def);
35
+ writeU8(ctx, ctx.operation);
36
+ const index = ctx.index;
37
+ ctx.index += 4;
38
+ if (isUint8) {
39
+ writeU8Array(ctx, val);
40
+ }
41
+ else {
42
+ size = write(ctx, val, ctx.index, def.compression === 0, lang);
43
+ if (size === null) {
44
+ throw RANGE_ERR;
45
+ }
46
+ ctx.index += size;
47
+ }
48
+ writeUint32(ctx.array, size, index);
49
+ markString(ctx, def);
50
+ };
51
+ //# sourceMappingURL=string.js.map
@@ -0,0 +1,3 @@
1
+ import { PropDef } from '@based/schema/def';
2
+ import { Ctx } from '../Ctx.js';
3
+ export declare const writeText: (ctx: Ctx, def: PropDef, val: any) => void;
@@ -0,0 +1,35 @@
1
+ import { deleteString, writeString } from './string.js';
2
+ import { markTextValue, markTextObj } from '../create/mark.js';
3
+ export const writeText = (ctx, def, val) => {
4
+ if (val === null) {
5
+ deleteString(ctx, def, ctx.locale);
6
+ return;
7
+ }
8
+ if (typeof val === 'string' || val instanceof Uint8Array) {
9
+ if (!ctx.locale) {
10
+ throw [def, val];
11
+ }
12
+ if (!ctx.schema.separateTextSort.localeToIndex.has(ctx.locale)) {
13
+ throw [def, val, 'Invalid locale'];
14
+ }
15
+ writeString(ctx, def, val, ctx.locale);
16
+ markTextValue(ctx, def, ctx.locale, true);
17
+ return;
18
+ }
19
+ if (typeof val === 'object') {
20
+ markTextObj(ctx);
21
+ for (const lang in val) {
22
+ const langU8 = ctx.schema.separateTextSort.localeStringToIndex.get(lang);
23
+ if (!langU8) {
24
+ throw [def, val, 'Invalid locale'];
25
+ }
26
+ const text = val[lang];
27
+ const locale = langU8[1];
28
+ writeString(ctx, def, text, locale);
29
+ markTextValue(ctx, def, locale, false);
30
+ }
31
+ return;
32
+ }
33
+ throw [def, val];
34
+ };
35
+ //# sourceMappingURL=text.js.map
@@ -0,0 +1,3 @@
1
+ import { Ctx } from '../Ctx.js';
2
+ import { PropDef } from '@based/schema/def';
3
+ export declare const writeVector: (ctx: Ctx, def: PropDef, val: any) => void;
@@ -0,0 +1,30 @@
1
+ import { deleteProp } from './delete.js';
2
+ import { validate } from '../validate.js';
3
+ import { PROP_CURSOR_SIZE, writePropCursor } from '../cursor.js';
4
+ import { reserve } from '../resize.js';
5
+ import { writePadding, writeU32, writeU8, writeU8Array } from '../uint.js';
6
+ export const writeVector = (ctx, def, val) => {
7
+ if (val === null) {
8
+ deleteProp(ctx, def);
9
+ return;
10
+ }
11
+ validate(def, val);
12
+ if (val.byteLength === 0) {
13
+ deleteProp(ctx, def);
14
+ return;
15
+ }
16
+ reserve(ctx, PROP_CURSOR_SIZE + 9 + val.byteLength);
17
+ writePropCursor(ctx, def);
18
+ writeU8(ctx, ctx.operation);
19
+ let size = Math.min(val.byteLength, def.len);
20
+ let padding = 0;
21
+ if (ctx.index % 4 !== 0) {
22
+ padding = ctx.index % 4;
23
+ size -= padding;
24
+ }
25
+ writeU32(ctx, size + 4);
26
+ writeU8(ctx, padding);
27
+ writePadding(ctx, 3 - padding);
28
+ writeU8Array(ctx, new Uint8Array(val.buffer).subarray(0, size));
29
+ };
30
+ //# sourceMappingURL=vector.js.map
@@ -0,0 +1,3 @@
1
+ import { Ctx } from './Ctx.js';
2
+ export declare const resize: (ctx: Ctx, size: number) => void;
3
+ export declare const reserve: (ctx: Ctx, size: number) => void;
@@ -0,0 +1,20 @@
1
+ import { RANGE_ERR } from './types.js';
2
+ export const resize = (ctx, size) => {
3
+ if (size > ctx.max) {
4
+ throw RANGE_ERR;
5
+ }
6
+ if (size > ctx.size) {
7
+ const avail = ctx.array.buffer.maxByteLength;
8
+ const affix = avail - ctx.max;
9
+ const required = size + affix;
10
+ const double = Math.max(required, ctx.size * 2);
11
+ const length = Math.min(avail, double);
12
+ // @ts-ignore
13
+ ctx.array.buffer.resize(length);
14
+ ctx.size = length - affix;
15
+ }
16
+ };
17
+ export const reserve = (ctx, size) => {
18
+ resize(ctx, ctx.index + size);
19
+ };
20
+ //# sourceMappingURL=resize.js.map
@@ -1,5 +1,4 @@
1
1
  import { LangName } from '@based/schema';
2
- import { ModifyError } from './ModifyRes.js';
3
2
  export declare const CREATE = 3;
4
3
  export declare const UPDATE = 6;
5
4
  export declare const UPDATE_PARTIAL = 5;
@@ -22,7 +21,6 @@ export declare const MOD_OPS_TO_STRING: Record<ModifyOp, 'create' | 'update'>;
22
21
  export declare const enum SIZE {
23
22
  DEFAULT_CURSOR = 11
24
23
  }
25
- export type ModifyErr = typeof RANGE_ERR | ModifyError | void;
26
24
  export type ModifyOpts = {
27
25
  unsafe?: boolean;
28
26
  locale?: LangName;
@@ -36,8 +34,9 @@ export declare const NOEDGE_NOINDEX_TMPID = 4;
36
34
  export declare const EDGE_NOINDEX_TMPID = 5;
37
35
  export declare const EDGE_INDEX_TMPID = 6;
38
36
  export declare const NOEDGE_INDEX_TMPID = 7;
39
- export declare const REF_OP_UPDATE = 1;
40
37
  export declare const REF_OP_OVERWRITE = 0;
38
+ export declare const REF_OP_UPDATE = 1;
39
+ export declare const REF_OP_DELETE = 2;
41
40
  export declare const REF_OP_PUT_OVERWRITE = 3;
42
41
  export declare const REF_OP_PUT_ADD = 4;
43
- export type REF_OP = typeof REF_OP_OVERWRITE | typeof REF_OP_UPDATE;
42
+ export type RefOp = typeof REF_OP_OVERWRITE | typeof REF_OP_UPDATE;
@@ -29,8 +29,9 @@ export const NOEDGE_NOINDEX_TMPID = 4;
29
29
  export const EDGE_NOINDEX_TMPID = 5;
30
30
  export const EDGE_INDEX_TMPID = 6;
31
31
  export const NOEDGE_INDEX_TMPID = 7;
32
- export const REF_OP_UPDATE = 1;
33
32
  export const REF_OP_OVERWRITE = 0;
33
+ export const REF_OP_UPDATE = 1;
34
+ export const REF_OP_DELETE = 2;
34
35
  export const REF_OP_PUT_OVERWRITE = 3;
35
36
  export const REF_OP_PUT_ADD = 4;
36
37
  //# sourceMappingURL=types.js.map
@@ -0,0 +1,7 @@
1
+ import { Ctx } from './Ctx.js';
2
+ export declare const writeU64: (ctx: Ctx, val: number) => void;
3
+ export declare const writeU32: (ctx: Ctx, val: number) => void;
4
+ export declare const writePadding: (ctx: Ctx, padding: number) => void;
5
+ export declare const writeU16: (ctx: Ctx, val: number) => void;
6
+ export declare const writeU8: (ctx: Ctx, val: number) => void;
7
+ export declare const writeU8Array: (ctx: Ctx, val: Uint8Array) => void;
@@ -0,0 +1,27 @@
1
+ import { writeInt64, writeUint16, writeUint32 } from '@based/utils';
2
+ export const writeU64 = (ctx, val) => {
3
+ writeInt64(ctx.array, val, ctx.index);
4
+ ctx.index += 8;
5
+ };
6
+ export const writeU32 = (ctx, val) => {
7
+ writeUint32(ctx.array, val, ctx.index);
8
+ ctx.index += 4;
9
+ };
10
+ export const writePadding = (ctx, padding) => {
11
+ while (padding--) {
12
+ ctx.array[ctx.index++] = 0;
13
+ }
14
+ };
15
+ export const writeU16 = (ctx, val) => {
16
+ writeUint16(ctx.array, val, ctx.index);
17
+ ctx.index += 2;
18
+ };
19
+ export const writeU8 = (ctx, val) => {
20
+ ctx.array[ctx.index] = val;
21
+ ctx.index += 1;
22
+ };
23
+ export const writeU8Array = (ctx, val) => {
24
+ ctx.array.set(val, ctx.index);
25
+ ctx.index += val.byteLength;
26
+ };
27
+ //# sourceMappingURL=uint.js.map
@@ -0,0 +1,3 @@
1
+ import { ModifyOpts } from '../types.js';
2
+ import { DbClient } from '../../../index.js';
3
+ export declare function update(db: DbClient, type: string, id: number, payload: any, opts: ModifyOpts): Promise<number>;
@@ -0,0 +1,75 @@
1
+ import { UPDATE, UPDATE_PARTIAL } from '../types.js';
2
+ import { getValidSchema, validateId, validatePayload } from '../validate.js';
3
+ import { langCodesMap } from '@based/schema';
4
+ import { handleError } from '../error.js';
5
+ import { Tmp } from '../Tmp.js';
6
+ import { writeObject } from '../props/object.js';
7
+ import { reserve } from '../resize.js';
8
+ import { FULL_CURSOR_SIZE, PROP_CURSOR_SIZE, writeMainCursor, writeNodeCursor, writeTypeCursor, } from '../cursor.js';
9
+ import { getByPath, writeUint32 } from '@based/utils';
10
+ import { writeU16, writeU8 } from '../uint.js';
11
+ import { writeFixed } from '../props/fixed.js';
12
+ import { schedule } from '../drain.js';
13
+ const writeUpdateTs = (ctx, payload) => {
14
+ if (ctx.schema.updateTs) {
15
+ const updateTs = Date.now();
16
+ for (const def of ctx.schema.updateTs) {
17
+ if (getByPath(payload, def.path) !== undefined) {
18
+ continue;
19
+ }
20
+ ctx.main.set(def, updateTs);
21
+ }
22
+ }
23
+ };
24
+ const writeMergeMain = (ctx) => {
25
+ if (ctx.main.size) {
26
+ reserve(ctx, PROP_CURSOR_SIZE + 5 + ctx.main.size * 4);
27
+ writeMainCursor(ctx);
28
+ writeU8(ctx, UPDATE_PARTIAL);
29
+ const index = ctx.index;
30
+ ctx.index += 4;
31
+ const start = ctx.index;
32
+ for (const [def, val] of ctx.main) {
33
+ writeU16(ctx, def.start);
34
+ writeU16(ctx, def.len);
35
+ writeFixed(ctx, def, val);
36
+ }
37
+ writeUint32(ctx.array, ctx.index - start, index);
38
+ }
39
+ };
40
+ const writeUpdate = (ctx, payload) => {
41
+ reserve(ctx, FULL_CURSOR_SIZE);
42
+ writeTypeCursor(ctx);
43
+ writeNodeCursor(ctx);
44
+ writeObject(ctx, ctx.schema.tree, payload);
45
+ writeUpdateTs(ctx, payload);
46
+ writeMergeMain(ctx);
47
+ };
48
+ export function update(db, type, id, payload, opts) {
49
+ const schema = getValidSchema(db, type);
50
+ const ctx = db.modifyCtx;
51
+ try {
52
+ validatePayload(payload);
53
+ validateId(id);
54
+ if (schema.hooks?.update) {
55
+ payload = schema.hooks.update(payload) || payload;
56
+ }
57
+ ctx.id = id;
58
+ ctx.schema = schema;
59
+ ctx.operation = UPDATE;
60
+ ctx.overwrite = opts?.overwrite;
61
+ ctx.locale = opts?.locale && langCodesMap.get(opts.locale);
62
+ ctx.start = ctx.index;
63
+ if (ctx.main.size) {
64
+ ctx.main.clear();
65
+ }
66
+ writeUpdate(ctx, payload);
67
+ const tmp = new Tmp(ctx, id);
68
+ schedule(db, ctx);
69
+ return tmp;
70
+ }
71
+ catch (e) {
72
+ return handleError(db, ctx, update, arguments, e);
73
+ }
74
+ }
75
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ import { DbClient } from '../../../index.js';
2
+ import { ModifyOpts } from '../types.js';
3
+ export declare const upsert: (db: DbClient, type: string, obj: any, opts: ModifyOpts) => Promise<number>;
@@ -1,5 +1,5 @@
1
- import { deepMerge } from '@saulx/utils';
2
1
  import { ALIAS, isPropDef } from '@based/schema/def';
2
+ import { deepMerge } from '@based/utils';
3
3
  const filterAliases = (obj, tree) => {
4
4
  let aliases;
5
5
  for (const key in obj) {
@@ -23,7 +23,7 @@ const filterAliases = (obj, tree) => {
23
23
  }
24
24
  return aliases;
25
25
  };
26
- export async function upsert(db, type, obj, opts) {
26
+ export const upsert = (db, type, obj, opts) => {
27
27
  const tree = db.schemaTypesParsed[type].tree;
28
28
  const aliases = filterAliases(obj, tree);
29
29
  const q = db.query(type, aliases);
@@ -50,5 +50,5 @@ export async function upsert(db, type, obj, opts) {
50
50
  };
51
51
  db.upserting.set(q.id, store);
52
52
  return store.p;
53
- }
54
- //# sourceMappingURL=upsert.js.map
53
+ };
54
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,6 @@
1
+ import { PropDef, PropDefEdge } from '@based/schema/def';
2
+ import { DbClient } from '../../index.js';
3
+ export declare const validate: (def: PropDef | PropDefEdge, val: any) => void;
4
+ export declare const validatePayload: (payload: any) => void;
5
+ export declare const validateId: (id: number) => void;
6
+ export declare const getValidSchema: (db: DbClient, type: string) => import("@based/schema/def").SchemaTypeDef;
@@ -0,0 +1,24 @@
1
+ import { isValidId } from '@based/schema/def';
2
+ export const validate = (def, val) => {
3
+ if (!def.validation(val, def)) {
4
+ throw [def, val];
5
+ }
6
+ };
7
+ export const validatePayload = (payload) => {
8
+ if (typeof payload !== 'object' || payload === null) {
9
+ throw 'Invalid payload';
10
+ }
11
+ };
12
+ export const validateId = (id) => {
13
+ if (!isValidId(id)) {
14
+ throw 'Invalid id';
15
+ }
16
+ };
17
+ export const getValidSchema = (db, type) => {
18
+ const schema = db.schemaTypesParsed[type];
19
+ if (schema) {
20
+ return schema;
21
+ }
22
+ throw `Unknown type: ${type}. Did you mean on of: ${Object.keys(db.schemaTypesParsed).join(', ')}`;
23
+ };
24
+ //# sourceMappingURL=validate.js.map
@@ -1,10 +1,12 @@
1
- import { QueryDef, QueryTarget, Operator, QueryByAliasObj, LangFallback } from './query.js';
2
- import { BasedQueryResponse } from './BasedIterable.js';
1
+ import { QueryDef, QueryTarget, Operator, QueryByAliasObj, LangFallback, IncludeOpts } from './query.js';
2
+ import { BasedQueryResponse } from './BasedQueryResponse.js';
3
3
  import { Search } from './search/index.js';
4
4
  import { OnData, OnError } from './subscription/index.js';
5
5
  import { DbClient } from '../index.js';
6
6
  import { LangName } from '@based/schema';
7
- import { FilterAst, FilterBranchFn, FilterOpts } from './filter/types.js';
7
+ import { FilterBranchFn, FilterOpts } from './filter/types.js';
8
+ import { StepInput, aggFnOptions } from './aggregates/types.js';
9
+ import { ReaderSchema } from '@based/protocol/db-read';
8
10
  export { QueryByAliasObj };
9
11
  export type SelectFn = (field: string) => BasedDbReferenceQuery;
10
12
  export type BranchInclude = (select: SelectFn) => any;
@@ -19,35 +21,36 @@ export declare class QueryBranch<T> {
19
21
  constructor(db: DbClient, def?: QueryDef);
20
22
  sort(field: string, order?: 'asc' | 'desc'): T;
21
23
  filter<O extends Operator>(field: string, operator?: O | boolean, value?: any, opts?: FilterOpts<O>): T;
22
- filterBatch(f: FilterAst): this;
23
24
  search(query: string, ...fields: Search[]): T;
24
25
  search(query: ArrayBufferView, field: string, opts?: Omit<FilterOpts, 'lowerCase'>): T;
25
- groupBy(field: string): T;
26
+ groupBy(field: string, step?: StepInput): T;
26
27
  count(field?: string): T;
27
- sum(...fields: (string | string[])[]): T;
28
- cardinality(...fields: (string | string[])[]): T;
29
- stddev(...fields: (string | string[])[]): T;
30
- var(...fields: (string | string[])[]): T;
31
- avg(...fields: (string | string[])[]): T;
32
- max(...fields: (string | string[])[]): T;
33
- min(...fields: (string | string[])[]): T;
28
+ sum(...fields: string[]): T;
29
+ cardinality(field: string): T;
30
+ stddev(...args: (string | aggFnOptions)[]): T;
31
+ var(...args: (string | aggFnOptions)[]): T;
32
+ avg(...fields: string[]): T;
33
+ harmonicMean(...fields: string[]): T;
34
+ max(...fields: string[]): T;
35
+ min(...fields: string[]): T;
34
36
  or(fn: FilterBranchFn): T;
35
37
  or(field: string, operator?: Operator | boolean, value?: any, opts?: FilterOpts): T;
36
38
  range(start: number, end?: number): T;
37
- include(...fields: (string | BranchInclude | string[])[]): T;
39
+ include(...fields: (string | BranchInclude | IncludeOpts | (string | IncludeOpts)[])[]): T;
38
40
  }
39
41
  export declare class BasedDbReferenceQuery extends QueryBranch<BasedDbReferenceQuery> {
40
42
  }
41
43
  declare class GetPromise extends Promise<BasedQueryResponse> {
42
44
  toObject(): Promise<any>;
43
- toJSON(): Promise<string>;
45
+ toJSON(replacer?: (this: any, key: string, value: any) => any, space?: string | number): Promise<string>;
44
46
  inspect(depth?: number, raw?: boolean): GetPromise;
45
47
  }
46
48
  export declare class BasedDbQuery extends QueryBranch<BasedDbQuery> {
47
49
  #private;
48
50
  skipValidation: boolean;
49
51
  target: QueryTarget;
50
- constructor(db: DbClient, type: string, rawTarget?: QueryByAliasObj | number | Uint32Array | (QueryByAliasObj | number)[], skipValidation?: boolean);
52
+ readSchema: ReaderSchema;
53
+ constructor(db: DbClient, type: string, rawTarget?: QueryByAliasObj | number | Promise<number> | Uint32Array | (QueryByAliasObj | number)[], skipValidation?: boolean);
51
54
  reset(): void;
52
55
  id: number;
53
56
  get(): GetPromise;