@taladb/react 0.5.0

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.
@@ -0,0 +1,92 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+ import { TalaDB, Document, Collection, Filter } from 'taladb';
4
+
5
+ interface TalaDBProviderProps {
6
+ /** The TalaDB instance returned by `openDB()`. */
7
+ db: TalaDB;
8
+ children: ReactNode;
9
+ }
10
+ /**
11
+ * Provides a TalaDB instance to all child hooks.
12
+ *
13
+ * @example
14
+ * const db = await openDB('myapp.db')
15
+ *
16
+ * function App() {
17
+ * return (
18
+ * <TalaDBProvider db={db}>
19
+ * <MyComponent />
20
+ * </TalaDBProvider>
21
+ * )
22
+ * }
23
+ */
24
+ declare function TalaDBProvider({ db, children }: TalaDBProviderProps): react_jsx_runtime.JSX.Element;
25
+ /**
26
+ * Returns the TalaDB instance from the nearest `<TalaDBProvider>`.
27
+ *
28
+ * @throws If called outside of a `<TalaDBProvider>`.
29
+ */
30
+ declare function useTalaDB(): TalaDB;
31
+
32
+ /**
33
+ * Returns a stable `Collection<T>` handle from the nearest `<TalaDBProvider>`.
34
+ *
35
+ * The returned collection is memoised — the same object reference is returned
36
+ * on every render unless the db instance or collection name changes. Pass it
37
+ * directly to `useFind` or `useFindOne` without wrapping in `useMemo`.
38
+ *
39
+ * @param name The collection name (e.g. `'articles'`).
40
+ *
41
+ * @example
42
+ * const articles = useCollection<Article>('articles')
43
+ * const { data, loading } = useFind(articles, { locale: 'en' })
44
+ */
45
+ declare function useCollection<T extends Document>(name: string): Collection<T>;
46
+
47
+ interface FindResult<T> {
48
+ /** The current matching documents. Empty array while loading. */
49
+ data: T[];
50
+ /** True until the first snapshot has been delivered from the database. */
51
+ loading: boolean;
52
+ }
53
+ /**
54
+ * Subscribe to a live query. Re-renders whenever the matching documents change.
55
+ *
56
+ * Backed by `useSyncExternalStore` for zero-tearing snapshots in concurrent React.
57
+ * Works in both React (browser / Node.js) and React Native.
58
+ *
59
+ * @param collection A `Collection<T>` instance (memoize or store outside the component).
60
+ * @param filter Optional filter. Inline objects are safe — the filter is
61
+ * serialised to a string for subscription identity checks so
62
+ * `{ active: true }` on every render does not re-subscribe.
63
+ *
64
+ * @example
65
+ * const articles = useMemo(() => db.collection<Article>('articles'), [db])
66
+ * const { data, loading } = useFind(articles, { locale: 'en' })
67
+ */
68
+ declare function useFind<T extends Document>(collection: Collection<T>, filter?: Filter<T>): FindResult<T>;
69
+
70
+ interface FindOneResult<T> {
71
+ /** The first matching document, or `null` when none matched or still loading. */
72
+ data: T | null;
73
+ /** True until the first snapshot has been delivered from the database. */
74
+ loading: boolean;
75
+ }
76
+ /**
77
+ * Subscribe to a single document live query. Re-renders when the matching
78
+ * document changes.
79
+ *
80
+ * Internally subscribes with the same filter as `useFind` and returns the
81
+ * first result. If you need all matching documents use `useFind` instead.
82
+ *
83
+ * @param collection A `Collection<T>` instance.
84
+ * @param filter Filter to identify the document. Inline objects are safe.
85
+ *
86
+ * @example
87
+ * const users = useMemo(() => db.collection<User>('users'), [db])
88
+ * const { data: user, loading } = useFindOne(users, { _id: userId })
89
+ */
90
+ declare function useFindOne<T extends Document>(collection: Collection<T>, filter: Filter<T>): FindOneResult<T>;
91
+
92
+ export { type FindOneResult, type FindResult, TalaDBProvider, type TalaDBProviderProps, useCollection, useFind, useFindOne, useTalaDB };
@@ -0,0 +1,92 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+ import { TalaDB, Document, Collection, Filter } from 'taladb';
4
+
5
+ interface TalaDBProviderProps {
6
+ /** The TalaDB instance returned by `openDB()`. */
7
+ db: TalaDB;
8
+ children: ReactNode;
9
+ }
10
+ /**
11
+ * Provides a TalaDB instance to all child hooks.
12
+ *
13
+ * @example
14
+ * const db = await openDB('myapp.db')
15
+ *
16
+ * function App() {
17
+ * return (
18
+ * <TalaDBProvider db={db}>
19
+ * <MyComponent />
20
+ * </TalaDBProvider>
21
+ * )
22
+ * }
23
+ */
24
+ declare function TalaDBProvider({ db, children }: TalaDBProviderProps): react_jsx_runtime.JSX.Element;
25
+ /**
26
+ * Returns the TalaDB instance from the nearest `<TalaDBProvider>`.
27
+ *
28
+ * @throws If called outside of a `<TalaDBProvider>`.
29
+ */
30
+ declare function useTalaDB(): TalaDB;
31
+
32
+ /**
33
+ * Returns a stable `Collection<T>` handle from the nearest `<TalaDBProvider>`.
34
+ *
35
+ * The returned collection is memoised — the same object reference is returned
36
+ * on every render unless the db instance or collection name changes. Pass it
37
+ * directly to `useFind` or `useFindOne` without wrapping in `useMemo`.
38
+ *
39
+ * @param name The collection name (e.g. `'articles'`).
40
+ *
41
+ * @example
42
+ * const articles = useCollection<Article>('articles')
43
+ * const { data, loading } = useFind(articles, { locale: 'en' })
44
+ */
45
+ declare function useCollection<T extends Document>(name: string): Collection<T>;
46
+
47
+ interface FindResult<T> {
48
+ /** The current matching documents. Empty array while loading. */
49
+ data: T[];
50
+ /** True until the first snapshot has been delivered from the database. */
51
+ loading: boolean;
52
+ }
53
+ /**
54
+ * Subscribe to a live query. Re-renders whenever the matching documents change.
55
+ *
56
+ * Backed by `useSyncExternalStore` for zero-tearing snapshots in concurrent React.
57
+ * Works in both React (browser / Node.js) and React Native.
58
+ *
59
+ * @param collection A `Collection<T>` instance (memoize or store outside the component).
60
+ * @param filter Optional filter. Inline objects are safe — the filter is
61
+ * serialised to a string for subscription identity checks so
62
+ * `{ active: true }` on every render does not re-subscribe.
63
+ *
64
+ * @example
65
+ * const articles = useMemo(() => db.collection<Article>('articles'), [db])
66
+ * const { data, loading } = useFind(articles, { locale: 'en' })
67
+ */
68
+ declare function useFind<T extends Document>(collection: Collection<T>, filter?: Filter<T>): FindResult<T>;
69
+
70
+ interface FindOneResult<T> {
71
+ /** The first matching document, or `null` when none matched or still loading. */
72
+ data: T | null;
73
+ /** True until the first snapshot has been delivered from the database. */
74
+ loading: boolean;
75
+ }
76
+ /**
77
+ * Subscribe to a single document live query. Re-renders when the matching
78
+ * document changes.
79
+ *
80
+ * Internally subscribes with the same filter as `useFind` and returns the
81
+ * first result. If you need all matching documents use `useFind` instead.
82
+ *
83
+ * @param collection A `Collection<T>` instance.
84
+ * @param filter Filter to identify the document. Inline objects are safe.
85
+ *
86
+ * @example
87
+ * const users = useMemo(() => db.collection<User>('users'), [db])
88
+ * const { data: user, loading } = useFindOne(users, { _id: userId })
89
+ */
90
+ declare function useFindOne<T extends Document>(collection: Collection<T>, filter: Filter<T>): FindOneResult<T>;
91
+
92
+ export { type FindOneResult, type FindResult, TalaDBProvider, type TalaDBProviderProps, useCollection, useFind, useFindOne, useTalaDB };
package/dist/index.js ADDED
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ TalaDBProvider: () => TalaDBProvider,
24
+ useCollection: () => useCollection,
25
+ useFind: () => useFind,
26
+ useFindOne: () => useFindOne,
27
+ useTalaDB: () => useTalaDB
28
+ });
29
+ module.exports = __toCommonJS(index_exports);
30
+
31
+ // src/context.tsx
32
+ var import_react = require("react");
33
+ var import_jsx_runtime = require("react/jsx-runtime");
34
+ var TalaDBContext = (0, import_react.createContext)(null);
35
+ function TalaDBProvider({ db, children }) {
36
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TalaDBContext.Provider, { value: db, children });
37
+ }
38
+ function useTalaDB() {
39
+ const db = (0, import_react.useContext)(TalaDBContext);
40
+ if (db === null) {
41
+ throw new Error("useTalaDB must be used inside <TalaDBProvider db={...}>");
42
+ }
43
+ return db;
44
+ }
45
+
46
+ // src/useCollection.ts
47
+ var import_react2 = require("react");
48
+ function useCollection(name) {
49
+ const db = useTalaDB();
50
+ return (0, import_react2.useMemo)(() => db.collection(name), [db, name]);
51
+ }
52
+
53
+ // src/useFind.ts
54
+ var import_react3 = require("react");
55
+ function useFind(collection, filter) {
56
+ const snapshotRef = (0, import_react3.useRef)({ data: [], loading: true });
57
+ const filterKey = JSON.stringify(filter ?? null);
58
+ const subscribe = (0, import_react3.useCallback)(
59
+ (notify) => {
60
+ snapshotRef.current = { data: snapshotRef.current.data, loading: true };
61
+ return collection.subscribe(filter, (docs) => {
62
+ snapshotRef.current = { data: docs, loading: false };
63
+ notify();
64
+ });
65
+ },
66
+ // filterKey captures the serialised filter; collection is the identity dep.
67
+ // eslint-disable-next-line react-hooks/exhaustive-deps
68
+ [collection, filterKey]
69
+ );
70
+ const getSnapshot = (0, import_react3.useCallback)(() => snapshotRef.current, []);
71
+ return (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
72
+ }
73
+
74
+ // src/useFindOne.ts
75
+ var import_react4 = require("react");
76
+ function useFindOne(collection, filter) {
77
+ const snapshotRef = (0, import_react4.useRef)({ data: null, loading: true });
78
+ const filterKey = JSON.stringify(filter);
79
+ const subscribe = (0, import_react4.useCallback)(
80
+ (notify) => {
81
+ snapshotRef.current = { data: snapshotRef.current.data, loading: true };
82
+ return collection.subscribe(filter, (docs) => {
83
+ snapshotRef.current = { data: docs[0] ?? null, loading: false };
84
+ notify();
85
+ });
86
+ },
87
+ // eslint-disable-next-line react-hooks/exhaustive-deps
88
+ [collection, filterKey]
89
+ );
90
+ const getSnapshot = (0, import_react4.useCallback)(() => snapshotRef.current, []);
91
+ return (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
92
+ }
93
+ // Annotate the CommonJS export names for ESM import in node:
94
+ 0 && (module.exports = {
95
+ TalaDBProvider,
96
+ useCollection,
97
+ useFind,
98
+ useFindOne,
99
+ useTalaDB
100
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,69 @@
1
+ // src/context.tsx
2
+ import { createContext, useContext } from "react";
3
+ import { jsx } from "react/jsx-runtime";
4
+ var TalaDBContext = createContext(null);
5
+ function TalaDBProvider({ db, children }) {
6
+ return /* @__PURE__ */ jsx(TalaDBContext.Provider, { value: db, children });
7
+ }
8
+ function useTalaDB() {
9
+ const db = useContext(TalaDBContext);
10
+ if (db === null) {
11
+ throw new Error("useTalaDB must be used inside <TalaDBProvider db={...}>");
12
+ }
13
+ return db;
14
+ }
15
+
16
+ // src/useCollection.ts
17
+ import { useMemo } from "react";
18
+ function useCollection(name) {
19
+ const db = useTalaDB();
20
+ return useMemo(() => db.collection(name), [db, name]);
21
+ }
22
+
23
+ // src/useFind.ts
24
+ import { useCallback, useRef, useSyncExternalStore } from "react";
25
+ function useFind(collection, filter) {
26
+ const snapshotRef = useRef({ data: [], loading: true });
27
+ const filterKey = JSON.stringify(filter ?? null);
28
+ const subscribe = useCallback(
29
+ (notify) => {
30
+ snapshotRef.current = { data: snapshotRef.current.data, loading: true };
31
+ return collection.subscribe(filter, (docs) => {
32
+ snapshotRef.current = { data: docs, loading: false };
33
+ notify();
34
+ });
35
+ },
36
+ // filterKey captures the serialised filter; collection is the identity dep.
37
+ // eslint-disable-next-line react-hooks/exhaustive-deps
38
+ [collection, filterKey]
39
+ );
40
+ const getSnapshot = useCallback(() => snapshotRef.current, []);
41
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
42
+ }
43
+
44
+ // src/useFindOne.ts
45
+ import { useCallback as useCallback2, useRef as useRef2, useSyncExternalStore as useSyncExternalStore2 } from "react";
46
+ function useFindOne(collection, filter) {
47
+ const snapshotRef = useRef2({ data: null, loading: true });
48
+ const filterKey = JSON.stringify(filter);
49
+ const subscribe = useCallback2(
50
+ (notify) => {
51
+ snapshotRef.current = { data: snapshotRef.current.data, loading: true };
52
+ return collection.subscribe(filter, (docs) => {
53
+ snapshotRef.current = { data: docs[0] ?? null, loading: false };
54
+ notify();
55
+ });
56
+ },
57
+ // eslint-disable-next-line react-hooks/exhaustive-deps
58
+ [collection, filterKey]
59
+ );
60
+ const getSnapshot = useCallback2(() => snapshotRef.current, []);
61
+ return useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
62
+ }
63
+ export {
64
+ TalaDBProvider,
65
+ useCollection,
66
+ useFind,
67
+ useFindOne,
68
+ useTalaDB
69
+ };
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@taladb/react",
3
+ "version": "0.5.0",
4
+ "description": "React hooks for TalaDB — useFind, useFindOne, and useCollection for React and React Native",
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
+ "react",
23
+ "react-native",
24
+ "hooks",
25
+ "offline",
26
+ "vector",
27
+ "live-queries"
28
+ ],
29
+ "license": "MIT",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/thinkgrid-labs/taladb.git",
33
+ "directory": "packages/taladb-react"
34
+ },
35
+ "homepage": "https://thinkgrid-labs.github.io/taladb/guide/react",
36
+ "bugs": {
37
+ "url": "https://github.com/thinkgrid-labs/taladb/issues"
38
+ },
39
+ "engines": {
40
+ "node": ">=18"
41
+ },
42
+ "peerDependencies": {
43
+ "react": ">=18.0.0",
44
+ "taladb": "0.5.0"
45
+ },
46
+ "devDependencies": {
47
+ "@testing-library/react": "^16.0.0",
48
+ "@types/react": "^18.0.0",
49
+ "jsdom": "^26.0.0",
50
+ "react": "^18.0.0",
51
+ "react-dom": "^18.0.0",
52
+ "tsup": "^8.0.0",
53
+ "typescript": "^5.9.3",
54
+ "vitest": "^3.2.0",
55
+ "taladb": "0.5.0"
56
+ },
57
+ "scripts": {
58
+ "build": "tsup",
59
+ "typecheck": "tsc --noEmit",
60
+ "test": "vitest run",
61
+ "test:watch": "vitest"
62
+ }
63
+ }