@hakodb/client 0.6.2 → 0.6.4
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/.github/workflows/ci-quick.yml +31 -31
- package/.github/workflows/publish.yml +36 -0
- package/README.md +27 -37
- package/examples/bun/index.ts +85 -85
- package/examples/bun/package.json +10 -10
- package/examples/node/index.ts +111 -111
- package/examples/node/package-lock.json +533 -533
- package/examples/node/package.json +13 -13
- package/examples/node/softdel.ts +70 -70
- package/package.json +38 -34
- package/src/bun-ffi.d.ts +6 -6
- package/src/client.ts +1653 -1627
- package/src/index.ts +37 -37
- package/src/native.ts +1620 -1605
- package/sync-native.ps1 +55 -55
- package/tsconfig.json +12 -12
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
# Quick gate for branch protection (required check on main).
|
|
2
|
-
# Typecheck only (no build step in these packages).
|
|
3
|
-
|
|
4
|
-
name: ci-quick
|
|
5
|
-
|
|
6
|
-
on:
|
|
7
|
-
pull_request:
|
|
8
|
-
branches: [main]
|
|
9
|
-
push:
|
|
10
|
-
branches: [main]
|
|
11
|
-
|
|
12
|
-
permissions:
|
|
13
|
-
contents: read
|
|
14
|
-
|
|
15
|
-
jobs:
|
|
16
|
-
check:
|
|
17
|
-
name: quick-check
|
|
18
|
-
runs-on: ubuntu-latest
|
|
19
|
-
steps:
|
|
20
|
-
- uses: actions/checkout@v4
|
|
21
|
-
|
|
22
|
-
- uses: actions/setup-node@v4
|
|
23
|
-
with:
|
|
24
|
-
node-version: 22
|
|
25
|
-
cache: npm
|
|
26
|
-
|
|
27
|
-
- name: install
|
|
28
|
-
run: npm ci
|
|
29
|
-
|
|
30
|
-
- name: typecheck
|
|
31
|
-
run: npx tsc --noEmit -p tsconfig.json
|
|
1
|
+
# Quick gate for branch protection (required check on main).
|
|
2
|
+
# Typecheck only (no build step in these packages).
|
|
3
|
+
|
|
4
|
+
name: ci-quick
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
pull_request:
|
|
8
|
+
branches: [main]
|
|
9
|
+
push:
|
|
10
|
+
branches: [main]
|
|
11
|
+
|
|
12
|
+
permissions:
|
|
13
|
+
contents: read
|
|
14
|
+
|
|
15
|
+
jobs:
|
|
16
|
+
check:
|
|
17
|
+
name: quick-check
|
|
18
|
+
runs-on: ubuntu-latest
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v4
|
|
21
|
+
|
|
22
|
+
- uses: actions/setup-node@v4
|
|
23
|
+
with:
|
|
24
|
+
node-version: 22
|
|
25
|
+
cache: npm
|
|
26
|
+
|
|
27
|
+
- name: install
|
|
28
|
+
run: npm ci
|
|
29
|
+
|
|
30
|
+
- name: typecheck
|
|
31
|
+
run: npx tsc --noEmit -p tsconfig.json
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Publish via npm trusted publishing (OIDC) — no long-lived token.
|
|
2
|
+
# The package must be configured on npmjs.com: Trusted Publisher =
|
|
3
|
+
# GitHub Actions, owner hakodb, this repo, workflow publish.yml.
|
|
4
|
+
# Tag discipline: bump the version on main via PR, merge, then push v*.
|
|
5
|
+
|
|
6
|
+
name: publish
|
|
7
|
+
|
|
8
|
+
on:
|
|
9
|
+
push:
|
|
10
|
+
tags: ["v*"]
|
|
11
|
+
|
|
12
|
+
permissions:
|
|
13
|
+
contents: read
|
|
14
|
+
id-token: write
|
|
15
|
+
|
|
16
|
+
jobs:
|
|
17
|
+
publish:
|
|
18
|
+
name: publish
|
|
19
|
+
runs-on: ubuntu-latest
|
|
20
|
+
steps:
|
|
21
|
+
- uses: actions/checkout@v4
|
|
22
|
+
|
|
23
|
+
- uses: actions/setup-node@v4
|
|
24
|
+
with:
|
|
25
|
+
node-version: 24
|
|
26
|
+
registry-url: "https://registry.npmjs.org"
|
|
27
|
+
package-manager-cache: false
|
|
28
|
+
|
|
29
|
+
- name: install
|
|
30
|
+
run: npm ci
|
|
31
|
+
|
|
32
|
+
- name: typecheck
|
|
33
|
+
run: npx tsc --noEmit -p tsconfig.json
|
|
34
|
+
|
|
35
|
+
- name: publish
|
|
36
|
+
run: npm publish --access public
|
package/README.md
CHANGED
|
@@ -1,37 +1,27 @@
|
|
|
1
|
-
# hakojs
|
|
2
|
-
|
|
3
|
-
> Part of [**HakoDB**](https://github.com/hakodb/hakodb) — embedded document DB with native vector search and multi-plane sync, written in Rust. The engine + C ABI live in `hakodb/hakodb`; this repo holds the JavaScript/TypeScript SDK.
|
|
4
|
-
|
|
5
|
-
JavaScript/TypeScript SDK for HakoDB:
|
|
6
|
-
high-level client (`client.ts`: `HakoClient`, `HakoConfig`) and native
|
|
7
|
-
FFI bindings over koffi/Bun (`native.ts`). Tauri lives in `@hakodb/tauri`
|
|
8
|
-
(sibling repo [`hakodb/hakotaurits`](https://github.com/hakodb/hakotaurits)).
|
|
9
|
-
|
|
10
|
-
## Compatibility
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
`native.ts` loads the HakoDB cdylib. Resolution order:
|
|
29
|
-
`loadNativeBindings(explicitPath)` → `HAKODB_LIB_PATH` env →
|
|
30
|
-
`./native/<lib>`. Populate it with the sync script:
|
|
31
|
-
|
|
32
|
-
```powershell
|
|
33
|
-
.\sync-native.ps1 -CoreDir C:\Dev\libs\hakodb
|
|
34
|
-
.\sync-native.ps1 -Tag v0.19.0
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
Binaries under `native/` are git-ignored.
|
|
1
|
+
# hakojs
|
|
2
|
+
|
|
3
|
+
> Part of [**HakoDB**](https://github.com/hakodb/hakodb) — embedded document DB with native vector search and multi-plane sync, written in Rust. The engine + C ABI live in `hakodb/hakodb`; this repo holds the JavaScript/TypeScript SDK.
|
|
4
|
+
|
|
5
|
+
JavaScript/TypeScript SDK for HakoDB:
|
|
6
|
+
high-level client (`client.ts`: `HakoClient`, `HakoConfig`) and native
|
|
7
|
+
FFI bindings over koffi/Bun (`native.ts`). Tauri lives in `@hakodb/tauri`
|
|
8
|
+
(sibling repo [`hakodb/hakotaurits`](https://github.com/hakodb/hakotaurits)).
|
|
9
|
+
|
|
10
|
+
## Compatibility
|
|
11
|
+
|
|
12
|
+
Works against the corresponding core release; each release pairs with a matching `hakodb` tag (see the releases page for the exact pairing).
|
|
13
|
+
|
|
14
|
+
## Setup — native library
|
|
15
|
+
|
|
16
|
+
`native.ts` loads the HakoDB cdylib. Resolution order:
|
|
17
|
+
`loadNativeBindings(explicitPath)` → `HAKODB_LIB_PATH` env →
|
|
18
|
+
`./native/<lib>`. Populate it with the sync script:
|
|
19
|
+
|
|
20
|
+
```powershell
|
|
21
|
+
# from a local core checkout (build it first: cargo build --release)
|
|
22
|
+
.\sync-native.ps1 -CoreDir C:\Dev\libs\hakodb
|
|
23
|
+
# or from a core release (pass the tag you want)
|
|
24
|
+
.\sync-native.ps1 -Tag <core-release-tag>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Binaries under `native/` are git-ignored.
|
package/examples/bun/index.ts
CHANGED
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HakoDB Bun example (TypeScript SDK, bun:ffi backend - no koffi needed).
|
|
3
|
-
*
|
|
4
|
-
* Setup & run (from the repository root):
|
|
5
|
-
*
|
|
6
|
-
* .\sync-native.ps1 -CoreDir C:\Dev\libs\firelite # -> native/hakodb.dll
|
|
7
|
-
* bun examples/bun/index.ts
|
|
8
|
-
*
|
|
9
|
-
* Bun runs TypeScript natively, so there is no install step. The SDK detects
|
|
10
|
-
* the Bun runtime automatically and uses bun:ffi instead of koffi.
|
|
11
|
-
*/
|
|
12
|
-
import path from "node:path";
|
|
13
|
-
import {
|
|
14
|
-
DurabilityMode,
|
|
15
|
-
HakoClient,
|
|
16
|
-
} from "../../src/index.ts";
|
|
17
|
-
|
|
18
|
-
const repoRoot = path.resolve(import.meta.dir, "..", "..");
|
|
19
|
-
const libName =
|
|
20
|
-
process.platform === "win32"
|
|
21
|
-
? "hakodb.dll"
|
|
22
|
-
: process.platform === "darwin"
|
|
23
|
-
? "libhakodb.dylib"
|
|
24
|
-
: "libhakodb.so";
|
|
25
|
-
const libraryPath = path.join(repoRoot, "native", libName);
|
|
26
|
-
|
|
27
|
-
async function main(): Promise<void> {
|
|
28
|
-
// ---- Open with a configuration ----
|
|
29
|
-
const config = await HakoClient.createConfig(libraryPath);
|
|
30
|
-
config.setDurability(DurabilityMode.Always);
|
|
31
|
-
const db = await HakoClient.open("demo.db", { config, libraryPath });
|
|
32
|
-
|
|
33
|
-
// ---- Write documents ----
|
|
34
|
-
await db.set("users", "u1", { name: "Alice", age: 32, active: true });
|
|
35
|
-
await db.set("users", "u2", { name: "Bob", age: 27, active: true });
|
|
36
|
-
|
|
37
|
-
// ---- Read ----
|
|
38
|
-
const snap = await db.get("users", "u1");
|
|
39
|
-
console.log("u1 ->", snap.exists ? snap.data() : "not found");
|
|
40
|
-
|
|
41
|
-
// ---- Query ----
|
|
42
|
-
const rows = await db.collection("users").where("age", ">=", 27).get();
|
|
43
|
-
console.log("query(age>=27) ->", rows);
|
|
44
|
-
|
|
45
|
-
// ---- Batch ----
|
|
46
|
-
const batch = db.batch();
|
|
47
|
-
batch.set(db.collection("users").doc("u3"), { name: "Carol", age: 41 });
|
|
48
|
-
await batch.commit();
|
|
49
|
-
|
|
50
|
-
// ---- Watch ----
|
|
51
|
-
const unsubscribe = db
|
|
52
|
-
.collection("users")
|
|
53
|
-
.onSnapshot((docs) => console.log("snapshot:", docs));
|
|
54
|
-
await db.set("users", "u4", { name: "Dave", age: 50 });
|
|
55
|
-
await Bun.sleep(250);
|
|
56
|
-
await unsubscribe();
|
|
57
|
-
|
|
58
|
-
// ---- Net + Cloud sync (requires --features net-sync,cloud-sync build;
|
|
59
|
-
// NetSync also needs a Tokio host runtime, so it degrades gracefully) ----
|
|
60
|
-
const syncer = db.createNetSyncer("demo-room", "secret-key");
|
|
61
|
-
try {
|
|
62
|
-
await syncer.start(4456);
|
|
63
|
-
console.log("net sync status:", await syncer.status());
|
|
64
|
-
} catch (err) {
|
|
65
|
-
console.warn("net sync skipped:", err);
|
|
66
|
-
} finally {
|
|
67
|
-
await syncer.close();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Room-agnostic cloud SERVER: not bound to any room, hosts any (room, key).
|
|
71
|
-
const server = db.createCloudSyncServer("bun-server", "server-token");
|
|
72
|
-
server.close();
|
|
73
|
-
|
|
74
|
-
// Offline-first cloud CLIENT: picks its own room + server.
|
|
75
|
-
const cloud = db.createCloudSyncClient("c1", "room", "secret-key", "token");
|
|
76
|
-
cloud.close();
|
|
77
|
-
|
|
78
|
-
await db.close();
|
|
79
|
-
console.log("done");
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
main().catch((err) => {
|
|
83
|
-
console.error(err);
|
|
84
|
-
process.exit(1);
|
|
85
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* HakoDB Bun example (TypeScript SDK, bun:ffi backend - no koffi needed).
|
|
3
|
+
*
|
|
4
|
+
* Setup & run (from the repository root):
|
|
5
|
+
*
|
|
6
|
+
* .\sync-native.ps1 -CoreDir C:\Dev\libs\firelite # -> native/hakodb.dll
|
|
7
|
+
* bun examples/bun/index.ts
|
|
8
|
+
*
|
|
9
|
+
* Bun runs TypeScript natively, so there is no install step. The SDK detects
|
|
10
|
+
* the Bun runtime automatically and uses bun:ffi instead of koffi.
|
|
11
|
+
*/
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
import {
|
|
14
|
+
DurabilityMode,
|
|
15
|
+
HakoClient,
|
|
16
|
+
} from "../../src/index.ts";
|
|
17
|
+
|
|
18
|
+
const repoRoot = path.resolve(import.meta.dir, "..", "..");
|
|
19
|
+
const libName =
|
|
20
|
+
process.platform === "win32"
|
|
21
|
+
? "hakodb.dll"
|
|
22
|
+
: process.platform === "darwin"
|
|
23
|
+
? "libhakodb.dylib"
|
|
24
|
+
: "libhakodb.so";
|
|
25
|
+
const libraryPath = path.join(repoRoot, "native", libName);
|
|
26
|
+
|
|
27
|
+
async function main(): Promise<void> {
|
|
28
|
+
// ---- Open with a configuration ----
|
|
29
|
+
const config = await HakoClient.createConfig(libraryPath);
|
|
30
|
+
config.setDurability(DurabilityMode.Always);
|
|
31
|
+
const db = await HakoClient.open("demo.db", { config, libraryPath });
|
|
32
|
+
|
|
33
|
+
// ---- Write documents ----
|
|
34
|
+
await db.set("users", "u1", { name: "Alice", age: 32, active: true });
|
|
35
|
+
await db.set("users", "u2", { name: "Bob", age: 27, active: true });
|
|
36
|
+
|
|
37
|
+
// ---- Read ----
|
|
38
|
+
const snap = await db.get("users", "u1");
|
|
39
|
+
console.log("u1 ->", snap.exists ? snap.data() : "not found");
|
|
40
|
+
|
|
41
|
+
// ---- Query ----
|
|
42
|
+
const rows = await db.collection("users").where("age", ">=", 27).get();
|
|
43
|
+
console.log("query(age>=27) ->", rows);
|
|
44
|
+
|
|
45
|
+
// ---- Batch ----
|
|
46
|
+
const batch = db.batch();
|
|
47
|
+
batch.set(db.collection("users").doc("u3"), { name: "Carol", age: 41 });
|
|
48
|
+
await batch.commit();
|
|
49
|
+
|
|
50
|
+
// ---- Watch ----
|
|
51
|
+
const unsubscribe = db
|
|
52
|
+
.collection("users")
|
|
53
|
+
.onSnapshot((docs) => console.log("snapshot:", docs));
|
|
54
|
+
await db.set("users", "u4", { name: "Dave", age: 50 });
|
|
55
|
+
await Bun.sleep(250);
|
|
56
|
+
await unsubscribe();
|
|
57
|
+
|
|
58
|
+
// ---- Net + Cloud sync (requires --features net-sync,cloud-sync build;
|
|
59
|
+
// NetSync also needs a Tokio host runtime, so it degrades gracefully) ----
|
|
60
|
+
const syncer = db.createNetSyncer("demo-room", "secret-key");
|
|
61
|
+
try {
|
|
62
|
+
await syncer.start(4456);
|
|
63
|
+
console.log("net sync status:", await syncer.status());
|
|
64
|
+
} catch (err) {
|
|
65
|
+
console.warn("net sync skipped:", err);
|
|
66
|
+
} finally {
|
|
67
|
+
await syncer.close();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Room-agnostic cloud SERVER: not bound to any room, hosts any (room, key).
|
|
71
|
+
const server = db.createCloudSyncServer("bun-server", "server-token");
|
|
72
|
+
server.close();
|
|
73
|
+
|
|
74
|
+
// Offline-first cloud CLIENT: picks its own room + server.
|
|
75
|
+
const cloud = db.createCloudSyncClient("c1", "room", "secret-key", "token");
|
|
76
|
+
cloud.close();
|
|
77
|
+
|
|
78
|
+
await db.close();
|
|
79
|
+
console.log("done");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
main().catch((err) => {
|
|
83
|
+
console.error(err);
|
|
84
|
+
process.exit(1);
|
|
85
|
+
});
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "hako-example-bun",
|
|
3
|
-
"private": true,
|
|
4
|
-
"version": "0.1.0",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"description": "HakoDB Bun example using the TypeScript SDK (bun:ffi backend)",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"start": "bun index.ts"
|
|
9
|
-
}
|
|
10
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "hako-example-bun",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "HakoDB Bun example using the TypeScript SDK (bun:ffi backend)",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "bun index.ts"
|
|
9
|
+
}
|
|
10
|
+
}
|
package/examples/node/index.ts
CHANGED
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HakoDB Node.js example (TypeScript SDK, koffi FFI backend).
|
|
3
|
-
*
|
|
4
|
-
* Setup & run (from the repository root):
|
|
5
|
-
*
|
|
6
|
-
* npm install # koffi + msgpack
|
|
7
|
-
* npm install --prefix examples/node # tsx for running TS
|
|
8
|
-
* ..\sync-native.ps1 -CoreDir C:\Dev\libs\firelite # -> native/hakodb.dll
|
|
9
|
-
* node --import tsx examples/node/index.ts # (or cd examples/node)
|
|
10
|
-
*
|
|
11
|
-
* You can also drop the --features and comment out the demoNetAndCloudSync()
|
|
12
|
-
* call if you only want the core CRUD/query part.
|
|
13
|
-
*/
|
|
14
|
-
import path from "node:path";
|
|
15
|
-
import { fileURLToPath } from "node:url";
|
|
16
|
-
import {
|
|
17
|
-
DurabilityMode,
|
|
18
|
-
HakoClient,
|
|
19
|
-
HakoConfig,
|
|
20
|
-
} from "../../src/index.ts";
|
|
21
|
-
|
|
22
|
-
const repoRoot = path.resolve(
|
|
23
|
-
path.dirname(fileURLToPath(import.meta.url)),
|
|
24
|
-
"..", ".."
|
|
25
|
-
);
|
|
26
|
-
const libName =
|
|
27
|
-
process.platform === "win32"
|
|
28
|
-
? "hakodb.dll"
|
|
29
|
-
: process.platform === "darwin"
|
|
30
|
-
? "libhakodb.dylib"
|
|
31
|
-
: "libhakodb.so";
|
|
32
|
-
const libraryPath = path.join(repoRoot, "native", libName);
|
|
33
|
-
|
|
34
|
-
async function main(): Promise<void> {
|
|
35
|
-
// ---- Open with a configuration (durability / tuning) ----
|
|
36
|
-
const config: HakoConfig = await HakoClient.createConfig(libraryPath);
|
|
37
|
-
config.setDurability(DurabilityMode.Always).setQueryWorkers(4);
|
|
38
|
-
const db = await HakoClient.open("demo.db", { config, libraryPath });
|
|
39
|
-
|
|
40
|
-
// ---- Write documents ----
|
|
41
|
-
await db.set("users", "u1", { name: "Alice", age: 32, active: true });
|
|
42
|
-
await db.set("users", "u2", { name: "Bob", age: 27, active: true });
|
|
43
|
-
console.log("inserted u1, u2");
|
|
44
|
-
|
|
45
|
-
// ---- Read a document ----
|
|
46
|
-
const snap = await db.get("users", "u1");
|
|
47
|
-
console.log("u1 ->", snap.exists ? snap.data() : "not found");
|
|
48
|
-
|
|
49
|
-
// ---- Query (firestore-style constraints) ----
|
|
50
|
-
const rows = await db
|
|
51
|
-
.collection("users")
|
|
52
|
-
.where("age", ">=", 27)
|
|
53
|
-
.orderBy("age", "desc")
|
|
54
|
-
.limit(10)
|
|
55
|
-
.get();
|
|
56
|
-
console.log("query(age>=27) ->", rows);
|
|
57
|
-
|
|
58
|
-
// ---- Aggregation ----
|
|
59
|
-
console.log(
|
|
60
|
-
"count(users) =",
|
|
61
|
-
await db.collection("users").count()
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
// ---- Atomic batch ----
|
|
65
|
-
const batch = db.batch();
|
|
66
|
-
batch.set(db.collection("users").doc("u3"), { name: "Carol", age: 41 });
|
|
67
|
-
batch.set(db.collection("users").doc("u4"), { name: "Dave", age: 50 });
|
|
68
|
-
await batch.commit();
|
|
69
|
-
console.log("batch committed (u3, u4)");
|
|
70
|
-
|
|
71
|
-
// ---- Real-time watch ----
|
|
72
|
-
const unsubscribe = db
|
|
73
|
-
.collection("users")
|
|
74
|
-
.onSnapshot((docs) => console.log("snapshot:", docs));
|
|
75
|
-
await db.set("users", "u5", { name: "Eve", age: 22 });
|
|
76
|
-
await new Promise((r) => setTimeout(r, 250));
|
|
77
|
-
await unsubscribe();
|
|
78
|
-
|
|
79
|
-
await demoNetAndCloudSync(db);
|
|
80
|
-
|
|
81
|
-
await db.close();
|
|
82
|
-
console.log("done");
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** Requires the shared library built with --features net-sync,cloud-sync
|
|
86
|
-
* and, for NetSync, a Tokio host runtime (use hako-cli serve --net-sync
|
|
87
|
-
* for a working LAN mesh from other languages). */
|
|
88
|
-
async function demoNetAndCloudSync(db: HakoClient): Promise<void> {
|
|
89
|
-
const syncer = db.createNetSyncer("demo-room", "secret-key");
|
|
90
|
-
try {
|
|
91
|
-
await syncer.start(4456);
|
|
92
|
-
console.log("net sync status:", await syncer.status());
|
|
93
|
-
} catch (err) {
|
|
94
|
-
console.warn("net sync skipped:", err);
|
|
95
|
-
} finally {
|
|
96
|
-
await syncer.close();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Room-agnostic cloud SERVER: not bound to any room, hosts any (room, key).
|
|
100
|
-
const server = db.createCloudSyncServer("node-server", "server-token");
|
|
101
|
-
server.close();
|
|
102
|
-
|
|
103
|
-
// Offline-first cloud CLIENT: picks its own room + server.
|
|
104
|
-
const cloud = db.createCloudSyncClient("c1", "room", "secret-key", "token");
|
|
105
|
-
cloud.close();
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
main().catch((err) => {
|
|
109
|
-
console.error(err);
|
|
110
|
-
process.exit(1);
|
|
111
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* HakoDB Node.js example (TypeScript SDK, koffi FFI backend).
|
|
3
|
+
*
|
|
4
|
+
* Setup & run (from the repository root):
|
|
5
|
+
*
|
|
6
|
+
* npm install # koffi + msgpack
|
|
7
|
+
* npm install --prefix examples/node # tsx for running TS
|
|
8
|
+
* ..\sync-native.ps1 -CoreDir C:\Dev\libs\firelite # -> native/hakodb.dll
|
|
9
|
+
* node --import tsx examples/node/index.ts # (or cd examples/node)
|
|
10
|
+
*
|
|
11
|
+
* You can also drop the --features and comment out the demoNetAndCloudSync()
|
|
12
|
+
* call if you only want the core CRUD/query part.
|
|
13
|
+
*/
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
16
|
+
import {
|
|
17
|
+
DurabilityMode,
|
|
18
|
+
HakoClient,
|
|
19
|
+
HakoConfig,
|
|
20
|
+
} from "../../src/index.ts";
|
|
21
|
+
|
|
22
|
+
const repoRoot = path.resolve(
|
|
23
|
+
path.dirname(fileURLToPath(import.meta.url)),
|
|
24
|
+
"..", ".."
|
|
25
|
+
);
|
|
26
|
+
const libName =
|
|
27
|
+
process.platform === "win32"
|
|
28
|
+
? "hakodb.dll"
|
|
29
|
+
: process.platform === "darwin"
|
|
30
|
+
? "libhakodb.dylib"
|
|
31
|
+
: "libhakodb.so";
|
|
32
|
+
const libraryPath = path.join(repoRoot, "native", libName);
|
|
33
|
+
|
|
34
|
+
async function main(): Promise<void> {
|
|
35
|
+
// ---- Open with a configuration (durability / tuning) ----
|
|
36
|
+
const config: HakoConfig = await HakoClient.createConfig(libraryPath);
|
|
37
|
+
config.setDurability(DurabilityMode.Always).setQueryWorkers(4);
|
|
38
|
+
const db = await HakoClient.open("demo.db", { config, libraryPath });
|
|
39
|
+
|
|
40
|
+
// ---- Write documents ----
|
|
41
|
+
await db.set("users", "u1", { name: "Alice", age: 32, active: true });
|
|
42
|
+
await db.set("users", "u2", { name: "Bob", age: 27, active: true });
|
|
43
|
+
console.log("inserted u1, u2");
|
|
44
|
+
|
|
45
|
+
// ---- Read a document ----
|
|
46
|
+
const snap = await db.get("users", "u1");
|
|
47
|
+
console.log("u1 ->", snap.exists ? snap.data() : "not found");
|
|
48
|
+
|
|
49
|
+
// ---- Query (firestore-style constraints) ----
|
|
50
|
+
const rows = await db
|
|
51
|
+
.collection("users")
|
|
52
|
+
.where("age", ">=", 27)
|
|
53
|
+
.orderBy("age", "desc")
|
|
54
|
+
.limit(10)
|
|
55
|
+
.get();
|
|
56
|
+
console.log("query(age>=27) ->", rows);
|
|
57
|
+
|
|
58
|
+
// ---- Aggregation ----
|
|
59
|
+
console.log(
|
|
60
|
+
"count(users) =",
|
|
61
|
+
await db.collection("users").count()
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
// ---- Atomic batch ----
|
|
65
|
+
const batch = db.batch();
|
|
66
|
+
batch.set(db.collection("users").doc("u3"), { name: "Carol", age: 41 });
|
|
67
|
+
batch.set(db.collection("users").doc("u4"), { name: "Dave", age: 50 });
|
|
68
|
+
await batch.commit();
|
|
69
|
+
console.log("batch committed (u3, u4)");
|
|
70
|
+
|
|
71
|
+
// ---- Real-time watch ----
|
|
72
|
+
const unsubscribe = db
|
|
73
|
+
.collection("users")
|
|
74
|
+
.onSnapshot((docs) => console.log("snapshot:", docs));
|
|
75
|
+
await db.set("users", "u5", { name: "Eve", age: 22 });
|
|
76
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
77
|
+
await unsubscribe();
|
|
78
|
+
|
|
79
|
+
await demoNetAndCloudSync(db);
|
|
80
|
+
|
|
81
|
+
await db.close();
|
|
82
|
+
console.log("done");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Requires the shared library built with --features net-sync,cloud-sync
|
|
86
|
+
* and, for NetSync, a Tokio host runtime (use hako-cli serve --net-sync
|
|
87
|
+
* for a working LAN mesh from other languages). */
|
|
88
|
+
async function demoNetAndCloudSync(db: HakoClient): Promise<void> {
|
|
89
|
+
const syncer = db.createNetSyncer("demo-room", "secret-key");
|
|
90
|
+
try {
|
|
91
|
+
await syncer.start(4456);
|
|
92
|
+
console.log("net sync status:", await syncer.status());
|
|
93
|
+
} catch (err) {
|
|
94
|
+
console.warn("net sync skipped:", err);
|
|
95
|
+
} finally {
|
|
96
|
+
await syncer.close();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Room-agnostic cloud SERVER: not bound to any room, hosts any (room, key).
|
|
100
|
+
const server = db.createCloudSyncServer("node-server", "server-token");
|
|
101
|
+
server.close();
|
|
102
|
+
|
|
103
|
+
// Offline-first cloud CLIENT: picks its own room + server.
|
|
104
|
+
const cloud = db.createCloudSyncClient("c1", "room", "secret-key", "token");
|
|
105
|
+
cloud.close();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
main().catch((err) => {
|
|
109
|
+
console.error(err);
|
|
110
|
+
process.exit(1);
|
|
111
|
+
});
|