datalevin-node 0.10.16
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/LICENSE +277 -0
- package/README.md +205 -0
- package/jars/.gitkeep +1 -0
- package/jars/datalevin-runtime-0.10.16.jar +0 -0
- package/package.json +45 -0
- package/scripts/vendor-runtime.mjs +19 -0
- package/src/client.js +175 -0
- package/src/connection.js +147 -0
- package/src/convert.js +322 -0
- package/src/errors.js +22 -0
- package/src/index.js +19 -0
- package/src/interop.js +314 -0
- package/src/jvm.js +318 -0
- package/src/kv.js +133 -0
- package/src/raw.js +166 -0
- package/src/resource.js +43 -0
- package/src/result.js +9 -0
package/src/client.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { toEdnForm, toQueryInput } from "./convert.js";
|
|
2
|
+
import { _BINDINGS } from "./interop.js";
|
|
3
|
+
import { classes } from "./jvm.js";
|
|
4
|
+
import { ResourceWrapper } from "./resource.js";
|
|
5
|
+
import { toJsResult } from "./result.js";
|
|
6
|
+
|
|
7
|
+
async function queryForm(value) {
|
|
8
|
+
if (typeof value === "string") {
|
|
9
|
+
return _BINDINGS.readEdn(value);
|
|
10
|
+
}
|
|
11
|
+
return toEdnForm(value);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function clientIdInput(value) {
|
|
15
|
+
if (typeof value === "string") {
|
|
16
|
+
const cls = await classes();
|
|
17
|
+
return cls.uuid.fromStringSync(value);
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class Client extends ResourceWrapper {
|
|
23
|
+
constructor(handle) {
|
|
24
|
+
super(
|
|
25
|
+
handle,
|
|
26
|
+
(rawHandle) => _BINDINGS.closeClient(rawHandle),
|
|
27
|
+
(rawHandle) => _BINDINGS.clientDisconnected(rawHandle),
|
|
28
|
+
"client"
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async disconnect() {
|
|
33
|
+
await this.close();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async disconnected() {
|
|
37
|
+
return this.closed();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async clientId() {
|
|
41
|
+
return toJsResult(await _BINDINGS.clientInvoke("get-id", [this.rawHandle()]));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async openDatabase(name, dbType, { schema = null, opts = null, info = false } = {}) {
|
|
45
|
+
if (info || schema !== null || opts !== null) {
|
|
46
|
+
return toJsResult(await _BINDINGS.clientInvoke("open-database", [
|
|
47
|
+
this.rawHandle(),
|
|
48
|
+
name,
|
|
49
|
+
dbType,
|
|
50
|
+
schema === null ? null : await _BINDINGS.schema(schema),
|
|
51
|
+
opts === null ? null : await _BINDINGS.options(opts),
|
|
52
|
+
Boolean(info)
|
|
53
|
+
]));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
await _BINDINGS.clientInvoke("open-database", [this.rawHandle(), name, dbType]);
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async closeDatabase(name) {
|
|
61
|
+
await _BINDINGS.clientInvoke("close-database", [this.rawHandle(), name]);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async createDatabase(name, dbType) {
|
|
65
|
+
await _BINDINGS.clientInvoke("create-database", [
|
|
66
|
+
this.rawHandle(),
|
|
67
|
+
name,
|
|
68
|
+
await _BINDINGS.databaseType(dbType)
|
|
69
|
+
]);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async dropDatabase(name) {
|
|
73
|
+
await _BINDINGS.clientInvoke("drop-database", [this.rawHandle(), name]);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async listDatabases() {
|
|
77
|
+
return toJsResult(await _BINDINGS.clientInvoke("list-databases", [this.rawHandle()]));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async listDatabasesInUse() {
|
|
81
|
+
return toJsResult(await _BINDINGS.clientInvoke("list-databases-in-use", [this.rawHandle()]));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async createUser(username, password) {
|
|
85
|
+
await _BINDINGS.clientInvoke("create-user", [this.rawHandle(), username, password]);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async dropUser(username) {
|
|
89
|
+
await _BINDINGS.clientInvoke("drop-user", [this.rawHandle(), username]);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async resetPassword(username, password) {
|
|
93
|
+
await _BINDINGS.clientInvoke("reset-password", [this.rawHandle(), username, password]);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async listUsers() {
|
|
97
|
+
return toJsResult(await _BINDINGS.clientInvoke("list-users", [this.rawHandle()]));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async createRole(role) {
|
|
101
|
+
await _BINDINGS.clientInvoke("create-role", [this.rawHandle(), await _BINDINGS.role(role)]);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async dropRole(role) {
|
|
105
|
+
await _BINDINGS.clientInvoke("drop-role", [this.rawHandle(), await _BINDINGS.role(role)]);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async listRoles() {
|
|
109
|
+
return toJsResult(await _BINDINGS.clientInvoke("list-roles", [this.rawHandle()]));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async assignRole(role, username) {
|
|
113
|
+
await _BINDINGS.clientInvoke("assign-role", [this.rawHandle(), await _BINDINGS.role(role), username]);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async withdrawRole(role, username) {
|
|
117
|
+
await _BINDINGS.clientInvoke("withdraw-role", [this.rawHandle(), await _BINDINGS.role(role), username]);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async listUserRoles(username) {
|
|
121
|
+
return toJsResult(await _BINDINGS.clientInvoke("list-user-roles", [this.rawHandle(), username]));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async grantPermission(role, act, obj, tgt = null) {
|
|
125
|
+
await _BINDINGS.clientInvoke("grant-permission", [
|
|
126
|
+
this.rawHandle(),
|
|
127
|
+
await _BINDINGS.role(role),
|
|
128
|
+
await _BINDINGS.permissionKeyword(act),
|
|
129
|
+
await _BINDINGS.permissionKeyword(obj),
|
|
130
|
+
await _BINDINGS.permissionTarget(obj, tgt)
|
|
131
|
+
]);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async revokePermission(role, act, obj, tgt = null) {
|
|
135
|
+
await _BINDINGS.clientInvoke("revoke-permission", [
|
|
136
|
+
this.rawHandle(),
|
|
137
|
+
await _BINDINGS.role(role),
|
|
138
|
+
await _BINDINGS.permissionKeyword(act),
|
|
139
|
+
await _BINDINGS.permissionKeyword(obj),
|
|
140
|
+
await _BINDINGS.permissionTarget(obj, tgt)
|
|
141
|
+
]);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async listRolePermissions(role) {
|
|
145
|
+
return toJsResult(
|
|
146
|
+
await _BINDINGS.clientInvoke("list-role-permissions", [this.rawHandle(), await _BINDINGS.role(role)])
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async listUserPermissions(username) {
|
|
151
|
+
return toJsResult(await _BINDINGS.clientInvoke("list-user-permissions", [this.rawHandle(), username]));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async querySystem(query, ...args) {
|
|
155
|
+
const normalizedInputs = [];
|
|
156
|
+
for (const value of args) {
|
|
157
|
+
normalizedInputs.push(await toQueryInput(value));
|
|
158
|
+
}
|
|
159
|
+
return toJsResult(
|
|
160
|
+
await _BINDINGS.clientInvoke("query-system", [
|
|
161
|
+
this.rawHandle(),
|
|
162
|
+
await queryForm(query),
|
|
163
|
+
...normalizedInputs
|
|
164
|
+
])
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async showClients() {
|
|
169
|
+
return toJsResult(await _BINDINGS.clientInvoke("show-clients", [this.rawHandle()]));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async disconnectClient(clientId) {
|
|
173
|
+
await _BINDINGS.clientInvoke("disconnect-client", [this.rawHandle(), await clientIdInput(clientId)]);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { toEdnForm, toJava, toQueryInput } from "./convert.js";
|
|
2
|
+
import { _BINDINGS } from "./interop.js";
|
|
3
|
+
import { callJavaMethod } from "./jvm.js";
|
|
4
|
+
import { ResourceWrapper } from "./resource.js";
|
|
5
|
+
import { toJsResult } from "./result.js";
|
|
6
|
+
|
|
7
|
+
async function pullSelector(value) {
|
|
8
|
+
if (typeof value === "string") {
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
return toEdnForm(value);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function queryForm(query) {
|
|
15
|
+
if (typeof query !== "string") {
|
|
16
|
+
return toEdnForm(query);
|
|
17
|
+
}
|
|
18
|
+
return query;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class Connection extends ResourceWrapper {
|
|
22
|
+
constructor(handle) {
|
|
23
|
+
super(
|
|
24
|
+
handle,
|
|
25
|
+
(rawHandle) => _BINDINGS.closeConnection(rawHandle),
|
|
26
|
+
(rawHandle) => _BINDINGS.connectionClosed(rawHandle),
|
|
27
|
+
"connection"
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async schema() {
|
|
32
|
+
return toJsResult(await _BINDINGS.coreInvoke("schema", [this.rawHandle()]));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async opts() {
|
|
36
|
+
return toJsResult(await _BINDINGS.coreInvoke("opts", [this.rawHandle()]));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async updateSchema(schemaUpdate, { delAttrs = null, renameMap = null } = {}) {
|
|
40
|
+
const args = [
|
|
41
|
+
this.rawHandle(),
|
|
42
|
+
schemaUpdate === null || schemaUpdate === undefined ? null : await _BINDINGS.schema(schemaUpdate)
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
if (renameMap !== null && renameMap !== undefined) {
|
|
46
|
+
args.push(await _BINDINGS.deleteAttrs(delAttrs));
|
|
47
|
+
args.push(await _BINDINGS.renameMap(renameMap));
|
|
48
|
+
} else if (delAttrs !== null && delAttrs !== undefined) {
|
|
49
|
+
args.push(await _BINDINGS.deleteAttrs(delAttrs));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return toJsResult(await _BINDINGS.coreInvoke("update-schema", args));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async clear() {
|
|
56
|
+
await _BINDINGS.coreInvoke("clear", [this.rawHandle()]);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async entid(eid) {
|
|
60
|
+
return toJsResult(await callJavaMethod(this.rawHandle(), "entid", await toJava(eid)));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async entity(eid) {
|
|
64
|
+
return toJsResult(await callJavaMethod(this.rawHandle(), "entityMap", await toJava(eid)));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async pull(selector, eid) {
|
|
68
|
+
return toJsResult(
|
|
69
|
+
await callJavaMethod(this.rawHandle(), "pull", await pullSelector(selector), await toJava(eid)),
|
|
70
|
+
{ bridge: true }
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async pullMany(selector, eids) {
|
|
75
|
+
return toJsResult(
|
|
76
|
+
await callJavaMethod(this.rawHandle(), "pullMany", await pullSelector(selector), await toJava(eids)),
|
|
77
|
+
{ bridge: true }
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async query(query, ...inputs) {
|
|
82
|
+
const normalizedInputs = [];
|
|
83
|
+
for (const input of inputs) {
|
|
84
|
+
normalizedInputs.push(await toQueryInput(input));
|
|
85
|
+
}
|
|
86
|
+
if (typeof query === "string") {
|
|
87
|
+
return toJsResult(
|
|
88
|
+
await callJavaMethod(this.rawHandle(), "query", query, await toJava(normalizedInputs)),
|
|
89
|
+
{ bridge: true }
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
return toJsResult(
|
|
93
|
+
await callJavaMethod(this.rawHandle(), "queryForm", await queryForm(query), await toJava(normalizedInputs)),
|
|
94
|
+
{ bridge: true }
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async explain(query, { inputs = [], optsEdn = null } = {}) {
|
|
99
|
+
const normalizedInputs = [];
|
|
100
|
+
for (const input of inputs) {
|
|
101
|
+
normalizedInputs.push(await toQueryInput(input));
|
|
102
|
+
}
|
|
103
|
+
if (optsEdn !== null && optsEdn !== undefined) {
|
|
104
|
+
if (typeof query === "string") {
|
|
105
|
+
return toJsResult(
|
|
106
|
+
await callJavaMethod(
|
|
107
|
+
this.rawHandle(),
|
|
108
|
+
"explain",
|
|
109
|
+
optsEdn,
|
|
110
|
+
query,
|
|
111
|
+
await toJava(normalizedInputs)
|
|
112
|
+
),
|
|
113
|
+
{ bridge: true }
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
return toJsResult(
|
|
117
|
+
await callJavaMethod(
|
|
118
|
+
this.rawHandle(),
|
|
119
|
+
"explainForm",
|
|
120
|
+
optsEdn,
|
|
121
|
+
await queryForm(query),
|
|
122
|
+
await toJava(normalizedInputs)
|
|
123
|
+
),
|
|
124
|
+
{ bridge: true }
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (typeof query === "string") {
|
|
129
|
+
return toJsResult(
|
|
130
|
+
await callJavaMethod(this.rawHandle(), "explain", query, await toJava(normalizedInputs)),
|
|
131
|
+
{ bridge: true }
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
return toJsResult(
|
|
135
|
+
await callJavaMethod(this.rawHandle(), "explainForm", await queryForm(query), await toJava(normalizedInputs)),
|
|
136
|
+
{ bridge: true }
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async transact(txData, txMeta = null) {
|
|
141
|
+
const args = [this.rawHandle(), await _BINDINGS.txData(txData)];
|
|
142
|
+
if (txMeta !== null && txMeta !== undefined) {
|
|
143
|
+
args.push(await toJava(txMeta));
|
|
144
|
+
}
|
|
145
|
+
return toJsResult(await _BINDINGS.coreInvoke("transact!", args));
|
|
146
|
+
}
|
|
147
|
+
}
|
package/src/convert.js
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { classes } from "./jvm.js";
|
|
2
|
+
|
|
3
|
+
const INT64_MIN = -(2n ** 63n);
|
|
4
|
+
const INT64_MAX = 2n ** 63n - 1n;
|
|
5
|
+
const FORM_SYMBOL_STRINGS = new Set(["*", "...", ".", "$", "%", "_"]);
|
|
6
|
+
|
|
7
|
+
function isPlainObject(value) {
|
|
8
|
+
if (value === null || typeof value !== "object") {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
const prototype = Object.getPrototypeOf(value);
|
|
12
|
+
return prototype === Object.prototype || prototype === null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function toSignedByteValues(value) {
|
|
16
|
+
return Array.from(new Int8Array(value.buffer, value.byteOffset, value.byteLength));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function toBufferView(value) {
|
|
20
|
+
if (Buffer.isBuffer(value)) {
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function materializeJavaCollection(collection) {
|
|
27
|
+
if (typeof collection?.toArraySync === "function") {
|
|
28
|
+
try {
|
|
29
|
+
const items = collection.toArraySync();
|
|
30
|
+
return Array.isArray(items) ? items : Array.from(items);
|
|
31
|
+
} catch {
|
|
32
|
+
// Fall back to iterator-based traversal when array materialization is unsupported.
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const items = [];
|
|
37
|
+
const iterator = collection.iteratorSync();
|
|
38
|
+
while (iterator.hasNextSync()) {
|
|
39
|
+
items.push(iterator.nextSync());
|
|
40
|
+
}
|
|
41
|
+
return items;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function instanceOf(value, type) {
|
|
45
|
+
if (value === null || typeof value !== "object" || typeof value.instanceOf !== "function") {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
return value.instanceOf(type);
|
|
51
|
+
} catch {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function toJavaMap(entries, converter) {
|
|
57
|
+
const cls = await classes();
|
|
58
|
+
const jmap = new cls.linkedHashMap();
|
|
59
|
+
for (const [key, item] of entries) {
|
|
60
|
+
jmap.putSync(await converter(key), await converter(item));
|
|
61
|
+
}
|
|
62
|
+
return jmap;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function toJavaSet(items, converter) {
|
|
66
|
+
const cls = await classes();
|
|
67
|
+
const jset = new cls.linkedHashSet();
|
|
68
|
+
for (const item of items) {
|
|
69
|
+
jset.addSync(await converter(item));
|
|
70
|
+
}
|
|
71
|
+
return jset;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function toJavaList(items, converter) {
|
|
75
|
+
const cls = await classes();
|
|
76
|
+
const jlist = new cls.arrayList();
|
|
77
|
+
for (const item of items) {
|
|
78
|
+
jlist.addSync(await converter(item));
|
|
79
|
+
}
|
|
80
|
+
return jlist;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function isJavaObject(value) {
|
|
84
|
+
return value !== null
|
|
85
|
+
&& (typeof value === "object" || typeof value === "function")
|
|
86
|
+
&& (typeof value.getClassSync === "function" || typeof value.instanceOf === "function");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function toJava(value) {
|
|
90
|
+
if (value === null || typeof value === "boolean" || typeof value === "string") {
|
|
91
|
+
return value;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (typeof value === "number") {
|
|
95
|
+
if (!Number.isInteger(value)) {
|
|
96
|
+
return value;
|
|
97
|
+
}
|
|
98
|
+
if (!Number.isSafeInteger(value)) {
|
|
99
|
+
throw new RangeError("Unsafe integer number; use bigint for Datalevin integer values.");
|
|
100
|
+
}
|
|
101
|
+
const cls = await classes();
|
|
102
|
+
return new cls.longClass(String(value));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (typeof value === "bigint") {
|
|
106
|
+
const cls = await classes();
|
|
107
|
+
if (value >= INT64_MIN && value <= INT64_MAX) {
|
|
108
|
+
return new cls.longClass(value.toString());
|
|
109
|
+
}
|
|
110
|
+
return new cls.bigInteger(value.toString());
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (isJavaObject(value)) {
|
|
114
|
+
return value;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (typeof value?.rawHandle === "function") {
|
|
118
|
+
return value.rawHandle();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (value instanceof Date) {
|
|
122
|
+
const cls = await classes();
|
|
123
|
+
return cls.instant.parseSync(value.toISOString());
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
|
|
127
|
+
return toSignedByteValues(value);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (value instanceof Map) {
|
|
131
|
+
return toJavaMap(value.entries(), toJava);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (value instanceof Set) {
|
|
135
|
+
return toJavaSet(value, toJava);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (Array.isArray(value)) {
|
|
139
|
+
return toJavaList(value, toJava);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (isPlainObject(value)) {
|
|
143
|
+
return toJavaMap(Object.entries(value), toJava);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return value;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export async function toJs(value) {
|
|
150
|
+
if (
|
|
151
|
+
value === null
|
|
152
|
+
|| typeof value === "boolean"
|
|
153
|
+
|| typeof value === "number"
|
|
154
|
+
|| typeof value === "string"
|
|
155
|
+
|| typeof value === "bigint"
|
|
156
|
+
) {
|
|
157
|
+
return value;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
|
|
161
|
+
return toBufferView(value);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (Array.isArray(value)) {
|
|
165
|
+
return Promise.all(value.map((item) => toJs(item)));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (!isJavaObject(value)) {
|
|
169
|
+
if (isPlainObject(value)) {
|
|
170
|
+
const result = {};
|
|
171
|
+
for (const [key, item] of Object.entries(value)) {
|
|
172
|
+
result[key] = await toJs(item);
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
return value;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const cls = await classes();
|
|
180
|
+
|
|
181
|
+
if (instanceOf(value, cls.uuid)) {
|
|
182
|
+
return value.toStringSync();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (instanceOf(value, cls.instant)) {
|
|
186
|
+
return new Date(value.toStringSync());
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (instanceOf(value, cls.date)) {
|
|
190
|
+
return new Date(Number(value.getTimeSync()));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (instanceOf(value, cls.bigInteger)) {
|
|
194
|
+
return BigInt(value.toStringSync());
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (instanceOf(value, cls.bigDecimal)) {
|
|
198
|
+
return value.toStringSync();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (instanceOf(value, cls.keywordType) || instanceOf(value, cls.symbolType)) {
|
|
202
|
+
return value.toStringSync();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (instanceOf(value, cls.mapType) || typeof value.entrySetSync === "function") {
|
|
206
|
+
const entries = [];
|
|
207
|
+
for (const entry of materializeJavaCollection(value.entrySetSync())) {
|
|
208
|
+
entries.push([await toJs(entry.getKeySync()), await toJs(entry.getValueSync())]);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (entries.every(([key]) => typeof key === "string")) {
|
|
212
|
+
return Object.fromEntries(entries);
|
|
213
|
+
}
|
|
214
|
+
return new Map(entries);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (instanceOf(value, cls.setType)) {
|
|
218
|
+
const items = [];
|
|
219
|
+
for (const item of materializeJavaCollection(value)) {
|
|
220
|
+
items.push(await toJs(item));
|
|
221
|
+
}
|
|
222
|
+
return new Set(items);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (
|
|
226
|
+
instanceOf(value, cls.listType)
|
|
227
|
+
|| instanceOf(value, cls.collectionType)
|
|
228
|
+
|| typeof value.iteratorSync === "function"
|
|
229
|
+
) {
|
|
230
|
+
const items = [];
|
|
231
|
+
for (const item of materializeJavaCollection(value)) {
|
|
232
|
+
items.push(await toJs(item));
|
|
233
|
+
}
|
|
234
|
+
return items;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return value;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async function toJavaFormMap(entries, converter) {
|
|
241
|
+
const cls = await classes();
|
|
242
|
+
const jmap = new cls.linkedHashMap();
|
|
243
|
+
for (const [key, item] of entries) {
|
|
244
|
+
jmap.putSync(await converter(key), await converter(item));
|
|
245
|
+
}
|
|
246
|
+
return jmap;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export async function toEdnForm(value) {
|
|
250
|
+
if (value === null || isJavaObject(value)) {
|
|
251
|
+
return value;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (typeof value?.rawHandle === "function") {
|
|
255
|
+
return value.rawHandle();
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (typeof value === "string") {
|
|
259
|
+
const cls = await classes();
|
|
260
|
+
if (value.startsWith(":")) {
|
|
261
|
+
return cls.interop.keywordSync(value);
|
|
262
|
+
}
|
|
263
|
+
if (value.startsWith("?") || FORM_SYMBOL_STRINGS.has(value)) {
|
|
264
|
+
return cls.interop.symbolSync(value);
|
|
265
|
+
}
|
|
266
|
+
return value;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (value instanceof Map) {
|
|
270
|
+
return toJavaFormMap(value.entries(), toEdnForm);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (value instanceof Set) {
|
|
274
|
+
return toJavaSet(value, toEdnForm);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (Array.isArray(value)) {
|
|
278
|
+
return toJavaList(value, toEdnForm);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (isPlainObject(value)) {
|
|
282
|
+
return toJavaFormMap(Object.entries(value), toEdnForm);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return toJava(value);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export async function toQueryInput(value) {
|
|
289
|
+
if (value === null || isJavaObject(value)) {
|
|
290
|
+
return value;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (typeof value?.rawHandle === "function") {
|
|
294
|
+
return value.rawHandle();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (typeof value === "string") {
|
|
298
|
+
const cls = await classes();
|
|
299
|
+
if (value.startsWith(":")) {
|
|
300
|
+
return cls.interop.keywordSync(value);
|
|
301
|
+
}
|
|
302
|
+
return value;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (value instanceof Map) {
|
|
306
|
+
return toJavaFormMap(value.entries(), toQueryInput);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (value instanceof Set) {
|
|
310
|
+
return toJavaSet(value, toQueryInput);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (Array.isArray(value)) {
|
|
314
|
+
return toJavaList(value, toQueryInput);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (isPlainObject(value)) {
|
|
318
|
+
return toJavaFormMap(Object.entries(value), toQueryInput);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return toJava(value);
|
|
322
|
+
}
|
package/src/errors.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export class DatalevinError extends Error {
|
|
2
|
+
constructor(message, { typeName = null, data = null, cause = null } = {}) {
|
|
3
|
+
super(message, cause === null ? undefined : { cause });
|
|
4
|
+
this.name = new.target.name;
|
|
5
|
+
this.typeName = typeName;
|
|
6
|
+
this.data = data;
|
|
7
|
+
if (cause !== null && this.cause === undefined) {
|
|
8
|
+
this.cause = cause;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class DatalevinConfigurationError extends DatalevinError {}
|
|
14
|
+
|
|
15
|
+
export class DatalevinJvmError extends DatalevinError {}
|
|
16
|
+
|
|
17
|
+
export class DatalevinJavaError extends DatalevinError {
|
|
18
|
+
constructor(message, { javaClass = null, typeName = null, data = null, cause = null } = {}) {
|
|
19
|
+
super(message, { typeName, data, cause });
|
|
20
|
+
this.javaClass = javaClass;
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export { Client } from "./client.js";
|
|
2
|
+
export { Connection } from "./connection.js";
|
|
3
|
+
export {
|
|
4
|
+
DatalevinConfigurationError,
|
|
5
|
+
DatalevinError,
|
|
6
|
+
DatalevinJavaError,
|
|
7
|
+
DatalevinJvmError
|
|
8
|
+
} from "./errors.js";
|
|
9
|
+
export {
|
|
10
|
+
apiInfo,
|
|
11
|
+
connect,
|
|
12
|
+
execJson,
|
|
13
|
+
jvmStarted,
|
|
14
|
+
newClient,
|
|
15
|
+
openKv,
|
|
16
|
+
startJvm
|
|
17
|
+
} from "./interop.js";
|
|
18
|
+
export { interop } from "./raw.js";
|
|
19
|
+
export { KV } from "./kv.js";
|