@thingd/thingdb 0.0.0-stage → 0.93.2

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 CHANGED
@@ -1,3 +1,40 @@
1
- # Temporary Holding Version
1
+ # @thingd/thingdb
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Standalone embedded ordered byte key/value storage for Node.js. The package
4
+ does not start Thingd or a network service.
5
+
6
+ Requires Node.js 16 or newer.
7
+
8
+ ```sh
9
+ npm install @thingd/thingdb
10
+ ```
11
+
12
+ ```js
13
+ import { ThingDB } from "@thingd/thingdb";
14
+
15
+ const db = new ThingDB("./data");
16
+ const users = db.keyspace("users");
17
+ await users.put(Buffer.from("alice"), Buffer.from('{"active":true}'));
18
+ const value = await users.get(Buffer.from("alice"));
19
+ await db.putMany([
20
+ { keyspace: "users", key: Buffer.from("bob"), value: Buffer.from("active") },
21
+ { keyspace: "users", key: Buffer.from("alice"), delete: true },
22
+ ]);
23
+ await db.persist();
24
+ db.close();
25
+ ```
26
+
27
+ Pass `":memory:"` for process-local storage that disappears when the process
28
+ exits. Filesystem paths use the durable engine. A durable database path is
29
+ owned by one process at a time; database operations are safe to call from
30
+ multiple threads. `putMany` is atomic across its keyspaces. Keys and values
31
+ are opaque bytes and scans return entries in byte order.
32
+ Methods accept `Buffer` or `Uint8Array` inputs and return byte buffers.
33
+
34
+ The on-disk format is experimental until ThingDB 1.0. Keep independent backups
35
+ and export/reimport logical records if a future format change requires it.
36
+ ThingDB is not SQL and this package does not include a standalone CLI.
37
+ Thingd's existing CLI remains the place for Thingd operation commands.
38
+
39
+ See the [ThingDB guide](https://engine.thingd.cloud/api-spec/thingdb) for the
40
+ Rust API and durability details.
package/index.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ /// Embedded byte key/value database. Durable paths allow one owning process.
2
+ export class ThingDB {
3
+ constructor(path: string);
4
+ keyspace(name: string): ThingDBKeyspace;
5
+ putMany(
6
+ operations: Array<{ keyspace: string; key: Uint8Array; value?: Uint8Array; delete?: boolean }>
7
+ ): Promise<void>;
8
+ persist(): Promise<void>;
9
+ compact(): Promise<void>;
10
+ snapshot(): Promise<ThingDBSnapshot>;
11
+ close(): void;
12
+ }
13
+
14
+ export class ThingDBSnapshot {
15
+ private constructor();
16
+ get(keyspace: string, key: Uint8Array): Uint8Array | null;
17
+ close(): void;
18
+ }
19
+
20
+ export class ThingDBKeyspace {
21
+ private constructor();
22
+ get(key: Uint8Array): Promise<Uint8Array | null>;
23
+ put(key: Uint8Array, value: Uint8Array): Promise<void>;
24
+ delete(key: Uint8Array): Promise<void>;
25
+ entries(): Promise<Array<{ key: Uint8Array; value: Uint8Array }>>;
26
+ prefix(prefix: Uint8Array): Promise<Array<{ key: Uint8Array; value: Uint8Array }>>;
27
+ }
28
+
29
+ export const loadedPath: string;
package/index.js ADDED
@@ -0,0 +1,158 @@
1
+ import { existsSync } from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import { dirname, join } from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+
6
+ const require = createRequire(import.meta.url);
7
+ const packageRoot = dirname(fileURLToPath(import.meta.url));
8
+ const localBuild = join(packageRoot, "dist", "thingdb_node.node");
9
+ const prebuild = join(
10
+ packageRoot,
11
+ "prebuilds",
12
+ `${process.platform}-${process.arch}`,
13
+ "thingdb_node.node"
14
+ );
15
+ const bindingPath = existsSync(localBuild) ? localBuild : prebuild;
16
+
17
+ if (!existsSync(bindingPath)) {
18
+ const error = new Error(
19
+ `No @thingd/thingdb binary is available for ${process.platform}-${process.arch}.`
20
+ );
21
+ error.code = "THINGDB_PREBUILD_MISSING";
22
+ throw error;
23
+ }
24
+
25
+ const binding = require(bindingPath);
26
+ const databaseHandles = new WeakMap();
27
+ const databaseFinalizer = new FinalizationRegistry((handle) => {
28
+ try {
29
+ binding.closeDatabase(handle);
30
+ } catch {
31
+ // The native registry may already have been cleared during process shutdown.
32
+ }
33
+ });
34
+ const snapshotFinalizer = new FinalizationRegistry((handle) => {
35
+ try {
36
+ binding.closeSnapshot(handle);
37
+ } catch {
38
+ // The native registry may already have been cleared during process shutdown.
39
+ }
40
+ });
41
+
42
+ export class ThingDB {
43
+ #finalizerToken = {};
44
+
45
+ constructor(path) {
46
+ const handle = binding.openDatabase(path);
47
+ databaseHandles.set(this, handle);
48
+ databaseFinalizer.register(this, handle, this.#finalizerToken);
49
+ }
50
+
51
+ keyspace(name) {
52
+ const handle = databaseHandle(this);
53
+ binding.createKeyspace(handle, name);
54
+ return new ThingDBKeyspace(this, name);
55
+ }
56
+
57
+ putMany(operations) {
58
+ return binding.putMany(
59
+ databaseHandle(this),
60
+ operations.map((operation) => ({
61
+ ...operation,
62
+ key: Buffer.from(operation.key),
63
+ value: operation.value === undefined ? undefined : Buffer.from(operation.value),
64
+ }))
65
+ );
66
+ }
67
+
68
+ persist() {
69
+ return binding.persist(databaseHandle(this));
70
+ }
71
+
72
+ compact() {
73
+ return binding.compact(databaseHandle(this));
74
+ }
75
+
76
+ async snapshot() {
77
+ return new ThingDBSnapshot(await binding.snapshot(databaseHandle(this)));
78
+ }
79
+
80
+ close() {
81
+ const handle = databaseHandle(this);
82
+ binding.closeDatabase(handle);
83
+ databaseHandles.delete(this);
84
+ databaseFinalizer.unregister(this.#finalizerToken);
85
+ }
86
+ }
87
+
88
+ export class ThingDBKeyspace {
89
+ #database;
90
+ #name;
91
+
92
+ constructor(database, name) {
93
+ this.#database = database;
94
+ this.#name = name;
95
+ }
96
+
97
+ get(key) {
98
+ return binding.get(databaseHandle(this.#database), this.#name, Buffer.from(key));
99
+ }
100
+
101
+ put(key, value) {
102
+ return binding.put(
103
+ databaseHandle(this.#database),
104
+ this.#name,
105
+ Buffer.from(key),
106
+ Buffer.from(value)
107
+ );
108
+ }
109
+
110
+ delete(key) {
111
+ return binding.delete(databaseHandle(this.#database), this.#name, Buffer.from(key));
112
+ }
113
+
114
+ entries() {
115
+ return binding.entries(databaseHandle(this.#database), this.#name);
116
+ }
117
+
118
+ prefix(prefix) {
119
+ return binding.prefix(databaseHandle(this.#database), this.#name, Buffer.from(prefix));
120
+ }
121
+ }
122
+
123
+ function databaseHandle(database) {
124
+ const handle = databaseHandles.get(database);
125
+ if (handle === undefined) {
126
+ throw new Error("ThingDB is closed");
127
+ }
128
+ return handle;
129
+ }
130
+
131
+ export class ThingDBSnapshot {
132
+ #handle;
133
+ #finalizerToken = {};
134
+
135
+ constructor(handle) {
136
+ this.#handle = handle;
137
+ snapshotFinalizer.register(this, handle, this.#finalizerToken);
138
+ }
139
+
140
+ get(keyspace, key) {
141
+ if (this.#handle === undefined) {
142
+ throw new Error("ThingDB snapshot is closed");
143
+ }
144
+ return binding.snapshotGet(this.#handle, keyspace, Buffer.from(key));
145
+ }
146
+
147
+ close() {
148
+ if (this.#handle === undefined) {
149
+ return;
150
+ }
151
+ const handle = this.#handle;
152
+ binding.closeSnapshot(handle);
153
+ this.#handle = undefined;
154
+ snapshotFinalizer.unregister(this.#finalizerToken);
155
+ }
156
+ }
157
+
158
+ export const loadedPath = bindingPath;
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
2
  "name": "@thingd/thingdb",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.93.2",
4
+ "description": "Embedded byte key-value database for Node.js, backed by ThingDB.",
5
+ "type": "module",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "homepage": "https://engine.thingd.cloud/api-spec/thingdb",
10
+ "author": "Sayan Mohsin",
11
+ "license": "Apache-2.0",
12
+ "engines": {
13
+ "node": ">=16.0.0"
14
+ },
15
+ "keywords": [
16
+ "thingdb",
17
+ "embedded",
18
+ "key-value",
19
+ "database",
20
+ "napi-rs"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/sayanmohsin/thingd.git"
25
+ },
26
+ "main": "./index.js",
27
+ "types": "./index.d.ts",
28
+ "files": [
29
+ "prebuilds",
30
+ "index.js",
31
+ "index.d.ts",
32
+ "README.md"
33
+ ],
34
+ "scripts": {
35
+ "build": "node scripts/build-native.mjs",
36
+ "build:prebuild": "node scripts/copy-prebuild.mjs",
37
+ "prepack": "node scripts/validate-package.mjs"
38
+ }
39
+ }