@lix-js/sdk 0.6.0-preview.5 → 0.6.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 (274) hide show
  1. package/README.md +76 -4
  2. package/dist/errors.d.ts +7 -0
  3. package/dist/errors.js +19 -0
  4. package/dist/index.d.ts +4 -5
  5. package/dist/index.js +3 -3
  6. package/dist/native.d.ts +1 -0
  7. package/dist/native.js +47 -0
  8. package/dist/open-lix.d.ts +38 -207
  9. package/dist/open-lix.js +59 -284
  10. package/dist/result.d.ts +18 -0
  11. package/dist/result.js +48 -0
  12. package/dist/types.d.ts +114 -1
  13. package/dist/value.d.ts +28 -0
  14. package/dist/value.js +245 -0
  15. package/package.json +38 -71
  16. package/SKILL.md +0 -507
  17. package/dist/builtin-schemas.d.ts +0 -1
  18. package/dist/builtin-schemas.js +0 -1
  19. package/dist/engine-wasm/index.d.ts +0 -87
  20. package/dist/engine-wasm/index.js +0 -339
  21. package/dist/engine-wasm/wasm/lix_engine.d.ts +0 -79
  22. package/dist/engine-wasm/wasm/lix_engine.js +0 -833
  23. package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
  24. package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +0 -27
  25. package/dist/generated/builtin-schemas.d.ts +0 -427
  26. package/dist/generated/builtin-schemas.js +0 -643
  27. package/dist/sqlite/index.d.ts +0 -12
  28. package/dist/sqlite/index.js +0 -359
  29. package/dist-engine-src/README.md +0 -18
  30. package/dist-engine-src/src/backend/capabilities.rs +0 -67
  31. package/dist-engine-src/src/backend/conformance/baseline.rs +0 -1127
  32. package/dist-engine-src/src/backend/conformance/factory.rs +0 -93
  33. package/dist-engine-src/src/backend/conformance/failure_tests.rs +0 -608
  34. package/dist-engine-src/src/backend/conformance/fixtures.rs +0 -26
  35. package/dist-engine-src/src/backend/conformance/mod.rs +0 -75
  36. package/dist-engine-src/src/backend/conformance/model.rs +0 -28
  37. package/dist-engine-src/src/backend/conformance/model_based.rs +0 -257
  38. package/dist-engine-src/src/backend/conformance/persistence.rs +0 -204
  39. package/dist-engine-src/src/backend/conformance/projection.rs +0 -21
  40. package/dist-engine-src/src/backend/conformance/pushdown.rs +0 -24
  41. package/dist-engine-src/src/backend/conformance/runner.rs +0 -90
  42. package/dist-engine-src/src/backend/conformance/scan.rs +0 -24
  43. package/dist-engine-src/src/backend/conformance/write.rs +0 -16
  44. package/dist-engine-src/src/backend/error.rs +0 -94
  45. package/dist-engine-src/src/backend/in_memory.rs +0 -670
  46. package/dist-engine-src/src/backend/mod.rs +0 -39
  47. package/dist-engine-src/src/backend/predicate.rs +0 -80
  48. package/dist-engine-src/src/backend/traits.rs +0 -260
  49. package/dist-engine-src/src/backend/types.rs +0 -239
  50. package/dist-engine-src/src/binary_cas/chunking.rs +0 -31
  51. package/dist-engine-src/src/binary_cas/codec.rs +0 -346
  52. package/dist-engine-src/src/binary_cas/context.rs +0 -139
  53. package/dist-engine-src/src/binary_cas/kv.rs +0 -1038
  54. package/dist-engine-src/src/binary_cas/mod.rs +0 -11
  55. package/dist-engine-src/src/binary_cas/types.rs +0 -121
  56. package/dist-engine-src/src/branch/context.rs +0 -40
  57. package/dist-engine-src/src/branch/lifecycle.rs +0 -221
  58. package/dist-engine-src/src/branch/mod.rs +0 -13
  59. package/dist-engine-src/src/branch/refs.rs +0 -321
  60. package/dist-engine-src/src/branch/stage_rows.rs +0 -67
  61. package/dist-engine-src/src/branch/types.rs +0 -21
  62. package/dist-engine-src/src/catalog/context.rs +0 -412
  63. package/dist-engine-src/src/catalog/mod.rs +0 -10
  64. package/dist-engine-src/src/catalog/schema.rs +0 -4
  65. package/dist-engine-src/src/catalog/snapshot.rs +0 -1114
  66. package/dist-engine-src/src/cel/context.rs +0 -86
  67. package/dist-engine-src/src/cel/error.rs +0 -19
  68. package/dist-engine-src/src/cel/mod.rs +0 -8
  69. package/dist-engine-src/src/cel/provider.rs +0 -9
  70. package/dist-engine-src/src/cel/runtime.rs +0 -167
  71. package/dist-engine-src/src/cel/value.rs +0 -50
  72. package/dist-engine-src/src/changelog/bench_support.rs +0 -785
  73. package/dist-engine-src/src/changelog/change.rs +0 -1
  74. package/dist-engine-src/src/changelog/codec.rs +0 -497
  75. package/dist-engine-src/src/changelog/commit.rs +0 -1
  76. package/dist-engine-src/src/changelog/context.rs +0 -1614
  77. package/dist-engine-src/src/changelog/mod.rs +0 -29
  78. package/dist-engine-src/src/changelog/store.rs +0 -163
  79. package/dist-engine-src/src/changelog/test_support.rs +0 -54
  80. package/dist-engine-src/src/changelog/types.rs +0 -213
  81. package/dist-engine-src/src/commit_graph/context.rs +0 -944
  82. package/dist-engine-src/src/commit_graph/mod.rs +0 -9
  83. package/dist-engine-src/src/commit_graph/types.rs +0 -89
  84. package/dist-engine-src/src/commit_graph/walker.rs +0 -786
  85. package/dist-engine-src/src/common/error.rs +0 -347
  86. package/dist-engine-src/src/common/fingerprint.rs +0 -3
  87. package/dist-engine-src/src/common/fs_path.rs +0 -1336
  88. package/dist-engine-src/src/common/identity.rs +0 -145
  89. package/dist-engine-src/src/common/json_pointer.rs +0 -67
  90. package/dist-engine-src/src/common/metadata.rs +0 -40
  91. package/dist-engine-src/src/common/mod.rs +0 -23
  92. package/dist-engine-src/src/common/types.rs +0 -105
  93. package/dist-engine-src/src/common/wire.rs +0 -222
  94. package/dist-engine-src/src/domain.rs +0 -320
  95. package/dist-engine-src/src/engine.rs +0 -203
  96. package/dist-engine-src/src/entity_pk.rs +0 -402
  97. package/dist-engine-src/src/functions/context.rs +0 -296
  98. package/dist-engine-src/src/functions/deterministic.rs +0 -113
  99. package/dist-engine-src/src/functions/mod.rs +0 -18
  100. package/dist-engine-src/src/functions/provider.rs +0 -130
  101. package/dist-engine-src/src/functions/state.rs +0 -335
  102. package/dist-engine-src/src/functions/types.rs +0 -37
  103. package/dist-engine-src/src/init.rs +0 -692
  104. package/dist-engine-src/src/json_store/compression.rs +0 -77
  105. package/dist-engine-src/src/json_store/context.rs +0 -172
  106. package/dist-engine-src/src/json_store/encoded.rs +0 -15
  107. package/dist-engine-src/src/json_store/mod.rs +0 -38
  108. package/dist-engine-src/src/json_store/store.rs +0 -494
  109. package/dist-engine-src/src/json_store/types.rs +0 -212
  110. package/dist-engine-src/src/lib.rs +0 -92
  111. package/dist-engine-src/src/live_state/context.rs +0 -1883
  112. package/dist-engine-src/src/live_state/mod.rs +0 -21
  113. package/dist-engine-src/src/live_state/overlay.rs +0 -75
  114. package/dist-engine-src/src/live_state/reader.rs +0 -23
  115. package/dist-engine-src/src/live_state/types.rs +0 -231
  116. package/dist-engine-src/src/live_state/visibility.rs +0 -666
  117. package/dist-engine-src/src/plugin/archive.rs +0 -438
  118. package/dist-engine-src/src/plugin/component.rs +0 -183
  119. package/dist-engine-src/src/plugin/install.rs +0 -619
  120. package/dist-engine-src/src/plugin/manifest.rs +0 -516
  121. package/dist-engine-src/src/plugin/materializer.rs +0 -202
  122. package/dist-engine-src/src/plugin/mod.rs +0 -33
  123. package/dist-engine-src/src/plugin/plugin_manifest.json +0 -119
  124. package/dist-engine-src/src/plugin/storage.rs +0 -74
  125. package/dist-engine-src/src/schema/annotations/defaults.rs +0 -275
  126. package/dist-engine-src/src/schema/annotations/mod.rs +0 -1
  127. package/dist-engine-src/src/schema/builtin/lix_account.json +0 -21
  128. package/dist-engine-src/src/schema/builtin/lix_active_account.json +0 -29
  129. package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +0 -29
  130. package/dist-engine-src/src/schema/builtin/lix_branch_descriptor.json +0 -34
  131. package/dist-engine-src/src/schema/builtin/lix_branch_ref.json +0 -48
  132. package/dist-engine-src/src/schema/builtin/lix_change.json +0 -63
  133. package/dist-engine-src/src/schema/builtin/lix_change_author.json +0 -45
  134. package/dist-engine-src/src/schema/builtin/lix_commit.json +0 -24
  135. package/dist-engine-src/src/schema/builtin/lix_commit_edge.json +0 -53
  136. package/dist-engine-src/src/schema/builtin/lix_directory_descriptor.json +0 -52
  137. package/dist-engine-src/src/schema/builtin/lix_file_descriptor.json +0 -52
  138. package/dist-engine-src/src/schema/builtin/lix_key_value.json +0 -40
  139. package/dist-engine-src/src/schema/builtin/lix_label.json +0 -29
  140. package/dist-engine-src/src/schema/builtin/lix_label_assignment.json +0 -74
  141. package/dist-engine-src/src/schema/builtin/lix_registered_schema.json +0 -25
  142. package/dist-engine-src/src/schema/builtin/mod.rs +0 -220
  143. package/dist-engine-src/src/schema/compatibility.rs +0 -787
  144. package/dist-engine-src/src/schema/definition.json +0 -187
  145. package/dist-engine-src/src/schema/definition.rs +0 -742
  146. package/dist-engine-src/src/schema/key.rs +0 -138
  147. package/dist-engine-src/src/schema/mod.rs +0 -20
  148. package/dist-engine-src/src/schema/seed.rs +0 -14
  149. package/dist-engine-src/src/schema/tests.rs +0 -780
  150. package/dist-engine-src/src/session/context.rs +0 -1059
  151. package/dist-engine-src/src/session/create_branch.rs +0 -94
  152. package/dist-engine-src/src/session/execute.rs +0 -681
  153. package/dist-engine-src/src/session/merge/analysis.rs +0 -108
  154. package/dist-engine-src/src/session/merge/branch.rs +0 -417
  155. package/dist-engine-src/src/session/merge/conflicts.rs +0 -63
  156. package/dist-engine-src/src/session/merge/mod.rs +0 -10
  157. package/dist-engine-src/src/session/merge/stats.rs +0 -61
  158. package/dist-engine-src/src/session/mod.rs +0 -30
  159. package/dist-engine-src/src/session/switch_branch.rs +0 -113
  160. package/dist-engine-src/src/session/transaction.rs +0 -557
  161. package/dist-engine-src/src/sql2/bind/classify.rs +0 -102
  162. package/dist-engine-src/src/sql2/bind/error.rs +0 -5
  163. package/dist-engine-src/src/sql2/bind/expr.rs +0 -29
  164. package/dist-engine-src/src/sql2/bind/mod.rs +0 -12
  165. package/dist-engine-src/src/sql2/bind/public_udf.rs +0 -306
  166. package/dist-engine-src/src/sql2/bind/read.rs +0 -65
  167. package/dist-engine-src/src/sql2/bind/statement.rs +0 -2236
  168. package/dist-engine-src/src/sql2/bind/table.rs +0 -273
  169. package/dist-engine-src/src/sql2/bind/write.rs +0 -86
  170. package/dist-engine-src/src/sql2/branch_scope.rs +0 -436
  171. package/dist-engine-src/src/sql2/catalog/capability.rs +0 -20
  172. package/dist-engine-src/src/sql2/catalog/entity_surface.rs +0 -296
  173. package/dist-engine-src/src/sql2/catalog/mod.rs +0 -15
  174. package/dist-engine-src/src/sql2/catalog/registry.rs +0 -556
  175. package/dist-engine-src/src/sql2/catalog/schema.rs +0 -88
  176. package/dist-engine-src/src/sql2/catalog/surface.rs +0 -41
  177. package/dist-engine-src/src/sql2/change_materialization.rs +0 -122
  178. package/dist-engine-src/src/sql2/context.rs +0 -317
  179. package/dist-engine-src/src/sql2/dml.rs +0 -148
  180. package/dist-engine-src/src/sql2/error.rs +0 -215
  181. package/dist-engine-src/src/sql2/exec/bound_public_write.rs +0 -1593
  182. package/dist-engine-src/src/sql2/exec/datafusion.rs +0 -5266
  183. package/dist-engine-src/src/sql2/exec/fast_write.rs +0 -82
  184. package/dist-engine-src/src/sql2/exec/mod.rs +0 -24
  185. package/dist-engine-src/src/sql2/exec/write.rs +0 -661
  186. package/dist-engine-src/src/sql2/filesystem_planner.rs +0 -1485
  187. package/dist-engine-src/src/sql2/filesystem_predicates.rs +0 -159
  188. package/dist-engine-src/src/sql2/filesystem_visibility.rs +0 -383
  189. package/dist-engine-src/src/sql2/history_projection.rs +0 -56
  190. package/dist-engine-src/src/sql2/history_route.rs +0 -661
  191. package/dist-engine-src/src/sql2/mod.rs +0 -52
  192. package/dist-engine-src/src/sql2/optimize/datafusion.rs +0 -1
  193. package/dist-engine-src/src/sql2/optimize/mod.rs +0 -2
  194. package/dist-engine-src/src/sql2/optimize/simple_write.rs +0 -116
  195. package/dist-engine-src/src/sql2/parse/mod.rs +0 -69
  196. package/dist-engine-src/src/sql2/parse/normalize.rs +0 -1
  197. package/dist-engine-src/src/sql2/plan/branch_scope.rs +0 -24
  198. package/dist-engine-src/src/sql2/plan/mod.rs +0 -5
  199. package/dist-engine-src/src/sql2/plan/predicate.rs +0 -22
  200. package/dist-engine-src/src/sql2/plan/write.rs +0 -147
  201. package/dist-engine-src/src/sql2/predicate_typecheck.rs +0 -504
  202. package/dist-engine-src/src/sql2/providers/branch.rs +0 -1206
  203. package/dist-engine-src/src/sql2/providers/change.rs +0 -445
  204. package/dist-engine-src/src/sql2/providers/directory.rs +0 -2422
  205. package/dist-engine-src/src/sql2/providers/directory_history.rs +0 -645
  206. package/dist-engine-src/src/sql2/providers/entity.rs +0 -1484
  207. package/dist-engine-src/src/sql2/providers/entity_history.rs +0 -452
  208. package/dist-engine-src/src/sql2/providers/file.rs +0 -3686
  209. package/dist-engine-src/src/sql2/providers/file_history.rs +0 -924
  210. package/dist-engine-src/src/sql2/providers/history.rs +0 -426
  211. package/dist-engine-src/src/sql2/providers/lix_state.rs +0 -2542
  212. package/dist-engine-src/src/sql2/providers/mod.rs +0 -508
  213. package/dist-engine-src/src/sql2/read_only.rs +0 -63
  214. package/dist-engine-src/src/sql2/record_batch.rs +0 -17
  215. package/dist-engine-src/src/sql2/result_metadata.rs +0 -29
  216. package/dist-engine-src/src/sql2/runtime.rs +0 -60
  217. package/dist-engine-src/src/sql2/session.rs +0 -83
  218. package/dist-engine-src/src/sql2/storage/constraints.rs +0 -1
  219. package/dist-engine-src/src/sql2/storage/mod.rs +0 -1
  220. package/dist-engine-src/src/sql2/test_support/differential.rs +0 -712
  221. package/dist-engine-src/src/sql2/test_support/generators.rs +0 -354
  222. package/dist-engine-src/src/sql2/test_support/mod.rs +0 -2
  223. package/dist-engine-src/src/sql2/udfs/common.rs +0 -295
  224. package/dist-engine-src/src/sql2/udfs/lix_active_branch_commit_id.rs +0 -53
  225. package/dist-engine-src/src/sql2/udfs/lix_empty_blob.rs +0 -47
  226. package/dist-engine-src/src/sql2/udfs/lix_json.rs +0 -100
  227. package/dist-engine-src/src/sql2/udfs/lix_json_get.rs +0 -99
  228. package/dist-engine-src/src/sql2/udfs/lix_json_get_text.rs +0 -99
  229. package/dist-engine-src/src/sql2/udfs/lix_text_decode.rs +0 -82
  230. package/dist-engine-src/src/sql2/udfs/lix_text_encode.rs +0 -85
  231. package/dist-engine-src/src/sql2/udfs/lix_timestamp.rs +0 -76
  232. package/dist-engine-src/src/sql2/udfs/lix_uuid_v7.rs +0 -76
  233. package/dist-engine-src/src/sql2/udfs/mod.rs +0 -86
  234. package/dist-engine-src/src/sql2/write_normalization.rs +0 -368
  235. package/dist-engine-src/src/storage/conformance.rs +0 -399
  236. package/dist-engine-src/src/storage/context.rs +0 -620
  237. package/dist-engine-src/src/storage/mod.rs +0 -52
  238. package/dist-engine-src/src/storage/point.rs +0 -440
  239. package/dist-engine-src/src/storage/read_scope.rs +0 -67
  240. package/dist-engine-src/src/storage/reader.rs +0 -867
  241. package/dist-engine-src/src/storage/scan.rs +0 -784
  242. package/dist-engine-src/src/storage/spaces.rs +0 -236
  243. package/dist-engine-src/src/storage/stats.rs +0 -80
  244. package/dist-engine-src/src/storage/write_set.rs +0 -962
  245. package/dist-engine-src/src/storage_bench.rs +0 -171
  246. package/dist-engine-src/src/test_support.rs +0 -450
  247. package/dist-engine-src/src/tracked_state/bench_support.rs +0 -394
  248. package/dist-engine-src/src/tracked_state/codec.rs +0 -1183
  249. package/dist-engine-src/src/tracked_state/commit_root_rebuild.rs +0 -358
  250. package/dist-engine-src/src/tracked_state/context.rs +0 -2801
  251. package/dist-engine-src/src/tracked_state/diff.rs +0 -2140
  252. package/dist-engine-src/src/tracked_state/merge.rs +0 -478
  253. package/dist-engine-src/src/tracked_state/mod.rs +0 -35
  254. package/dist-engine-src/src/tracked_state/row_materialization.rs +0 -275
  255. package/dist-engine-src/src/tracked_state/storage.rs +0 -427
  256. package/dist-engine-src/src/tracked_state/tree.rs +0 -3063
  257. package/dist-engine-src/src/tracked_state/types.rs +0 -238
  258. package/dist-engine-src/src/transaction/bench_support.rs +0 -407
  259. package/dist-engine-src/src/transaction/commit.rs +0 -1592
  260. package/dist-engine-src/src/transaction/context.rs +0 -1653
  261. package/dist-engine-src/src/transaction/mod.rs +0 -24
  262. package/dist-engine-src/src/transaction/normalization.rs +0 -877
  263. package/dist-engine-src/src/transaction/prep.rs +0 -37
  264. package/dist-engine-src/src/transaction/schema_resolver.rs +0 -163
  265. package/dist-engine-src/src/transaction/staging.rs +0 -1525
  266. package/dist-engine-src/src/transaction/types.rs +0 -403
  267. package/dist-engine-src/src/transaction/validation.rs +0 -5766
  268. package/dist-engine-src/src/untracked_state/codec.rs +0 -615
  269. package/dist-engine-src/src/untracked_state/context.rs +0 -98
  270. package/dist-engine-src/src/untracked_state/materialization.rs +0 -63
  271. package/dist-engine-src/src/untracked_state/mod.rs +0 -15
  272. package/dist-engine-src/src/untracked_state/storage.rs +0 -898
  273. package/dist-engine-src/src/untracked_state/types.rs +0 -146
  274. package/dist-engine-src/src/wasm/mod.rs +0 -60
package/dist/open-lix.js CHANGED
@@ -1,307 +1,82 @@
1
- import init, { resolveEngineWasmModuleOrPath, Value, } from "./engine-wasm/index.js";
2
- import * as wasmModule from "./engine-wasm/index.js";
3
- export class Row {
4
- columns;
5
- valuesByIndex;
6
- constructor(columns, values) {
7
- this.columns = columns;
8
- this.valuesByIndex = values;
9
- }
10
- get(columnName) {
11
- return valueToNative(this.value(columnName));
12
- }
13
- tryGet(columnName) {
14
- const value = this.tryValue(columnName);
15
- return value === undefined ? undefined : valueToNative(value);
16
- }
17
- value(columnName) {
18
- const index = this.columns.indexOf(columnName);
19
- if (index === -1) {
20
- throw createLixError("LIX_COLUMN_NOT_FOUND", `Column "${columnName}" does not exist. Available columns: ${this.availableColumns()}`);
1
+ import { invalidArgument } from "./errors.js";
2
+ import { addon } from "./native.js";
3
+ import { normalizeOptionals, wrapExecuteResult } from "./result.js";
4
+ import { normalizeParam, toNativeValue } from "./value.js";
5
+ export class SqliteBackend {
6
+ path;
7
+ constructor(options) {
8
+ if (!options || typeof options.path !== "string" || options.path.length === 0) {
9
+ throw new TypeError("SqliteBackend requires a non-empty path");
21
10
  }
22
- const value = this.valuesByIndex[index];
23
- if (value === undefined) {
24
- throw createLixError("LIX_COLUMN_NOT_FOUND", `Column "${columnName}" is outside row width ${this.valuesByIndex.length}.`);
25
- }
26
- return value;
11
+ this.path = options.path;
27
12
  }
28
- tryValue(columnName) {
29
- const index = this.columns.indexOf(columnName);
30
- return index === -1 ? undefined : this.valuesByIndex[index];
13
+ }
14
+ export async function openLix(options = {}) {
15
+ if (!options || typeof options !== "object") {
16
+ throw new TypeError("openLix() options must be an object");
31
17
  }
32
- getAt(index) {
33
- return valueToNative(this.valueAt(index));
18
+ if (options.backend === undefined) {
19
+ return new Lix(addon.Lix.openMemory());
34
20
  }
35
- valueAt(index) {
36
- const value = this.valuesByIndex[index];
37
- if (value === undefined) {
38
- throw createLixError("LIX_COLUMN_NOT_FOUND", `Column index ${index} is outside row width ${this.valuesByIndex.length}.`);
39
- }
40
- return value;
21
+ if (!(options.backend instanceof SqliteBackend)) {
22
+ throw new TypeError("openLix() requires { backend: new SqliteBackend({ path }) }");
41
23
  }
42
- values() {
43
- return [...this.valuesByIndex];
24
+ return new Lix(addon.Lix.openSqlite(options.backend.path));
25
+ }
26
+ export class Lix {
27
+ native;
28
+ constructor(native) {
29
+ this.native = native;
44
30
  }
45
- toObject() {
46
- return Object.fromEntries(this.columns.map((column, index) => [
47
- column,
48
- valueToNative(this.valueAt(index)),
49
- ]));
31
+ async execute(sql, params = []) {
32
+ assertExecuteArgs("lix", sql, params);
33
+ return wrapExecuteResult(this.native.execute(sql, params.map((param, index) => toNativeValue(normalizeParam(param, index)))));
50
34
  }
51
- toValueMap() {
52
- return Object.fromEntries(this.columns.map((column, index) => [column, this.valueAt(index)]));
35
+ async beginTransaction() {
36
+ return new LixTransaction(this.native.beginTransaction());
53
37
  }
54
- availableColumns() {
55
- return this.columns.length === 0 ? "<none>" : this.columns.join(", ");
38
+ async activeBranchId() {
39
+ return this.native.activeBranchId();
56
40
  }
57
- }
58
- function valueToNative(value) {
59
- switch (value.kind) {
60
- case "null":
61
- return null;
62
- case "boolean":
63
- case "integer":
64
- case "real":
65
- case "text":
66
- case "json":
67
- return value.value;
68
- case "blob":
69
- return value.asBlob() ?? new Uint8Array();
41
+ async createBranch(options) {
42
+ return this.native.createBranch(options);
70
43
  }
71
- }
72
- let wasmReady = null;
73
- async function ensureWasmReady() {
74
- if (!wasmReady) {
75
- wasmReady = resolveEngineWasmModuleOrPath()
76
- .then((module_or_path) => init({ module_or_path }))
77
- .then(() => undefined);
44
+ async switchBranch(options) {
45
+ return this.native.switchBranch(options);
78
46
  }
79
- await wasmReady;
80
- }
81
- export async function openLix(options = {}) {
82
- await ensureWasmReady();
83
- try {
84
- const wasmLix = (await wasmModule.openLix(options));
85
- return createLixHandle(wasmLix);
47
+ async mergeBranchPreview(options) {
48
+ return normalizeOptionals(this.native.mergeBranchPreview(options));
86
49
  }
87
- catch (error) {
88
- try {
89
- options.backend?.close?.();
90
- }
91
- catch {
92
- // Preserve the original open failure.
93
- }
94
- throw normalizeThrownError(error);
50
+ async mergeBranch(options) {
51
+ const receipt = normalizeOptionals(this.native.mergeBranch(options));
52
+ receipt.createdMergeCommitId ??= null;
53
+ return receipt;
95
54
  }
96
- }
97
- function createLixHandle(wasmLix) {
98
- let operationQueue = Promise.resolve();
99
- const acquireOperationSlot = async () => {
100
- const previous = operationQueue;
101
- let releaseCurrent;
102
- const current = new Promise((resolve) => {
103
- releaseCurrent = resolve;
104
- });
105
- operationQueue = previous.then(() => current);
106
- await previous;
107
- return () => releaseCurrent?.();
108
- };
109
- const runQueued = async (operation) => {
110
- const release = await acquireOperationSlot();
111
- try {
112
- return await operation();
113
- }
114
- catch (error) {
115
- throw normalizeThrownError(error);
116
- }
117
- finally {
118
- release();
119
- }
120
- };
121
- return {
122
- async execute(sql, params = []) {
123
- validateExecuteArguments(sql, params);
124
- const values = params.map((param, index) => valueFromExecuteParam(param, index));
125
- const result = await runQueued(() => wasmLix.execute(sql, values));
126
- return normalizeExecuteResult(result);
127
- },
128
- async beginTransaction() {
129
- const wasmTransaction = await runQueued(() => wasmLix.beginTransaction());
130
- return createLixTransactionHandle(wasmTransaction, runQueued);
131
- },
132
- async activeBranchId() {
133
- return await runQueued(() => wasmLix.activeBranchId());
134
- },
135
- async createBranch(options) {
136
- return await runQueued(() => wasmLix.createBranch(options));
137
- },
138
- async switchBranch(options) {
139
- return await runQueued(() => wasmLix.switchBranch(options));
140
- },
141
- async mergeBranchPreview(options) {
142
- return await runQueued(() => wasmLix.mergeBranchPreview(options));
143
- },
144
- async mergeBranch(options) {
145
- return await runQueued(() => wasmLix.mergeBranch(options));
146
- },
147
- async close() {
148
- await runQueued(() => wasmLix.close());
149
- },
150
- };
151
- }
152
- function createLixTransactionHandle(wasmTransaction, runQueued) {
153
- let closed = false;
154
- const ensureOpen = () => {
155
- if (closed) {
156
- throw createLixError("LIX_INVALID_TRANSACTION_STATE", "Lix transaction is closed");
157
- }
158
- };
159
- return {
160
- async execute(sql, params = []) {
161
- ensureOpen();
162
- validateExecuteArguments(sql, params);
163
- const values = params.map((param, index) => valueFromExecuteParam(param, index));
164
- const result = await runQueued(() => wasmTransaction.execute(sql, values));
165
- return normalizeExecuteResult(result);
166
- },
167
- async commit() {
168
- ensureOpen();
169
- try {
170
- await runQueued(() => wasmTransaction.commit());
171
- }
172
- finally {
173
- closed = true;
174
- }
175
- },
176
- async rollback() {
177
- ensureOpen();
178
- try {
179
- await runQueued(() => wasmTransaction.rollback());
180
- }
181
- finally {
182
- closed = true;
183
- }
184
- },
185
- };
186
- }
187
- function validateExecuteArguments(sql, params) {
188
- if (typeof sql !== "string") {
189
- throw invalidArgumentError("execute", "sql", "string", sql);
190
- }
191
- if (!Array.isArray(params)) {
192
- throw invalidArgumentError("execute", "params", "array", params);
55
+ async close() {
56
+ return this.native.close();
193
57
  }
194
58
  }
195
- function invalidArgumentError(operation, argument, expected, actualValue) {
196
- return createLixError("LIX_INVALID_ARGUMENT", `lix.${operation}() expected ${argument} to be ${expectedArticle(expected)} ${expected}`, {
197
- details: {
198
- operation,
199
- argument,
200
- expected,
201
- actual: runtimeTypeName(actualValue),
202
- },
203
- });
204
- }
205
- function valueFromExecuteParam(param, index) {
206
- try {
207
- return Value.from(param);
208
- }
209
- catch (error) {
210
- throw invalidParamError(index, param, error);
59
+ export class LixTransaction {
60
+ native;
61
+ constructor(native) {
62
+ this.native = native;
211
63
  }
212
- }
213
- function invalidParamError(index, actualValue, cause) {
214
- const message = cause instanceof Error && cause.message
215
- ? cause.message
216
- : "parameter is not a valid Lix SQL value";
217
- return createLixError("LIX_INVALID_PARAM", `lix.execute() invalid parameter $${index + 1}: ${message}`, {
218
- details: {
219
- operation: "execute",
220
- parameter_index: index + 1,
221
- argument: `params[${index}]`,
222
- actual: runtimeTypeName(actualValue),
223
- },
224
- cause,
225
- });
226
- }
227
- function expectedArticle(expected) {
228
- return /^[aeiou]/i.test(expected) ? "an" : "a";
229
- }
230
- function runtimeTypeName(value) {
231
- if (value === null)
232
- return "null";
233
- if (Array.isArray(value))
234
- return "array";
235
- if (value instanceof Date)
236
- return "Date";
237
- if (value instanceof ArrayBuffer)
238
- return "ArrayBuffer";
239
- if (ArrayBuffer.isView(value))
240
- return value.constructor.name;
241
- return typeof value;
242
- }
243
- function normalizeExecuteResult(result) {
244
- const columns = [...result.columns];
245
- return {
246
- columns,
247
- rows: result.rows.map((row) => new Row(columns, row.map((value) => Value.from(value)))),
248
- rowsAffected: result.rowsAffected,
249
- notices: result.notices ?? [],
250
- };
251
- }
252
- function createLixError(code, message, options = {}) {
253
- const error = new Error(message);
254
- error.name = "LixError";
255
- error.code = code;
256
- if (options.hint !== undefined) {
257
- error.hint = options.hint;
64
+ async execute(sql, params = []) {
65
+ assertExecuteArgs("lixTransaction", sql, params);
66
+ return wrapExecuteResult(this.native.execute(sql, params.map((param, index) => toNativeValue(normalizeParam(param, index)))));
258
67
  }
259
- if (options.details !== undefined) {
260
- error.details = options.details;
68
+ async commit() {
69
+ return this.native.commit();
261
70
  }
262
- if (options.cause !== undefined) {
263
- error.cause = options.cause;
71
+ async rollback() {
72
+ return this.native.rollback();
264
73
  }
265
- return error;
266
74
  }
267
- function normalizeThrownError(error) {
268
- if (isLixErrorLike(error)) {
269
- const hint = typeof error.hint === "string"
270
- ? error.hint
271
- : extractHintFromMessage(error.message);
272
- const details = "details" in error ? error.details : undefined;
273
- if (error instanceof Error) {
274
- if (hint !== undefined && error.hint === undefined) {
275
- error.hint = hint;
276
- }
277
- if (details !== undefined && error.details === undefined) {
278
- error.details = details;
279
- }
280
- return error;
281
- }
282
- const message = typeof error.message === "string" ? error.message : error.code;
283
- return createLixError(error.code, message, { hint, details });
284
- }
285
- if (error instanceof WebAssembly.RuntimeError) {
286
- return createLixError("LIX_WASM_RUNTIME_ERROR", error.message, {
287
- hint: "The Lix engine encountered a WebAssembly runtime trap. Please report this as an engine bug with the SQL statement or API call that triggered it.",
288
- cause: error,
289
- });
75
+ function assertExecuteArgs(receiver, sql, params) {
76
+ if (typeof sql !== "string") {
77
+ throw invalidArgument("execute", "sql", "string", typeof sql, receiver);
290
78
  }
291
- if (error instanceof Error) {
292
- return createLixError("LIX_ERROR_UNKNOWN", error.message, { cause: error });
79
+ if (!Array.isArray(params)) {
80
+ throw invalidArgument("execute", "params", "array", typeof params, receiver);
293
81
  }
294
- return createLixError("LIX_ERROR_UNKNOWN", String(error));
295
- }
296
- function extractHintFromMessage(message) {
297
- if (typeof message !== "string")
298
- return undefined;
299
- const match = message.match(/(?:^|\n)hint:\s*(.+)$/s);
300
- return match?.[1]?.trim();
301
- }
302
- function isLixErrorLike(error) {
303
- return (typeof error === "object" &&
304
- error !== null &&
305
- typeof error.code === "string" &&
306
- error.code.startsWith("LIX_"));
307
82
  }
@@ -0,0 +1,18 @@
1
+ import { type NativeLixValue, Value } from "./value.js";
2
+ import type { ExecuteResult, LixValue } from "./types.js";
3
+ export declare class Row {
4
+ private readonly columns;
5
+ private readonly values;
6
+ constructor(columns: string[], values: Value[]);
7
+ static fromRaw(columns: string[], values: LixValue[]): Row;
8
+ get(column: string): unknown;
9
+ value(column: string): Value;
10
+ toObject(): Record<string, unknown>;
11
+ toValueMap(): Record<string, Value>;
12
+ }
13
+ type NativeExecuteResult = Omit<ExecuteResult, "rows"> & {
14
+ rows: NativeLixValue[][];
15
+ };
16
+ export declare function wrapExecuteResult(result: NativeExecuteResult): ExecuteResult;
17
+ export declare function normalizeOptionals<T>(value: T): T;
18
+ export {};
package/dist/result.js ADDED
@@ -0,0 +1,48 @@
1
+ import { fromNativeValue, Value } from "./value.js";
2
+ export class Row {
3
+ columns;
4
+ values;
5
+ constructor(columns, values) {
6
+ this.columns = columns;
7
+ this.values = values;
8
+ }
9
+ static fromRaw(columns, values) {
10
+ return new Row(columns, values.map((value) => Value._fromNative(value)));
11
+ }
12
+ get(column) {
13
+ return this.value(column).toJS();
14
+ }
15
+ value(column) {
16
+ const index = this.columns.indexOf(column);
17
+ if (index === -1) {
18
+ throw new Error(`Unknown column "${column}". Available columns: ${this.columns.join(", ")}`);
19
+ }
20
+ const value = this.values[index];
21
+ if (!value) {
22
+ throw new Error(`Column "${column}" is missing a value`);
23
+ }
24
+ return value;
25
+ }
26
+ toObject() {
27
+ return Object.fromEntries(this.columns.map((column, index) => [column, this.values[index]?.toJS()]));
28
+ }
29
+ toValueMap() {
30
+ return Object.fromEntries(this.columns.map((column, index) => [column, this.values[index]]));
31
+ }
32
+ }
33
+ export function wrapExecuteResult(result) {
34
+ return {
35
+ ...result,
36
+ rows: result.rows.map((row) => Row.fromRaw(result.columns, row.map(fromNativeValue))),
37
+ };
38
+ }
39
+ export function normalizeOptionals(value) {
40
+ if (Array.isArray(value))
41
+ return value.map(normalizeOptionals);
42
+ if (!value || typeof value !== "object")
43
+ return value;
44
+ return Object.fromEntries(Object.entries(value).map(([key, entry]) => [
45
+ key,
46
+ entry === undefined ? null : normalizeOptionals(entry),
47
+ ]));
48
+ }
package/dist/types.d.ts CHANGED
@@ -1 +1,114 @@
1
- export type { JsonValue, LixRuntimeValue } from "./open-lix.js";
1
+ export type SqliteBackendOptions = {
2
+ path: string;
3
+ };
4
+ export type OpenLixOptions = {
5
+ backend?: import("./open-lix.js").SqliteBackend;
6
+ };
7
+ export type LixValue = {
8
+ kind: "null";
9
+ value: null;
10
+ } | {
11
+ kind: "boolean";
12
+ value: boolean;
13
+ } | {
14
+ kind: "integer";
15
+ value: number;
16
+ } | {
17
+ kind: "real";
18
+ value: number;
19
+ } | {
20
+ kind: "text";
21
+ value: string;
22
+ } | {
23
+ kind: "json";
24
+ value: JsonValue;
25
+ } | {
26
+ kind: "blob";
27
+ value: Uint8Array;
28
+ };
29
+ export type JsonValue = null | boolean | number | string | readonly JsonValue[] | {
30
+ readonly [key: string]: JsonValue;
31
+ };
32
+ export type SqlParam = JsonValue | Uint8Array | import("./value.js").Value;
33
+ export type ExecuteResult = {
34
+ columns: string[];
35
+ rows: RowLike[];
36
+ rowsAffected: number;
37
+ notices: Array<{
38
+ code: string;
39
+ message: string;
40
+ hint?: string;
41
+ }>;
42
+ };
43
+ export type RowLike = {
44
+ get(column: string): unknown;
45
+ value(column: string): ValueLike;
46
+ toObject(): Record<string, unknown>;
47
+ toValueMap(): Record<string, ValueLike>;
48
+ };
49
+ export type ValueLike = {
50
+ readonly kind: LixValue["kind"];
51
+ toJS(): unknown;
52
+ asBytes(): Uint8Array | undefined;
53
+ };
54
+ export type CreateBranchOptions = {
55
+ id?: string;
56
+ name: string;
57
+ fromCommitId?: string;
58
+ };
59
+ export type CreateBranchReceipt = {
60
+ id: string;
61
+ name: string;
62
+ hidden: boolean;
63
+ commitId: string;
64
+ };
65
+ export type SwitchBranchOptions = {
66
+ branchId: string;
67
+ };
68
+ export type SwitchBranchReceipt = {
69
+ branchId: string;
70
+ };
71
+ export type MergeBranchOptions = {
72
+ sourceBranchId: string;
73
+ };
74
+ export type MergeBranchOutcome = "alreadyUpToDate" | "fastForward" | "mergeCommitted";
75
+ export type MergeBranchReceipt = {
76
+ outcome: MergeBranchOutcome;
77
+ targetBranchId: string;
78
+ sourceBranchId: string;
79
+ baseCommitId: string;
80
+ targetHeadBeforeCommitId: string;
81
+ sourceHeadBeforeCommitId: string;
82
+ targetHeadAfterCommitId: string;
83
+ createdMergeCommitId: string | null;
84
+ changeStats: MergeChangeStats;
85
+ };
86
+ export type MergeBranchPreview = {
87
+ outcome: MergeBranchOutcome;
88
+ targetBranchId: string;
89
+ sourceBranchId: string;
90
+ baseCommitId: string;
91
+ targetHeadCommitId: string;
92
+ sourceHeadCommitId: string;
93
+ changeStats: MergeChangeStats;
94
+ conflicts: MergeConflict[];
95
+ };
96
+ export type MergeChangeStats = {
97
+ total: number;
98
+ added: number;
99
+ modified: number;
100
+ removed: number;
101
+ };
102
+ export type MergeConflict = {
103
+ kind: "sameEntityChanged";
104
+ schemaKey: string;
105
+ entityPk: unknown;
106
+ fileId: string | null;
107
+ target: MergeConflictSide;
108
+ source: MergeConflictSide;
109
+ };
110
+ export type MergeConflictSide = {
111
+ kind: "added" | "modified" | "removed";
112
+ beforeChangeId: string | null;
113
+ afterChangeId: string | null;
114
+ };
@@ -0,0 +1,28 @@
1
+ import type { JsonValue, LixValue, SqlParam } from "./types.js";
2
+ export declare class Value {
3
+ #private;
4
+ readonly kind: LixValue["kind"];
5
+ private constructor();
6
+ static null(): Value;
7
+ static boolean(value: boolean): Value;
8
+ static integer(value: number): Value;
9
+ static real(value: number): Value;
10
+ static text(value: string): Value;
11
+ static json(value: JsonValue): Value;
12
+ static blob(value: Uint8Array): Value;
13
+ static from(value: SqlParam): Value;
14
+ static _fromNative(value: LixValue): Value;
15
+ _toNative(): NativeLixValue;
16
+ toJS(): unknown;
17
+ asBytes(): Uint8Array<ArrayBuffer> | undefined;
18
+ }
19
+ export type NativeLixValue = Exclude<LixValue, {
20
+ kind: "blob";
21
+ }> | {
22
+ kind: "blob";
23
+ value?: null;
24
+ blob: Uint8Array;
25
+ };
26
+ export declare function toNativeValue(value: LixValue): NativeLixValue;
27
+ export declare function fromNativeValue(value: NativeLixValue): LixValue;
28
+ export declare function normalizeParam(value: SqlParam, index?: number, seen?: WeakSet<object>): LixValue;