@hakodb/client 0.5.12

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 HakoDB Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/LICENSE-MIT ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 HakoDB Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # hako-js
2
+
3
+ JavaScript/TypeScript SDK for HakoDB:
4
+ high-level client (`client.ts`: `HakoClient`, `HakoConfig`) and native
5
+ FFI bindings over koffi/Bun (`native.ts`). Tauri lives in `@hakodb/tauri`
6
+ (sibling dir, renaming with repo creation).
7
+
8
+ ## Compatibility
9
+
10
+ | hako-js | hako core |
11
+ |---|---|
12
+ | 0.5.12 | `cloud_sync` branch / `v0.8.21`+ release asset |
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
+ .\sync-native.ps1 -CoreDir C:\Dev\libs\firelite
22
+ .\sync-native.ps1 -Tag v0.8.21
23
+ ```
24
+
25
+ Binaries under `native/` are git-ignored.
@@ -0,0 +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
+ });
@@ -0,0 +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
+ }
@@ -0,0 +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
+ });