@yydb/yydb 0.0.4 → 0.0.5
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 +74 -22
- package/dist/{cli → node/cli}/cli.js +1 -1
- package/dist/{database.d.ts → node/database.d.ts} +2 -2
- package/dist/{database.js → node/database.js} +1 -9
- package/dist/{index.d.ts → node/index.d.ts} +2 -2
- package/dist/{index.js → node/index.js} +2 -2
- package/dist/{micro.d.ts → shared/micro.d.ts} +2 -0
- package/dist/{micro.js → shared/micro.js} +9 -0
- package/dist/shared/wasm-envelope.d.ts +37 -0
- package/dist/shared/wasm-envelope.js +46 -0
- package/dist/wasm/binding.d.ts +1 -0
- package/dist/wasm/binding.js +1 -0
- package/dist/wasm/database.d.ts +53 -0
- package/dist/wasm/database.js +116 -0
- package/dist/wasm/index.d.ts +5 -0
- package/dist/wasm/index.js +4 -0
- package/package.json +26 -16
- /package/dist/{cli → node/cli}/cli.d.ts +0 -0
- /package/dist/{cli → node/cli}/info-cmd.d.ts +0 -0
- /package/dist/{cli → node/cli}/info-cmd.js +0 -0
- /package/dist/{cli → node/cli}/init-cmd.d.ts +0 -0
- /package/dist/{cli → node/cli}/init-cmd.js +0 -0
- /package/dist/{cli → node/cli}/options.d.ts +0 -0
- /package/dist/{cli → node/cli}/options.js +0 -0
- /package/dist/{cli → node/cli}/serve-cmd.d.ts +0 -0
- /package/dist/{cli → node/cli}/serve-cmd.js +0 -0
- /package/dist/{cli → node/cli}/version-cmd.d.ts +0 -0
- /package/dist/{cli → node/cli}/version-cmd.js +0 -0
- /package/dist/{cli.d.ts → node/cli.d.ts} +0 -0
- /package/dist/{cli.js → node/cli.js} +0 -0
- /package/dist/{native.d.ts → node/native.d.ts} +0 -0
- /package/dist/{native.js → node/native.js} +0 -0
- /package/dist/{resolve-bin.d.ts → node/resolve-bin.d.ts} +0 -0
- /package/dist/{resolve-bin.js → node/resolve-bin.js} +0 -0
- /package/dist/{udf-types.d.ts → shared/udf-types.d.ts} +0 -0
- /package/dist/{udf-types.js → shared/udf-types.js} +0 -0
package/README.md
CHANGED
|
@@ -1,29 +1,36 @@
|
|
|
1
1
|
# `@yydb/yydb`
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
Node all-in-one and browser wasm core from one package. On Node.js, `Database.open` starts a private loopback `yydb serve`
|
|
4
|
+
process and talks over the YY wire protocol. In browsers, `@yydb/yydb/wasm` loads an in-process wasm session with KV,
|
|
5
|
+
schema, and VOS `query` / `execute` on memory or OPFS paths.
|
|
6
|
+
|
|
7
|
+
For a wire client only (renderer or an app that already runs `yydb serve`), use
|
|
8
|
+
[`@yydb/yydb-client`](https://www.npmjs.com/package/@yydb/yydb-client).
|
|
6
9
|
|
|
7
10
|
[](https://www.npmjs.com/package/@yydb/yydb)
|
|
8
11
|
[](https://www.npmjs.com/package/@yydb/yydb)
|
|
9
12
|
|
|
10
13
|
## Exports
|
|
11
14
|
|
|
12
|
-
| Import
|
|
13
|
-
|
|
14
|
-
| `@yydb/yydb`
|
|
15
|
-
| `@yydb/yydb
|
|
15
|
+
| Import | Runtime | Role |
|
|
16
|
+
|--------|---------|------|
|
|
17
|
+
| `@yydb/yydb` | Node (`node` export condition) | `Database.open`, micro helpers |
|
|
18
|
+
| `@yydb/yydb` | Browser / bundler (`default`) | wasm `Database` |
|
|
19
|
+
| `@yydb/yydb/node` | Node (explicit) | Same as Node default |
|
|
20
|
+
| `@yydb/yydb/wasm` | Browser (explicit) | wasm `Database`, `initWasm`, `checkSchema` |
|
|
21
|
+
| `@yydb/yydb/cli` | Node | CLI (`version`, `init`, `info`, `serve`) |
|
|
16
22
|
|
|
17
|
-
Node.js 18+.
|
|
23
|
+
Node.js 18+. Platform `.node` bindings install via optional dependencies.
|
|
18
24
|
|
|
19
|
-
Schema truth lives in the `.yydb` file via `ensureSchema` and VOS documents.
|
|
25
|
+
Schema truth lives in the `.yydb` file via `ensureSchema` and VOS documents. Wasm sessions mirror KV and schema calls
|
|
26
|
+
and add VOS pipelines directly on the embedded core.
|
|
20
27
|
|
|
21
|
-
##
|
|
28
|
+
## Node example
|
|
22
29
|
|
|
23
30
|
```ts
|
|
24
|
-
import {Database} from
|
|
31
|
+
import { Database } from '@yydb/yydb';
|
|
25
32
|
|
|
26
|
-
const db = await Database.open(
|
|
33
|
+
const db = await Database.open('./data/app.yydb');
|
|
27
34
|
|
|
28
35
|
await db.ensureSchema(
|
|
29
36
|
1,
|
|
@@ -34,14 +41,56 @@ await db.ensureSchema(
|
|
|
34
41
|
}`,
|
|
35
42
|
);
|
|
36
43
|
|
|
37
|
-
await db.put(
|
|
38
|
-
console.log(await db.get(
|
|
44
|
+
await db.put('theme', 'dark');
|
|
45
|
+
console.log(await db.get('theme'));
|
|
39
46
|
await db.close();
|
|
40
47
|
```
|
|
41
48
|
|
|
42
49
|
The file is created when missing. Schema version and VOS document persist with the data.
|
|
43
50
|
|
|
44
|
-
##
|
|
51
|
+
## Browser example
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { Database } from '@yydb/yydb/wasm';
|
|
55
|
+
|
|
56
|
+
const db = await Database.openInMemory();
|
|
57
|
+
|
|
58
|
+
await db.ensureSchema(
|
|
59
|
+
1,
|
|
60
|
+
`table Setting {
|
|
61
|
+
@@id: uuid,
|
|
62
|
+
key: utf8,
|
|
63
|
+
value: utf8,
|
|
64
|
+
}`,
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
await db.put('theme', 'dark');
|
|
68
|
+
console.log(await db.get('theme'));
|
|
69
|
+
db.close();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Use `Database.open('app.yydb')` for OPFS-backed paths in supported browsers.
|
|
73
|
+
|
|
74
|
+
## Wasm API
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const db = await Database.openInMemory();
|
|
78
|
+
// or: const db = await Database.open('app.yydb');
|
|
79
|
+
|
|
80
|
+
await db.ensureSchema(version, vosDocument);
|
|
81
|
+
await db.getSchema();
|
|
82
|
+
await db.put(key, stringOrBytes);
|
|
83
|
+
await db.get(key);
|
|
84
|
+
await db.info();
|
|
85
|
+
await db.serverVersion();
|
|
86
|
+
await db.registerMicro(microDefinition);
|
|
87
|
+
await db.callScalar('double', 1, [21]);
|
|
88
|
+
db.query('User.filter(x => x.active).collect()');
|
|
89
|
+
db.execute('User { name: "Ada" }.insert()');
|
|
90
|
+
db.close();
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Node API
|
|
45
94
|
|
|
46
95
|
```ts
|
|
47
96
|
const db = await Database.open(path, options?);
|
|
@@ -52,6 +101,8 @@ await db.put(key, stringOrBytes);
|
|
|
52
101
|
await db.get(key);
|
|
53
102
|
await db.info();
|
|
54
103
|
await db.serverVersion();
|
|
104
|
+
await db.registerMicro(microDefinition);
|
|
105
|
+
await db.callScalar('double', 1, [21]);
|
|
55
106
|
await db.close();
|
|
56
107
|
```
|
|
57
108
|
|
|
@@ -60,14 +111,15 @@ arguments). `binary` is a deprecated alias for `cli`.
|
|
|
60
111
|
|
|
61
112
|
## Related packages
|
|
62
113
|
|
|
63
|
-
| Need
|
|
64
|
-
|
|
65
|
-
| Node all-in-one (this package) | **`@yydb/yydb`**
|
|
66
|
-
|
|
|
67
|
-
|
|
|
68
|
-
|
|
|
114
|
+
| Need | Package |
|
|
115
|
+
|------|---------|
|
|
116
|
+
| Node all-in-one (this package) | **`@yydb/yydb`** or `@yydb/yydb/node` |
|
|
117
|
+
| Browser wasm entry | **`@yydb/yydb/wasm`** |
|
|
118
|
+
| Wire client only | [`@yydb/yydb-client`](https://www.npmjs.com/package/@yydb/yydb-client) |
|
|
119
|
+
| In-process Rust embed | [`yydb`](https://crates.io/crates/yydb) |
|
|
120
|
+
| Agent skills (wire protocol) | [`@yydb/yydb-skills`](https://www.npmjs.com/package/@yydb/yydb-skills) |
|
|
69
121
|
|
|
70
|
-
Electron: main process can use `@yydb/yydb`; renderer code can use `@yydb/yydb-client` against the main-process
|
|
122
|
+
Electron: main process can use `@yydb/yydb/node`; renderer code can use `@yydb/yydb-client` against the main-process
|
|
71
123
|
endpoint.
|
|
72
124
|
|
|
73
125
|
The managed engine listens on `127.0.0.1` with no authentication — keep the endpoint private.
|
|
@@ -6,7 +6,7 @@ import { registerInitCommand } from './init-cmd.js';
|
|
|
6
6
|
import { registerServeCommand } from './serve-cmd.js';
|
|
7
7
|
import { registerVersionCommand } from './version-cmd.js';
|
|
8
8
|
function buildYydbCli() {
|
|
9
|
-
const localesRoot = join(dirname(fileURLToPath(import.meta.url)), '
|
|
9
|
+
const localesRoot = join(dirname(fileURLToPath(import.meta.url)), '../../../locales');
|
|
10
10
|
const cli = createCli('yydb').locales(localesRoot).intro('cli.intro');
|
|
11
11
|
registerVersionCommand(cli);
|
|
12
12
|
registerInitCommand(cli);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { defineMicro as buildMicro, type DefinedMicro, type MicroHandle } from '
|
|
2
|
-
import type { UdfTypeDescriptor } from '
|
|
1
|
+
import { defineMicro as buildMicro, type DefinedMicro, type MicroHandle } from '../shared/micro.js';
|
|
2
|
+
import type { UdfTypeDescriptor } from '../shared/udf-types.js';
|
|
3
3
|
export type { SchemaVersion } from '@yydb/yydb-client';
|
|
4
4
|
export interface OpenOptions {
|
|
5
5
|
/** Override CLI script path (tests / special installs). */
|
|
@@ -3,16 +3,8 @@ import fs from 'node:fs';
|
|
|
3
3
|
import net from 'node:net';
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { connect } from '@yydb/yydb-client/node';
|
|
6
|
-
import { defineMicro as buildMicro, MicroSessionRegistry } from '
|
|
6
|
+
import { defineMicro as buildMicro, microFingerprint, MicroSessionRegistry } from '../shared/micro.js';
|
|
7
7
|
import { resolveYydbCli } from './resolve-bin.js';
|
|
8
|
-
function microFingerprint(name, version) {
|
|
9
|
-
const bytes = new Uint8Array(32);
|
|
10
|
-
const seed = `${name}@${version}`;
|
|
11
|
-
for (let index = 0; index < bytes.byteLength; index += 1) {
|
|
12
|
-
bytes[index] = seed.charCodeAt(index % seed.length) & 0xff;
|
|
13
|
-
}
|
|
14
|
-
return bytes;
|
|
15
|
-
}
|
|
16
8
|
async function freeLoopbackPort() {
|
|
17
9
|
return await new Promise((resolve, reject) => {
|
|
18
10
|
const server = net.createServer();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { Database, type OpenOptions } from './database.js';
|
|
2
|
-
export { defineMicro, type DefinedMicro, type MicroHandle, type TsMicroDefinitionInput, MicroSessionRegistry } from '
|
|
3
|
-
export { bool, i64, nullType, text, type InferUdfArgs, type InferUdfReturn, type MicroEffect, type MicroInvocationMode, type UdfScalarKind, type UdfTypeDescriptor, } from '
|
|
2
|
+
export { defineMicro, type DefinedMicro, type MicroHandle, type TsMicroDefinitionInput, MicroSessionRegistry } from '../shared/micro.js';
|
|
3
|
+
export { bool, i64, nullType, text, type InferUdfArgs, type InferUdfReturn, type MicroEffect, type MicroInvocationMode, type UdfScalarKind, type UdfTypeDescriptor, } from '../shared/udf-types.js';
|
|
4
4
|
export { nativeBindingName, platformKey, resolveYydbBinary, resolveYydbCli } from './resolve-bin.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { Database } from './database.js';
|
|
2
|
-
export { defineMicro, MicroSessionRegistry } from '
|
|
3
|
-
export { bool, i64, nullType, text, } from '
|
|
2
|
+
export { defineMicro, MicroSessionRegistry } from '../shared/micro.js';
|
|
3
|
+
export { bool, i64, nullType, text, } from '../shared/udf-types.js';
|
|
4
4
|
export { nativeBindingName, platformKey, resolveYydbBinary, resolveYydbCli } from './resolve-bin.js';
|
|
@@ -60,4 +60,6 @@ export declare class MicroSessionRegistry {
|
|
|
60
60
|
}
|
|
61
61
|
/** Builds a session-local micro definition without registering it. */
|
|
62
62
|
export declare function defineMicro<TArgs extends readonly UdfTypeDescriptor[], TReturn extends UdfTypeDescriptor>(input: TsMicroDefinitionInput<TArgs, TReturn>): DefinedMicro<TArgs, TReturn>;
|
|
63
|
+
/** Stable 32-byte fingerprint for a logical micro name and version. */
|
|
64
|
+
export declare function microFingerprint(name: string, version: number): Uint8Array;
|
|
63
65
|
export {};
|
|
@@ -99,6 +99,15 @@ export function defineMicro(input) {
|
|
|
99
99
|
fn: input.fn,
|
|
100
100
|
};
|
|
101
101
|
}
|
|
102
|
+
/** Stable 32-byte fingerprint for a logical micro name and version. */
|
|
103
|
+
export function microFingerprint(name, version) {
|
|
104
|
+
const bytes = new Uint8Array(32);
|
|
105
|
+
const seed = `${name}@${version}`;
|
|
106
|
+
for (let index = 0; index < bytes.byteLength; index += 1) {
|
|
107
|
+
bytes[index] = seed.charCodeAt(index % seed.length) & 0xff;
|
|
108
|
+
}
|
|
109
|
+
return bytes;
|
|
110
|
+
}
|
|
102
111
|
function logicalKey(name, version) {
|
|
103
112
|
return `${name}@${version}`;
|
|
104
113
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Parsed `{ ok, rows, error }` envelope from wasm VOS calls. */
|
|
2
|
+
export type WasmRowsResult = {
|
|
3
|
+
ok: boolean;
|
|
4
|
+
rows: Record<string, unknown>[];
|
|
5
|
+
error: string | null;
|
|
6
|
+
};
|
|
7
|
+
/** Parsed `{ ok, schema, error }` envelope from wasm `getSchema`. */
|
|
8
|
+
export type WasmSchemaResult = {
|
|
9
|
+
ok: boolean;
|
|
10
|
+
schema: {
|
|
11
|
+
version: number;
|
|
12
|
+
document: string;
|
|
13
|
+
} | null;
|
|
14
|
+
error: string | null;
|
|
15
|
+
};
|
|
16
|
+
/** Parsed `{ ok, value, error }` envelope from wasm `get`. */
|
|
17
|
+
export type WasmKvGetResult = {
|
|
18
|
+
ok: boolean;
|
|
19
|
+
value: number[] | null;
|
|
20
|
+
error: string | null;
|
|
21
|
+
};
|
|
22
|
+
/** Parsed `{ ok, value, error }` envelope from wasm `callScalar`. */
|
|
23
|
+
export type WasmScalarResult = {
|
|
24
|
+
ok: boolean;
|
|
25
|
+
value: string | number | boolean | null;
|
|
26
|
+
error: string | null;
|
|
27
|
+
};
|
|
28
|
+
export type WireScalarValue = string | number | boolean | null;
|
|
29
|
+
export type SchemaVersion = {
|
|
30
|
+
version: number;
|
|
31
|
+
document: string;
|
|
32
|
+
};
|
|
33
|
+
export declare function parseWasmRowsResult(payload: string): WasmRowsResult;
|
|
34
|
+
export declare function parseWasmUnitResult(payload: string): void;
|
|
35
|
+
export declare function parseWasmSchemaResult(payload: string): SchemaVersion | null;
|
|
36
|
+
export declare function parseWasmScalarResult(payload: string): WireScalarValue;
|
|
37
|
+
export declare function parseWasmKvGetResult(payload: string): Uint8Array | null;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
function parseJson(payload, label) {
|
|
2
|
+
try {
|
|
3
|
+
return JSON.parse(payload);
|
|
4
|
+
}
|
|
5
|
+
catch (error) {
|
|
6
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
7
|
+
throw new Error(`${label}: invalid JSON (${message})`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function parseWasmRowsResult(payload) {
|
|
11
|
+
const parsed = parseJson(payload, 'wasm query/execute');
|
|
12
|
+
if (!parsed.ok) {
|
|
13
|
+
throw new Error(parsed.error ?? 'wasm query/execute failed');
|
|
14
|
+
}
|
|
15
|
+
return parsed;
|
|
16
|
+
}
|
|
17
|
+
export function parseWasmUnitResult(payload) {
|
|
18
|
+
const parsed = parseJson(payload, 'wasm unit call');
|
|
19
|
+
if (!parsed.ok) {
|
|
20
|
+
throw new Error(parsed.error ?? 'wasm unit call failed');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export function parseWasmSchemaResult(payload) {
|
|
24
|
+
const parsed = parseJson(payload, 'wasm getSchema');
|
|
25
|
+
if (!parsed.ok) {
|
|
26
|
+
throw new Error(parsed.error ?? 'wasm getSchema failed');
|
|
27
|
+
}
|
|
28
|
+
return parsed.schema;
|
|
29
|
+
}
|
|
30
|
+
export function parseWasmScalarResult(payload) {
|
|
31
|
+
const parsed = parseJson(payload, 'wasm callScalar');
|
|
32
|
+
if (!parsed.ok) {
|
|
33
|
+
throw new Error(parsed.error ?? 'wasm callScalar failed');
|
|
34
|
+
}
|
|
35
|
+
return parsed.value;
|
|
36
|
+
}
|
|
37
|
+
export function parseWasmKvGetResult(payload) {
|
|
38
|
+
const parsed = parseJson(payload, 'wasm get');
|
|
39
|
+
if (!parsed.ok) {
|
|
40
|
+
throw new Error(parsed.error ?? 'wasm get failed');
|
|
41
|
+
}
|
|
42
|
+
if (parsed.value === null) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
return Uint8Array.from(parsed.value);
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { checkSchema, createMemorySession, initWasm, introspectSchema, openPersistentSession, queryMemory, resetWasmBindingForTests, yydbVersion, type CheckSchemaResult, type InitWasmOptions, type WasmInitInput, type WasmSession, } from '@yydb/yydb-unknown-wasm32';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { checkSchema, createMemorySession, initWasm, introspectSchema, openPersistentSession, queryMemory, resetWasmBindingForTests, yydbVersion, } from '@yydb/yydb-unknown-wasm32';
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { defineMicro as buildMicro, type DefinedMicro, type MicroHandle } from '../shared/micro.js';
|
|
2
|
+
import type { UdfTypeDescriptor } from '../shared/udf-types.js';
|
|
3
|
+
import { type SchemaVersion } from '../shared/wasm-envelope.js';
|
|
4
|
+
import { type CheckSchemaResult, type InitWasmOptions } from './binding.js';
|
|
5
|
+
export type WasmOpenOptions = InitWasmOptions;
|
|
6
|
+
/**
|
|
7
|
+
* Browser-facing YYDB handle backed by an in-process wasm session.
|
|
8
|
+
* Use `open` for OPFS-backed paths and `openInMemory` for ephemeral state.
|
|
9
|
+
*/
|
|
10
|
+
export declare class Database {
|
|
11
|
+
readonly path: string;
|
|
12
|
+
private session;
|
|
13
|
+
private readonly kind;
|
|
14
|
+
private readonly microRegistry;
|
|
15
|
+
private constructor();
|
|
16
|
+
/** Open an OPFS-backed database at `path` (for example `app.yydb`). */
|
|
17
|
+
static open(path: string, options?: WasmOpenOptions): Promise<Database>;
|
|
18
|
+
/** Open a stateful in-memory database session. */
|
|
19
|
+
static openInMemory(options?: WasmOpenOptions): Promise<Database>;
|
|
20
|
+
/** Library version from the wasm binding. */
|
|
21
|
+
version(): string;
|
|
22
|
+
/** Engine version string (Node API parity). */
|
|
23
|
+
serverVersion(): Promise<string>;
|
|
24
|
+
/** Database info text (`path=…\\n…`), matching the YY wire `Info` reply. */
|
|
25
|
+
info(): Promise<string>;
|
|
26
|
+
/** Parse and validate a VOS schema document without opening a session. */
|
|
27
|
+
checkSchema(source: string): CheckSchemaResult;
|
|
28
|
+
/** Persist schema document. `version` is accepted for Node API parity; wasm uses the document body. */
|
|
29
|
+
ensureSchema(_version: number, document: string): Promise<void>;
|
|
30
|
+
getSchema(): Promise<SchemaVersion | null>;
|
|
31
|
+
get(key: string): Promise<Uint8Array | null>;
|
|
32
|
+
put(key: string, value: Uint8Array | string): Promise<void>;
|
|
33
|
+
/** VOS read pipeline (for example `User.filter(x => x.active).collect()`). */
|
|
34
|
+
query(source: string): Record<string, unknown>[];
|
|
35
|
+
/** Unit-valued VOS write programs (for example `User { … }.insert()`). */
|
|
36
|
+
execute(source: string): void;
|
|
37
|
+
/**
|
|
38
|
+
* Build a typed session-local micro definition. Does not register it with
|
|
39
|
+
* the engine until `registerMicro` is called.
|
|
40
|
+
*/
|
|
41
|
+
defineMicro<TArgs extends readonly UdfTypeDescriptor[], TReturn extends UdfTypeDescriptor>(definition: Parameters<typeof buildMicro<TArgs, TReturn>>[0]): DefinedMicro<TArgs, TReturn>;
|
|
42
|
+
/**
|
|
43
|
+
* Install a `defineMicro` definition into the current session registry and
|
|
44
|
+
* sync metadata to the embedded engine. Host micros are session-local.
|
|
45
|
+
*/
|
|
46
|
+
registerMicro<TArgs extends readonly UdfTypeDescriptor[], TReturn extends UdfTypeDescriptor>(definition: DefinedMicro<TArgs, TReturn>): Promise<MicroHandle>;
|
|
47
|
+
/** Invoke a registered scalar UDF on the embedded engine. */
|
|
48
|
+
callScalar(name: string, version: number, args: readonly (string | number | boolean | null)[]): Promise<string | number | boolean | null>;
|
|
49
|
+
/** Whether this handle owns a persistent OPFS session. */
|
|
50
|
+
isPersistent(): boolean;
|
|
51
|
+
close(): void;
|
|
52
|
+
private requireSession;
|
|
53
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { encodeMicroRegister, encodeScalarCall } from '@yydb/yydb-client';
|
|
2
|
+
import { defineMicro as buildMicro, microFingerprint, MicroSessionRegistry } from '../shared/micro.js';
|
|
3
|
+
import { parseWasmKvGetResult, parseWasmRowsResult, parseWasmScalarResult, parseWasmSchemaResult, parseWasmUnitResult, } from '../shared/wasm-envelope.js';
|
|
4
|
+
import { checkSchema, createMemorySession, initWasm, openPersistentSession, yydbVersion, } from './binding.js';
|
|
5
|
+
/**
|
|
6
|
+
* Browser-facing YYDB handle backed by an in-process wasm session.
|
|
7
|
+
* Use `open` for OPFS-backed paths and `openInMemory` for ephemeral state.
|
|
8
|
+
*/
|
|
9
|
+
export class Database {
|
|
10
|
+
path;
|
|
11
|
+
session;
|
|
12
|
+
kind;
|
|
13
|
+
microRegistry;
|
|
14
|
+
constructor(kind, path, session) {
|
|
15
|
+
this.kind = kind;
|
|
16
|
+
this.path = path;
|
|
17
|
+
this.session = session;
|
|
18
|
+
this.microRegistry = new MicroSessionRegistry();
|
|
19
|
+
session.setMicroHostInvoker((payload) => this.microRegistry.invokeFromWire(payload));
|
|
20
|
+
}
|
|
21
|
+
/** Open an OPFS-backed database at `path` (for example `app.yydb`). */
|
|
22
|
+
static async open(path, options = {}) {
|
|
23
|
+
await initWasm(options);
|
|
24
|
+
const session = openPersistentSession(path);
|
|
25
|
+
return new Database('persistent', session.path ?? path, session);
|
|
26
|
+
}
|
|
27
|
+
/** Open a stateful in-memory database session. */
|
|
28
|
+
static async openInMemory(options = {}) {
|
|
29
|
+
await initWasm(options);
|
|
30
|
+
const session = createMemorySession();
|
|
31
|
+
return new Database('memory', ':memory:', session);
|
|
32
|
+
}
|
|
33
|
+
/** Library version from the wasm binding. */
|
|
34
|
+
version() {
|
|
35
|
+
return yydbVersion();
|
|
36
|
+
}
|
|
37
|
+
/** Engine version string (Node API parity). */
|
|
38
|
+
async serverVersion() {
|
|
39
|
+
return yydbVersion();
|
|
40
|
+
}
|
|
41
|
+
/** Database info text (`path=…\\n…`), matching the YY wire `Info` reply. */
|
|
42
|
+
async info() {
|
|
43
|
+
return this.requireSession().info();
|
|
44
|
+
}
|
|
45
|
+
/** Parse and validate a VOS schema document without opening a session. */
|
|
46
|
+
checkSchema(source) {
|
|
47
|
+
return checkSchema(source);
|
|
48
|
+
}
|
|
49
|
+
/** Persist schema document. `version` is accepted for Node API parity; wasm uses the document body. */
|
|
50
|
+
async ensureSchema(_version, document) {
|
|
51
|
+
parseWasmUnitResult(this.requireSession().ensureSchema(document));
|
|
52
|
+
}
|
|
53
|
+
async getSchema() {
|
|
54
|
+
return parseWasmSchemaResult(this.requireSession().getSchema());
|
|
55
|
+
}
|
|
56
|
+
async get(key) {
|
|
57
|
+
return parseWasmKvGetResult(this.requireSession().get(key));
|
|
58
|
+
}
|
|
59
|
+
async put(key, value) {
|
|
60
|
+
const bytes = typeof value === 'string' ? new TextEncoder().encode(value) : value;
|
|
61
|
+
parseWasmUnitResult(this.requireSession().put(key, bytes));
|
|
62
|
+
}
|
|
63
|
+
/** VOS read pipeline (for example `User.filter(x => x.active).collect()`). */
|
|
64
|
+
query(source) {
|
|
65
|
+
return parseWasmRowsResult(this.requireSession().query(source)).rows;
|
|
66
|
+
}
|
|
67
|
+
/** Unit-valued VOS write programs (for example `User { … }.insert()`). */
|
|
68
|
+
execute(source) {
|
|
69
|
+
parseWasmUnitResult(this.requireSession().execute(source));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Build a typed session-local micro definition. Does not register it with
|
|
73
|
+
* the engine until `registerMicro` is called.
|
|
74
|
+
*/
|
|
75
|
+
defineMicro(definition) {
|
|
76
|
+
return buildMicro(definition);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Install a `defineMicro` definition into the current session registry and
|
|
80
|
+
* sync metadata to the embedded engine. Host micros are session-local.
|
|
81
|
+
*/
|
|
82
|
+
async registerMicro(definition) {
|
|
83
|
+
const handle = this.microRegistry.register(definition);
|
|
84
|
+
const body = encodeMicroRegister({
|
|
85
|
+
hostId: handle.hostId,
|
|
86
|
+
handleVersion: handle.implementationVersion,
|
|
87
|
+
udfVersion: definition.version,
|
|
88
|
+
name: definition.name,
|
|
89
|
+
functionId: handle.functionId,
|
|
90
|
+
args: definition.args.map((arg) => arg.kind),
|
|
91
|
+
returns: definition.returns.kind,
|
|
92
|
+
fingerprint: microFingerprint(definition.name, definition.version),
|
|
93
|
+
});
|
|
94
|
+
parseWasmUnitResult(this.requireSession().registerMicro(body));
|
|
95
|
+
return handle;
|
|
96
|
+
}
|
|
97
|
+
/** Invoke a registered scalar UDF on the embedded engine. */
|
|
98
|
+
async callScalar(name, version, args) {
|
|
99
|
+
const body = encodeScalarCall(name, version, args);
|
|
100
|
+
return parseWasmScalarResult(this.requireSession().callScalar(body));
|
|
101
|
+
}
|
|
102
|
+
/** Whether this handle owns a persistent OPFS session. */
|
|
103
|
+
isPersistent() {
|
|
104
|
+
return this.kind === 'persistent';
|
|
105
|
+
}
|
|
106
|
+
close() {
|
|
107
|
+
this.session?.close();
|
|
108
|
+
this.session = null;
|
|
109
|
+
}
|
|
110
|
+
requireSession() {
|
|
111
|
+
if (!this.session) {
|
|
112
|
+
throw new Error('@yydb/yydb/wasm: database session is closed');
|
|
113
|
+
}
|
|
114
|
+
return this.session;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { Database, type WasmOpenOptions } from './database.js';
|
|
2
|
+
export { checkSchema, createMemorySession, initWasm, introspectSchema, openPersistentSession, queryMemory, resetWasmBindingForTests, yydbVersion, type CheckSchemaResult, type InitWasmOptions, type WasmInitInput, type WasmSession, } from './binding.js';
|
|
3
|
+
export { defineMicro, type DefinedMicro, type MicroHandle, type TsMicroDefinitionInput, MicroSessionRegistry } from '../shared/micro.js';
|
|
4
|
+
export { bool, i64, nullType, text, type InferUdfArgs, type InferUdfReturn, type MicroEffect, type MicroInvocationMode, type UdfScalarKind, type UdfTypeDescriptor, } from '../shared/udf-types.js';
|
|
5
|
+
export type { SchemaVersion } from '../shared/wasm-envelope.js';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { Database } from './database.js';
|
|
2
|
+
export { checkSchema, createMemorySession, initWasm, introspectSchema, openPersistentSession, queryMemory, resetWasmBindingForTests, yydbVersion, } from './binding.js';
|
|
3
|
+
export { defineMicro, MicroSessionRegistry } from '../shared/micro.js';
|
|
4
|
+
export { bool, i64, nullType, text, } from '../shared/udf-types.js';
|
package/package.json
CHANGED
|
@@ -1,22 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yydb/yydb",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "YYDB all-in-one
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"description": "YYDB all-in-one: Node embedded engine and CLI, browser wasm core",
|
|
5
5
|
"license": "MPL-2.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"bin": {
|
|
8
|
-
"yydb": "./dist/cli.js"
|
|
8
|
+
"yydb": "./dist/node/cli.js"
|
|
9
9
|
},
|
|
10
|
-
"main": "./dist/index.js",
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
10
|
+
"main": "./dist/node/index.js",
|
|
11
|
+
"types": "./dist/node/index.d.ts",
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|
|
14
|
-
"types": "./dist/index.d.ts",
|
|
15
|
-
"
|
|
14
|
+
"types": "./dist/node/index.d.ts",
|
|
15
|
+
"node": "./dist/node/index.js",
|
|
16
|
+
"default": "./dist/wasm/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./node": {
|
|
19
|
+
"types": "./dist/node/index.d.ts",
|
|
20
|
+
"import": "./dist/node/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./wasm": {
|
|
23
|
+
"types": "./dist/wasm/index.d.ts",
|
|
24
|
+
"import": "./dist/wasm/index.js"
|
|
16
25
|
},
|
|
17
26
|
"./cli": {
|
|
18
|
-
"types": "./dist/cli.d.ts",
|
|
19
|
-
"import": "./dist/cli.js"
|
|
27
|
+
"types": "./dist/node/cli.d.ts",
|
|
28
|
+
"import": "./dist/node/cli.js"
|
|
20
29
|
}
|
|
21
30
|
},
|
|
22
31
|
"files": [
|
|
@@ -24,8 +33,8 @@
|
|
|
24
33
|
"README.md"
|
|
25
34
|
],
|
|
26
35
|
"scripts": {
|
|
27
|
-
"build": "tsc -p tsconfig.json",
|
|
28
|
-
"test": "pnpm run build && node --experimental-strip-types --test src/**/*.test.ts"
|
|
36
|
+
"build": "tsc -p tsconfig.node.json && tsc -p tsconfig.wasm.json",
|
|
37
|
+
"test": "pnpm run build && node --experimental-strip-types --test src/node/**/*.test.ts src/shared/**/*.test.ts"
|
|
29
38
|
},
|
|
30
39
|
"repository": {
|
|
31
40
|
"type": "git",
|
|
@@ -48,7 +57,8 @@
|
|
|
48
57
|
},
|
|
49
58
|
"dependencies": {
|
|
50
59
|
"@vmz/commander": "^0.2.0",
|
|
51
|
-
"@yydb/yydb-client": "0.0.
|
|
60
|
+
"@yydb/yydb-client": "0.0.5",
|
|
61
|
+
"@yydb/yydb-unknown-wasm32": "0.0.5"
|
|
52
62
|
},
|
|
53
63
|
"devDependencies": {
|
|
54
64
|
"@types/node": "^20.10.0",
|
|
@@ -58,9 +68,9 @@
|
|
|
58
68
|
"node": ">=18"
|
|
59
69
|
},
|
|
60
70
|
"optionalDependencies": {
|
|
61
|
-
"@yydb/yydb-win32-x64": "0.0.
|
|
62
|
-
"@yydb/yydb-linux-x64": "0.0.
|
|
63
|
-
"@yydb/yydb-darwin-x64": "0.0.
|
|
64
|
-
"@yydb/yydb-darwin-arm64": "0.0.
|
|
71
|
+
"@yydb/yydb-win32-x64": "0.0.5",
|
|
72
|
+
"@yydb/yydb-linux-x64": "0.0.5",
|
|
73
|
+
"@yydb/yydb-darwin-x64": "0.0.5",
|
|
74
|
+
"@yydb/yydb-darwin-arm64": "0.0.5"
|
|
65
75
|
}
|
|
66
76
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|