@docstack/react 0.0.1 → 0.0.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.
Files changed (2) hide show
  1. package/README.md +187 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,187 @@
1
+ # @docstack/react
2
+
3
+ React bindings for **DocStack Client**, enabling seamless integration of the intelligent, offline-first database engine into React applications.
4
+
5
+ This package provides a context provider and hooks to initialize the database, subscribe to data changes, and perform write operations.
6
+
7
+ ## 📦 Installation
8
+
9
+ ```bash
10
+ npm install @docstack/react @docstack/client
11
+ ```
12
+
13
+ ## 🚀 Usage
14
+
15
+ ### 1. Setup the Provider
16
+
17
+ Wrap your application root with `<StackProvider>`. This component initializes the DocStack engine and applies any schema patches provided in the configuration.
18
+
19
+ ```tsx
20
+ import { StackProvider } from "@docstack/react";
21
+
22
+ const DB_NAME = 'my-app-db';
23
+
24
+ // Define schema patches (versioned changes to your data model)
25
+ const PATCHES = [
26
+ {
27
+ "~class": "patch",
28
+ version: "1.0.0",
29
+ active: true,
30
+ docs: [
31
+ {
32
+ _id: "Todo",
33
+ "~class": "class",
34
+ name: "Todo",
35
+ schema: {
36
+ title: { name: "title", type: "string", config: { mandatory: true } },
37
+ completed: { name: "completed", type: "boolean", config: { defaultValue: false } }
38
+ }
39
+ }
40
+ ]
41
+ }
42
+ ];
43
+
44
+ function App() {
45
+ return (
46
+ <StackProvider config={[{ name: DB_NAME, patches: PATCHES }]}>
47
+ <MyComponent />
48
+ </StackProvider>
49
+ );
50
+ }
51
+ ```
52
+
53
+ ### 2. Read Data (Reactive)
54
+
55
+ Use the `useClassDocs` hook to fetch documents for a specific class. This hook automatically subscribes to changes, so your component re-renders whenever the underlying data changes.
56
+
57
+ ```tsx
58
+ import { useClassDocs } from "@docstack/react";
59
+
60
+ function TodoList() {
61
+ // Fetch all documents of class 'Todo' from 'my-app-db'
62
+ const { docs: todos, loading } = useClassDocs('my-app-db', 'Todo');
63
+
64
+ if (loading) return <p>Loading...</p>;
65
+
66
+ return (
67
+ <ul>
68
+ {todos.map(todo => (
69
+ <li key={todo._id}>
70
+ {todo.title} {todo.completed ? '✅' : '⭕'}
71
+ </li>
72
+ ))}
73
+ </ul>
74
+ );
75
+ }
76
+ ```
77
+
78
+ ### 3. Write Data
79
+
80
+ Use the `useDocStack` hook to access the underlying DocStack client instance for creating, updating, or deleting data.
81
+
82
+ ```tsx
83
+ import { useState } from 'react';
84
+ import { useClass } from "@docstack/react";
85
+
86
+ function AddTodo() {
87
+ const { classObj: todoClass } = useClass('my-app-db', 'Todo');
88
+ const [title, setTitle] = useState("");
89
+
90
+ const handleAdd = async () => {
91
+ if (!todoClass || !title) return;
92
+
93
+ // Add the new document
94
+ await todoClass.add({ title, completed: false });
95
+
96
+ setTitle("");
97
+ };
98
+
99
+ return (
100
+ <div>
101
+ <input value={title} onChange={e => setTitle(e.target.value)} />
102
+ <button onClick={handleAdd}>Add Task</button>
103
+ </div>
104
+ );
105
+ }
106
+ ```
107
+
108
+ ## 📚 API Reference
109
+
110
+ ### `<StackProvider />`
111
+
112
+ The context provider that manages the lifecycle of the DocStack client.
113
+
114
+ | Prop | Type | Description |
115
+ |------|------|-------------|
116
+ | `config` | `Array<{ name: string, patches: any[] }>` | Configuration for initializing stacks, including database names and schema patches. |
117
+
118
+ ### `useClassDocs(dbName: string, className: string)`
119
+
120
+ A hook that subscribes to a specific class in the database.
121
+
122
+ - **Arguments**:
123
+ - `dbName`: The name of the database stack.
124
+ - `className`: The name of the class to query.
125
+ - **Returns**:
126
+ - `docs`: An array of documents belonging to the class.
127
+ - `loading`: A boolean indicating if the initial data load is in progress.
128
+
129
+ ### `useClass(dbName: string, className: string)`
130
+
131
+ A hook that retrieves the class definition for writing data.
132
+
133
+ - **Arguments**:
134
+ - `dbName`: The name of the database stack.
135
+ - `className`: The name of the class.
136
+ - **Returns**:
137
+ - `classObj`: The class instance used for operations like `add` or `updateCard`.
138
+ - `loading`: Boolean indicating if the class is loading.
139
+
140
+ ### `useDocStack()`
141
+
142
+ A hook that returns the initialized DocStack client instance.
143
+
144
+ - **Returns**: The `DocStack` instance (or `null` if not yet initialized). Use this to access `getStack()`, run queries, or execute jobs.
145
+
146
+ ### `useQuerySQL(dbName: string, sql: string, ...params: any[])`
147
+
148
+ A hook to execute SQL queries against the local database.
149
+
150
+ - **Arguments**:
151
+ - `dbName`: The name of the database stack.
152
+ - `sql`: The SQL query string.
153
+ - `params`: Variable arguments for query parameters.
154
+ - **Returns**:
155
+ - `result`: An object containing `rows` (the query results).
156
+ - `loading`: Boolean indicating execution status.
157
+
158
+ ### `useFind(dbName: string, query: { selector: object, fields?: string[] })`
159
+
160
+ A hook to find documents using a MongoDB-style selector.
161
+
162
+ - **Arguments**:
163
+ - `dbName`: The name of the database stack.
164
+ - `query`: An object with a `selector` property (Mango query syntax).
165
+ - **Returns**:
166
+ - `docs`: An array of matching documents.
167
+ - `loading`: Boolean indicating if the search is in progress.
168
+
169
+ ### `useClassList(dbName: string, selector: object)`
170
+
171
+ A hook to retrieve a list of available classes (schemas) in the database.
172
+
173
+ - **Arguments**:
174
+ - `dbName`: The name of the database stack.
175
+ - `selector`: A filter object to select specific classes.
176
+ - **Returns**:
177
+ - `classList`: An array of `Class` instances.
178
+ - `loading`: Boolean indicating if the list is loading.
179
+
180
+ ### `useClassCreate(dbName: string)`
181
+
182
+ A hook that returns a function to create new classes dynamically.
183
+
184
+ - **Arguments**:
185
+ - `dbName`: The name of the database stack.
186
+ - **Returns**:
187
+ - A function `(className: string, description?: string) => Promise<Class>` to create a new class.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docstack/react",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "One does not simply stack documents.",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.js",