taladb 0.1.1

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) 2025 thinkgrid-labs
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,150 @@
1
+ # taladb
2
+
3
+ Local-first document database for React, React Native, and Node.js — powered by a Rust/WASM core with zero GC pauses.
4
+
5
+ [![npm](https://img.shields.io/npm/v/taladb)](https://www.npmjs.com/package/taladb)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-orange.svg)](LICENSE)
7
+
8
+ ## What is TalaDB?
9
+
10
+ TalaDB gives you a MongoDB-style document API that runs entirely on-device — in the browser via WASM + OPFS, in React Native via JSI, and in Node.js via a native module. No cloud, no sync server, no garbage-collection pauses.
11
+
12
+ ```ts
13
+ import { openDB } from 'taladb';
14
+
15
+ const db = await openDB('myapp.db');
16
+ const users = db.collection('users');
17
+
18
+ await users.createIndex('email');
19
+
20
+ const id = await users.insert({ name: 'Alice', email: 'alice@example.com', age: 30 });
21
+
22
+ const alice = await users.findOne({ email: 'alice@example.com' });
23
+ const adults = await users.find({ age: { $gte: 18 } });
24
+
25
+ await users.updateOne({ email: 'alice@example.com' }, { $inc: { age: 1 } });
26
+ await users.deleteOne({ email: 'alice@example.com' });
27
+
28
+ await db.close();
29
+ ```
30
+
31
+ ## Installation
32
+
33
+ ```bash
34
+ # npm
35
+ npm install taladb
36
+
37
+ # pnpm
38
+ pnpm add taladb
39
+
40
+ # yarn
41
+ yarn add taladb
42
+ ```
43
+
44
+ The package automatically loads the right backend for your platform:
45
+
46
+ | Platform | Backend |
47
+ |----------|---------|
48
+ | Browser | `@taladb/web` (WASM + OPFS) |
49
+ | Node.js | `@taladb/node` (napi-rs native module) |
50
+ | React Native | `@taladb/react-native` (JSI TurboModule) |
51
+
52
+ Install the appropriate platform package as well:
53
+
54
+ ```bash
55
+ # Browser
56
+ pnpm add taladb @taladb/web
57
+
58
+ # Node.js
59
+ pnpm add taladb @taladb/node
60
+
61
+ # React Native
62
+ pnpm add taladb @taladb/react-native
63
+ ```
64
+
65
+ ## API
66
+
67
+ ### Database
68
+
69
+ ```ts
70
+ const db = await openDB(name?: string): Promise<TalaDB>
71
+
72
+ db.collection<T>(name: string): Collection<T>
73
+ db.close(): Promise<void>
74
+ ```
75
+
76
+ ### Collection
77
+
78
+ ```ts
79
+ // Write
80
+ col.insert(doc): Promise<string> // returns ULID id
81
+ col.insertMany(docs): Promise<string[]>
82
+ col.updateOne(filter, update): Promise<boolean>
83
+ col.updateMany(filter, update): Promise<number>
84
+ col.deleteOne(filter): Promise<boolean>
85
+ col.deleteMany(filter): Promise<number>
86
+
87
+ // Read
88
+ col.find(filter?): Promise<T[]>
89
+ col.findOne(filter): Promise<T | null>
90
+ col.count(filter?): Promise<number>
91
+
92
+ // Indexes
93
+ col.createIndex(field): Promise<void> // idempotent
94
+ col.dropIndex(field): Promise<void>
95
+ ```
96
+
97
+ ### Filters
98
+
99
+ ```ts
100
+ { field: value } // equality
101
+ { field: { $eq, $ne, $gt, $gte, $lt, $lte } } // comparisons
102
+ { field: { $in: [...], $nin: [...] } } // set membership
103
+ { field: { $exists: true | false } } // field presence
104
+ { $and: [...filters] }
105
+ { $or: [...filters] }
106
+ { $not: filter }
107
+ ```
108
+
109
+ ### Updates
110
+
111
+ ```ts
112
+ { $set: { field: value } } // set or replace
113
+ { $unset: { field: true } } // remove field
114
+ { $inc: { field: number } } // increment / decrement
115
+ { $push: { field: value } } // append to array
116
+ { $pull: { field: value } } // remove from array
117
+ ```
118
+
119
+ ## Migrations
120
+
121
+ Run schema migrations at open time — each migration runs exactly once, in version order:
122
+
123
+ ```ts
124
+ import { runMigrations } from 'taladb';
125
+
126
+ await runMigrations(db, [
127
+ {
128
+ fromVersion: 0,
129
+ toVersion: 1,
130
+ description: 'add default role',
131
+ async up(col) {
132
+ await col('users').updateMany({}, { $set: { role: 'viewer' } });
133
+ },
134
+ },
135
+ ]);
136
+ ```
137
+
138
+ ## Full Documentation
139
+
140
+ **[https://thinkgrid-labs.github.io/taladb](https://thinkgrid-labs.github.io/taladb)**
141
+
142
+ - [Introduction](https://thinkgrid-labs.github.io/taladb/introduction)
143
+ - [Web (Browser / WASM) Guide](https://thinkgrid-labs.github.io/taladb/guide/web)
144
+ - [Node.js Guide](https://thinkgrid-labs.github.io/taladb/guide/node)
145
+ - [React Native Guide](https://thinkgrid-labs.github.io/taladb/guide/react-native)
146
+ - [API Reference](https://thinkgrid-labs.github.io/taladb/api/collection)
147
+
148
+ ## License
149
+
150
+ MIT © [ThinkGrid Labs](https://github.com/thinkgrid-labs)
@@ -0,0 +1,91 @@
1
+ type Value = null | boolean | number | string | Uint8Array | Value[] | {
2
+ [key: string]: Value;
3
+ };
4
+ type Document = {
5
+ _id?: string;
6
+ [key: string]: Value | undefined;
7
+ };
8
+ type FieldOps<T> = T extends null | undefined ? {
9
+ $exists?: boolean;
10
+ } : {
11
+ $eq?: T;
12
+ $ne?: T;
13
+ $gt?: T;
14
+ $gte?: T;
15
+ $lt?: T;
16
+ $lte?: T;
17
+ $in?: T[];
18
+ $nin?: T[];
19
+ $exists?: boolean;
20
+ /** Full-text search: matches documents where this string field contains the given token. */
21
+ $contains?: string;
22
+ };
23
+ type Filter<T extends Document = Document> = {
24
+ [K in keyof T]?: T[K] | FieldOps<T[K]>;
25
+ } & {
26
+ $and?: Filter<T>[];
27
+ $or?: Filter<T>[];
28
+ $not?: Filter<T>;
29
+ };
30
+ type Update<T extends Document = Document> = {
31
+ $set?: Partial<T>;
32
+ $unset?: {
33
+ [K in keyof T]?: true;
34
+ };
35
+ $inc?: {
36
+ [K in keyof T]?: number;
37
+ };
38
+ $push?: {
39
+ [K in keyof T]?: Value;
40
+ };
41
+ $pull?: {
42
+ [K in keyof T]?: Value;
43
+ };
44
+ };
45
+ interface Collection<T extends Document = Document> {
46
+ insert(doc: Omit<T, '_id'>): Promise<string>;
47
+ insertMany(docs: Omit<T, '_id'>[]): Promise<string[]>;
48
+ find(filter?: Filter<T>): Promise<T[]>;
49
+ findOne(filter: Filter<T>): Promise<T | null>;
50
+ updateOne(filter: Filter<T>, update: Update<T>): Promise<boolean>;
51
+ updateMany(filter: Filter<T>, update: Update<T>): Promise<number>;
52
+ deleteOne(filter: Filter<T>): Promise<boolean>;
53
+ deleteMany(filter: Filter<T>): Promise<number>;
54
+ count(filter?: Filter<T>): Promise<number>;
55
+ createIndex(field: keyof Omit<T, '_id'> & string): Promise<void>;
56
+ dropIndex(field: keyof Omit<T, '_id'> & string): Promise<void>;
57
+ /**
58
+ * Subscribe to live query results. The callback receives a full snapshot of
59
+ * matching documents immediately and again after every write that could
60
+ * affect the result set.
61
+ *
62
+ * @returns An unsubscribe function. Call it to stop receiving updates.
63
+ *
64
+ * @example
65
+ * const unsub = users.subscribe({ active: true }, (docs) => {
66
+ * console.log('active users:', docs);
67
+ * });
68
+ * // later…
69
+ * unsub();
70
+ */
71
+ subscribe(filter: Filter<T>, callback: (docs: T[]) => void): () => void;
72
+ }
73
+ interface TalaDB {
74
+ collection<T extends Document = Document>(name: string): Collection<T>;
75
+ close(): Promise<void>;
76
+ }
77
+
78
+ /**
79
+ * Open a TalaDB database.
80
+ *
81
+ * @param dbName Name of the database file (used for OPFS and native file paths).
82
+ * Ignored for in-memory databases.
83
+ *
84
+ * @example
85
+ * const db = await openDB('myapp.db');
86
+ * const users = db.collection<User>('users');
87
+ * const id = await users.insert({ name: 'Alice', age: 30 });
88
+ */
89
+ declare function openDB(dbName?: string): Promise<TalaDB>;
90
+
91
+ export { type Collection, type Document, type Filter, type TalaDB, type Update, type Value, openDB };
@@ -0,0 +1,91 @@
1
+ type Value = null | boolean | number | string | Uint8Array | Value[] | {
2
+ [key: string]: Value;
3
+ };
4
+ type Document = {
5
+ _id?: string;
6
+ [key: string]: Value | undefined;
7
+ };
8
+ type FieldOps<T> = T extends null | undefined ? {
9
+ $exists?: boolean;
10
+ } : {
11
+ $eq?: T;
12
+ $ne?: T;
13
+ $gt?: T;
14
+ $gte?: T;
15
+ $lt?: T;
16
+ $lte?: T;
17
+ $in?: T[];
18
+ $nin?: T[];
19
+ $exists?: boolean;
20
+ /** Full-text search: matches documents where this string field contains the given token. */
21
+ $contains?: string;
22
+ };
23
+ type Filter<T extends Document = Document> = {
24
+ [K in keyof T]?: T[K] | FieldOps<T[K]>;
25
+ } & {
26
+ $and?: Filter<T>[];
27
+ $or?: Filter<T>[];
28
+ $not?: Filter<T>;
29
+ };
30
+ type Update<T extends Document = Document> = {
31
+ $set?: Partial<T>;
32
+ $unset?: {
33
+ [K in keyof T]?: true;
34
+ };
35
+ $inc?: {
36
+ [K in keyof T]?: number;
37
+ };
38
+ $push?: {
39
+ [K in keyof T]?: Value;
40
+ };
41
+ $pull?: {
42
+ [K in keyof T]?: Value;
43
+ };
44
+ };
45
+ interface Collection<T extends Document = Document> {
46
+ insert(doc: Omit<T, '_id'>): Promise<string>;
47
+ insertMany(docs: Omit<T, '_id'>[]): Promise<string[]>;
48
+ find(filter?: Filter<T>): Promise<T[]>;
49
+ findOne(filter: Filter<T>): Promise<T | null>;
50
+ updateOne(filter: Filter<T>, update: Update<T>): Promise<boolean>;
51
+ updateMany(filter: Filter<T>, update: Update<T>): Promise<number>;
52
+ deleteOne(filter: Filter<T>): Promise<boolean>;
53
+ deleteMany(filter: Filter<T>): Promise<number>;
54
+ count(filter?: Filter<T>): Promise<number>;
55
+ createIndex(field: keyof Omit<T, '_id'> & string): Promise<void>;
56
+ dropIndex(field: keyof Omit<T, '_id'> & string): Promise<void>;
57
+ /**
58
+ * Subscribe to live query results. The callback receives a full snapshot of
59
+ * matching documents immediately and again after every write that could
60
+ * affect the result set.
61
+ *
62
+ * @returns An unsubscribe function. Call it to stop receiving updates.
63
+ *
64
+ * @example
65
+ * const unsub = users.subscribe({ active: true }, (docs) => {
66
+ * console.log('active users:', docs);
67
+ * });
68
+ * // later…
69
+ * unsub();
70
+ */
71
+ subscribe(filter: Filter<T>, callback: (docs: T[]) => void): () => void;
72
+ }
73
+ interface TalaDB {
74
+ collection<T extends Document = Document>(name: string): Collection<T>;
75
+ close(): Promise<void>;
76
+ }
77
+
78
+ /**
79
+ * Open a TalaDB database.
80
+ *
81
+ * @param dbName Name of the database file (used for OPFS and native file paths).
82
+ * Ignored for in-memory databases.
83
+ *
84
+ * @example
85
+ * const db = await openDB('myapp.db');
86
+ * const users = db.collection<User>('users');
87
+ * const id = await users.insert({ name: 'Alice', age: 30 });
88
+ */
89
+ declare function openDB(dbName?: string): Promise<TalaDB>;
90
+
91
+ export { type Collection, type Document, type Filter, type TalaDB, type Update, type Value, openDB };
package/dist/index.js ADDED
@@ -0,0 +1,312 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ openDB: () => openDB
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var import_meta = {};
37
+ function detectPlatform() {
38
+ if (globalThis.nativeCallSyncHook !== void 0) {
39
+ return "react-native";
40
+ }
41
+ if (globalThis.window !== void 0 && typeof navigator !== "undefined") {
42
+ return "browser";
43
+ }
44
+ return "node";
45
+ }
46
+ var WorkerProxy = class {
47
+ constructor(port) {
48
+ this.pending = /* @__PURE__ */ new Map();
49
+ this.nextId = 1;
50
+ this.port = port;
51
+ this.port.onmessage = (e) => {
52
+ const { id, result, error } = e.data;
53
+ const p = this.pending.get(id);
54
+ if (p) {
55
+ this.pending.delete(id);
56
+ if (error === void 0) p.resolve(result);
57
+ else p.reject(new Error(error));
58
+ }
59
+ };
60
+ this.port.start();
61
+ }
62
+ send(op, args = {}) {
63
+ return new Promise((resolve, reject) => {
64
+ const id = this.nextId++;
65
+ this.pending.set(id, { resolve, reject });
66
+ this.port.postMessage({ id, op, ...args });
67
+ });
68
+ }
69
+ };
70
+ async function createInMemoryBrowserDB(_dbName) {
71
+ const wasmUrl = new URL("@taladb/web/pkg/taladb_wasm.js", import_meta.url);
72
+ const wasm = await import(
73
+ /* @vite-ignore */
74
+ wasmUrl.href
75
+ );
76
+ await wasm.default();
77
+ const db = wasm.TalaDBWasm.openInMemory();
78
+ function wrapCollection(name) {
79
+ const col = db.collection(name);
80
+ return {
81
+ insert: async (doc) => col.insert(doc),
82
+ insertMany: async (docs) => col.insertMany(docs),
83
+ find: async (filter) => col.find(filter ?? null),
84
+ findOne: async (filter) => col.findOne(filter) ?? null,
85
+ updateOne: async (filter, update) => col.updateOne(filter, update),
86
+ updateMany: async (filter, update) => col.updateMany(filter, update),
87
+ deleteOne: async (filter) => col.deleteOne(filter),
88
+ deleteMany: async (filter) => col.deleteMany(filter),
89
+ count: async (filter) => col.count(filter ?? null),
90
+ createIndex: async (field) => col.createIndex(field),
91
+ dropIndex: async (field) => col.dropIndex(field),
92
+ subscribe: (filter, callback) => {
93
+ let active = true;
94
+ let lastJson = "";
95
+ const poll = async () => {
96
+ if (!active) return;
97
+ try {
98
+ const docs = await col.find(filter ?? null);
99
+ const json = JSON.stringify(docs);
100
+ if (json !== lastJson) {
101
+ lastJson = json;
102
+ callback(docs);
103
+ }
104
+ } catch {
105
+ }
106
+ if (active) setTimeout(poll, 300);
107
+ };
108
+ poll();
109
+ return () => {
110
+ active = false;
111
+ };
112
+ }
113
+ };
114
+ }
115
+ return {
116
+ collection: (name) => wrapCollection(name),
117
+ close: async () => {
118
+ }
119
+ };
120
+ }
121
+ async function createBrowserDB(dbName) {
122
+ if (typeof SharedWorker === "undefined") {
123
+ return createInMemoryBrowserDB(dbName);
124
+ }
125
+ const workerUrl = new URL("@taladb/web/worker/taladb.worker.js", import_meta.url);
126
+ const worker = new SharedWorker(workerUrl, { type: "module", name: "taladb" });
127
+ const proxy = new WorkerProxy(worker.port);
128
+ await proxy.send("init", { dbName });
129
+ function wrapCollection(name) {
130
+ const s = JSON.stringify;
131
+ return {
132
+ insert: (doc) => proxy.send("insert", { collection: name, docJson: s(doc) }),
133
+ insertMany: async (docs) => {
134
+ const json = await proxy.send("insertMany", {
135
+ collection: name,
136
+ docsJson: s(docs)
137
+ });
138
+ return JSON.parse(json);
139
+ },
140
+ find: async (filter) => {
141
+ const json = await proxy.send("find", {
142
+ collection: name,
143
+ filterJson: filter ? s(filter) : "null"
144
+ });
145
+ return JSON.parse(json);
146
+ },
147
+ findOne: async (filter) => {
148
+ const json = await proxy.send("findOne", {
149
+ collection: name,
150
+ filterJson: filter ? s(filter) : "null"
151
+ });
152
+ return JSON.parse(json);
153
+ },
154
+ updateOne: (filter, update) => proxy.send("updateOne", {
155
+ collection: name,
156
+ filterJson: s(filter),
157
+ updateJson: s(update)
158
+ }),
159
+ updateMany: (filter, update) => proxy.send("updateMany", {
160
+ collection: name,
161
+ filterJson: s(filter),
162
+ updateJson: s(update)
163
+ }),
164
+ deleteOne: (filter) => proxy.send("deleteOne", { collection: name, filterJson: s(filter) }),
165
+ deleteMany: (filter) => proxy.send("deleteMany", { collection: name, filterJson: s(filter) }),
166
+ count: (filter) => proxy.send("count", {
167
+ collection: name,
168
+ filterJson: filter ? s(filter) : "null"
169
+ }),
170
+ createIndex: (field) => proxy.send("createIndex", { collection: name, field }),
171
+ dropIndex: (field) => proxy.send("dropIndex", { collection: name, field }),
172
+ subscribe: (filter, callback) => {
173
+ let active = true;
174
+ let lastJson = "";
175
+ const poll = async () => {
176
+ if (!active) return;
177
+ try {
178
+ const json = await proxy.send("find", {
179
+ collection: name,
180
+ filterJson: filter ? s(filter) : "null"
181
+ });
182
+ if (json !== lastJson) {
183
+ lastJson = json;
184
+ callback(JSON.parse(json));
185
+ }
186
+ } catch {
187
+ }
188
+ if (active) setTimeout(poll, 300);
189
+ };
190
+ poll();
191
+ return () => {
192
+ active = false;
193
+ };
194
+ }
195
+ };
196
+ }
197
+ return {
198
+ collection: (name) => wrapCollection(name),
199
+ close: () => proxy.send("close")
200
+ };
201
+ }
202
+ async function createNodeDB(dbName) {
203
+ const { TalaDBNode } = await import("@taladb/node");
204
+ const db = TalaDBNode.open(dbName);
205
+ function wrapCollection(name) {
206
+ const col = db.collection(name);
207
+ return {
208
+ insert: async (doc) => col.insert(doc),
209
+ insertMany: async (docs) => col.insertMany(docs),
210
+ find: async (filter) => col.find(filter ?? null),
211
+ findOne: async (filter) => col.findOne(filter) ?? null,
212
+ updateOne: async (filter, update) => col.updateOne(filter, update),
213
+ updateMany: async (filter, update) => col.updateMany(filter, update),
214
+ deleteOne: async (filter) => col.deleteOne(filter),
215
+ deleteMany: async (filter) => col.deleteMany(filter),
216
+ count: async (filter) => col.count(filter ?? null),
217
+ createIndex: async (field) => col.createIndex(field),
218
+ dropIndex: async (field) => col.dropIndex(field),
219
+ subscribe: (filter, callback) => {
220
+ let active = true;
221
+ let lastJson = "";
222
+ const poll = async () => {
223
+ if (!active) return;
224
+ try {
225
+ const docs = await col.find(filter ?? null);
226
+ const json = JSON.stringify(docs);
227
+ if (json !== lastJson) {
228
+ lastJson = json;
229
+ callback(docs);
230
+ }
231
+ } catch {
232
+ }
233
+ if (active) setTimeout(poll, 300);
234
+ };
235
+ poll();
236
+ return () => {
237
+ active = false;
238
+ };
239
+ }
240
+ };
241
+ }
242
+ return {
243
+ collection: (name) => wrapCollection(name),
244
+ close: async () => {
245
+ }
246
+ };
247
+ }
248
+ async function createNativeDB(_dbName) {
249
+ const maybeNative = globalThis.__TalaDB__;
250
+ if (!maybeNative) {
251
+ throw new Error(
252
+ "@taladb/react-native JSI HostObject not found. Did you call TalaDBModule.initialize() in your app entry point?"
253
+ );
254
+ }
255
+ const native = maybeNative;
256
+ function wrapCollection(name) {
257
+ const col = native.collection(name);
258
+ return {
259
+ insert: async (doc) => col.insert(doc),
260
+ insertMany: async (docs) => col.insertMany(docs),
261
+ find: async (filter) => col.find(filter ?? {}),
262
+ findOne: async (filter) => col.findOne(filter ?? {}),
263
+ updateOne: async (filter, update) => col.updateOne(filter, update),
264
+ updateMany: async (filter, update) => col.updateMany(filter, update),
265
+ deleteOne: async (filter) => col.deleteOne(filter),
266
+ deleteMany: async (filter) => col.deleteMany(filter),
267
+ count: async (filter) => col.count(filter ?? {}),
268
+ createIndex: async (field) => col.createIndex(field),
269
+ dropIndex: async (field) => col.dropIndex(field),
270
+ subscribe: (filter, callback) => {
271
+ let active = true;
272
+ let lastJson = "";
273
+ const poll = () => {
274
+ if (!active) return;
275
+ try {
276
+ const docs = col.find(filter ?? {});
277
+ const json = JSON.stringify(docs);
278
+ if (json !== lastJson) {
279
+ lastJson = json;
280
+ callback(docs);
281
+ }
282
+ } catch {
283
+ }
284
+ if (active) setTimeout(poll, 300);
285
+ };
286
+ poll();
287
+ return () => {
288
+ active = false;
289
+ };
290
+ }
291
+ };
292
+ }
293
+ return {
294
+ collection: (name) => wrapCollection(name),
295
+ close: async () => native.close()
296
+ };
297
+ }
298
+ async function openDB(dbName = "taladb.db") {
299
+ const platform = detectPlatform();
300
+ switch (platform) {
301
+ case "browser":
302
+ return createBrowserDB(dbName);
303
+ case "react-native":
304
+ return createNativeDB(dbName);
305
+ case "node":
306
+ return createNodeDB(dbName);
307
+ }
308
+ }
309
+ // Annotate the CommonJS export names for ESM import in node:
310
+ 0 && (module.exports = {
311
+ openDB
312
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,276 @@
1
+ // src/index.ts
2
+ function detectPlatform() {
3
+ if (globalThis.nativeCallSyncHook !== void 0) {
4
+ return "react-native";
5
+ }
6
+ if (globalThis.window !== void 0 && typeof navigator !== "undefined") {
7
+ return "browser";
8
+ }
9
+ return "node";
10
+ }
11
+ var WorkerProxy = class {
12
+ constructor(port) {
13
+ this.pending = /* @__PURE__ */ new Map();
14
+ this.nextId = 1;
15
+ this.port = port;
16
+ this.port.onmessage = (e) => {
17
+ const { id, result, error } = e.data;
18
+ const p = this.pending.get(id);
19
+ if (p) {
20
+ this.pending.delete(id);
21
+ if (error === void 0) p.resolve(result);
22
+ else p.reject(new Error(error));
23
+ }
24
+ };
25
+ this.port.start();
26
+ }
27
+ send(op, args = {}) {
28
+ return new Promise((resolve, reject) => {
29
+ const id = this.nextId++;
30
+ this.pending.set(id, { resolve, reject });
31
+ this.port.postMessage({ id, op, ...args });
32
+ });
33
+ }
34
+ };
35
+ async function createInMemoryBrowserDB(_dbName) {
36
+ const wasmUrl = new URL("@taladb/web/pkg/taladb_wasm.js", import.meta.url);
37
+ const wasm = await import(
38
+ /* @vite-ignore */
39
+ wasmUrl.href
40
+ );
41
+ await wasm.default();
42
+ const db = wasm.TalaDBWasm.openInMemory();
43
+ function wrapCollection(name) {
44
+ const col = db.collection(name);
45
+ return {
46
+ insert: async (doc) => col.insert(doc),
47
+ insertMany: async (docs) => col.insertMany(docs),
48
+ find: async (filter) => col.find(filter ?? null),
49
+ findOne: async (filter) => col.findOne(filter) ?? null,
50
+ updateOne: async (filter, update) => col.updateOne(filter, update),
51
+ updateMany: async (filter, update) => col.updateMany(filter, update),
52
+ deleteOne: async (filter) => col.deleteOne(filter),
53
+ deleteMany: async (filter) => col.deleteMany(filter),
54
+ count: async (filter) => col.count(filter ?? null),
55
+ createIndex: async (field) => col.createIndex(field),
56
+ dropIndex: async (field) => col.dropIndex(field),
57
+ subscribe: (filter, callback) => {
58
+ let active = true;
59
+ let lastJson = "";
60
+ const poll = async () => {
61
+ if (!active) return;
62
+ try {
63
+ const docs = await col.find(filter ?? null);
64
+ const json = JSON.stringify(docs);
65
+ if (json !== lastJson) {
66
+ lastJson = json;
67
+ callback(docs);
68
+ }
69
+ } catch {
70
+ }
71
+ if (active) setTimeout(poll, 300);
72
+ };
73
+ poll();
74
+ return () => {
75
+ active = false;
76
+ };
77
+ }
78
+ };
79
+ }
80
+ return {
81
+ collection: (name) => wrapCollection(name),
82
+ close: async () => {
83
+ }
84
+ };
85
+ }
86
+ async function createBrowserDB(dbName) {
87
+ if (typeof SharedWorker === "undefined") {
88
+ return createInMemoryBrowserDB(dbName);
89
+ }
90
+ const workerUrl = new URL("@taladb/web/worker/taladb.worker.js", import.meta.url);
91
+ const worker = new SharedWorker(workerUrl, { type: "module", name: "taladb" });
92
+ const proxy = new WorkerProxy(worker.port);
93
+ await proxy.send("init", { dbName });
94
+ function wrapCollection(name) {
95
+ const s = JSON.stringify;
96
+ return {
97
+ insert: (doc) => proxy.send("insert", { collection: name, docJson: s(doc) }),
98
+ insertMany: async (docs) => {
99
+ const json = await proxy.send("insertMany", {
100
+ collection: name,
101
+ docsJson: s(docs)
102
+ });
103
+ return JSON.parse(json);
104
+ },
105
+ find: async (filter) => {
106
+ const json = await proxy.send("find", {
107
+ collection: name,
108
+ filterJson: filter ? s(filter) : "null"
109
+ });
110
+ return JSON.parse(json);
111
+ },
112
+ findOne: async (filter) => {
113
+ const json = await proxy.send("findOne", {
114
+ collection: name,
115
+ filterJson: filter ? s(filter) : "null"
116
+ });
117
+ return JSON.parse(json);
118
+ },
119
+ updateOne: (filter, update) => proxy.send("updateOne", {
120
+ collection: name,
121
+ filterJson: s(filter),
122
+ updateJson: s(update)
123
+ }),
124
+ updateMany: (filter, update) => proxy.send("updateMany", {
125
+ collection: name,
126
+ filterJson: s(filter),
127
+ updateJson: s(update)
128
+ }),
129
+ deleteOne: (filter) => proxy.send("deleteOne", { collection: name, filterJson: s(filter) }),
130
+ deleteMany: (filter) => proxy.send("deleteMany", { collection: name, filterJson: s(filter) }),
131
+ count: (filter) => proxy.send("count", {
132
+ collection: name,
133
+ filterJson: filter ? s(filter) : "null"
134
+ }),
135
+ createIndex: (field) => proxy.send("createIndex", { collection: name, field }),
136
+ dropIndex: (field) => proxy.send("dropIndex", { collection: name, field }),
137
+ subscribe: (filter, callback) => {
138
+ let active = true;
139
+ let lastJson = "";
140
+ const poll = async () => {
141
+ if (!active) return;
142
+ try {
143
+ const json = await proxy.send("find", {
144
+ collection: name,
145
+ filterJson: filter ? s(filter) : "null"
146
+ });
147
+ if (json !== lastJson) {
148
+ lastJson = json;
149
+ callback(JSON.parse(json));
150
+ }
151
+ } catch {
152
+ }
153
+ if (active) setTimeout(poll, 300);
154
+ };
155
+ poll();
156
+ return () => {
157
+ active = false;
158
+ };
159
+ }
160
+ };
161
+ }
162
+ return {
163
+ collection: (name) => wrapCollection(name),
164
+ close: () => proxy.send("close")
165
+ };
166
+ }
167
+ async function createNodeDB(dbName) {
168
+ const { TalaDBNode } = await import("@taladb/node");
169
+ const db = TalaDBNode.open(dbName);
170
+ function wrapCollection(name) {
171
+ const col = db.collection(name);
172
+ return {
173
+ insert: async (doc) => col.insert(doc),
174
+ insertMany: async (docs) => col.insertMany(docs),
175
+ find: async (filter) => col.find(filter ?? null),
176
+ findOne: async (filter) => col.findOne(filter) ?? null,
177
+ updateOne: async (filter, update) => col.updateOne(filter, update),
178
+ updateMany: async (filter, update) => col.updateMany(filter, update),
179
+ deleteOne: async (filter) => col.deleteOne(filter),
180
+ deleteMany: async (filter) => col.deleteMany(filter),
181
+ count: async (filter) => col.count(filter ?? null),
182
+ createIndex: async (field) => col.createIndex(field),
183
+ dropIndex: async (field) => col.dropIndex(field),
184
+ subscribe: (filter, callback) => {
185
+ let active = true;
186
+ let lastJson = "";
187
+ const poll = async () => {
188
+ if (!active) return;
189
+ try {
190
+ const docs = await col.find(filter ?? null);
191
+ const json = JSON.stringify(docs);
192
+ if (json !== lastJson) {
193
+ lastJson = json;
194
+ callback(docs);
195
+ }
196
+ } catch {
197
+ }
198
+ if (active) setTimeout(poll, 300);
199
+ };
200
+ poll();
201
+ return () => {
202
+ active = false;
203
+ };
204
+ }
205
+ };
206
+ }
207
+ return {
208
+ collection: (name) => wrapCollection(name),
209
+ close: async () => {
210
+ }
211
+ };
212
+ }
213
+ async function createNativeDB(_dbName) {
214
+ const maybeNative = globalThis.__TalaDB__;
215
+ if (!maybeNative) {
216
+ throw new Error(
217
+ "@taladb/react-native JSI HostObject not found. Did you call TalaDBModule.initialize() in your app entry point?"
218
+ );
219
+ }
220
+ const native = maybeNative;
221
+ function wrapCollection(name) {
222
+ const col = native.collection(name);
223
+ return {
224
+ insert: async (doc) => col.insert(doc),
225
+ insertMany: async (docs) => col.insertMany(docs),
226
+ find: async (filter) => col.find(filter ?? {}),
227
+ findOne: async (filter) => col.findOne(filter ?? {}),
228
+ updateOne: async (filter, update) => col.updateOne(filter, update),
229
+ updateMany: async (filter, update) => col.updateMany(filter, update),
230
+ deleteOne: async (filter) => col.deleteOne(filter),
231
+ deleteMany: async (filter) => col.deleteMany(filter),
232
+ count: async (filter) => col.count(filter ?? {}),
233
+ createIndex: async (field) => col.createIndex(field),
234
+ dropIndex: async (field) => col.dropIndex(field),
235
+ subscribe: (filter, callback) => {
236
+ let active = true;
237
+ let lastJson = "";
238
+ const poll = () => {
239
+ if (!active) return;
240
+ try {
241
+ const docs = col.find(filter ?? {});
242
+ const json = JSON.stringify(docs);
243
+ if (json !== lastJson) {
244
+ lastJson = json;
245
+ callback(docs);
246
+ }
247
+ } catch {
248
+ }
249
+ if (active) setTimeout(poll, 300);
250
+ };
251
+ poll();
252
+ return () => {
253
+ active = false;
254
+ };
255
+ }
256
+ };
257
+ }
258
+ return {
259
+ collection: (name) => wrapCollection(name),
260
+ close: async () => native.close()
261
+ };
262
+ }
263
+ async function openDB(dbName = "taladb.db") {
264
+ const platform = detectPlatform();
265
+ switch (platform) {
266
+ case "browser":
267
+ return createBrowserDB(dbName);
268
+ case "react-native":
269
+ return createNativeDB(dbName);
270
+ case "node":
271
+ return createNodeDB(dbName);
272
+ }
273
+ }
274
+ export {
275
+ openDB
276
+ };
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "taladb",
3
+ "version": "0.1.1",
4
+ "description": "Local-first document database for React, React Native, and Node.js",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "sideEffects": false,
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.mjs",
13
+ "require": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "keywords": [
20
+ "database",
21
+ "local-first",
22
+ "offline",
23
+ "wasm",
24
+ "react-native"
25
+ ],
26
+ "license": "MIT",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/thinkgrid-labs/taladb.git",
30
+ "directory": "packages/taladb"
31
+ },
32
+ "homepage": "https://thinkgrid-labs.github.io/taladb/",
33
+ "bugs": {
34
+ "url": "https://github.com/thinkgrid-labs/taladb/issues"
35
+ },
36
+ "engines": {
37
+ "node": ">=18"
38
+ },
39
+ "devDependencies": {
40
+ "tsup": "^8.0.0",
41
+ "typescript": "^5.9.3",
42
+ "vitest": "^3.2.0"
43
+ },
44
+ "optionalDependencies": {
45
+ "@taladb/web": "0.1.1",
46
+ "@taladb/node": "0.1.1"
47
+ },
48
+ "scripts": {
49
+ "build": "tsup src/index.ts --format cjs,esm --dts --out-dir dist --external @taladb/web --external @taladb/node --external @taladb/react-native",
50
+ "typecheck": "tsc --noEmit",
51
+ "test": "vitest run",
52
+ "test:watch": "vitest"
53
+ }
54
+ }