datalevin-node 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +293 -31
- package/jars/{datalevin-runtime-1.0.2.jar → datalevin-runtime-1.1.0.jar} +0 -0
- package/package.json +1 -1
- package/src/client.js +5 -3
- package/src/connection.js +23 -9
- package/src/convert.js +73 -0
- package/src/form.js +408 -0
- package/src/index.js +26 -1
- package/src/interop.js +16 -0
- package/src/query.js +1352 -0
- package/src/transaction.js +312 -0
- package/src/udf-value.js +252 -0
- package/src/udf.js +56 -65
package/README.md
CHANGED
|
@@ -19,7 +19,14 @@ normal usage does not require building Datalevin from source.
|
|
|
19
19
|
## Quick Start
|
|
20
20
|
|
|
21
21
|
```js
|
|
22
|
-
import { connect } from "datalevin-node";
|
|
22
|
+
import { connect, q, tx } from "datalevin-node";
|
|
23
|
+
|
|
24
|
+
const entity = q.var("entity");
|
|
25
|
+
const name = q.var("name");
|
|
26
|
+
const allNames = q.query({
|
|
27
|
+
find: q.collection(name),
|
|
28
|
+
where: [q.datom(entity, "name", name)]
|
|
29
|
+
});
|
|
23
30
|
|
|
24
31
|
const conn = await connect("/tmp/dtlv-js", {
|
|
25
32
|
schema: {
|
|
@@ -31,12 +38,12 @@ const conn = await connect("/tmp/dtlv-js", {
|
|
|
31
38
|
});
|
|
32
39
|
|
|
33
40
|
try {
|
|
34
|
-
await conn.transact(
|
|
35
|
-
{
|
|
36
|
-
{
|
|
37
|
-
|
|
41
|
+
await conn.transact(tx.data(
|
|
42
|
+
tx.entity({ name: "Ada" }),
|
|
43
|
+
tx.entity({ name: "Bob" })
|
|
44
|
+
));
|
|
38
45
|
|
|
39
|
-
const names = await conn.query(
|
|
46
|
+
const names = await conn.query(allNames);
|
|
40
47
|
const ada = await conn.pull([":name"], 1);
|
|
41
48
|
|
|
42
49
|
console.log(names);
|
|
@@ -46,16 +53,247 @@ try {
|
|
|
46
53
|
}
|
|
47
54
|
```
|
|
48
55
|
|
|
56
|
+
The `q` and `tx` builders are synchronous, pure JavaScript values. Composing
|
|
57
|
+
them does not start the JVM; a connection lowers them to the active backend only
|
|
58
|
+
when `query()` or `transact()` is called.
|
|
59
|
+
|
|
60
|
+
Their contract is an immutable snapshot, not a mutable builder. Construction
|
|
61
|
+
recursively detaches arrays, plain objects, `Map`, `Set`, `Date`, and byte-array
|
|
62
|
+
values. Arrays and objects are frozen; collection and mutable scalar snapshots
|
|
63
|
+
reject mutation, including through values returned by `toForm()`. Mutating an
|
|
64
|
+
input container later therefore cannot change an existing form. Keyword,
|
|
65
|
+
Datalog-symbol, and UUID values are interned by normalized type and value, so
|
|
66
|
+
`q.kw("user/id") === q.kw(":user/id")` and repeated equivalent values occupy
|
|
67
|
+
one `Map` key. JavaScript compound objects still use the language's normal
|
|
68
|
+
identity comparison; the immutable snapshot, rather than structural `===`, is their
|
|
69
|
+
composition contract. Non-structural objects such as backend handles are
|
|
70
|
+
treated as atomic values.
|
|
71
|
+
|
|
72
|
+
## Composing Queries
|
|
73
|
+
|
|
74
|
+
Use normal JavaScript control flow to assemble clauses. Variables, attributes,
|
|
75
|
+
symbols, and keyword values are explicit, so strings beginning with `?` or `:`
|
|
76
|
+
remain literal strings.
|
|
77
|
+
|
|
78
|
+
The same rule applies recursively to runtime inputs passed to a typed query.
|
|
79
|
+
Use `q.kw("active")` when an input is a keyword. EDN-string and native-array
|
|
80
|
+
queries keep the legacy `":active"` keyword shorthand for compatibility.
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
const entity = q.var("entity");
|
|
84
|
+
const name = q.var("name");
|
|
85
|
+
const age = q.var("age");
|
|
86
|
+
const minimum = q.var("minimum");
|
|
87
|
+
|
|
88
|
+
const where = [
|
|
89
|
+
q.datom(entity, "person/name", name),
|
|
90
|
+
q.datom(entity, "person/age", age)
|
|
91
|
+
];
|
|
92
|
+
if (adultsOnly) {
|
|
93
|
+
where.push(q.predicate(">=", age, minimum));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const people = q.query({
|
|
97
|
+
find: q.collection(name),
|
|
98
|
+
inputs: adultsOnly ? [q.DB, minimum] : [],
|
|
99
|
+
where
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const names = adultsOnly
|
|
103
|
+
? await conn.query(people, 18)
|
|
104
|
+
: await conn.query(people);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Available helpers cover relation, collection, tuple, and scalar finds;
|
|
108
|
+
aggregates and pull expressions; database patterns, predicate,
|
|
109
|
+
function-binding, rule, and logical clauses; input bindings, joins, rules,
|
|
110
|
+
ordering, limits, offsets, and timeouts. `q.datom(e, a, v)` is the common
|
|
111
|
+
three-term form; use variable-arity `q.pattern(e, a)` for a presence pattern or
|
|
112
|
+
other supported database-pattern arity. `q.raw()` remains the structured escape
|
|
113
|
+
hatch for new syntax; its tokens must use `q.kw()` and `q.sym()` explicitly.
|
|
114
|
+
Relation queries return an Array of row Arrays. Without `orderBy`, row order is
|
|
115
|
+
unspecified.
|
|
116
|
+
|
|
117
|
+
Typed forms validate structural grammar when they are composed: keyed result
|
|
118
|
+
names must match a relation/tuple find, join variables must be non-empty and
|
|
119
|
+
distinct, ordering must reference distinct projected variables or valid column
|
|
120
|
+
indexes, and branches of one rule name must have matching required/free arity.
|
|
121
|
+
`q.and()` is a branch group for `q.or()` or `q.orJoin()` and is rejected as a
|
|
122
|
+
top-level `:where`, `not`, `not-join`, or rule-body clause. `q.rules(...)` exposes
|
|
123
|
+
the same `asData()` inspection helper as the other typed forms. `q.raw()`
|
|
124
|
+
deliberately bypasses these typed checks.
|
|
125
|
+
|
|
126
|
+
### Composing search clauses
|
|
127
|
+
|
|
128
|
+
Full-text, vector, and indexed-document searches have dedicated clauses and
|
|
129
|
+
position-aware option builders:
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
const entity = q.var("entity");
|
|
133
|
+
const attribute = q.var("attribute");
|
|
134
|
+
const value = q.var("value");
|
|
135
|
+
const score = q.var("score");
|
|
136
|
+
const distance = q.var("distance");
|
|
137
|
+
|
|
138
|
+
const textClause = q.fulltext(
|
|
139
|
+
q.var("term"),
|
|
140
|
+
[entity, attribute, value, score],
|
|
141
|
+
{
|
|
142
|
+
attribute: "document/text",
|
|
143
|
+
options: q.fulltextOptions({ top: 5, display: "refs+scores" })
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
const vectorClause = q.vecNeighbors(
|
|
148
|
+
q.var("embedding"),
|
|
149
|
+
[entity, attribute, value, distance],
|
|
150
|
+
{
|
|
151
|
+
options: q.vectorSearchOptions({
|
|
152
|
+
domains: ["documents"], top: 10, display: "refs+dists"
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
const idocClause = q.idocMatch(
|
|
158
|
+
q.var("predicate"),
|
|
159
|
+
[entity, attribute, value],
|
|
160
|
+
{ options: q.idocMatchOptions({ domains: ["profiles"] }) }
|
|
161
|
+
);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Pass either `attribute` or a domain list, as supported by the corresponding
|
|
165
|
+
index. Result Arrays are lowered to relation bindings; an explicit
|
|
166
|
+
`q.relationBinding(...)` is also accepted. Static `display` options validate
|
|
167
|
+
the result width while composing the query: full-text supports `refs`,
|
|
168
|
+
`refs+scores`, `texts`, `offsets`, and `texts+offsets`; vector search supports
|
|
169
|
+
`refs` and `refs+dists`. Use a `q.var()` as `options` when the option map is a
|
|
170
|
+
runtime query input. `source` selects a non-default source.
|
|
171
|
+
|
|
172
|
+
### Composing pull selectors
|
|
173
|
+
|
|
174
|
+
Pull selectors use position-aware forms so option values remain ordinary
|
|
175
|
+
JavaScript data:
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
const person = q.selector(
|
|
179
|
+
"person/name",
|
|
180
|
+
q.pullAttr("person/nickname", { default: "none", as: "display" }),
|
|
181
|
+
q.pullAttr("person/age", { xform: "str" }),
|
|
182
|
+
q.pullNested("person/friend", q.selector("person/name")),
|
|
183
|
+
q.pullRecursive("person/manager", 2)
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
const result = await conn.pull(person, entityId);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
`default` and `as` accept arbitrary values and preserve strings recursively.
|
|
190
|
+
A string `xform` is a symbol name; an explicit `q.sym()` is also accepted.
|
|
191
|
+
Omit the second argument to `pullRecursive` for unbounded `...` recursion.
|
|
192
|
+
Existing native-array selectors remain supported and are normalized by pull
|
|
193
|
+
grammar position rather than by their string contents.
|
|
194
|
+
|
|
195
|
+
## Composing Transactions
|
|
196
|
+
|
|
197
|
+
Transaction items compose the same way:
|
|
198
|
+
|
|
199
|
+
```js
|
|
200
|
+
const items = [tx.entity(-1, { "person/name": "Ada" })];
|
|
201
|
+
if (nickname !== null) {
|
|
202
|
+
items.push(tx.add(-1, "person/nickname", nickname));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const report = await conn.transact(tx.data(items));
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Use `tx.entity(attrs)` when Datalevin should allocate the entity id, and
|
|
209
|
+
`tx.entity(id, attrs)` when supplying an explicit id or temporary id.
|
|
210
|
+
`tx.entity(null, attrs)` remains accepted for compatibility but is unnecessary.
|
|
211
|
+
|
|
212
|
+
In addition to entity maps, `tx` provides `add`, `retract`,
|
|
213
|
+
`retractAttribute`, `retractEntity`, `compareAndSwap`/`cas`, `call`, `ensure`,
|
|
214
|
+
and `patchIdoc` operations. Context-sensitive transaction values also have
|
|
215
|
+
explicit builders:
|
|
216
|
+
|
|
217
|
+
```js
|
|
218
|
+
const eve = tx.lookupRef("user/handle", "eve");
|
|
219
|
+
|
|
220
|
+
const report = await conn.transact(tx.data(
|
|
221
|
+
tx.entity(-1, {
|
|
222
|
+
"user/handle": "alice",
|
|
223
|
+
"user/friend": eve,
|
|
224
|
+
"user/child": tx.entity(-2, { "user/handle": "child" })
|
|
225
|
+
}),
|
|
226
|
+
tx.patchIdoc(eve, "user/profile", [
|
|
227
|
+
tx.patchSet(["status"], "active"),
|
|
228
|
+
tx.patchUpdate(["visits"], "inc"),
|
|
229
|
+
tx.patchUnset(["obsolete"])
|
|
230
|
+
]),
|
|
231
|
+
tx.invoke("people/audit", eve)
|
|
232
|
+
));
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
`lookupRef` turns only its attribute into a keyword, so its lookup value stays
|
|
236
|
+
literal. The patch builders similarly type only `set`, `unset`, `update`, and
|
|
237
|
+
the update operation; paths, assigned values, and update arguments stay
|
|
238
|
+
ordinary JavaScript values. `invoke` emits the direct form for a transaction
|
|
239
|
+
function installed under a database ident, while `call` emits
|
|
240
|
+
`:db.fn/call`.
|
|
241
|
+
|
|
242
|
+
Wrap a nested entity object in `tx.entity(...)`, as above. A plain nested
|
|
243
|
+
object with ordinary string keys remains data; the builder does not recursively
|
|
244
|
+
convert it into an entity. More generally, values remain ordinary JavaScript
|
|
245
|
+
data; `q.kw()` and `q.sym()` add explicit Datalevin tokens where needed.
|
|
246
|
+
|
|
247
|
+
JavaScript has no built-in UUID scalar. The synchronous `uuid()` helper creates
|
|
248
|
+
an immutable UUID value without starting the JVM; it is validated,
|
|
249
|
+
canonicalized, and lowered only when used by an operation:
|
|
250
|
+
|
|
251
|
+
```js
|
|
252
|
+
import { tx, uuid } from "datalevin-node";
|
|
253
|
+
|
|
254
|
+
const id = uuid("550e8400-e29b-41d4-a716-446655440000");
|
|
255
|
+
await conn.transact(tx.data(tx.entity({ "record/id": id })));
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Repeated equivalent UUID values are interned. UUID results remain canonical
|
|
259
|
+
strings for compatibility; pass such a result through `uuid()` when using it
|
|
260
|
+
again in a typed input position.
|
|
261
|
+
|
|
262
|
+
### EDN lists and quoting
|
|
263
|
+
|
|
264
|
+
JavaScript Arrays represent EDN vectors. Use `q.ednList()` when EDN syntax
|
|
265
|
+
requires a list, such as an IDoc predicate, and `q.quote()` for the common
|
|
266
|
+
`(quote value)` form used by nested queries:
|
|
267
|
+
|
|
268
|
+
```js
|
|
269
|
+
const ageFilter = {
|
|
270
|
+
profile: { age: q.ednList(q.sym(">="), 30) }
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const age = q.var("age");
|
|
274
|
+
const inner = q.query({
|
|
275
|
+
find: q.relation(q.aggregate("min", age)),
|
|
276
|
+
where: [q.datom(q.IGNORE, "person/age", age)]
|
|
277
|
+
});
|
|
278
|
+
const quotedInner = q.quote(inner);
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
Both forms are immutable, backend-neutral values and do not start the JVM.
|
|
282
|
+
Their top-level aliases are `ednList()` and `quote()`. Ordinary strings inside
|
|
283
|
+
an EDN list remain strings; use `q.sym()` for an operator or other symbol. This
|
|
284
|
+
keeps these cases composable without `readEdn()`.
|
|
285
|
+
|
|
49
286
|
## Data Style
|
|
50
287
|
|
|
51
|
-
|
|
288
|
+
The existing EDN and native-form APIs remain supported alongside the builders:
|
|
52
289
|
|
|
53
290
|
- schemas are objects keyed by colon-prefixed attribute strings
|
|
54
|
-
- transaction
|
|
55
|
-
- query and pull forms may be EDN strings or JavaScript arrays
|
|
291
|
+
- transaction data may use `tx`, objects/arrays, or existing `tx*` helpers
|
|
292
|
+
- query and pull forms may be builder values, EDN strings, or JavaScript arrays
|
|
56
293
|
- colon-prefixed strings are converted to keywords in schema/query/form
|
|
57
|
-
positions
|
|
58
|
-
- use `
|
|
294
|
+
positions for the legacy native-form API
|
|
295
|
+
- use synchronous `q.kw()` in builder values, or `keyword()` with the legacy
|
|
296
|
+
API, when the stored value itself is a keyword
|
|
59
297
|
|
|
60
298
|
```js
|
|
61
299
|
import { keyword, readEdn, schemaAttr, txAdd, txEntity, writeEdn } from "datalevin-node";
|
|
@@ -194,37 +432,54 @@ const report = await conn.transactAsync([
|
|
|
194
432
|
]);
|
|
195
433
|
```
|
|
196
434
|
|
|
197
|
-
##
|
|
435
|
+
## Composing UDFs
|
|
198
436
|
|
|
199
|
-
Use
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
`
|
|
203
|
-
`
|
|
204
|
-
|
|
437
|
+
Use the immutable `UdfDescriptor` value with the typed API. It preserves
|
|
438
|
+
keyword types when nested in query inputs, transactions, schema attributes, or
|
|
439
|
+
search-domain options. `q.bindUdf()` binds a query-function result and
|
|
440
|
+
`q.udfPredicate()` creates a predicate clause. Transaction UDFs have explicit
|
|
441
|
+
`tx.callUdf()`, `tx.installUdf()`, and `tx.uninstallUdf()` helpers; an installed
|
|
442
|
+
descriptor can subsequently be called with `tx.invoke()`. Both transaction UDFs
|
|
443
|
+
and predicate UDFs used by `tx.ensure()` receive a `Database` as their first
|
|
444
|
+
callback argument.
|
|
205
445
|
|
|
206
446
|
```js
|
|
207
|
-
import { connect, createUdfRegistry,
|
|
447
|
+
import { UdfDescriptor, connect, createUdfRegistry, q } from "datalevin-node";
|
|
208
448
|
|
|
209
449
|
const registry = await createUdfRegistry();
|
|
210
|
-
|
|
450
|
+
const descriptor = UdfDescriptor.queryFn("math/inc");
|
|
451
|
+
await registry.register(descriptor, (value) => Number(value) + 1);
|
|
211
452
|
|
|
212
|
-
const descriptor = udfDescriptor(":math/inc");
|
|
213
453
|
const conn = await connect("/tmp/dtlv-js-udf", {
|
|
214
454
|
opts: { ":runtime-opts": { ":udf-registry": registry } }
|
|
215
455
|
});
|
|
216
456
|
|
|
217
457
|
try {
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
458
|
+
const descriptorInput = q.var("descriptor");
|
|
459
|
+
const number = q.var("number");
|
|
460
|
+
const value = q.var("value");
|
|
461
|
+
const increment = q.query({
|
|
462
|
+
find: q.scalar(value),
|
|
463
|
+
inputs: [q.DB, descriptorInput, number],
|
|
464
|
+
where: [q.bindUdf(descriptorInput, value, number)]
|
|
465
|
+
});
|
|
466
|
+
console.log(await conn.query(increment, descriptor, 41));
|
|
223
467
|
} finally {
|
|
224
468
|
await conn.close();
|
|
225
469
|
}
|
|
226
470
|
```
|
|
227
471
|
|
|
472
|
+
`udfDescriptor()` remains available and returns the original mutable
|
|
473
|
+
colon-string object for EDN-string and native-array compatibility code.
|
|
474
|
+
`UdfDescriptor` factories, bare IDs passed to a registry, and the
|
|
475
|
+
`queryUdf()`/`predicateUdf()`/`txUdf()`/analyzer registration conveniences
|
|
476
|
+
default to `:javascript`. The legacy helper retains its `:java` default. Both
|
|
477
|
+
forms are accepted by a registry, explicit `{ lang: "java" }` remains
|
|
478
|
+
available, and the typed UDF helpers normalize legacy descriptors when they are
|
|
479
|
+
used explicitly. Typed descriptors are interned by normalized language, kind,
|
|
480
|
+
id, and version, so equivalent descriptors also behave as one key in a
|
|
481
|
+
JavaScript `Map`.
|
|
482
|
+
|
|
228
483
|
## Fulltext Analyzer UDF Example
|
|
229
484
|
|
|
230
485
|
Use analyzer UDFs when a Datalog fulltext domain needs host-language tokenizing.
|
|
@@ -237,14 +492,14 @@ import {
|
|
|
237
492
|
createUdfRegistry,
|
|
238
493
|
schemaAttr,
|
|
239
494
|
searchDomain,
|
|
240
|
-
|
|
495
|
+
UdfDescriptor
|
|
241
496
|
} from "datalevin-node";
|
|
242
497
|
|
|
243
498
|
const registry = await createUdfRegistry();
|
|
244
|
-
const analyzer =
|
|
245
|
-
const queryAnalyzer =
|
|
499
|
+
const analyzer = UdfDescriptor.analyzer("text/hashtags");
|
|
500
|
+
const queryAnalyzer = UdfDescriptor.queryAnalyzer("text/plain-query");
|
|
246
501
|
|
|
247
|
-
await registry.
|
|
502
|
+
await registry.register(analyzer, (text) => {
|
|
248
503
|
const tokens = [];
|
|
249
504
|
const pattern = /#\w+/g;
|
|
250
505
|
const source = String(text);
|
|
@@ -255,7 +510,7 @@ await registry.analyzerUdf(":text/hashtags", (text) => {
|
|
|
255
510
|
return tokens;
|
|
256
511
|
});
|
|
257
512
|
|
|
258
|
-
await registry.
|
|
513
|
+
await registry.register(queryAnalyzer, (text) => (
|
|
259
514
|
String(text).trim().split(/\s+/).filter(Boolean)
|
|
260
515
|
.map((token, position) => [token, position, position])
|
|
261
516
|
));
|
|
@@ -577,6 +832,13 @@ npm install
|
|
|
577
832
|
npm test
|
|
578
833
|
```
|
|
579
834
|
|
|
835
|
+
The test suite also executes every case selected by the active release in the
|
|
836
|
+
sibling `dtlvtest` golden spec. It discovers `../dtlvtest` automatically; set
|
|
837
|
+
`DTLVTEST_ROOT=/path/to/dtlvtest` for a checkout elsewhere. The conformance
|
|
838
|
+
adapter reads `spec/manifest.edn`, preserves EDN keyword and symbol types, and
|
|
839
|
+
lowers each dataset, transaction, and query through the typed JavaScript
|
|
840
|
+
builders. The test is skipped only when no `dtlvtest` checkout is available.
|
|
841
|
+
|
|
580
842
|
`vendor-jar` builds a platform-specific runtime jar for the current build host
|
|
581
843
|
by default. To keep the cross-platform native payloads, pass:
|
|
582
844
|
|
|
Binary file
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { toEdnForm, toQueryInput } from "./convert.js";
|
|
1
|
+
import { toEdnForm, toJava, toQueryInput } from "./convert.js";
|
|
2
|
+
import { Form } from "./form.js";
|
|
2
3
|
import { _BINDINGS } from "./interop.js";
|
|
3
4
|
import { classes } from "./jvm.js";
|
|
4
5
|
import { ResourceWrapper } from "./resource.js";
|
|
@@ -16,7 +17,7 @@ async function clientIdInput(value) {
|
|
|
16
17
|
const cls = await classes();
|
|
17
18
|
return cls.uuid.fromStringSync(value);
|
|
18
19
|
}
|
|
19
|
-
return value;
|
|
20
|
+
return toJava(value);
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
export class Client extends ResourceWrapper {
|
|
@@ -164,9 +165,10 @@ export class Client extends ResourceWrapper {
|
|
|
164
165
|
}
|
|
165
166
|
|
|
166
167
|
async querySystem(query, ...args) {
|
|
168
|
+
const convertInput = query instanceof Form ? toJava : toQueryInput;
|
|
167
169
|
const normalizedInputs = [];
|
|
168
170
|
for (const value of args) {
|
|
169
|
-
normalizedInputs.push(await
|
|
171
|
+
normalizedInputs.push(await convertInput(value));
|
|
170
172
|
}
|
|
171
173
|
return toJsResult(
|
|
172
174
|
await _BINDINGS.clientInvoke("query-system", [
|
package/src/connection.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
toEdnForm,
|
|
3
|
+
toJava,
|
|
4
|
+
toJs,
|
|
5
|
+
toJsQueryResult,
|
|
6
|
+
toQueryInput
|
|
7
|
+
} from "./convert.js";
|
|
2
8
|
import { txReportToJs } from "./database.js";
|
|
9
|
+
import { Form } from "./form.js";
|
|
3
10
|
import { _BINDINGS } from "./interop.js";
|
|
4
11
|
import { callJavaMethod, javaBridgeModule } from "./jvm.js";
|
|
5
12
|
import { ResourceWrapper } from "./resource.js";
|
|
@@ -195,26 +202,33 @@ export class Connection extends ResourceWrapper {
|
|
|
195
202
|
}
|
|
196
203
|
|
|
197
204
|
async query(query, ...inputs) {
|
|
205
|
+
const convertInput = query instanceof Form ? toJava : toQueryInput;
|
|
198
206
|
const normalizedInputs = [];
|
|
199
207
|
for (const input of inputs) {
|
|
200
|
-
normalizedInputs.push(await
|
|
208
|
+
normalizedInputs.push(await convertInput(input));
|
|
201
209
|
}
|
|
202
210
|
if (typeof query === "string") {
|
|
203
|
-
return
|
|
204
|
-
await
|
|
205
|
-
|
|
211
|
+
return toJsQueryResult(
|
|
212
|
+
await _BINDINGS.connectionQuery(
|
|
213
|
+
this.rawHandle(), query, await toJava(normalizedInputs)
|
|
214
|
+
)
|
|
206
215
|
);
|
|
207
216
|
}
|
|
208
|
-
return
|
|
209
|
-
await
|
|
210
|
-
|
|
217
|
+
return toJsQueryResult(
|
|
218
|
+
await _BINDINGS.connectionQuery(
|
|
219
|
+
this.rawHandle(),
|
|
220
|
+
await queryForm(query),
|
|
221
|
+
await toJava(normalizedInputs),
|
|
222
|
+
{ form: true }
|
|
223
|
+
)
|
|
211
224
|
);
|
|
212
225
|
}
|
|
213
226
|
|
|
214
227
|
async explain(query, { inputs = [], optsEdn = null } = {}) {
|
|
228
|
+
const convertInput = query instanceof Form ? toJava : toQueryInput;
|
|
215
229
|
const normalizedInputs = [];
|
|
216
230
|
for (const input of inputs) {
|
|
217
|
-
normalizedInputs.push(await
|
|
231
|
+
normalizedInputs.push(await convertInput(input));
|
|
218
232
|
}
|
|
219
233
|
if (optsEdn !== null && optsEdn !== undefined) {
|
|
220
234
|
if (typeof query === "string") {
|
package/src/convert.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { classes } from "./jvm.js";
|
|
2
|
+
import { DatalogSymbol, EdnList, Form, Keyword, Uuid } from "./form.js";
|
|
2
3
|
|
|
3
4
|
const INT64_MIN = -(2n ** 63n);
|
|
4
5
|
const INT64_MAX = 2n ** 63n - 1n;
|
|
@@ -114,6 +115,34 @@ export async function toJava(value) {
|
|
|
114
115
|
return value;
|
|
115
116
|
}
|
|
116
117
|
|
|
118
|
+
if (value instanceof Keyword) {
|
|
119
|
+
const cls = await classes();
|
|
120
|
+
return cls.interop.keywordSync(value.toString());
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (value instanceof DatalogSymbol) {
|
|
124
|
+
const cls = await classes();
|
|
125
|
+
return cls.interop.symbolSync(value.toString());
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (value instanceof Uuid) {
|
|
129
|
+
const cls = await classes();
|
|
130
|
+
return cls.uuid.fromStringSync(value.toString());
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (value instanceof EdnList) {
|
|
134
|
+
const cls = await classes();
|
|
135
|
+
const items = new cls.arrayList();
|
|
136
|
+
for (const item of value) {
|
|
137
|
+
items.addSync(await toJava(item));
|
|
138
|
+
}
|
|
139
|
+
return cls.interop.ednListSync(items);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (value instanceof Form) {
|
|
143
|
+
return toJava(value.toForm());
|
|
144
|
+
}
|
|
145
|
+
|
|
117
146
|
if (typeof value?.rawHandle === "function") {
|
|
118
147
|
return value.rawHandle();
|
|
119
148
|
}
|
|
@@ -237,6 +266,20 @@ export async function toJs(value) {
|
|
|
237
266
|
return value;
|
|
238
267
|
}
|
|
239
268
|
|
|
269
|
+
export async function toJsQueryResult(value) {
|
|
270
|
+
if (isJavaObject(value)) {
|
|
271
|
+
const cls = await classes();
|
|
272
|
+
if (instanceOf(value, cls.setType)) {
|
|
273
|
+
const rows = [];
|
|
274
|
+
for (const item of materializeJavaCollection(value)) {
|
|
275
|
+
rows.push(await toJs(item));
|
|
276
|
+
}
|
|
277
|
+
return rows;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return toJs(value);
|
|
281
|
+
}
|
|
282
|
+
|
|
240
283
|
async function toJavaFormMap(entries, converter) {
|
|
241
284
|
const cls = await classes();
|
|
242
285
|
const jmap = new cls.linkedHashMap();
|
|
@@ -251,6 +294,22 @@ export async function toEdnForm(value) {
|
|
|
251
294
|
return value;
|
|
252
295
|
}
|
|
253
296
|
|
|
297
|
+
if (value instanceof Keyword) {
|
|
298
|
+
const cls = await classes();
|
|
299
|
+
return cls.interop.keywordSync(value.toString());
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (value instanceof DatalogSymbol) {
|
|
303
|
+
const cls = await classes();
|
|
304
|
+
return cls.interop.symbolSync(value.toString());
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (value instanceof Form) {
|
|
308
|
+
// Builder forms carry explicit Keyword/DatalogSymbol nodes. Lower through
|
|
309
|
+
// toJava so strings such as "?literal" remain ordinary string constants.
|
|
310
|
+
return toJava(value);
|
|
311
|
+
}
|
|
312
|
+
|
|
254
313
|
if (typeof value?.rawHandle === "function") {
|
|
255
314
|
return value.rawHandle();
|
|
256
315
|
}
|
|
@@ -290,6 +349,20 @@ export async function toQueryInput(value) {
|
|
|
290
349
|
return value;
|
|
291
350
|
}
|
|
292
351
|
|
|
352
|
+
if (value instanceof Keyword) {
|
|
353
|
+
const cls = await classes();
|
|
354
|
+
return cls.interop.keywordSync(value.toString());
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (value instanceof DatalogSymbol) {
|
|
358
|
+
const cls = await classes();
|
|
359
|
+
return cls.interop.symbolSync(value.toString());
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (value instanceof Form) {
|
|
363
|
+
return toJava(value);
|
|
364
|
+
}
|
|
365
|
+
|
|
293
366
|
if (typeof value?.rawHandle === "function") {
|
|
294
367
|
return value.rawHandle();
|
|
295
368
|
}
|