@lix-js/sdk 0.6.0-preview.4 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) 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 +39 -201
  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 +20 -50
  16. package/SKILL.md +0 -506
  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 -821
  23. package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
  24. package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +0 -26
  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 -303
  29. package/dist-engine-src/README.md +0 -18
  30. package/dist-engine-src/src/backend/kv.rs +0 -358
  31. package/dist-engine-src/src/backend/mod.rs +0 -12
  32. package/dist-engine-src/src/backend/testing.rs +0 -658
  33. package/dist-engine-src/src/backend/types.rs +0 -96
  34. package/dist-engine-src/src/binary_cas/chunking.rs +0 -31
  35. package/dist-engine-src/src/binary_cas/codec.rs +0 -346
  36. package/dist-engine-src/src/binary_cas/context.rs +0 -139
  37. package/dist-engine-src/src/binary_cas/kv.rs +0 -1063
  38. package/dist-engine-src/src/binary_cas/mod.rs +0 -11
  39. package/dist-engine-src/src/binary_cas/types.rs +0 -121
  40. package/dist-engine-src/src/catalog/context.rs +0 -412
  41. package/dist-engine-src/src/catalog/mod.rs +0 -10
  42. package/dist-engine-src/src/catalog/schema.rs +0 -4
  43. package/dist-engine-src/src/catalog/snapshot.rs +0 -1114
  44. package/dist-engine-src/src/cel/context.rs +0 -86
  45. package/dist-engine-src/src/cel/error.rs +0 -19
  46. package/dist-engine-src/src/cel/mod.rs +0 -8
  47. package/dist-engine-src/src/cel/provider.rs +0 -9
  48. package/dist-engine-src/src/cel/runtime.rs +0 -167
  49. package/dist-engine-src/src/cel/value.rs +0 -50
  50. package/dist-engine-src/src/commit_graph/context.rs +0 -901
  51. package/dist-engine-src/src/commit_graph/mod.rs +0 -11
  52. package/dist-engine-src/src/commit_graph/types.rs +0 -109
  53. package/dist-engine-src/src/commit_graph/walker.rs +0 -756
  54. package/dist-engine-src/src/commit_store/codec.rs +0 -887
  55. package/dist-engine-src/src/commit_store/context.rs +0 -944
  56. package/dist-engine-src/src/commit_store/materialization.rs +0 -84
  57. package/dist-engine-src/src/commit_store/mod.rs +0 -16
  58. package/dist-engine-src/src/commit_store/storage.rs +0 -600
  59. package/dist-engine-src/src/commit_store/types.rs +0 -215
  60. package/dist-engine-src/src/common/error.rs +0 -313
  61. package/dist-engine-src/src/common/fingerprint.rs +0 -3
  62. package/dist-engine-src/src/common/fs_path.rs +0 -1336
  63. package/dist-engine-src/src/common/identity.rs +0 -145
  64. package/dist-engine-src/src/common/json_pointer.rs +0 -67
  65. package/dist-engine-src/src/common/metadata.rs +0 -40
  66. package/dist-engine-src/src/common/mod.rs +0 -23
  67. package/dist-engine-src/src/common/types.rs +0 -105
  68. package/dist-engine-src/src/common/wire.rs +0 -222
  69. package/dist-engine-src/src/domain.rs +0 -324
  70. package/dist-engine-src/src/engine.rs +0 -225
  71. package/dist-engine-src/src/entity_identity.rs +0 -405
  72. package/dist-engine-src/src/functions/context.rs +0 -292
  73. package/dist-engine-src/src/functions/deterministic.rs +0 -113
  74. package/dist-engine-src/src/functions/mod.rs +0 -18
  75. package/dist-engine-src/src/functions/provider.rs +0 -130
  76. package/dist-engine-src/src/functions/state.rs +0 -336
  77. package/dist-engine-src/src/functions/types.rs +0 -37
  78. package/dist-engine-src/src/init.rs +0 -558
  79. package/dist-engine-src/src/json_store/compression.rs +0 -77
  80. package/dist-engine-src/src/json_store/context.rs +0 -423
  81. package/dist-engine-src/src/json_store/encoded.rs +0 -15
  82. package/dist-engine-src/src/json_store/mod.rs +0 -12
  83. package/dist-engine-src/src/json_store/store.rs +0 -1109
  84. package/dist-engine-src/src/json_store/types.rs +0 -217
  85. package/dist-engine-src/src/lib.rs +0 -62
  86. package/dist-engine-src/src/live_state/context.rs +0 -2019
  87. package/dist-engine-src/src/live_state/mod.rs +0 -15
  88. package/dist-engine-src/src/live_state/overlay.rs +0 -75
  89. package/dist-engine-src/src/live_state/reader.rs +0 -23
  90. package/dist-engine-src/src/live_state/types.rs +0 -222
  91. package/dist-engine-src/src/live_state/visibility.rs +0 -223
  92. package/dist-engine-src/src/plugin/archive.rs +0 -438
  93. package/dist-engine-src/src/plugin/component.rs +0 -183
  94. package/dist-engine-src/src/plugin/install.rs +0 -619
  95. package/dist-engine-src/src/plugin/manifest.rs +0 -516
  96. package/dist-engine-src/src/plugin/materializer.rs +0 -477
  97. package/dist-engine-src/src/plugin/mod.rs +0 -33
  98. package/dist-engine-src/src/plugin/plugin_manifest.json +0 -118
  99. package/dist-engine-src/src/plugin/storage.rs +0 -74
  100. package/dist-engine-src/src/schema/annotations/defaults.rs +0 -275
  101. package/dist-engine-src/src/schema/annotations/mod.rs +0 -1
  102. package/dist-engine-src/src/schema/builtin/lix_account.json +0 -21
  103. package/dist-engine-src/src/schema/builtin/lix_active_account.json +0 -29
  104. package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +0 -29
  105. package/dist-engine-src/src/schema/builtin/lix_change.json +0 -63
  106. package/dist-engine-src/src/schema/builtin/lix_change_author.json +0 -45
  107. package/dist-engine-src/src/schema/builtin/lix_commit.json +0 -24
  108. package/dist-engine-src/src/schema/builtin/lix_commit_edge.json +0 -53
  109. package/dist-engine-src/src/schema/builtin/lix_directory_descriptor.json +0 -52
  110. package/dist-engine-src/src/schema/builtin/lix_file_descriptor.json +0 -52
  111. package/dist-engine-src/src/schema/builtin/lix_key_value.json +0 -40
  112. package/dist-engine-src/src/schema/builtin/lix_label.json +0 -29
  113. package/dist-engine-src/src/schema/builtin/lix_label_assignment.json +0 -74
  114. package/dist-engine-src/src/schema/builtin/lix_registered_schema.json +0 -25
  115. package/dist-engine-src/src/schema/builtin/lix_version_descriptor.json +0 -34
  116. package/dist-engine-src/src/schema/builtin/lix_version_ref.json +0 -48
  117. package/dist-engine-src/src/schema/builtin/mod.rs +0 -222
  118. package/dist-engine-src/src/schema/compatibility.rs +0 -787
  119. package/dist-engine-src/src/schema/definition.json +0 -187
  120. package/dist-engine-src/src/schema/definition.rs +0 -742
  121. package/dist-engine-src/src/schema/key.rs +0 -138
  122. package/dist-engine-src/src/schema/mod.rs +0 -20
  123. package/dist-engine-src/src/schema/seed.rs +0 -14
  124. package/dist-engine-src/src/schema/tests.rs +0 -780
  125. package/dist-engine-src/src/session/context.rs +0 -404
  126. package/dist-engine-src/src/session/create_version.rs +0 -88
  127. package/dist-engine-src/src/session/execute.rs +0 -541
  128. package/dist-engine-src/src/session/merge/analysis.rs +0 -102
  129. package/dist-engine-src/src/session/merge/apply.rs +0 -23
  130. package/dist-engine-src/src/session/merge/conflicts.rs +0 -63
  131. package/dist-engine-src/src/session/merge/mod.rs +0 -11
  132. package/dist-engine-src/src/session/merge/stats.rs +0 -65
  133. package/dist-engine-src/src/session/merge/version.rs +0 -427
  134. package/dist-engine-src/src/session/mod.rs +0 -27
  135. package/dist-engine-src/src/session/optimization9_sql2_bench.rs +0 -100
  136. package/dist-engine-src/src/session/switch_version.rs +0 -110
  137. package/dist-engine-src/src/session/transaction.rs +0 -76
  138. package/dist-engine-src/src/sql2/change_provider.rs +0 -331
  139. package/dist-engine-src/src/sql2/classify.rs +0 -174
  140. package/dist-engine-src/src/sql2/context.rs +0 -311
  141. package/dist-engine-src/src/sql2/directory_history_provider.rs +0 -631
  142. package/dist-engine-src/src/sql2/directory_provider.rs +0 -2453
  143. package/dist-engine-src/src/sql2/dml.rs +0 -148
  144. package/dist-engine-src/src/sql2/entity_history_provider.rs +0 -440
  145. package/dist-engine-src/src/sql2/entity_provider.rs +0 -3211
  146. package/dist-engine-src/src/sql2/error.rs +0 -215
  147. package/dist-engine-src/src/sql2/execute.rs +0 -3533
  148. package/dist-engine-src/src/sql2/file_history_provider.rs +0 -910
  149. package/dist-engine-src/src/sql2/file_provider.rs +0 -3679
  150. package/dist-engine-src/src/sql2/filesystem_planner.rs +0 -1490
  151. package/dist-engine-src/src/sql2/filesystem_predicates.rs +0 -159
  152. package/dist-engine-src/src/sql2/filesystem_visibility.rs +0 -383
  153. package/dist-engine-src/src/sql2/history_projection.rs +0 -56
  154. package/dist-engine-src/src/sql2/history_provider.rs +0 -412
  155. package/dist-engine-src/src/sql2/history_route.rs +0 -657
  156. package/dist-engine-src/src/sql2/lix_state_provider.rs +0 -2512
  157. package/dist-engine-src/src/sql2/mod.rs +0 -47
  158. package/dist-engine-src/src/sql2/predicate_typecheck.rs +0 -246
  159. package/dist-engine-src/src/sql2/public_bind/assignment.rs +0 -46
  160. package/dist-engine-src/src/sql2/public_bind/capability.rs +0 -41
  161. package/dist-engine-src/src/sql2/public_bind/dml.rs +0 -172
  162. package/dist-engine-src/src/sql2/public_bind/mod.rs +0 -26
  163. package/dist-engine-src/src/sql2/public_bind/table.rs +0 -168
  164. package/dist-engine-src/src/sql2/read_only.rs +0 -63
  165. package/dist-engine-src/src/sql2/record_batch.rs +0 -17
  166. package/dist-engine-src/src/sql2/result_metadata.rs +0 -29
  167. package/dist-engine-src/src/sql2/runtime.rs +0 -60
  168. package/dist-engine-src/src/sql2/session.rs +0 -132
  169. package/dist-engine-src/src/sql2/udfs/common.rs +0 -295
  170. package/dist-engine-src/src/sql2/udfs/lix_active_version_commit_id.rs +0 -53
  171. package/dist-engine-src/src/sql2/udfs/lix_empty_blob.rs +0 -47
  172. package/dist-engine-src/src/sql2/udfs/lix_json.rs +0 -100
  173. package/dist-engine-src/src/sql2/udfs/lix_json_get.rs +0 -99
  174. package/dist-engine-src/src/sql2/udfs/lix_json_get_text.rs +0 -99
  175. package/dist-engine-src/src/sql2/udfs/lix_text_decode.rs +0 -82
  176. package/dist-engine-src/src/sql2/udfs/lix_text_encode.rs +0 -85
  177. package/dist-engine-src/src/sql2/udfs/lix_timestamp.rs +0 -76
  178. package/dist-engine-src/src/sql2/udfs/lix_uuid_v7.rs +0 -76
  179. package/dist-engine-src/src/sql2/udfs/mod.rs +0 -89
  180. package/dist-engine-src/src/sql2/udfs/public_call.rs +0 -238
  181. package/dist-engine-src/src/sql2/version_provider.rs +0 -1202
  182. package/dist-engine-src/src/sql2/version_scope.rs +0 -394
  183. package/dist-engine-src/src/sql2/write_normalization.rs +0 -345
  184. package/dist-engine-src/src/storage/context.rs +0 -356
  185. package/dist-engine-src/src/storage/mod.rs +0 -14
  186. package/dist-engine-src/src/storage/read_scope.rs +0 -88
  187. package/dist-engine-src/src/storage/types.rs +0 -501
  188. package/dist-engine-src/src/storage_bench.rs +0 -4863
  189. package/dist-engine-src/src/test_support.rs +0 -228
  190. package/dist-engine-src/src/tracked_state/by_file_index.rs +0 -98
  191. package/dist-engine-src/src/tracked_state/codec.rs +0 -2085
  192. package/dist-engine-src/src/tracked_state/context.rs +0 -1867
  193. package/dist-engine-src/src/tracked_state/diff.rs +0 -686
  194. package/dist-engine-src/src/tracked_state/materialization.rs +0 -403
  195. package/dist-engine-src/src/tracked_state/materializer.rs +0 -488
  196. package/dist-engine-src/src/tracked_state/merge.rs +0 -492
  197. package/dist-engine-src/src/tracked_state/mod.rs +0 -32
  198. package/dist-engine-src/src/tracked_state/storage.rs +0 -375
  199. package/dist-engine-src/src/tracked_state/tree.rs +0 -3187
  200. package/dist-engine-src/src/tracked_state/types.rs +0 -231
  201. package/dist-engine-src/src/transaction/commit.rs +0 -1484
  202. package/dist-engine-src/src/transaction/context.rs +0 -1548
  203. package/dist-engine-src/src/transaction/live_state_overlay.rs +0 -35
  204. package/dist-engine-src/src/transaction/mod.rs +0 -13
  205. package/dist-engine-src/src/transaction/normalization.rs +0 -890
  206. package/dist-engine-src/src/transaction/prep.rs +0 -37
  207. package/dist-engine-src/src/transaction/schema_resolver.rs +0 -149
  208. package/dist-engine-src/src/transaction/staging.rs +0 -1731
  209. package/dist-engine-src/src/transaction/types.rs +0 -460
  210. package/dist-engine-src/src/transaction/validation.rs +0 -5830
  211. package/dist-engine-src/src/untracked_state/codec.rs +0 -307
  212. package/dist-engine-src/src/untracked_state/context.rs +0 -98
  213. package/dist-engine-src/src/untracked_state/materialization.rs +0 -63
  214. package/dist-engine-src/src/untracked_state/mod.rs +0 -15
  215. package/dist-engine-src/src/untracked_state/storage.rs +0 -396
  216. package/dist-engine-src/src/untracked_state/types.rs +0 -146
  217. package/dist-engine-src/src/version/context.rs +0 -40
  218. package/dist-engine-src/src/version/lifecycle.rs +0 -221
  219. package/dist-engine-src/src/version/mod.rs +0 -13
  220. package/dist-engine-src/src/version/refs.rs +0 -330
  221. package/dist-engine-src/src/version/stage_rows.rs +0 -67
  222. package/dist-engine-src/src/version/types.rs +0 -21
  223. 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 activeVersionId() {
133
- return await runQueued(() => wasmLix.activeVersionId());
134
- },
135
- async createVersion(options) {
136
- return await runQueued(() => wasmLix.createVersion(options));
137
- },
138
- async switchVersion(options) {
139
- return await runQueued(() => wasmLix.switchVersion(options));
140
- },
141
- async mergeVersionPreview(options) {
142
- return await runQueued(() => wasmLix.mergeVersionPreview(options));
143
- },
144
- async mergeVersion(options) {
145
- return await runQueued(() => wasmLix.mergeVersion(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;