@based/db 0.0.71 → 0.1.1

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 (259) 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/mblen.h +40 -0
  9. package/dist/lib/darwin_aarch64/include/selva/selva_string.h +8 -5
  10. package/dist/lib/darwin_aarch64/include/selva/thread.h +37 -0
  11. package/dist/lib/darwin_aarch64/include/selva/types.h +18 -4
  12. package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
  13. package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
  14. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  15. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  16. package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
  17. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  18. package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
  19. package/dist/lib/linux_aarch64/include/libdeflate.h +23 -0
  20. package/dist/lib/linux_aarch64/include/selva/db.h +0 -41
  21. package/dist/lib/linux_aarch64/include/selva/fast_linear_search.h +6 -2
  22. package/dist/lib/linux_aarch64/include/selva/fields.h +52 -37
  23. package/dist/lib/linux_aarch64/include/selva/gmtime.h +137 -0
  24. package/dist/lib/linux_aarch64/include/selva/mblen.h +40 -0
  25. package/dist/lib/linux_aarch64/include/selva/selva_string.h +8 -5
  26. package/dist/lib/linux_aarch64/include/selva/thread.h +37 -0
  27. package/dist/lib/linux_aarch64/include/selva/types.h +18 -4
  28. package/dist/lib/linux_aarch64/libdeflate.so +0 -0
  29. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  30. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  31. package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
  32. package/dist/lib/linux_aarch64/libselva.so +0 -0
  33. package/dist/lib/linux_x86_64/include/libdeflate.h +23 -0
  34. package/dist/lib/linux_x86_64/include/selva/db.h +0 -41
  35. package/dist/lib/linux_x86_64/include/selva/fast_linear_search.h +6 -2
  36. package/dist/lib/linux_x86_64/include/selva/fields.h +52 -37
  37. package/dist/lib/linux_x86_64/include/selva/gmtime.h +137 -0
  38. package/dist/lib/linux_x86_64/include/selva/mblen.h +40 -0
  39. package/dist/lib/linux_x86_64/include/selva/selva_string.h +8 -5
  40. package/dist/lib/linux_x86_64/include/selva/thread.h +37 -0
  41. package/dist/lib/linux_x86_64/include/selva/types.h +18 -4
  42. package/dist/lib/linux_x86_64/libdeflate.so +0 -0
  43. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  44. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  45. package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
  46. package/dist/lib/linux_x86_64/libselva.so +0 -0
  47. package/dist/src/client/index.d.ts +16 -21
  48. package/dist/src/client/index.js +48 -59
  49. package/dist/src/client/modify/Ctx.d.ts +38 -0
  50. package/dist/src/client/modify/Ctx.js +30 -0
  51. package/dist/src/client/modify/Tmp.d.ts +19 -0
  52. package/dist/src/client/modify/Tmp.js +67 -0
  53. package/dist/src/client/modify/create/index.d.ts +6 -0
  54. package/dist/src/client/modify/create/index.js +191 -0
  55. package/dist/src/client/modify/create/mark.d.ts +7 -0
  56. package/dist/src/client/modify/create/mark.js +42 -0
  57. package/dist/src/client/modify/cursor.d.ts +10 -0
  58. package/dist/src/client/modify/cursor.js +48 -0
  59. package/dist/src/client/modify/delete/index.d.ts +2 -0
  60. package/dist/src/client/modify/delete/index.js +39 -0
  61. package/dist/src/client/modify/drain.d.ts +7 -0
  62. package/dist/src/client/modify/drain.js +77 -0
  63. package/dist/src/client/modify/edges/binary.d.ts +3 -0
  64. package/dist/src/client/modify/edges/binary.js +28 -0
  65. package/dist/src/client/modify/edges/cardinality.d.ts +3 -0
  66. package/dist/src/client/modify/edges/cardinality.js +15 -0
  67. package/dist/src/client/modify/edges/header.d.ts +5 -0
  68. package/dist/src/client/modify/edges/header.js +21 -0
  69. package/dist/src/client/modify/edges/index.d.ts +3 -0
  70. package/dist/src/client/modify/edges/index.js +128 -0
  71. package/dist/src/client/modify/edges/reference.d.ts +3 -0
  72. package/dist/src/client/modify/edges/reference.js +31 -0
  73. package/dist/src/client/modify/edges/references.d.ts +3 -0
  74. package/dist/src/client/modify/edges/references.js +38 -0
  75. package/dist/src/client/modify/edges/separate.d.ts +3 -0
  76. package/dist/src/client/modify/edges/separate.js +24 -0
  77. package/dist/src/client/modify/edges/string.d.ts +3 -0
  78. package/dist/src/client/modify/edges/string.js +30 -0
  79. package/dist/src/client/modify/error.d.ts +7 -0
  80. package/dist/src/client/modify/error.js +68 -0
  81. package/dist/src/client/modify/expire/index.d.ts +2 -0
  82. package/dist/src/client/modify/expire/index.js +31 -0
  83. package/dist/src/client/modify/props/alias.d.ts +3 -0
  84. package/dist/src/client/modify/props/alias.js +26 -0
  85. package/dist/src/client/modify/props/binary.d.ts +5 -0
  86. package/dist/src/client/modify/props/binary.js +52 -0
  87. package/dist/src/client/modify/props/cardinality.d.ts +4 -0
  88. package/dist/src/client/modify/props/cardinality.js +46 -0
  89. package/dist/src/client/modify/props/delete.d.ts +3 -0
  90. package/dist/src/client/modify/props/delete.js +13 -0
  91. package/dist/src/client/modify/props/fixed.d.ts +4 -0
  92. package/dist/src/client/modify/props/fixed.js +105 -0
  93. package/dist/src/client/modify/props/increment.d.ts +3 -0
  94. package/dist/src/client/modify/props/increment.js +28 -0
  95. package/dist/src/client/modify/props/json.d.ts +3 -0
  96. package/dist/src/client/modify/props/json.js +5 -0
  97. package/dist/src/client/modify/props/main.d.ts +4 -0
  98. package/dist/src/client/modify/props/main.js +22 -0
  99. package/dist/src/client/modify/props/object.d.ts +3 -0
  100. package/dist/src/client/modify/props/object.js +37 -0
  101. package/dist/src/client/modify/props/reference.d.ts +6 -0
  102. package/dist/src/client/modify/props/reference.js +92 -0
  103. package/dist/src/client/modify/props/references.d.ts +3 -0
  104. package/dist/src/client/modify/props/references.js +231 -0
  105. package/dist/src/client/modify/props/separate.d.ts +3 -0
  106. package/dist/src/client/modify/props/separate.js +43 -0
  107. package/dist/src/client/modify/props/string.d.ts +5 -0
  108. package/dist/src/client/modify/props/string.js +51 -0
  109. package/dist/src/client/modify/props/text.d.ts +3 -0
  110. package/dist/src/client/modify/props/text.js +35 -0
  111. package/dist/src/client/modify/props/vector.d.ts +3 -0
  112. package/dist/src/client/modify/props/vector.js +48 -0
  113. package/dist/src/client/modify/resize.d.ts +3 -0
  114. package/dist/src/client/modify/resize.js +20 -0
  115. package/dist/src/client/modify/types.d.ts +3 -4
  116. package/dist/src/client/modify/types.js +2 -1
  117. package/dist/src/client/modify/uint.d.ts +7 -0
  118. package/dist/src/client/modify/uint.js +27 -0
  119. package/dist/src/client/modify/update/index.d.ts +3 -0
  120. package/dist/src/client/modify/update/index.js +75 -0
  121. package/dist/src/client/modify/upsert/index.d.ts +3 -0
  122. package/dist/src/client/modify/{upsert.js → upsert/index.js} +4 -4
  123. package/dist/src/client/modify/validate.d.ts +6 -0
  124. package/dist/src/client/modify/validate.js +24 -0
  125. package/dist/src/client/query/BasedDbQuery.d.ts +17 -14
  126. package/dist/src/client/query/BasedDbQuery.js +90 -93
  127. package/dist/src/client/query/{BasedIterable.d.ts → BasedQueryResponse.d.ts} +5 -4
  128. package/dist/src/client/query/{BasedIterable.js → BasedQueryResponse.js} +15 -10
  129. package/dist/src/client/query/aggregates/aggregation.d.ts +5 -3
  130. package/dist/src/client/query/aggregates/aggregation.js +105 -48
  131. package/dist/src/client/query/aggregates/types.d.ts +33 -11
  132. package/dist/src/client/query/aggregates/types.js +29 -11
  133. package/dist/src/client/query/debug.js +1 -1
  134. package/dist/src/client/query/display.d.ts +1 -1
  135. package/dist/src/client/query/display.js +21 -4
  136. package/dist/src/client/query/filter/FilterBranch.js +2 -2
  137. package/dist/src/client/query/filter/convertFilter.d.ts +3 -2
  138. package/dist/src/client/query/filter/convertFilter.js +13 -1
  139. package/dist/src/client/query/filter/createFixedFilterBuffer.js +16 -30
  140. package/dist/src/client/query/filter/createReferenceFilter.js +6 -20
  141. package/dist/src/client/query/filter/createVariableFilterBuffer.js +15 -32
  142. package/dist/src/client/query/filter/parseFilterValue.js +3 -11
  143. package/dist/src/client/query/filter/primitiveFilter.js +1 -1
  144. package/dist/src/client/query/filter/toBuffer.js +9 -15
  145. package/dist/src/client/query/filter/types.d.ts +6 -6
  146. package/dist/src/client/query/filter/types.js +8 -8
  147. package/dist/src/client/query/include/include.d.ts +3 -0
  148. package/dist/src/client/query/include/include.js +58 -0
  149. package/dist/src/client/query/include/props.d.ts +7 -7
  150. package/dist/src/client/query/include/props.js +26 -21
  151. package/dist/src/client/query/include/toByteCode.js +188 -0
  152. package/dist/src/client/query/include/utils.d.ts +3 -1
  153. package/dist/src/client/query/include/utils.js +13 -0
  154. package/dist/src/client/query/include/walk.d.ts +2 -2
  155. package/dist/src/client/query/include/walk.js +38 -23
  156. package/dist/src/client/query/query.d.ts +2 -3
  157. package/dist/src/client/query/query.js +2 -3
  158. package/dist/src/client/query/queryDef.d.ts +4 -2
  159. package/dist/src/client/query/queryDef.js +3 -5
  160. package/dist/src/client/query/queryDefToReadSchema.d.ts +3 -0
  161. package/dist/src/client/query/queryDefToReadSchema.js +126 -0
  162. package/dist/src/client/query/registerQuery.js +15 -4
  163. package/dist/src/client/query/search/index.d.ts +2 -1
  164. package/dist/src/client/query/search/index.js +9 -2
  165. package/dist/src/client/query/sort.js +3 -0
  166. package/dist/src/client/query/subscription/index.d.ts +1 -1
  167. package/dist/src/client/query/subscription/index.js +2 -2
  168. package/dist/src/client/query/subscription/types.d.ts +1 -1
  169. package/dist/src/client/query/thresholds.d.ts +1 -1
  170. package/dist/src/client/query/thresholds.js +1 -1
  171. package/dist/src/client/query/toByteCode/{toBuffer.js → toByteCode.js} +8 -8
  172. package/dist/src/client/query/types.d.ts +40 -25
  173. package/dist/src/client/query/types.js +2 -6
  174. package/dist/src/client/query/validation.d.ts +6 -0
  175. package/dist/src/client/query/validation.js +31 -7
  176. package/dist/src/client/setLocalClientSchema.js +6 -10
  177. package/dist/src/client/string.d.ts +3 -7
  178. package/dist/src/client/string.js +18 -47
  179. package/dist/src/hooks.d.ts +2 -3
  180. package/dist/src/hooks.js +14 -9
  181. package/dist/src/index.d.ts +3 -5
  182. package/dist/src/index.js +4 -6
  183. package/dist/src/native.d.ts +3 -2
  184. package/dist/src/native.js +9 -8
  185. package/dist/src/server/IoWorker.js +2 -3
  186. package/dist/src/server/QueryWorker.js +1 -1
  187. package/dist/src/server/blocks.js +5 -4
  188. package/dist/src/server/dbHash.js +1 -1
  189. package/dist/src/server/index.d.ts +4 -6
  190. package/dist/src/server/index.js +63 -96
  191. package/dist/src/server/migrate/index.d.ts +2 -4
  192. package/dist/src/server/migrate/index.js +32 -16
  193. package/dist/src/server/migrate/worker.js +2 -27
  194. package/dist/src/server/save.js +3 -2
  195. package/dist/src/server/schema.js +9 -19
  196. package/dist/src/server/start.d.ts +1 -0
  197. package/dist/src/server/start.js +18 -10
  198. package/dist/src/server/tree.d.ts +20 -0
  199. package/dist/src/server/tree.js +17 -2
  200. package/dist/src/server/workers/DbWorker.d.ts +1 -0
  201. package/dist/src/server/workers/DbWorker.js +8 -1
  202. package/dist/src/server/workers/io_worker.js +1 -1
  203. package/dist/src/server/workers/worker.js +9 -4
  204. package/dist/src/table.d.ts +4 -0
  205. package/dist/src/table.js +97 -0
  206. package/package.json +18 -17
  207. package/dist/lib/darwin_aarch64/include/selva/worker_ctx.h +0 -29
  208. package/dist/lib/linux_aarch64/include/selva/worker_ctx.h +0 -29
  209. package/dist/lib/linux_x86_64/include/selva/worker_ctx.h +0 -29
  210. package/dist/src/client/flushModify.d.ts +0 -36
  211. package/dist/src/client/flushModify.js +0 -197
  212. package/dist/src/client/modify/ModifyRes.d.ts +0 -23
  213. package/dist/src/client/modify/ModifyRes.js +0 -101
  214. package/dist/src/client/modify/alias.d.ts +0 -4
  215. package/dist/src/client/modify/alias.js +0 -57
  216. package/dist/src/client/modify/binary.d.ts +0 -6
  217. package/dist/src/client/modify/binary.js +0 -80
  218. package/dist/src/client/modify/cardinality.d.ts +0 -6
  219. package/dist/src/client/modify/cardinality.js +0 -63
  220. package/dist/src/client/modify/create.d.ts +0 -5
  221. package/dist/src/client/modify/create.js +0 -228
  222. package/dist/src/client/modify/delete.d.ts +0 -4
  223. package/dist/src/client/modify/delete.js +0 -47
  224. package/dist/src/client/modify/expire.d.ts +0 -3
  225. package/dist/src/client/modify/expire.js +0 -25
  226. package/dist/src/client/modify/fixed.d.ts +0 -5
  227. package/dist/src/client/modify/fixed.js +0 -235
  228. package/dist/src/client/modify/index.d.ts +0 -4
  229. package/dist/src/client/modify/index.js +0 -4
  230. package/dist/src/client/modify/json.d.ts +0 -4
  231. package/dist/src/client/modify/json.js +0 -26
  232. package/dist/src/client/modify/modify.d.ts +0 -5
  233. package/dist/src/client/modify/modify.js +0 -134
  234. package/dist/src/client/modify/references/appendEdgeRefs.d.ts +0 -4
  235. package/dist/src/client/modify/references/appendEdgeRefs.js +0 -33
  236. package/dist/src/client/modify/references/edge.d.ts +0 -5
  237. package/dist/src/client/modify/references/edge.js +0 -336
  238. package/dist/src/client/modify/references/getEdgeSize.d.ts +0 -3
  239. package/dist/src/client/modify/references/getEdgeSize.js +0 -27
  240. package/dist/src/client/modify/references/reference.d.ts +0 -11
  241. package/dist/src/client/modify/references/reference.js +0 -124
  242. package/dist/src/client/modify/references/references.d.ts +0 -16
  243. package/dist/src/client/modify/references/references.js +0 -316
  244. package/dist/src/client/modify/setCursor.d.ts +0 -5
  245. package/dist/src/client/modify/setCursor.js +0 -33
  246. package/dist/src/client/modify/string.d.ts +0 -5
  247. package/dist/src/client/modify/string.js +0 -71
  248. package/dist/src/client/modify/text.d.ts +0 -8
  249. package/dist/src/client/modify/text.js +0 -69
  250. package/dist/src/client/modify/update.d.ts +0 -6
  251. package/dist/src/client/modify/update.js +0 -90
  252. package/dist/src/client/modify/upsert.d.ts +0 -3
  253. package/dist/src/client/modify/vector.d.ts +0 -4
  254. package/dist/src/client/modify/vector.js +0 -53
  255. package/dist/src/client/query/include/toBuffer.js +0 -123
  256. package/dist/src/client/query/read/read.d.ts +0 -9
  257. package/dist/src/client/query/read/read.js +0 -483
  258. /package/dist/src/client/query/include/{toBuffer.d.ts → toByteCode.d.ts} +0 -0
  259. /package/dist/src/client/query/toByteCode/{toBuffer.d.ts → toByteCode.d.ts} +0 -0
@@ -1,23 +1,24 @@
1
1
  import { parse } from '@based/schema';
2
- import { create } from './modify/create.js';
3
- import { flushBuffer, makeFlushIsReady, ModifyCtx, startDrain, } from './flushModify.js';
4
2
  import { BasedDbQuery } from './query/BasedDbQuery.js';
5
- import { ModifyState } from './modify/ModifyRes.js';
6
- import { upsert } from './modify/upsert.js';
7
- import { update } from './modify/update.js';
8
- import { deleteFn } from './modify/delete.js';
9
- import { expire } from './modify/expire.js';
10
3
  import { debugMode } from '../utils.js';
11
4
  import { DbShared } from '../shared/DbBase.js';
12
5
  import { setLocalClientSchema } from './setLocalClientSchema.js';
6
+ import { create } from './modify/create/index.js';
7
+ import { Ctx } from './modify/Ctx.js';
8
+ import { update } from './modify/update/index.js';
9
+ import { del } from './modify/delete/index.js';
10
+ import { expire } from './modify/expire/index.js';
11
+ import { cancel, drain, schedule } from './modify/drain.js';
12
+ import { upsert } from './modify/upsert/index.js';
13
13
  export class DbClient extends DbShared {
14
14
  constructor({ hooks, maxModifySize = 100 * 1e3 * 1e3, flushTime = 0, debug, }) {
15
15
  super();
16
16
  this.hooks = hooks;
17
17
  this.maxModifySize = maxModifySize;
18
- this.modifyCtx = new ModifyCtx(this);
18
+ this.modifyCtx = new Ctx(0, new Uint8Array(new ArrayBuffer(Math.min(1e3, maxModifySize), {
19
+ maxByteLength: maxModifySize,
20
+ })));
19
21
  this.flushTime = flushTime;
20
- makeFlushIsReady(this);
21
22
  if (debug) {
22
23
  debugMode(this);
23
24
  }
@@ -29,8 +30,6 @@ export class DbClient extends DbShared {
29
30
  hooks;
30
31
  // modify
31
32
  flushTime;
32
- flushReady;
33
- flushIsReady;
34
33
  writeTime = 0;
35
34
  isDraining = false;
36
35
  modifyCtx;
@@ -92,87 +91,77 @@ export class DbClient extends DbShared {
92
91
  if (type === undefined) {
93
92
  return new BasedDbQuery(this, '_root', 1);
94
93
  }
95
- // this is now double resolve
96
- if (Array.isArray(id)) {
97
- let i = id.length;
98
- while (i--) {
99
- if (typeof id[i] === 'object') {
100
- if (id[i] instanceof ModifyState) {
101
- // @ts-ignore
102
- id[i] = id[i].tmpId;
103
- }
104
- else {
105
- // it's get by alias
106
- }
107
- }
108
- }
109
- }
110
- else if (id instanceof Uint32Array) {
111
- // all good
112
- }
113
- else if (typeof id === 'object') {
114
- if (id instanceof ModifyState) {
115
- id = id.tmpId;
116
- }
117
- else {
118
- // it's get by alias
119
- }
120
- }
121
94
  return new BasedDbQuery(this, type, id);
122
95
  }
123
96
  update(typeOrValue, idOverwriteOrValue, value, opts) {
124
- if (typeof typeOrValue === 'string') {
125
- if (typeof idOverwriteOrValue === 'object') {
126
- if (idOverwriteOrValue instanceof ModifyState) {
127
- return update(this, typeOrValue, idOverwriteOrValue.tmpId, value, opts);
128
- }
129
- if ('id' in idOverwriteOrValue) {
130
- const { id, ...props } = idOverwriteOrValue;
131
- return this.update(typeOrValue, id, props, opts);
97
+ if (typeof typeOrValue !== 'string') {
98
+ return this.update('_root', 1, typeOrValue, idOverwriteOrValue);
99
+ }
100
+ if (typeof idOverwriteOrValue === 'object') {
101
+ if ('then' in idOverwriteOrValue &&
102
+ typeof idOverwriteOrValue.then === 'function') {
103
+ // @ts-ignore
104
+ if (idOverwriteOrValue.id) {
105
+ // @ts-ignore
106
+ return this.update(typeOrValue, idOverwriteOrValue.id, value, opts);
132
107
  }
108
+ return idOverwriteOrValue.then((id) => {
109
+ return this.update(typeOrValue, id, value, opts);
110
+ });
111
+ }
112
+ if ('id' in idOverwriteOrValue) {
113
+ const { id, ...props } = idOverwriteOrValue;
114
+ return this.update(typeOrValue, id, props, opts);
133
115
  }
134
- return update(this, typeOrValue, idOverwriteOrValue, value, opts);
135
116
  }
136
- // else it is rootProps
137
- return update(this, '_root', 1, typeOrValue, idOverwriteOrValue);
117
+ return update(this, typeOrValue, idOverwriteOrValue, value, opts);
138
118
  }
139
119
  upsert(type, obj, opts) {
140
120
  return upsert(this, type, obj, opts);
141
121
  }
142
122
  delete(type, id) {
143
- return deleteFn(this, type, typeof id === 'number' ? id : id.tmpId);
123
+ if (typeof id === 'object' &&
124
+ id !== null &&
125
+ 'then' in id &&
126
+ typeof id.then === 'function') {
127
+ // @ts-ignore
128
+ if (id.id) {
129
+ // @ts-ignore
130
+ id = id.id;
131
+ }
132
+ else {
133
+ return id.then((id) => this.delete(type, id));
134
+ }
135
+ }
136
+ // @ts-ignore
137
+ return del(this, type, id);
144
138
  }
145
139
  expire(type, id, seconds) {
146
- return expire(this, type, typeof id === 'number' ? id : id.tmpId, seconds);
140
+ return expire(this, type, id, seconds);
147
141
  }
148
142
  destroy() {
149
143
  this.stop();
150
144
  delete this.listeners;
151
- this.modifyCtx.db = null; // Make sure we don't have a circular ref and leak mem
152
145
  }
153
146
  stop() {
154
147
  for (const [, { onClose }] of this.subs) {
155
148
  onClose();
156
149
  }
157
150
  this.subs.clear();
158
- this.modifyCtx.len = 0;
151
+ cancel(this.modifyCtx, Error('Db stopped - in-flight modify cancelled'));
159
152
  }
160
153
  // For more advanced / internal usage - use isModified instead for most cases
161
154
  async drain() {
162
155
  if (this.upserting.size) {
163
156
  await Promise.all(Array.from(this.upserting).map(([, { p }]) => p));
164
157
  }
165
- await flushBuffer(this);
158
+ await drain(this, this.modifyCtx);
166
159
  const t = this.writeTime;
167
160
  this.writeTime = 0;
168
161
  return t;
169
162
  }
170
- async isModified() {
171
- if (!this.isDraining) {
172
- startDrain(this);
173
- }
174
- await this.flushIsReady;
175
- return;
163
+ isModified() {
164
+ return schedule(this, this.modifyCtx);
176
165
  }
177
166
  }
178
167
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,38 @@
1
+ import type { SchemaTypeDef, PropDef } from '@based/schema/def';
2
+ import type { LangCode } from '@based/schema';
3
+ import { type ModifyOp } from './types.js';
4
+ import type { Tmp } from './Tmp.js';
5
+ export declare class Ctx {
6
+ constructor(schemaChecksum: number, array: Uint8Array<ArrayBufferLike>);
7
+ id: number;
8
+ start: number;
9
+ index: number;
10
+ schema: SchemaTypeDef;
11
+ array: Uint8Array<ArrayBufferLike>;
12
+ max: number;
13
+ size: number;
14
+ unsafe?: boolean;
15
+ overwrite?: boolean;
16
+ operation: ModifyOp;
17
+ main: Map<PropDef, any>;
18
+ draining: Promise<void>;
19
+ scheduled: Promise<void>;
20
+ created: Record<number, number>;
21
+ locale: LangCode;
22
+ sort: number;
23
+ sortText: number;
24
+ defaults: number;
25
+ cursor: {
26
+ id?: number;
27
+ type?: number;
28
+ prop?: number;
29
+ main?: number;
30
+ operation?: ModifyOp;
31
+ };
32
+ batch: {
33
+ promises?: Tmp[];
34
+ offsets?: Record<number, number>;
35
+ ready?: boolean;
36
+ error?: Error;
37
+ };
38
+ }
@@ -0,0 +1,30 @@
1
+ import { writeUint64 } from '@based/utils';
2
+ export class Ctx {
3
+ constructor(schemaChecksum, array) {
4
+ this.array = array;
5
+ this.max = array.buffer.maxByteLength - 4; // dataLen
6
+ this.size = array.buffer.byteLength - this.max;
7
+ writeUint64(array, schemaChecksum, 0);
8
+ }
9
+ id;
10
+ start;
11
+ index = 8;
12
+ schema;
13
+ array;
14
+ max;
15
+ size;
16
+ unsafe;
17
+ overwrite;
18
+ operation;
19
+ main = new Map();
20
+ draining;
21
+ scheduled;
22
+ created = {}; // <typeId, count
23
+ locale;
24
+ sort = 0;
25
+ sortText = 0;
26
+ defaults = 0;
27
+ cursor = {};
28
+ batch = {};
29
+ }
30
+ //# sourceMappingURL=Ctx.js.map
@@ -0,0 +1,19 @@
1
+ import { Ctx } from './Ctx.js';
2
+ export declare const resolveTmp: (tmp: Tmp) => void;
3
+ export declare const rejectTmp: (tmp: Tmp) => void;
4
+ export declare class Tmp implements Promise<number> {
5
+ #private;
6
+ constructor(ctx: Ctx, id?: number);
7
+ get [Symbol.toStringTag](): string;
8
+ get error(): Error;
9
+ get id(): number;
10
+ type: number;
11
+ tmpId: number;
12
+ batch: Ctx['batch'];
13
+ promise?: Promise<number>;
14
+ resolve?: (value: number | PromiseLike<number>) => void;
15
+ reject?: (reason?: any) => void;
16
+ then<Res1 = number, Res2 = never>(onfulfilled?: ((value: number) => Res1 | PromiseLike<Res1>) | null, onrejected?: ((reason: any) => Res2 | PromiseLike<Res2>) | null): Promise<Res1 | Res2>;
17
+ catch<Res = never>(onrejected?: ((reason: any) => Res | PromiseLike<Res>) | null): Promise<number | Res>;
18
+ finally(onfinally?: (() => void) | null): Promise<number>;
19
+ }
@@ -0,0 +1,67 @@
1
+ const promisify = (tmp) => {
2
+ if (!tmp.promise) {
3
+ const id = tmp.id;
4
+ if (id) {
5
+ tmp.promise = Promise.resolve(id);
6
+ }
7
+ else {
8
+ tmp.promise = new Promise((resolve, reject) => {
9
+ tmp.resolve = resolve;
10
+ tmp.reject = reject;
11
+ tmp.batch.promises ??= [];
12
+ tmp.batch.promises.push(tmp);
13
+ });
14
+ }
15
+ }
16
+ return tmp.promise;
17
+ };
18
+ export const resolveTmp = (tmp) => {
19
+ tmp.resolve(tmp.id);
20
+ };
21
+ export const rejectTmp = (tmp) => tmp.reject(tmp.error);
22
+ export class Tmp {
23
+ constructor(ctx, id) {
24
+ this.type = ctx.cursor.type;
25
+ this.tmpId = ctx.id;
26
+ this.batch = ctx.batch;
27
+ this.#id = id;
28
+ }
29
+ get [Symbol.toStringTag]() {
30
+ return 'ModifyPromise';
31
+ }
32
+ #id;
33
+ get error() {
34
+ if (!this.batch.ready) {
35
+ return;
36
+ }
37
+ return this.batch.error;
38
+ }
39
+ get id() {
40
+ if (!this.batch.ready) {
41
+ return;
42
+ }
43
+ if (!this.#id) {
44
+ if (this.batch.offsets) {
45
+ const offset = this.batch.offsets[this.type];
46
+ this.#id = this.tmpId + offset;
47
+ }
48
+ }
49
+ return this.#id;
50
+ }
51
+ type;
52
+ tmpId;
53
+ batch;
54
+ promise;
55
+ resolve;
56
+ reject;
57
+ then(onfulfilled, onrejected) {
58
+ return promisify(this).then(onfulfilled, onrejected);
59
+ }
60
+ catch(onrejected) {
61
+ return promisify(this).catch(onrejected);
62
+ }
63
+ finally(onfinally) {
64
+ return promisify(this).finally(onfinally);
65
+ }
66
+ }
67
+ //# sourceMappingURL=Tmp.js.map
@@ -0,0 +1,6 @@
1
+ import { SchemaTypeDef } from '@based/schema/def';
2
+ import { Ctx } from '../Ctx.js';
3
+ import { DbClient } from '../../../index.js';
4
+ import { ModifyOpts } from '../types.js';
5
+ export declare const writeCreate: (ctx: Ctx, schema: SchemaTypeDef, payload: any, opts: ModifyOpts) => void;
6
+ export declare function create(db: DbClient, type: string, payload: any, opts: ModifyOpts): Promise<number>;
@@ -0,0 +1,191 @@
1
+ import { TEXT } from '@based/schema/def';
2
+ import { writeObject } from '../props/object.js';
3
+ import { reserve } from '../resize.js';
4
+ import { FULL_CURSOR_SIZE, PROP_CURSOR_SIZE, writeMainCursor, writeNodeCursor, writeTypeCursor, } from '../cursor.js';
5
+ import { getByPath, writeUint16 } from '@based/utils';
6
+ import { writeMainBuffer, writeMainValue } from '../props/main.js';
7
+ import { Tmp } from '../Tmp.js';
8
+ import { schedule } from '../drain.js';
9
+ import { ADD_EMPTY_SORT, ADD_EMPTY_SORT_TEXT, CREATE, } from '../types.js';
10
+ import { inverseLangMap, langCodesMap } from '@based/schema';
11
+ import { writeSeparate } from '../props/separate.js';
12
+ import { writeString } from '../props/string.js';
13
+ import { writeU8 } from '../uint.js';
14
+ import { getValidSchema, validatePayload } from '../validate.js';
15
+ import { handleError } from '../error.js';
16
+ const writeDefaults = (ctx, payload) => {
17
+ if (!ctx.schema.hasSeperateDefaults) {
18
+ return;
19
+ }
20
+ if (ctx.defaults !== ctx.schema.separateDefaults.props.size) {
21
+ for (const def of ctx.schema.separateDefaults.props.values()) {
22
+ const type = def.typeIndex;
23
+ if (ctx.schema.separateDefaults.bufferTmp[def.prop] === 0) {
24
+ writeSeparate(ctx, def, def.default);
25
+ continue;
26
+ }
27
+ if (type !== TEXT) {
28
+ continue;
29
+ }
30
+ const buf = ctx.schema.separateTextSort.bufferTmp;
31
+ const amount = ctx.schema.localeSize + 1;
32
+ const len = amount * ctx.schema.separateTextSort.props.length;
33
+ for (const sortDef of ctx.schema.separateTextSort.props) {
34
+ const index = sortDef.prop * amount;
35
+ if (buf[index] === 0) {
36
+ continue;
37
+ }
38
+ for (let i = index + 1; i < len + index; i++) {
39
+ const lang = buf[i];
40
+ if (lang === 0) {
41
+ continue;
42
+ }
43
+ const val = def.default[inverseLangMap.get(lang)];
44
+ if (val !== undefined) {
45
+ writeString(ctx, def, val, lang);
46
+ }
47
+ }
48
+ }
49
+ }
50
+ }
51
+ };
52
+ const writeSortable = (ctx) => {
53
+ if (!ctx.schema.hasSeperateSort) {
54
+ return;
55
+ }
56
+ if (ctx.sort !== ctx.schema.separateSort.size) {
57
+ reserve(ctx, 3);
58
+ writeU8(ctx, ADD_EMPTY_SORT);
59
+ const index = ctx.index;
60
+ ctx.index += 2;
61
+ const start = ctx.index;
62
+ for (const def of ctx.schema.separateSort.props) {
63
+ if (ctx.schema.separateSort.bufferTmp[def.prop] === 0) {
64
+ reserve(ctx, 1);
65
+ writeU8(ctx, def.prop);
66
+ }
67
+ }
68
+ writeUint16(ctx.array, ctx.index - start, index);
69
+ }
70
+ };
71
+ const writeSortableText = (ctx) => {
72
+ if (!ctx.schema.hasSeperateTextSort) {
73
+ return;
74
+ }
75
+ if (ctx.sortText !== ctx.schema.separateTextSort.size) {
76
+ reserve(ctx, 3);
77
+ writeU8(ctx, ADD_EMPTY_SORT_TEXT);
78
+ const index = ctx.index;
79
+ ctx.index += 2;
80
+ const start = ctx.index;
81
+ const amount = ctx.schema.localeSize + 1;
82
+ const len = amount * ctx.schema.separateTextSort.props.length;
83
+ const buf = ctx.schema.separateTextSort.bufferTmp;
84
+ for (const def of ctx.schema.separateTextSort.props) {
85
+ const index = def.prop * amount;
86
+ if (buf[index] === 0) {
87
+ continue;
88
+ }
89
+ reserve(ctx, 2);
90
+ writeU8(ctx, def.prop);
91
+ writeU8(ctx, buf[index]);
92
+ for (let i = index + 1; i < len + index; i++) {
93
+ const lang = buf[i];
94
+ if (lang === 0) {
95
+ continue;
96
+ }
97
+ reserve(ctx, 1);
98
+ writeU8(ctx, lang);
99
+ }
100
+ }
101
+ writeUint16(ctx.array, ctx.index - start, index);
102
+ if (ctx.sortText) {
103
+ buf.set(ctx.schema.separateTextSort.buffer, 0);
104
+ }
105
+ }
106
+ };
107
+ const writeCreateTs = (ctx, payload) => {
108
+ if (!ctx.schema.createTs) {
109
+ return;
110
+ }
111
+ let createTs;
112
+ for (const prop of ctx.schema.createTs) {
113
+ if (getByPath(payload, prop.path) !== undefined) {
114
+ continue;
115
+ }
116
+ createTs ??= Date.now();
117
+ writeMainValue(ctx, prop, createTs);
118
+ }
119
+ };
120
+ export const writeCreate = (ctx, schema, payload, opts) => {
121
+ validatePayload(payload);
122
+ validatePayload(schema);
123
+ if (schema.hooks?.create) {
124
+ payload = schema.hooks.create(payload) || payload;
125
+ }
126
+ if (payload.id) {
127
+ if (!opts?.unsafe) {
128
+ throw 'Invalid payload. "id" not allowed';
129
+ }
130
+ ctx.id = payload.id;
131
+ }
132
+ else {
133
+ if (!(schema.id in ctx.created)) {
134
+ ctx.created[schema.id] = 0;
135
+ ctx.max -= 6;
136
+ ctx.size -= 6;
137
+ }
138
+ ctx.id = ctx.created[schema.id] + 1;
139
+ }
140
+ if (ctx.defaults) {
141
+ ctx.defaults = 0;
142
+ schema.separateDefaults?.bufferTmp.fill(0);
143
+ }
144
+ if (ctx.sort) {
145
+ ctx.sort = 0;
146
+ schema.separateSort.bufferTmp.set(schema.separateSort.buffer);
147
+ }
148
+ if (ctx.sortText) {
149
+ ctx.sortText = 0;
150
+ schema.separateTextSort.bufferTmp.set(schema.separateTextSort.buffer);
151
+ }
152
+ ctx.schema = schema;
153
+ ctx.operation = CREATE;
154
+ ctx.overwrite = true;
155
+ ctx.unsafe = opts?.unsafe;
156
+ ctx.locale = opts?.locale && langCodesMap.get(opts.locale);
157
+ ctx.start = ctx.index;
158
+ reserve(ctx, FULL_CURSOR_SIZE);
159
+ writeTypeCursor(ctx);
160
+ writeNodeCursor(ctx);
161
+ const index = ctx.index;
162
+ writeObject(ctx, ctx.schema.tree, payload);
163
+ if (ctx.index === index || ctx.schema.mainLen === 0) {
164
+ reserve(ctx, PROP_CURSOR_SIZE);
165
+ writeMainCursor(ctx);
166
+ }
167
+ writeCreateTs(ctx, payload);
168
+ if (!ctx.cursor.main && !ctx.schema.mainEmptyAllZeroes) {
169
+ writeMainBuffer(ctx);
170
+ }
171
+ writeDefaults(ctx, payload);
172
+ writeSortable(ctx);
173
+ writeSortableText(ctx);
174
+ if (schema.id in ctx.created) {
175
+ ctx.created[schema.id]++;
176
+ }
177
+ };
178
+ export function create(db, type, payload, opts) {
179
+ const schema = getValidSchema(db, type);
180
+ const ctx = db.modifyCtx;
181
+ try {
182
+ writeCreate(ctx, schema, payload, opts);
183
+ const tmp = new Tmp(ctx);
184
+ schedule(db, ctx);
185
+ return tmp;
186
+ }
187
+ catch (e) {
188
+ return handleError(db, ctx, create, arguments, e);
189
+ }
190
+ }
191
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ import { PropDef } from '@based/schema/def';
2
+ import { Ctx } from '../Ctx.js';
3
+ import { LangCode } from '@based/schema';
4
+ export declare const markString: (ctx: Ctx, def: PropDef) => void;
5
+ export declare const markDefaults: (ctx: Ctx, def: PropDef, val: any) => void;
6
+ export declare const markTextObj: (ctx: Ctx) => void;
7
+ export declare const markTextValue: (ctx: Ctx, def: PropDef, locale: LangCode, textStringValue: boolean) => void;
@@ -0,0 +1,42 @@
1
+ import { CREATE } from '../types.js';
2
+ export const markString = (ctx, def) => {
3
+ if (ctx.operation === CREATE) {
4
+ ctx.schema.separateSort.bufferTmp[def.prop] = 2;
5
+ ctx.sort++;
6
+ if (ctx.schema.hasSeperateDefaults) {
7
+ ctx.schema.separateDefaults.bufferTmp[def.prop] = 1;
8
+ ctx.defaults++;
9
+ }
10
+ }
11
+ };
12
+ export const markDefaults = (ctx, def, val) => {
13
+ if (ctx.operation === CREATE &&
14
+ ctx.schema.hasSeperateDefaults &&
15
+ val !== null) {
16
+ if (!ctx.schema.separateDefaults.bufferTmp[def.prop]) {
17
+ ctx.schema.separateDefaults.bufferTmp[def.prop] = 1;
18
+ ctx.defaults++;
19
+ }
20
+ }
21
+ };
22
+ export const markTextObj = (ctx) => {
23
+ if (ctx.operation === CREATE && ctx.schema.hasSeperateDefaults) {
24
+ ctx.defaults++;
25
+ }
26
+ };
27
+ export const markTextValue = (ctx, def, locale, textStringValue) => {
28
+ if (ctx.operation === CREATE) {
29
+ const index = def.prop * (1 + ctx.schema.localeSize);
30
+ const langIndex = ctx.schema.separateTextSort.localeToIndex.get(locale);
31
+ ctx.schema.separateTextSort.bufferTmp[index] -= 1;
32
+ ctx.schema.separateTextSort.bufferTmp[index + langIndex] = 0;
33
+ ctx.sortText++;
34
+ if (ctx.schema.hasSeperateDefaults) {
35
+ ctx.schema.separateDefaults.bufferTmp[def.prop]++;
36
+ if (textStringValue) {
37
+ ctx.defaults++;
38
+ }
39
+ }
40
+ }
41
+ };
42
+ //# sourceMappingURL=mark.js.map
@@ -0,0 +1,10 @@
1
+ import { PropDef } from '@based/schema/def';
2
+ import { Ctx } from './Ctx.js';
3
+ export declare const TYPE_CURSOR_SIZE = 3;
4
+ export declare const PROP_CURSOR_SIZE = 3;
5
+ export declare const NODE_CURSOR_SIZE = 5;
6
+ export declare const FULL_CURSOR_SIZE: number;
7
+ export declare const writeTypeCursor: (ctx: Ctx) => void;
8
+ export declare const writePropCursor: (ctx: Ctx, def: PropDef) => void;
9
+ export declare const writeMainCursor: (ctx: Ctx) => void;
10
+ export declare const writeNodeCursor: (ctx: Ctx) => void;
@@ -0,0 +1,48 @@
1
+ import { MICRO_BUFFER } from '@based/schema/def';
2
+ import { writeUint32 } from '@based/utils';
3
+ import { SWITCH_TYPE, SWITCH_FIELD, CREATE, SWITCH_ID_CREATE, SWITCH_ID_UPDATE, } from './types.js';
4
+ export const TYPE_CURSOR_SIZE = 3;
5
+ export const PROP_CURSOR_SIZE = 3;
6
+ export const NODE_CURSOR_SIZE = 5;
7
+ export const FULL_CURSOR_SIZE = TYPE_CURSOR_SIZE + NODE_CURSOR_SIZE + PROP_CURSOR_SIZE;
8
+ export const writeTypeCursor = (ctx) => {
9
+ if (ctx.schema.id !== ctx.cursor.type) {
10
+ ctx.array[ctx.index] = SWITCH_TYPE; // switch node type
11
+ ctx.array.set(ctx.schema.idUint8, ctx.index + 1);
12
+ ctx.index += 3;
13
+ ctx.cursor.type = ctx.schema.id;
14
+ ctx.cursor.main = null;
15
+ ctx.cursor.prop = null;
16
+ ctx.cursor.id = null;
17
+ }
18
+ };
19
+ export const writePropCursor = (ctx, def) => {
20
+ if (def.prop !== ctx.cursor.prop) {
21
+ ctx.array[ctx.index] = SWITCH_FIELD;
22
+ ctx.array[ctx.index + 1] = def.prop;
23
+ ctx.array[ctx.index + 2] = def.typeIndex;
24
+ ctx.index += 3;
25
+ ctx.cursor.prop = def.prop;
26
+ }
27
+ };
28
+ export const writeMainCursor = (ctx) => {
29
+ if (ctx.cursor.prop !== 0) {
30
+ ctx.array[ctx.index] = SWITCH_FIELD;
31
+ ctx.array[ctx.index + 1] = 0;
32
+ ctx.array[ctx.index + 2] = MICRO_BUFFER;
33
+ ctx.index += 3;
34
+ ctx.cursor.prop = 0;
35
+ }
36
+ };
37
+ export const writeNodeCursor = (ctx) => {
38
+ if (ctx.id !== ctx.cursor.id || ctx.operation !== ctx.cursor.operation) {
39
+ ctx.array[ctx.index] =
40
+ ctx.operation === CREATE ? SWITCH_ID_CREATE : SWITCH_ID_UPDATE; // swtch id
41
+ writeUint32(ctx.array, ctx.id, ctx.index + 1);
42
+ ctx.index += 5;
43
+ ctx.cursor.id = ctx.id;
44
+ ctx.cursor.main = null;
45
+ ctx.cursor.operation = ctx.operation;
46
+ }
47
+ };
48
+ //# sourceMappingURL=cursor.js.map
@@ -0,0 +1,2 @@
1
+ import { DbClient } from '../../../index.js';
2
+ export declare function del(db: DbClient, type: string, id: number): Promise<number>;
@@ -0,0 +1,39 @@
1
+ import { getValidSchema, validateId } from '../validate.js';
2
+ import { FULL_CURSOR_SIZE, writeMainCursor, writeNodeCursor, writePropCursor, writeTypeCursor, } from '../cursor.js';
3
+ import { reserve } from '../resize.js';
4
+ import { DELETE_NODE, DELETE_SORT_INDEX, UPDATE } from '../types.js';
5
+ import { writeU8 } from '../uint.js';
6
+ import { handleError } from '../error.js';
7
+ import { Tmp } from '../Tmp.js';
8
+ import { schedule } from '../drain.js';
9
+ export function del(db, type, id) {
10
+ const schema = getValidSchema(db, type);
11
+ const ctx = db.modifyCtx;
12
+ try {
13
+ if (schema.insertOnly) {
14
+ throw `This type is insertOnly`;
15
+ }
16
+ ctx.id = id;
17
+ ctx.start = ctx.index;
18
+ ctx.schema = schema;
19
+ ctx.operation = UPDATE;
20
+ validateId(id);
21
+ reserve(ctx, FULL_CURSOR_SIZE + 2 + schema.separate.length * 12); // 12 too much?
22
+ writeTypeCursor(ctx);
23
+ writeMainCursor(ctx);
24
+ writeNodeCursor(ctx);
25
+ writeU8(ctx, DELETE_SORT_INDEX);
26
+ for (const def of schema.separate) {
27
+ writePropCursor(ctx, def);
28
+ writeU8(ctx, DELETE_SORT_INDEX);
29
+ }
30
+ writeU8(ctx, DELETE_NODE);
31
+ const tmp = new Tmp(ctx, id);
32
+ schedule(db, ctx);
33
+ return tmp;
34
+ }
35
+ catch (e) {
36
+ return handleError(db, ctx, del, arguments, e);
37
+ }
38
+ }
39
+ //# sourceMappingURL=index.js.map