@based/db 0.0.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 (296) hide show
  1. package/README.md +43 -0
  2. package/dist/lib/darwin_aarch64/include/cdefs.h +317 -0
  3. package/dist/lib/darwin_aarch64/include/libdeflate.h +322 -0
  4. package/dist/lib/darwin_aarch64/include/libdeflate_strings.h +35 -0
  5. package/dist/lib/darwin_aarch64/include/linker_set.h +109 -0
  6. package/dist/lib/darwin_aarch64/include/queue.h +627 -0
  7. package/dist/lib/darwin_aarch64/include/selva/_export.h +7 -0
  8. package/dist/lib/darwin_aarch64/include/selva/align.h +9 -0
  9. package/dist/lib/darwin_aarch64/include/selva/backoff_timeout.h +29 -0
  10. package/dist/lib/darwin_aarch64/include/selva/base64.h +59 -0
  11. package/dist/lib/darwin_aarch64/include/selva/base64url.h +59 -0
  12. package/dist/lib/darwin_aarch64/include/selva/bitmap.h +95 -0
  13. package/dist/lib/darwin_aarch64/include/selva/crc32c.h +17 -0
  14. package/dist/lib/darwin_aarch64/include/selva/ctime.h +135 -0
  15. package/dist/lib/darwin_aarch64/include/selva/db.h +284 -0
  16. package/dist/lib/darwin_aarch64/include/selva/endian.h +301 -0
  17. package/dist/lib/darwin_aarch64/include/selva/fast_linear_search.h +23 -0
  18. package/dist/lib/darwin_aarch64/include/selva/fast_memmem.h +11 -0
  19. package/dist/lib/darwin_aarch64/include/selva/fast_parsei.h +36 -0
  20. package/dist/lib/darwin_aarch64/include/selva/fields.h +338 -0
  21. package/dist/lib/darwin_aarch64/include/selva/find.h +47 -0
  22. package/dist/lib/darwin_aarch64/include/selva/lpf.h +28 -0
  23. package/dist/lib/darwin_aarch64/include/selva/node_id_set.h +43 -0
  24. package/dist/lib/darwin_aarch64/include/selva/poptop.h +114 -0
  25. package/dist/lib/darwin_aarch64/include/selva/queue_r.h +190 -0
  26. package/dist/lib/darwin_aarch64/include/selva/selva_hash128.h +39 -0
  27. package/dist/lib/darwin_aarch64/include/selva/selva_lang.h +105 -0
  28. package/dist/lib/darwin_aarch64/include/selva/selva_math.h +37 -0
  29. package/dist/lib/darwin_aarch64/include/selva/selva_string.h +672 -0
  30. package/dist/lib/darwin_aarch64/include/selva/sort.h +119 -0
  31. package/dist/lib/darwin_aarch64/include/selva/strsearch.h +43 -0
  32. package/dist/lib/darwin_aarch64/include/selva/timestamp.h +25 -0
  33. package/dist/lib/darwin_aarch64/include/selva/traverse.h +65 -0
  34. package/dist/lib/darwin_aarch64/include/selva/types.h +110 -0
  35. package/dist/lib/darwin_aarch64/include/selva/vector.h +14 -0
  36. package/dist/lib/darwin_aarch64/include/selva/worker_ctx.h +13 -0
  37. package/dist/lib/darwin_aarch64/include/selva_error.h +137 -0
  38. package/dist/lib/darwin_aarch64/include/selva_lang_code.h +157 -0
  39. package/dist/lib/darwin_aarch64/include/tree.h +852 -0
  40. package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
  41. package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
  42. package/dist/lib/darwin_aarch64/libnode-v20.11.1.node +0 -0
  43. package/dist/lib/darwin_aarch64/libnode-v20.18.1.node +0 -0
  44. package/dist/lib/darwin_aarch64/libnode-v22.13.0.node +0 -0
  45. package/dist/lib/darwin_aarch64/libnode-v22.8.0.node +0 -0
  46. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  47. package/dist/lib/darwin_aarch64/libxxhash.0.8.2.dylib +0 -0
  48. package/dist/lib/darwin_aarch64/libxxhash.0.dylib +0 -0
  49. package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
  50. package/dist/lib/linux_aarch64/include/cdefs.h +317 -0
  51. package/dist/lib/linux_aarch64/include/libdeflate.h +322 -0
  52. package/dist/lib/linux_aarch64/include/libdeflate_strings.h +35 -0
  53. package/dist/lib/linux_aarch64/include/linker_set.h +109 -0
  54. package/dist/lib/linux_aarch64/include/queue.h +627 -0
  55. package/dist/lib/linux_aarch64/include/selva/_export.h +7 -0
  56. package/dist/lib/linux_aarch64/include/selva/align.h +9 -0
  57. package/dist/lib/linux_aarch64/include/selva/backoff_timeout.h +29 -0
  58. package/dist/lib/linux_aarch64/include/selva/base64.h +59 -0
  59. package/dist/lib/linux_aarch64/include/selva/base64url.h +59 -0
  60. package/dist/lib/linux_aarch64/include/selva/bitmap.h +95 -0
  61. package/dist/lib/linux_aarch64/include/selva/crc32c.h +17 -0
  62. package/dist/lib/linux_aarch64/include/selva/ctime.h +135 -0
  63. package/dist/lib/linux_aarch64/include/selva/db.h +284 -0
  64. package/dist/lib/linux_aarch64/include/selva/endian.h +301 -0
  65. package/dist/lib/linux_aarch64/include/selva/fast_linear_search.h +23 -0
  66. package/dist/lib/linux_aarch64/include/selva/fast_memmem.h +11 -0
  67. package/dist/lib/linux_aarch64/include/selva/fast_parsei.h +36 -0
  68. package/dist/lib/linux_aarch64/include/selva/fields.h +338 -0
  69. package/dist/lib/linux_aarch64/include/selva/find.h +47 -0
  70. package/dist/lib/linux_aarch64/include/selva/lpf.h +28 -0
  71. package/dist/lib/linux_aarch64/include/selva/node_id_set.h +43 -0
  72. package/dist/lib/linux_aarch64/include/selva/poptop.h +114 -0
  73. package/dist/lib/linux_aarch64/include/selva/queue_r.h +190 -0
  74. package/dist/lib/linux_aarch64/include/selva/selva_hash128.h +39 -0
  75. package/dist/lib/linux_aarch64/include/selva/selva_lang.h +105 -0
  76. package/dist/lib/linux_aarch64/include/selva/selva_math.h +37 -0
  77. package/dist/lib/linux_aarch64/include/selva/selva_string.h +672 -0
  78. package/dist/lib/linux_aarch64/include/selva/sort.h +119 -0
  79. package/dist/lib/linux_aarch64/include/selva/strsearch.h +43 -0
  80. package/dist/lib/linux_aarch64/include/selva/timestamp.h +25 -0
  81. package/dist/lib/linux_aarch64/include/selva/traverse.h +65 -0
  82. package/dist/lib/linux_aarch64/include/selva/types.h +110 -0
  83. package/dist/lib/linux_aarch64/include/selva/vector.h +14 -0
  84. package/dist/lib/linux_aarch64/include/selva/worker_ctx.h +13 -0
  85. package/dist/lib/linux_aarch64/include/selva_error.h +137 -0
  86. package/dist/lib/linux_aarch64/include/selva_lang_code.h +157 -0
  87. package/dist/lib/linux_aarch64/include/tree.h +852 -0
  88. package/dist/lib/linux_aarch64/libdeflate.so +0 -0
  89. package/dist/lib/linux_aarch64/libjemalloc_selva.so.2 +0 -0
  90. package/dist/lib/linux_aarch64/libnode-v20.11.1.node +0 -0
  91. package/dist/lib/linux_aarch64/libnode-v20.18.1.node +0 -0
  92. package/dist/lib/linux_aarch64/libnode-v22.13.0.node +0 -0
  93. package/dist/lib/linux_aarch64/libselva.so +0 -0
  94. package/dist/lib/linux_aarch64/libxxhash.so +0 -0
  95. package/dist/lib/linux_aarch64/libxxhash.so.0 +0 -0
  96. package/dist/lib/linux_aarch64/libxxhash.so.0.8.2 +0 -0
  97. package/dist/lib/linux_x86_64/include/cdefs.h +317 -0
  98. package/dist/lib/linux_x86_64/include/libdeflate.h +322 -0
  99. package/dist/lib/linux_x86_64/include/libdeflate_strings.h +35 -0
  100. package/dist/lib/linux_x86_64/include/linker_set.h +109 -0
  101. package/dist/lib/linux_x86_64/include/queue.h +627 -0
  102. package/dist/lib/linux_x86_64/include/selva/_export.h +7 -0
  103. package/dist/lib/linux_x86_64/include/selva/align.h +9 -0
  104. package/dist/lib/linux_x86_64/include/selva/backoff_timeout.h +29 -0
  105. package/dist/lib/linux_x86_64/include/selva/base64.h +59 -0
  106. package/dist/lib/linux_x86_64/include/selva/base64url.h +59 -0
  107. package/dist/lib/linux_x86_64/include/selva/bitmap.h +95 -0
  108. package/dist/lib/linux_x86_64/include/selva/crc32c.h +17 -0
  109. package/dist/lib/linux_x86_64/include/selva/ctime.h +135 -0
  110. package/dist/lib/linux_x86_64/include/selva/db.h +284 -0
  111. package/dist/lib/linux_x86_64/include/selva/endian.h +301 -0
  112. package/dist/lib/linux_x86_64/include/selva/fast_linear_search.h +23 -0
  113. package/dist/lib/linux_x86_64/include/selva/fast_memmem.h +11 -0
  114. package/dist/lib/linux_x86_64/include/selva/fast_parsei.h +36 -0
  115. package/dist/lib/linux_x86_64/include/selva/fields.h +338 -0
  116. package/dist/lib/linux_x86_64/include/selva/find.h +47 -0
  117. package/dist/lib/linux_x86_64/include/selva/lpf.h +28 -0
  118. package/dist/lib/linux_x86_64/include/selva/node_id_set.h +43 -0
  119. package/dist/lib/linux_x86_64/include/selva/poptop.h +114 -0
  120. package/dist/lib/linux_x86_64/include/selva/queue_r.h +190 -0
  121. package/dist/lib/linux_x86_64/include/selva/selva_hash128.h +39 -0
  122. package/dist/lib/linux_x86_64/include/selva/selva_lang.h +105 -0
  123. package/dist/lib/linux_x86_64/include/selva/selva_math.h +37 -0
  124. package/dist/lib/linux_x86_64/include/selva/selva_string.h +672 -0
  125. package/dist/lib/linux_x86_64/include/selva/sort.h +119 -0
  126. package/dist/lib/linux_x86_64/include/selva/strsearch.h +43 -0
  127. package/dist/lib/linux_x86_64/include/selva/timestamp.h +25 -0
  128. package/dist/lib/linux_x86_64/include/selva/traverse.h +65 -0
  129. package/dist/lib/linux_x86_64/include/selva/types.h +110 -0
  130. package/dist/lib/linux_x86_64/include/selva/vector.h +14 -0
  131. package/dist/lib/linux_x86_64/include/selva/worker_ctx.h +13 -0
  132. package/dist/lib/linux_x86_64/include/selva_error.h +137 -0
  133. package/dist/lib/linux_x86_64/include/selva_lang_code.h +157 -0
  134. package/dist/lib/linux_x86_64/include/tree.h +852 -0
  135. package/dist/lib/linux_x86_64/libdeflate.so +0 -0
  136. package/dist/lib/linux_x86_64/libjemalloc_selva.so.2 +0 -0
  137. package/dist/lib/linux_x86_64/libnode-v20.11.1.node +0 -0
  138. package/dist/lib/linux_x86_64/libnode-v20.18.1.node +0 -0
  139. package/dist/lib/linux_x86_64/libnode-v22.13.0.node +0 -0
  140. package/dist/lib/linux_x86_64/libselva.so +0 -0
  141. package/dist/lib/linux_x86_64/libxxhash.so +0 -0
  142. package/dist/lib/linux_x86_64/libxxhash.so.0 +0 -0
  143. package/dist/lib/linux_x86_64/libxxhash.so.0.8.2 +0 -0
  144. package/dist/src/client/crc32.d.ts +1 -0
  145. package/dist/src/client/crc32.js +281 -0
  146. package/dist/src/client/index.d.ts +58 -0
  147. package/dist/src/client/index.js +120 -0
  148. package/dist/src/client/modify/ModifyRes.d.ts +26 -0
  149. package/dist/src/client/modify/ModifyRes.js +98 -0
  150. package/dist/src/client/modify/alias.d.ts +4 -0
  151. package/dist/src/client/modify/alias.js +47 -0
  152. package/dist/src/client/modify/binary.d.ts +6 -0
  153. package/dist/src/client/modify/binary.js +61 -0
  154. package/dist/src/client/modify/create.d.ts +4 -0
  155. package/dist/src/client/modify/create.js +108 -0
  156. package/dist/src/client/modify/fixed.d.ts +5 -0
  157. package/dist/src/client/modify/fixed.js +118 -0
  158. package/dist/src/client/modify/hll.d.ts +4 -0
  159. package/dist/src/client/modify/hll.js +58 -0
  160. package/dist/src/client/modify/index.d.ts +4 -0
  161. package/dist/src/client/modify/index.js +4 -0
  162. package/dist/src/client/modify/modify.d.ts +5 -0
  163. package/dist/src/client/modify/modify.js +126 -0
  164. package/dist/src/client/modify/references/edge.d.ts +6 -0
  165. package/dist/src/client/modify/references/edge.js +158 -0
  166. package/dist/src/client/modify/references/reference.d.ts +11 -0
  167. package/dist/src/client/modify/references/reference.js +92 -0
  168. package/dist/src/client/modify/references/references.d.ts +17 -0
  169. package/dist/src/client/modify/references/references.js +271 -0
  170. package/dist/src/client/modify/remove.d.ts +2 -0
  171. package/dist/src/client/modify/remove.js +36 -0
  172. package/dist/src/client/modify/setCursor.d.ts +4 -0
  173. package/dist/src/client/modify/setCursor.js +36 -0
  174. package/dist/src/client/modify/string.d.ts +5 -0
  175. package/dist/src/client/modify/string.js +56 -0
  176. package/dist/src/client/modify/text.d.ts +5 -0
  177. package/dist/src/client/modify/text.js +14 -0
  178. package/dist/src/client/modify/types.d.ts +14 -0
  179. package/dist/src/client/modify/types.js +12 -0
  180. package/dist/src/client/modify/update.d.ts +5 -0
  181. package/dist/src/client/modify/update.js +80 -0
  182. package/dist/src/client/modify/upsert.d.ts +2 -0
  183. package/dist/src/client/modify/upsert.js +40 -0
  184. package/dist/src/client/operations.d.ts +31 -0
  185. package/dist/src/client/operations.js +110 -0
  186. package/dist/src/client/query/BasedDbQuery.d.ts +40 -0
  187. package/dist/src/client/query/BasedDbQuery.js +235 -0
  188. package/dist/src/client/query/BasedIterable.d.ts +26 -0
  189. package/dist/src/client/query/BasedIterable.js +113 -0
  190. package/dist/src/client/query/debug.d.ts +3 -0
  191. package/dist/src/client/query/debug.js +117 -0
  192. package/dist/src/client/query/display.d.ts +5 -0
  193. package/dist/src/client/query/display.js +233 -0
  194. package/dist/src/client/query/filter/FilterBranch.d.ts +13 -0
  195. package/dist/src/client/query/filter/FilterBranch.js +29 -0
  196. package/dist/src/client/query/filter/createFixedFilterBuffer.d.ts +3 -0
  197. package/dist/src/client/query/filter/createFixedFilterBuffer.js +101 -0
  198. package/dist/src/client/query/filter/createReferenceFilter.d.ts +2 -0
  199. package/dist/src/client/query/filter/createReferenceFilter.js +24 -0
  200. package/dist/src/client/query/filter/createVariableFilterBuffer.d.ts +2 -0
  201. package/dist/src/client/query/filter/createVariableFilterBuffer.js +81 -0
  202. package/dist/src/client/query/filter/filter.d.ts +10 -0
  203. package/dist/src/client/query/filter/filter.js +134 -0
  204. package/dist/src/client/query/filter/operators.d.ts +6 -0
  205. package/dist/src/client/query/filter/operators.js +98 -0
  206. package/dist/src/client/query/filter/parseFilterValue.d.ts +2 -0
  207. package/dist/src/client/query/filter/parseFilterValue.js +99 -0
  208. package/dist/src/client/query/filter/primitiveFilter.d.ts +4 -0
  209. package/dist/src/client/query/filter/primitiveFilter.js +47 -0
  210. package/dist/src/client/query/filter/toBuffer.d.ts +3 -0
  211. package/dist/src/client/query/filter/toBuffer.js +100 -0
  212. package/dist/src/client/query/filter/types.d.ts +6 -0
  213. package/dist/src/client/query/filter/types.js +7 -0
  214. package/dist/src/client/query/include/props.d.ts +6 -0
  215. package/dist/src/client/query/include/props.js +44 -0
  216. package/dist/src/client/query/include/toBuffer.d.ts +3 -0
  217. package/dist/src/client/query/include/toBuffer.js +82 -0
  218. package/dist/src/client/query/include/utils.d.ts +6 -0
  219. package/dist/src/client/query/include/utils.js +41 -0
  220. package/dist/src/client/query/include/walk.d.ts +3 -0
  221. package/dist/src/client/query/include/walk.js +70 -0
  222. package/dist/src/client/query/query.d.ts +10 -0
  223. package/dist/src/client/query/query.js +11 -0
  224. package/dist/src/client/query/queryDef.d.ts +3 -0
  225. package/dist/src/client/query/queryDef.js +54 -0
  226. package/dist/src/client/query/read/read.d.ts +8 -0
  227. package/dist/src/client/query/read/read.js +303 -0
  228. package/dist/src/client/query/registerQuery.d.ts +2 -0
  229. package/dist/src/client/query/registerQuery.js +31 -0
  230. package/dist/src/client/query/search/index.d.ts +6 -0
  231. package/dist/src/client/query/search/index.js +82 -0
  232. package/dist/src/client/query/sort.d.ts +3 -0
  233. package/dist/src/client/query/sort.js +18 -0
  234. package/dist/src/client/query/subscription/index.d.ts +5 -0
  235. package/dist/src/client/query/subscription/index.js +52 -0
  236. package/dist/src/client/query/subscription/markers.d.ts +10 -0
  237. package/dist/src/client/query/subscription/markers.js +213 -0
  238. package/dist/src/client/query/subscription/run.d.ts +5 -0
  239. package/dist/src/client/query/subscription/run.js +69 -0
  240. package/dist/src/client/query/subscription/types.d.ts +33 -0
  241. package/dist/src/client/query/subscription/types.js +3 -0
  242. package/dist/src/client/query/thresholds.d.ts +6 -0
  243. package/dist/src/client/query/thresholds.js +16 -0
  244. package/dist/src/client/query/toBuffer.d.ts +3 -0
  245. package/dist/src/client/query/toBuffer.js +229 -0
  246. package/dist/src/client/query/types.d.ts +88 -0
  247. package/dist/src/client/query/types.js +15 -0
  248. package/dist/src/client/query/validation.d.ts +15 -0
  249. package/dist/src/client/query/validation.js +59 -0
  250. package/dist/src/client/string.d.ts +5 -0
  251. package/dist/src/client/string.js +73 -0
  252. package/dist/src/client/tree.d.ts +1 -0
  253. package/dist/src/client/tree.js +5 -0
  254. package/dist/src/index.d.ts +31 -0
  255. package/dist/src/index.js +85 -0
  256. package/dist/src/native.d.ts +28 -0
  257. package/dist/src/native.js +96 -0
  258. package/dist/src/server/csmt/draw-dot.d.ts +2 -0
  259. package/dist/src/server/csmt/draw-dot.js +32 -0
  260. package/dist/src/server/csmt/index.d.ts +4 -0
  261. package/dist/src/server/csmt/index.js +5 -0
  262. package/dist/src/server/csmt/match.d.ts +7 -0
  263. package/dist/src/server/csmt/match.js +10 -0
  264. package/dist/src/server/csmt/memebership-proof.d.ts +7 -0
  265. package/dist/src/server/csmt/memebership-proof.js +122 -0
  266. package/dist/src/server/csmt/tree-utils.d.ts +6 -0
  267. package/dist/src/server/csmt/tree-utils.js +33 -0
  268. package/dist/src/server/csmt/tree.d.ts +2 -0
  269. package/dist/src/server/csmt/tree.js +209 -0
  270. package/dist/src/server/csmt/types.d.ts +40 -0
  271. package/dist/src/server/csmt/types.js +2 -0
  272. package/dist/src/server/index.d.ts +85 -0
  273. package/dist/src/server/index.js +407 -0
  274. package/dist/src/server/migrate/index.d.ts +7 -0
  275. package/dist/src/server/migrate/index.js +108 -0
  276. package/dist/src/server/migrate/worker.d.ts +1 -0
  277. package/dist/src/server/migrate/worker.js +81 -0
  278. package/dist/src/server/save.d.ts +2 -0
  279. package/dist/src/server/save.js +66 -0
  280. package/dist/src/server/schema/schema.d.ts +3 -0
  281. package/dist/src/server/schema/schema.js +4 -0
  282. package/dist/src/server/schema/selvaBuffer.d.ts +4 -0
  283. package/dist/src/server/schema/selvaBuffer.js +102 -0
  284. package/dist/src/server/schema/typeDef.d.ts +6 -0
  285. package/dist/src/server/schema/typeDef.js +315 -0
  286. package/dist/src/server/schema/types.d.ts +127 -0
  287. package/dist/src/server/schema/types.js +113 -0
  288. package/dist/src/server/schema/utils.d.ts +6 -0
  289. package/dist/src/server/schema/utils.js +21 -0
  290. package/dist/src/server/start.d.ts +6 -0
  291. package/dist/src/server/start.js +86 -0
  292. package/dist/src/server/tree.d.ts +13 -0
  293. package/dist/src/server/tree.js +33 -0
  294. package/dist/src/server/worker.d.ts +1 -0
  295. package/dist/src/server/worker.js +35 -0
  296. package/package.json +48 -0
@@ -0,0 +1,92 @@
1
+ import { ModifyError, ModifyState } from '../ModifyRes.js';
2
+ import { setCursor } from '../setCursor.js';
3
+ import { DELETE, RANGE_ERR } from '../types.js';
4
+ import { getEdgeSize, writeEdges } from './edge.js';
5
+ import { dbUpdateFromUpsert } from './references.js';
6
+ function writeRef(id, ctx, schema, def, parentId, modifyOp, hasEdges) {
7
+ if (ctx.len + 16 > ctx.max) {
8
+ return RANGE_ERR;
9
+ }
10
+ ctx.markNodeDirty(ctx.db.schemaTypesParsed[def.inverseTypeName], id);
11
+ setCursor(ctx, schema, def.prop, parentId, modifyOp);
12
+ ctx.buf[ctx.len++] = modifyOp;
13
+ ctx.buf[ctx.len++] = hasEdges ? 1 : 0;
14
+ ctx.buf[ctx.len++] = id;
15
+ ctx.buf[ctx.len++] = id >>>= 8;
16
+ ctx.buf[ctx.len++] = id >>>= 8;
17
+ ctx.buf[ctx.len++] = id >>>= 8;
18
+ }
19
+ function singleReferenceEdges(ref, ctx, schema, def, parentId, modifyOp) {
20
+ let id = ref.id;
21
+ if (typeof id !== 'number') {
22
+ if (id instanceof ModifyState) {
23
+ if (id.error) {
24
+ return id.error;
25
+ }
26
+ id = id.tmpId;
27
+ }
28
+ }
29
+ if (id > 0) {
30
+ const edgesLen = def.edgesTotalLen || getEdgeSize(def, ref);
31
+ if (edgesLen === 0) {
32
+ return writeRef(id, ctx, schema, def, parentId, modifyOp, false);
33
+ }
34
+ let err = writeRef(id, ctx, schema, def, parentId, modifyOp, true);
35
+ if (err) {
36
+ return err;
37
+ }
38
+ if (ctx.len + 4 + edgesLen > ctx.max) {
39
+ return RANGE_ERR;
40
+ }
41
+ let sizepos = ctx.len;
42
+ ctx.len += 4;
43
+ err = writeEdges(def, ref, ctx);
44
+ if (err) {
45
+ return err;
46
+ }
47
+ let size = ctx.len - sizepos;
48
+ ctx.buf[sizepos++] = size;
49
+ ctx.buf[sizepos++] = size >>>= 8;
50
+ ctx.buf[sizepos++] = size >>>= 8;
51
+ ctx.buf[sizepos] = size >>>= 8;
52
+ }
53
+ else {
54
+ return new ModifyError(def, ref);
55
+ }
56
+ }
57
+ export function writeReference(value, ctx, schema, def, res, modifyOp) {
58
+ if (value === null) {
59
+ if (ctx.len + 11 > ctx.max) {
60
+ return RANGE_ERR;
61
+ }
62
+ setCursor(ctx, schema, def.prop, res.tmpId, modifyOp);
63
+ ctx.buf[ctx.len++] = DELETE;
64
+ }
65
+ else if (typeof value === 'number') {
66
+ return writeRef(value, ctx, schema, def, res.tmpId, modifyOp, false);
67
+ }
68
+ else if (value instanceof ModifyState) {
69
+ if (value.error) {
70
+ return value.error;
71
+ }
72
+ return writeRef(value.tmpId, ctx, schema, def, res.tmpId, modifyOp, false);
73
+ }
74
+ else if (typeof value === 'object' && value !== null) {
75
+ if (def.edges) {
76
+ return singleReferenceEdges(value, ctx, schema, def, res.tmpId, modifyOp);
77
+ }
78
+ else if (typeof value.id === 'number') {
79
+ return writeRef(value.id, ctx, schema, def, res.tmpId, modifyOp, false);
80
+ }
81
+ else if (typeof value.upsert === 'object' && value.upsert !== null) {
82
+ dbUpdateFromUpsert(ctx, schema, def, res, ctx.db.upsert(def.inverseTypeName, value.upsert));
83
+ }
84
+ else {
85
+ return new ModifyError(def, value);
86
+ }
87
+ }
88
+ else {
89
+ return new ModifyError(def, value);
90
+ }
91
+ }
92
+ //# sourceMappingURL=reference.js.map
@@ -0,0 +1,17 @@
1
+ import { ModifyCtx } from '../../../index.js';
2
+ import { PropDef, SchemaTypeDef } from '../../../server/schema/types.js';
3
+ import { ModifyState } from '../ModifyRes.js';
4
+ import { ModifyErr, ModifyOp } from '../types.js';
5
+ export type RefModifyOpts = {
6
+ id?: number | ModifyState;
7
+ $index?: number;
8
+ } & Record<`$${string}`, any>;
9
+ export type RefModify = ModifyState | RefModifyOpts | number;
10
+ export type Refs = RefModify[] | {
11
+ add?: RefModify[] | RefModify;
12
+ update?: RefModify[] | RefModify;
13
+ delete?: RefModify[] | RefModify;
14
+ upsert: RefModify[] | RefModify;
15
+ };
16
+ export declare function writeReferences(value: any, ctx: ModifyCtx, schema: SchemaTypeDef, def: PropDef, res: ModifyState, mod: ModifyOp): ModifyErr;
17
+ export declare const dbUpdateFromUpsert: (ctx: ModifyCtx, schema: SchemaTypeDef, def: PropDef, res: ModifyState, promise: Promise<any>) => void;
@@ -0,0 +1,271 @@
1
+ import { REFERENCES, } from '../../../server/schema/types.js';
2
+ import { ModifyError, ModifyState } from '../ModifyRes.js';
3
+ import { setCursor } from '../setCursor.js';
4
+ import { DELETE, RANGE_ERR } from '../types.js';
5
+ import { writeEdges } from './edge.js';
6
+ export function writeReferences(value, ctx, schema, def, res, mod) {
7
+ if (typeof value !== 'object') {
8
+ return new ModifyError(def, value);
9
+ }
10
+ if (value === null) {
11
+ if (ctx.len + 11 > ctx.max) {
12
+ return RANGE_ERR;
13
+ }
14
+ setCursor(ctx, schema, def.prop, res.tmpId, mod);
15
+ ctx.buf[ctx.len++] = DELETE;
16
+ return;
17
+ }
18
+ if (Array.isArray(value)) {
19
+ return updateRefs(def, ctx, schema, mod, value, res.tmpId, 0);
20
+ }
21
+ for (const key in value) {
22
+ const val = value[key];
23
+ let err;
24
+ if (!Array.isArray(val)) {
25
+ err = new ModifyError(def, value);
26
+ }
27
+ else if (key === 'delete') {
28
+ err = deleteRefs(def, ctx, schema, mod, val, res.tmpId);
29
+ }
30
+ else if (key === 'add') {
31
+ err = updateRefs(def, ctx, schema, mod, val, res.tmpId, 1);
32
+ }
33
+ else if (key === 'upsert') {
34
+ dbUpdateFromUpsert(ctx, schema, def, res, Promise.all(val.map((val) => ctx.db.upsert(def.inverseTypeName, val))));
35
+ }
36
+ else {
37
+ err = new ModifyError(def, value);
38
+ }
39
+ if (err) {
40
+ return err;
41
+ }
42
+ }
43
+ }
44
+ export const dbUpdateFromUpsert = (ctx, schema, def, res, promise) => {
45
+ res.promises ??= [];
46
+ res.promises.push(promise.then((result) => {
47
+ let payload = {};
48
+ let i = 0;
49
+ for (; i < def.path.length - 1; i++) {
50
+ payload = payload[def.path[i]] = {};
51
+ }
52
+ payload[def.path[i]] = result;
53
+ return ctx.db.update(schema.type, res.tmpId, payload);
54
+ }));
55
+ };
56
+ function deleteRefs(def, ctx, schema, modifyOp, refs, parentId) {
57
+ let size = 4 * refs.length + 1;
58
+ if (ctx.len + 10 + size > ctx.max) {
59
+ return RANGE_ERR;
60
+ }
61
+ setCursor(ctx, schema, def.prop, parentId, modifyOp);
62
+ ctx.buf[ctx.len++] = modifyOp;
63
+ ctx.buf[ctx.len++] = size;
64
+ ctx.buf[ctx.len++] = size >>>= 8;
65
+ ctx.buf[ctx.len++] = size >>>= 8;
66
+ ctx.buf[ctx.len++] = size >>>= 8;
67
+ ctx.buf[ctx.len++] = 2;
68
+ for (let ref of refs) {
69
+ if (typeof ref === 'number') {
70
+ ctx.buf[ctx.len++] = ref;
71
+ ctx.buf[ctx.len++] = ref >>>= 8;
72
+ ctx.buf[ctx.len++] = ref >>>= 8;
73
+ ctx.buf[ctx.len++] = ref >>>= 8;
74
+ }
75
+ else if (ref instanceof ModifyState) {
76
+ if (ref.error) {
77
+ return ref.error;
78
+ }
79
+ ref = ref.tmpId;
80
+ ctx.buf[ctx.len++] = ref;
81
+ ctx.buf[ctx.len++] = ref >>>= 8;
82
+ ctx.buf[ctx.len++] = ref >>>= 8;
83
+ ctx.buf[ctx.len++] = ref >>>= 8;
84
+ }
85
+ else {
86
+ return new ModifyError(def, refs);
87
+ }
88
+ }
89
+ }
90
+ function updateRefs(def, ctx, schema, mod, refs, parentId, op) {
91
+ if (ctx.len + 19 + refs.length * 4 > ctx.max) {
92
+ return RANGE_ERR;
93
+ }
94
+ setCursor(ctx, schema, def.prop, parentId, mod);
95
+ const initpos = ctx.len;
96
+ const nrOrErr = putRefs(def, ctx, mod, refs, op);
97
+ if (nrOrErr) {
98
+ if (typeof nrOrErr === 'number') {
99
+ if (nrOrErr === refs.length) {
100
+ // reset
101
+ ctx.len = initpos;
102
+ }
103
+ else if (ctx.len + 2 > ctx.max) {
104
+ return RANGE_ERR;
105
+ }
106
+ else {
107
+ ctx.buf[ctx.len++] = 0;
108
+ ctx.buf[ctx.len++] = REFERENCES;
109
+ }
110
+ return appendRefs(def, ctx, mod, refs, op, nrOrErr);
111
+ }
112
+ return nrOrErr;
113
+ }
114
+ }
115
+ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
116
+ if (ctx.len + 10 > ctx.max) {
117
+ return RANGE_ERR;
118
+ }
119
+ const hasEdges = !!def.edges;
120
+ ctx.buf[ctx.len++] = modifyOp;
121
+ let i = refs.length - remaining;
122
+ let totalpos = ctx.len;
123
+ ctx.len += 4;
124
+ ctx.buf[ctx.len++] = i === 0 ? op : 1; // if it just did a PUT, it should ADD not overwrite the remaining
125
+ ctx.buf[ctx.len++] = remaining;
126
+ ctx.buf[ctx.len++] = remaining >>>= 8;
127
+ ctx.buf[ctx.len++] = remaining >>>= 8;
128
+ ctx.buf[ctx.len++] = remaining >>>= 8;
129
+ for (; i < refs.length; i++) {
130
+ const ref = refs[i];
131
+ let id;
132
+ let index;
133
+ if (typeof ref === 'object') {
134
+ if (ref instanceof ModifyState) {
135
+ if (ref.error) {
136
+ return ref.error;
137
+ }
138
+ id = ref.tmpId;
139
+ }
140
+ else if (ref.id instanceof ModifyState) {
141
+ if (ref.id.error) {
142
+ return ref.id.error;
143
+ }
144
+ id = ref.id.tmpId;
145
+ index = ref.$index;
146
+ }
147
+ else if (ref.id > 0) {
148
+ id = ref.id;
149
+ index = ref.$index;
150
+ }
151
+ else {
152
+ return new ModifyError(def, refs);
153
+ }
154
+ }
155
+ else if (ref > 0) {
156
+ id = ref;
157
+ }
158
+ else {
159
+ return new ModifyError(def, refs);
160
+ }
161
+ if (hasEdges) {
162
+ if (index === undefined) {
163
+ if (ctx.len + 9 > ctx.max) {
164
+ return RANGE_ERR;
165
+ }
166
+ ctx.buf[ctx.len++] = 1;
167
+ ctx.buf[ctx.len++] = id;
168
+ ctx.buf[ctx.len++] = id >>>= 8;
169
+ ctx.buf[ctx.len++] = id >>>= 8;
170
+ ctx.buf[ctx.len++] = id >>>= 8;
171
+ }
172
+ else if (typeof index === 'number') {
173
+ if (ctx.len + 13 > ctx.max) {
174
+ return RANGE_ERR;
175
+ }
176
+ ctx.buf[ctx.len++] = 2;
177
+ ctx.buf[ctx.len++] = id;
178
+ ctx.buf[ctx.len++] = id >>>= 8;
179
+ ctx.buf[ctx.len++] = id >>>= 8;
180
+ ctx.buf[ctx.len++] = id >>>= 8;
181
+ ctx.buf[ctx.len++] = index;
182
+ ctx.buf[ctx.len++] = index >>>= 8;
183
+ ctx.buf[ctx.len++] = index >>>= 8;
184
+ ctx.buf[ctx.len++] = index >>>= 8;
185
+ }
186
+ else {
187
+ return new ModifyError(def, refs);
188
+ }
189
+ let sizepos = ctx.len;
190
+ ctx.len += 4;
191
+ const err = writeEdges(def, ref, ctx);
192
+ if (err) {
193
+ return err;
194
+ }
195
+ let size = ctx.len - sizepos - 4;
196
+ ctx.buf[sizepos++] = size;
197
+ ctx.buf[sizepos++] = size >>>= 8;
198
+ ctx.buf[sizepos++] = size >>>= 8;
199
+ ctx.buf[sizepos] = size >>>= 8;
200
+ }
201
+ else if (index === undefined) {
202
+ if (ctx.len + 5 > ctx.max) {
203
+ return RANGE_ERR;
204
+ }
205
+ ctx.buf[ctx.len++] = 0;
206
+ ctx.buf[ctx.len++] = id;
207
+ ctx.buf[ctx.len++] = id >>>= 8;
208
+ ctx.buf[ctx.len++] = id >>>= 8;
209
+ ctx.buf[ctx.len++] = id >>>= 8;
210
+ }
211
+ else if (typeof index === 'number') {
212
+ if (ctx.len + 9 > ctx.max) {
213
+ return RANGE_ERR;
214
+ }
215
+ ctx.buf[ctx.len++] = 3;
216
+ ctx.buf[ctx.len++] = id;
217
+ ctx.buf[ctx.len++] = id >>>= 8;
218
+ ctx.buf[ctx.len++] = id >>>= 8;
219
+ ctx.buf[ctx.len++] = id >>>= 8;
220
+ ctx.buf[ctx.len++] = index;
221
+ ctx.buf[ctx.len++] = index >>>= 8;
222
+ ctx.buf[ctx.len++] = index >>>= 8;
223
+ ctx.buf[ctx.len++] = index >>>= 8;
224
+ }
225
+ else {
226
+ return new ModifyError(def, refs);
227
+ }
228
+ ctx.markNodeDirty(ctx.db.schemaTypesParsed[def.inverseTypeName], id);
229
+ }
230
+ let size = ctx.len - totalpos - 4;
231
+ ctx.buf[totalpos++] = size;
232
+ ctx.buf[totalpos++] = size >>>= 8;
233
+ ctx.buf[totalpos++] = size >>>= 8;
234
+ ctx.buf[totalpos] = size >>>= 8;
235
+ }
236
+ function putRefs(def, ctx, modifyOp, refs, op) {
237
+ let size = refs.length * 4 + 1;
238
+ ctx.buf[ctx.len++] = modifyOp;
239
+ ctx.buf[ctx.len++] = size;
240
+ ctx.buf[ctx.len++] = size >>>= 8;
241
+ ctx.buf[ctx.len++] = size >>>= 8;
242
+ ctx.buf[ctx.len++] = size >>>= 8;
243
+ ctx.buf[ctx.len++] = op === 0 ? 3 : 4;
244
+ ctx.len = (ctx.len + 3) & ~3;
245
+ let i = 0;
246
+ for (; i < refs.length; i++) {
247
+ let ref = refs[i];
248
+ if (typeof ref === 'number') {
249
+ ctx.buf[ctx.len++] = ref;
250
+ ctx.buf[ctx.len++] = ref >>>= 8;
251
+ ctx.buf[ctx.len++] = ref >>>= 8;
252
+ ctx.buf[ctx.len++] = ref >>>= 8;
253
+ }
254
+ else if (ref instanceof ModifyState) {
255
+ if (ref.error) {
256
+ return ref.error;
257
+ }
258
+ ref = ref.tmpId;
259
+ ctx.buf[ctx.len++] = ref;
260
+ ctx.buf[ctx.len++] = ref >>>= 8;
261
+ ctx.buf[ctx.len++] = ref >>>= 8;
262
+ ctx.buf[ctx.len++] = ref >>>= 8;
263
+ }
264
+ else {
265
+ break;
266
+ }
267
+ ctx.markNodeDirty(ctx.db.schemaTypesParsed[def.inverseTypeName], ref);
268
+ }
269
+ return refs.length - i;
270
+ }
271
+ //# sourceMappingURL=references.js.map
@@ -0,0 +1,2 @@
1
+ import { DbClient } from '../index.js';
2
+ export declare const remove: (db: DbClient, type: string, id: number) => boolean;
@@ -0,0 +1,36 @@
1
+ import { flushBuffer, startDrain } from '../operations.js';
2
+ import { setCursor } from './setCursor.js';
3
+ import { UPDATE } from './types.js';
4
+ export const remove = (db, type, id) => {
5
+ const ctx = db.modifyCtx;
6
+ const schema = db.schemaTypesParsed[type];
7
+ const separate = schema.separate;
8
+ if (separate) {
9
+ const size = 12 + separate.length * 12;
10
+ if (ctx.len + size > ctx.max) {
11
+ flushBuffer(db);
12
+ return remove(db, type, id);
13
+ }
14
+ setCursor(ctx, schema, 0, id, UPDATE);
15
+ ctx.buf[ctx.len++] = 4;
16
+ for (const s of separate) {
17
+ setCursor(ctx, schema, s.prop, id, UPDATE);
18
+ ctx.buf[ctx.len++] = 4;
19
+ }
20
+ ctx.buf[ctx.len++] = 10;
21
+ }
22
+ else {
23
+ if (ctx.len + 12 > ctx.max) {
24
+ flushBuffer(db);
25
+ return remove(db, type, id);
26
+ }
27
+ setCursor(ctx, schema, 0, id, UPDATE);
28
+ ctx.buf[ctx.len++] = 4;
29
+ ctx.buf[ctx.len++] = 10;
30
+ }
31
+ if (!db.isDraining) {
32
+ startDrain(db);
33
+ }
34
+ return true;
35
+ };
36
+ //# sourceMappingURL=remove.js.map
@@ -0,0 +1,4 @@
1
+ import { ModifyCtx } from '../../index.js';
2
+ import { SchemaTypeDef } from '../../server/schema/types.js';
3
+ import { ModifyOp } from './types.js';
4
+ export declare const setCursor: (ctx: ModifyCtx, schema: SchemaTypeDef, field: number, id: number, modifyOp: ModifyOp, ignoreField?: boolean) => void;
@@ -0,0 +1,36 @@
1
+ import { CREATE, SWITCH_FIELD, SWITCH_ID_CREATE, SWITCH_ID_UPDATE, SWITCH_TYPE, } from './types.js';
2
+ export const setCursor = (ctx, schema, field, id, modifyOp, ignoreField) => {
3
+ const prefix0 = schema.idUint8[0];
4
+ const prefix1 = schema.idUint8[1];
5
+ if (ctx.prefix0 !== prefix0 || ctx.prefix1 !== prefix1) {
6
+ ctx.buf[ctx.len++] = SWITCH_TYPE; // switch node type
7
+ ctx.buf[ctx.len++] = prefix0; // type1
8
+ ctx.buf[ctx.len++] = prefix1; // type2
9
+ ctx.prefix0 = prefix0;
10
+ ctx.prefix1 = prefix1;
11
+ ctx.field = -1;
12
+ ctx.id = -1;
13
+ ctx.lastMain = -1;
14
+ if (modifyOp === CREATE) {
15
+ ctx.markTypeDirty(schema);
16
+ }
17
+ }
18
+ if (!ignoreField && ctx.field !== field) {
19
+ ctx.buf[ctx.len++] = SWITCH_FIELD; // switch field
20
+ ctx.buf[ctx.len++] = field; // actual field
21
+ // add start and len if its main
22
+ ctx.field = field;
23
+ }
24
+ if (ctx.id !== id) {
25
+ ctx.markNodeDirty(schema, id);
26
+ ctx.id = id;
27
+ ctx.lastMain = -1;
28
+ ctx.buf[ctx.len++] =
29
+ modifyOp === CREATE ? SWITCH_ID_CREATE : SWITCH_ID_UPDATE; // swtch id
30
+ ctx.buf[ctx.len++] = id;
31
+ ctx.buf[ctx.len++] = id >>>= 8;
32
+ ctx.buf[ctx.len++] = id >>>= 8;
33
+ ctx.buf[ctx.len++] = id >>>= 8;
34
+ }
35
+ };
36
+ //# sourceMappingURL=setCursor.js.map
@@ -0,0 +1,5 @@
1
+ import { LangCode } from '@based/schema';
2
+ import { ModifyCtx } from '../../index.js';
3
+ import { SchemaTypeDef, PropDef } from '../../server/schema/types.js';
4
+ import { ModifyOp, ModifyErr } from './types.js';
5
+ export declare function writeString(lang: LangCode, value: string | null | Buffer, ctx: ModifyCtx, def: SchemaTypeDef, t: PropDef, parentId: number, modifyOp: ModifyOp): ModifyErr;
@@ -0,0 +1,56 @@
1
+ import { CREATE, UPDATE, RANGE_ERR, DELETE, } from './types.js';
2
+ import { ModifyError } from './ModifyRes.js';
3
+ import { setCursor } from './setCursor.js';
4
+ import { write } from '../string.js';
5
+ // allow setting buffer in modify create for strings
6
+ // add compression handling for main buffer
7
+ // add compression handling for edge fields
8
+ export function writeString(lang, value, ctx, def, t, parentId, modifyOp) {
9
+ const isBuffer = value instanceof Buffer;
10
+ if (typeof value !== 'string' && value !== null && !isBuffer) {
11
+ return new ModifyError(t, value);
12
+ }
13
+ const len = value?.length;
14
+ if (!len) {
15
+ if (modifyOp === UPDATE) {
16
+ if (ctx.len + 11 > ctx.max) {
17
+ return RANGE_ERR;
18
+ }
19
+ setCursor(ctx, def, t.prop, parentId, modifyOp);
20
+ ctx.buf[ctx.len++] = DELETE;
21
+ }
22
+ }
23
+ else {
24
+ let size = isBuffer
25
+ ? value.byteLength
26
+ : Buffer.byteLength(value, 'utf8') + 6;
27
+ if (ctx.len + 20 + size > ctx.max) {
28
+ // 5 compression size
29
+ return RANGE_ERR;
30
+ }
31
+ if (modifyOp === CREATE) {
32
+ def.stringPropsCurrent[t.prop] = 2;
33
+ ctx.hasStringField++;
34
+ }
35
+ setCursor(ctx, def, t.prop, parentId, modifyOp);
36
+ // TODO if buffer check if second byte is zero or one
37
+ // modOp | size u32 | stringprotocol | string
38
+ ctx.buf[ctx.len] = modifyOp;
39
+ ctx.len += 5;
40
+ if (isBuffer) {
41
+ ctx.buf.set(value, ctx.len);
42
+ }
43
+ else {
44
+ const isNoCompression = t.compression === 0;
45
+ // @ts-ignore stupid str
46
+ size = write(ctx.buf, value, ctx.len, isNoCompression, lang);
47
+ }
48
+ let sizepos = ctx.len + 1 - 5;
49
+ ctx.len += size;
50
+ ctx.buf[sizepos++] = size;
51
+ ctx.buf[sizepos++] = size >>>= 8;
52
+ ctx.buf[sizepos++] = size >>>= 8;
53
+ ctx.buf[sizepos] = size >>>= 8;
54
+ }
55
+ }
56
+ //# sourceMappingURL=string.js.map
@@ -0,0 +1,5 @@
1
+ import { ModifyCtx } from '../../index.js';
2
+ import { ModifyOp, ModifyErr } from './types.js';
3
+ import { SchemaTypeDef, PropDef } from '../../server/schema/types.js';
4
+ export declare function writeText(value: any, // TODO YOLO
5
+ ctx: ModifyCtx, def: SchemaTypeDef, t: PropDef, parentId: number, modifyOp: ModifyOp): ModifyErr;
@@ -0,0 +1,14 @@
1
+ import { langCodesMap } from '@based/schema';
2
+ import { writeString } from './string.js';
3
+ export function writeText(value, // TODO YOLO
4
+ ctx, def, t, parentId, modifyOp) {
5
+ for (const lang in value) {
6
+ const langCode = langCodesMap.get(lang) || langCodesMap.get('en'); // TODO use proper fallback
7
+ const s = value[lang];
8
+ const err = writeString(langCode, s, ctx, def, t, parentId, modifyOp);
9
+ if (err) {
10
+ return err;
11
+ }
12
+ }
13
+ }
14
+ //# sourceMappingURL=text.js.map
@@ -0,0 +1,14 @@
1
+ import { ModifyError } from './ModifyRes.js';
2
+ export declare const CREATE = 3;
3
+ export declare const UPDATE = 6;
4
+ export declare const DELETE = 11;
5
+ export declare const MERGE_MAIN = 4;
6
+ export declare const RANGE_ERR = 1;
7
+ export declare const INCREMENT = 12;
8
+ export declare const DECREMENT = 13;
9
+ export declare const SWITCH_TYPE = 2;
10
+ export declare const SWITCH_FIELD = 0;
11
+ export declare const SWITCH_ID_CREATE = 9;
12
+ export declare const SWITCH_ID_UPDATE = 1;
13
+ export type ModifyErr = typeof RANGE_ERR | ModifyError | void;
14
+ export type ModifyOp = typeof CREATE | typeof UPDATE | typeof INCREMENT;
@@ -0,0 +1,12 @@
1
+ export const CREATE = 3;
2
+ export const UPDATE = 6;
3
+ export const DELETE = 11;
4
+ export const MERGE_MAIN = 4;
5
+ export const RANGE_ERR = 1;
6
+ export const INCREMENT = 12;
7
+ export const DECREMENT = 13;
8
+ export const SWITCH_TYPE = 2;
9
+ export const SWITCH_FIELD = 0;
10
+ export const SWITCH_ID_CREATE = 9;
11
+ export const SWITCH_ID_UPDATE = 1;
12
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,5 @@
1
+ import { ModifyRes } from './ModifyRes.js';
2
+ import { DbClient } from '../index.js';
3
+ type Payload = Record<string, any>;
4
+ export declare const update: (db: DbClient, type: string, id: number, obj: Payload, overwrite?: boolean) => ModifyRes;
5
+ export {};
@@ -0,0 +1,80 @@
1
+ import { startDrain, flushBuffer } from '../operations.js';
2
+ import { setCursor } from './setCursor.js';
3
+ import { modify } from './modify.js';
4
+ import { ModifyState } from './ModifyRes.js';
5
+ import { RANGE_ERR, UPDATE } from './types.js';
6
+ import { appendFixedValue } from './fixed.js';
7
+ import { getSubscriptionMarkers } from '../query/subscription/index.js';
8
+ const appendUpdate = (ctx, def, obj, res, overwrite) => {
9
+ const err = modify(ctx, res, obj, def, UPDATE, def.tree, overwrite);
10
+ if (err) {
11
+ return err;
12
+ }
13
+ if (def.updateTs) {
14
+ const updateTs = Date.now();
15
+ for (const prop of def.updateTs) {
16
+ if (ctx.mergeMain) {
17
+ ctx.mergeMain.push(prop, updateTs);
18
+ ctx.mergeMainSize += prop.len + 4;
19
+ }
20
+ else {
21
+ ctx.mergeMain = [prop, updateTs];
22
+ ctx.mergeMainSize = prop.len + 4;
23
+ }
24
+ }
25
+ }
26
+ if (ctx.mergeMain) {
27
+ let { mergeMain, mergeMainSize } = ctx;
28
+ ctx.mergeMainSize = 0;
29
+ ctx.mergeMain = null;
30
+ if (ctx.len + 15 + mergeMain.length * 4 > ctx.max) {
31
+ return RANGE_ERR;
32
+ }
33
+ setCursor(ctx, def, 0, res.tmpId, UPDATE);
34
+ ctx.buf[ctx.len++] = 5;
35
+ ctx.buf[ctx.len++] = mergeMainSize;
36
+ ctx.buf[ctx.len++] = mergeMainSize >>>= 8;
37
+ ctx.buf[ctx.len++] = mergeMainSize >>>= 8;
38
+ ctx.buf[ctx.len++] = mergeMainSize >>>= 8;
39
+ for (let i = 0; i < mergeMain.length; i += 2) {
40
+ const t = mergeMain[i];
41
+ const v = mergeMain[i + 1];
42
+ let { start, len } = t;
43
+ ctx.buf[ctx.len++] = start;
44
+ ctx.buf[ctx.len++] = start >>>= 8;
45
+ ctx.buf[ctx.len++] = len;
46
+ ctx.buf[ctx.len++] = len >>>= 8;
47
+ const err = appendFixedValue(ctx, v, t);
48
+ if (err) {
49
+ return err;
50
+ }
51
+ }
52
+ }
53
+ };
54
+ export const update = (db, type, id, obj, overwrite) => {
55
+ const def = db.schemaTypesParsed[type];
56
+ const ctx = db.modifyCtx;
57
+ const pos = ctx.len;
58
+ const res = new ModifyState(def.id, id, db, getSubscriptionMarkers(db, def.id, id, false));
59
+ const err = appendUpdate(ctx, def, obj, res, overwrite);
60
+ if (err) {
61
+ ctx.prefix0 = -1; // force a new cursor
62
+ ctx.len = pos;
63
+ if (err === RANGE_ERR) {
64
+ if (pos === 0) {
65
+ throw new Error('out of range');
66
+ }
67
+ flushBuffer(db);
68
+ return update(db, type, id, obj, overwrite);
69
+ }
70
+ res.error = err;
71
+ // @ts-ignore
72
+ return res;
73
+ }
74
+ if (!db.isDraining) {
75
+ startDrain(db);
76
+ }
77
+ // @ts-ignore
78
+ return res;
79
+ };
80
+ //# sourceMappingURL=update.js.map
@@ -0,0 +1,2 @@
1
+ import { DbClient } from '../index.js';
2
+ export declare function upsert(db: DbClient, type: string, obj: Record<string, any>): Promise<any>;