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/kv.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { toEdnForm, toJava } 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
|
+
function slicePage(items, limit = null, offset = null) {
|
|
8
|
+
const start = offset === null || offset === undefined ? 0 : Math.max(offset, 0);
|
|
9
|
+
if (limit === null || limit === undefined) {
|
|
10
|
+
return items.slice(start);
|
|
11
|
+
}
|
|
12
|
+
return items.slice(start, start + Math.max(limit, 0));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class KV extends ResourceWrapper {
|
|
16
|
+
constructor(handle) {
|
|
17
|
+
super(
|
|
18
|
+
handle,
|
|
19
|
+
(rawHandle) => _BINDINGS.closeKeyValue(rawHandle),
|
|
20
|
+
(rawHandle) => _BINDINGS.keyValueClosed(rawHandle),
|
|
21
|
+
"kv"
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async dir() {
|
|
26
|
+
return toJsResult(await _BINDINGS.coreInvoke("dir", [this.rawHandle()]));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async openDbi(name, opts = null) {
|
|
30
|
+
const args = [this.rawHandle(), name];
|
|
31
|
+
if (opts !== null && opts !== undefined) {
|
|
32
|
+
args.push(await _BINDINGS.options(opts));
|
|
33
|
+
}
|
|
34
|
+
await _BINDINGS.coreInvoke("open-dbi", args);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async openListDbi(name, opts = null) {
|
|
38
|
+
const args = [this.rawHandle(), name];
|
|
39
|
+
if (opts !== null && opts !== undefined) {
|
|
40
|
+
args.push(await _BINDINGS.options(opts));
|
|
41
|
+
}
|
|
42
|
+
await _BINDINGS.coreInvoke("open-list-dbi", args);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async listDbis() {
|
|
46
|
+
return toJsResult(await _BINDINGS.coreInvoke("list-dbis", [this.rawHandle()]));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async entries(dbiName) {
|
|
50
|
+
return toJsResult(await _BINDINGS.coreInvoke("entries", [this.rawHandle(), dbiName]));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async transact(txs, { dbiName = null, kType = null, vType = null } = {}) {
|
|
54
|
+
if (dbiName === null && (kType !== null || vType !== null)) {
|
|
55
|
+
throw new TypeError("kType and vType require dbiName for KV transact().");
|
|
56
|
+
}
|
|
57
|
+
if (vType !== null && kType === null) {
|
|
58
|
+
throw new TypeError("vType requires kType for KV transact().");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const args = [this.rawHandle()];
|
|
62
|
+
const normalizedTxs = kType === null ? await _BINDINGS.kvTxs(txs) : await _BINDINGS.kvTxsWithTypes(txs, kType, vType);
|
|
63
|
+
if (dbiName === null) {
|
|
64
|
+
args.push(normalizedTxs);
|
|
65
|
+
} else {
|
|
66
|
+
args.push(dbiName);
|
|
67
|
+
args.push(normalizedTxs);
|
|
68
|
+
if (kType !== null) {
|
|
69
|
+
args.push(await _BINDINGS.kvType(kType));
|
|
70
|
+
if (vType !== null) {
|
|
71
|
+
args.push(await _BINDINGS.kvType(vType));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return toJsResult(await _BINDINGS.coreInvoke("transact-kv", args));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async getValue(dbiName, key, { kType = null, vType = null, ignoreKey = false } = {}) {
|
|
79
|
+
if ((kType === null) !== (vType === null)) {
|
|
80
|
+
throw new TypeError("kType and vType must be provided together for KV getValue().");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const args = [this.rawHandle(), dbiName];
|
|
84
|
+
if (kType === null) {
|
|
85
|
+
args.push(await toJava(key));
|
|
86
|
+
return toJsResult(await _BINDINGS.coreInvoke("get-value", args));
|
|
87
|
+
}
|
|
88
|
+
return toJsResult(
|
|
89
|
+
await callJavaMethod(
|
|
90
|
+
this.rawHandle(),
|
|
91
|
+
"getValue",
|
|
92
|
+
dbiName,
|
|
93
|
+
await toJava(key),
|
|
94
|
+
kType,
|
|
95
|
+
vType,
|
|
96
|
+
Boolean(ignoreKey)
|
|
97
|
+
)
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async getRange(
|
|
102
|
+
dbiName,
|
|
103
|
+
keyRange,
|
|
104
|
+
{ kType = null, vType = null, limit = null, offset = null } = {}
|
|
105
|
+
) {
|
|
106
|
+
if (keyRange === null || keyRange === undefined) {
|
|
107
|
+
throw new TypeError("keyRange is required for KV getRange().");
|
|
108
|
+
}
|
|
109
|
+
if (vType !== null && kType === null) {
|
|
110
|
+
throw new TypeError("vType requires kType for KV getRange().");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const args = [this.rawHandle(), dbiName];
|
|
114
|
+
if (kType === null) {
|
|
115
|
+
args.push(await toEdnForm(keyRange));
|
|
116
|
+
} else {
|
|
117
|
+
args.push(await _BINDINGS.kvRange(keyRange, kType));
|
|
118
|
+
args.push(await _BINDINGS.kvType(kType));
|
|
119
|
+
if (vType !== null) {
|
|
120
|
+
args.push(await _BINDINGS.kvType(vType));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return slicePage(await toJsResult(await _BINDINGS.coreInvoke("get-range", args)), limit, offset);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async clearDbi(dbiName) {
|
|
127
|
+
await _BINDINGS.coreInvoke("clear-dbi", [this.rawHandle(), dbiName]);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async dropDbi(dbiName) {
|
|
131
|
+
await _BINDINGS.coreInvoke("drop-dbi", [this.rawHandle(), dbiName]);
|
|
132
|
+
}
|
|
133
|
+
}
|
package/src/raw.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { DatalevinError } from "./errors.js";
|
|
2
|
+
import { _BINDINGS } from "./interop.js";
|
|
3
|
+
|
|
4
|
+
function resourceHandle(value) {
|
|
5
|
+
if (typeof value?.rawHandle === "function") {
|
|
6
|
+
return value.rawHandle();
|
|
7
|
+
}
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class RawInterop {
|
|
12
|
+
async apiInfo() {
|
|
13
|
+
return _BINDINGS.apiInfoRaw();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async execJson(op, args = null) {
|
|
17
|
+
return _BINDINGS.execJson(op, args);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async coreInvoke(functionName, args = null) {
|
|
21
|
+
return _BINDINGS.coreInvoke(functionName, args);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async clientInvoke(functionName, args = null) {
|
|
25
|
+
return _BINDINGS.clientInvoke(functionName, args);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async createConnection(dir = null, schema = null, opts = null, { shared = false } = {}) {
|
|
29
|
+
return _BINDINGS.createConnection(dir, schema, opts, { shared });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async closeConnection(handle) {
|
|
33
|
+
await _BINDINGS.closeConnection(resourceHandle(handle));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async connectionClosed(handle) {
|
|
37
|
+
return _BINDINGS.connectionClosed(resourceHandle(handle));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async connectionDb(handle) {
|
|
41
|
+
throw new DatalevinError(
|
|
42
|
+
"Raw database values are not exposed by the Node binding. Use Connection query/pull/entity methods instead."
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async openKeyValue(dir, opts = null) {
|
|
47
|
+
return _BINDINGS.openKeyValue(dir, opts);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async closeKeyValue(handle) {
|
|
51
|
+
await _BINDINGS.closeKeyValue(resourceHandle(handle));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async keyValueClosed(handle) {
|
|
55
|
+
return _BINDINGS.keyValueClosed(resourceHandle(handle));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async newClient(uri, opts = null) {
|
|
59
|
+
return _BINDINGS.newClient(uri, opts);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async closeClient(handle) {
|
|
63
|
+
await _BINDINGS.closeClient(resourceHandle(handle));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async clientDisconnected(handle) {
|
|
67
|
+
return _BINDINGS.clientDisconnected(resourceHandle(handle));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async readEdn(edn) {
|
|
71
|
+
return _BINDINGS.readEdn(edn);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async keyword(value) {
|
|
75
|
+
return _BINDINGS.keyword(value);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async symbol(value) {
|
|
79
|
+
return _BINDINGS.symbol(value);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async schema(schema) {
|
|
83
|
+
return _BINDINGS.schema(schema);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async options(opts) {
|
|
87
|
+
return _BINDINGS.options(opts);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async udfDescriptor(descriptor) {
|
|
91
|
+
return _BINDINGS.udfDescriptor(descriptor);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async createUdfRegistry() {
|
|
95
|
+
return _BINDINGS.createUdfRegistry();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async registerUdf(registry, descriptor, fn) {
|
|
99
|
+
return _BINDINGS.registerUdf(registry, descriptor, fn);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async unregisterUdf(registry, descriptor) {
|
|
103
|
+
return _BINDINGS.unregisterUdf(registry, descriptor);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async registeredUdf(registry, descriptor) {
|
|
107
|
+
return _BINDINGS.registeredUdf(registry, descriptor);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async renameMap(renameMap) {
|
|
111
|
+
return _BINDINGS.renameMap(renameMap);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async deleteAttrs(attrs) {
|
|
115
|
+
return _BINDINGS.deleteAttrs(attrs);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async lookupRef(value) {
|
|
119
|
+
return _BINDINGS.lookupRef(value);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async txData(txData) {
|
|
123
|
+
return _BINDINGS.txData(txData);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async kvTxs(txs, kType = null, vType = null) {
|
|
127
|
+
if (kType === null && vType === null) {
|
|
128
|
+
return _BINDINGS.kvTxs(txs);
|
|
129
|
+
}
|
|
130
|
+
return _BINDINGS.kvTxsWithTypes(txs, kType, vType);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async kvInput(value, type) {
|
|
134
|
+
return _BINDINGS.kvInput(value, type);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async kvRange(range, type) {
|
|
138
|
+
return _BINDINGS.kvRange(range, type);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async kvType(value) {
|
|
142
|
+
return _BINDINGS.kvType(value);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async databaseType(value) {
|
|
146
|
+
return _BINDINGS.databaseType(value);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async role(role) {
|
|
150
|
+
return _BINDINGS.role(role);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async permissionKeyword(value) {
|
|
154
|
+
return _BINDINGS.permissionKeyword(value);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async permissionTarget(objectType, target) {
|
|
158
|
+
return _BINDINGS.permissionTarget(objectType, target);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const RAW_INTEROP = new RawInterop();
|
|
163
|
+
|
|
164
|
+
export function interop() {
|
|
165
|
+
return RAW_INTEROP;
|
|
166
|
+
}
|
package/src/resource.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import util from "node:util";
|
|
2
|
+
|
|
3
|
+
export class ResourceWrapper {
|
|
4
|
+
constructor(handle, closeFn, closedFn, label) {
|
|
5
|
+
this._handle = handle;
|
|
6
|
+
this._closeFn = closeFn;
|
|
7
|
+
this._closedFn = closedFn;
|
|
8
|
+
this._label = label;
|
|
9
|
+
this._closed = false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
rawHandle() {
|
|
13
|
+
return this._handle;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async close() {
|
|
17
|
+
if (this._closed) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
await this._closeFn(this._handle);
|
|
21
|
+
this._closed = true;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async closed() {
|
|
25
|
+
if (this._closed) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
this._closed = Boolean(await this._closedFn(this._handle));
|
|
29
|
+
return this._closed;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
toString() {
|
|
33
|
+
return `<${this.constructor.name} ${this._closed ? "closed" : "open"}>`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
[util.inspect.custom]() {
|
|
37
|
+
return this.toString();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async [Symbol.asyncDispose]() {
|
|
41
|
+
await this.close();
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/result.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { isJavaObject, toJs } from "./convert.js";
|
|
2
|
+
import { _BINDINGS } from "./interop.js";
|
|
3
|
+
|
|
4
|
+
export async function toJsResult(value, { bridge = false } = {}) {
|
|
5
|
+
if (bridge && isJavaObject(value)) {
|
|
6
|
+
return toJs(await _BINDINGS.bridgeResult(value));
|
|
7
|
+
}
|
|
8
|
+
return toJs(value);
|
|
9
|
+
}
|