@astriveone/adapter-react 1.0.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/dist/index.d.ts +11 -0
- package/dist/index.js +71 -0
- package/package.json +13 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AstriveOne, Doc, FindOptions } from "@astriveone/core";
|
|
2
|
+
export declare function useCollection(db: AstriveOne, collection: string, options?: FindOptions): Doc[];
|
|
3
|
+
export declare function useDocument(db: AstriveOne, collection: string, id: string | undefined): Doc | null;
|
|
4
|
+
/** Imperative mutation hook with built-in pending/error state for forms */
|
|
5
|
+
export declare function useMutation(db: AstriveOne, collection: string): {
|
|
6
|
+
pending: any;
|
|
7
|
+
error: any;
|
|
8
|
+
set: (doc: Doc) => Promise<Doc | undefined>;
|
|
9
|
+
update: (id: string, patch: Partial<Doc>) => Promise<Doc | null | undefined>;
|
|
10
|
+
remove: (id: string) => Promise<boolean | undefined>;
|
|
11
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* astriveone/adapters/react — React hooks for AstriveOne.
|
|
3
|
+
* Peer dependency: react >= 18 (not bundled, to avoid version conflicts).
|
|
4
|
+
*
|
|
5
|
+
* import { useCollection, useDocument } from "astriveone/adapters/react";
|
|
6
|
+
*
|
|
7
|
+
* function UserList({ db }) {
|
|
8
|
+
* const users = useCollection(db, "users");
|
|
9
|
+
* return <ul>{users.map(u => <li key={u.id}>{u.username}</li>)}</ul>;
|
|
10
|
+
* }
|
|
11
|
+
*/
|
|
12
|
+
import { useEffect, useState } from "react";
|
|
13
|
+
export function useCollection(db, collection, options) {
|
|
14
|
+
const [data, setData] = useState([]);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
let active = true;
|
|
17
|
+
const refresh = () => db.find(collection, options).then((docs) => active && setData(docs));
|
|
18
|
+
refresh();
|
|
19
|
+
const unsub = db.watch(collection, refresh);
|
|
20
|
+
return () => {
|
|
21
|
+
active = false;
|
|
22
|
+
unsub();
|
|
23
|
+
};
|
|
24
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
25
|
+
}, [db, collection, JSON.stringify(options)]);
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
export function useDocument(db, collection, id) {
|
|
29
|
+
const [doc, setDoc] = useState(null);
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (!id) {
|
|
32
|
+
setDoc(null);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
let active = true;
|
|
36
|
+
const refresh = () => db.get(collection, id).then((d) => active && setDoc(d));
|
|
37
|
+
refresh();
|
|
38
|
+
const unsub = db.watch(collection, refresh, id);
|
|
39
|
+
return () => {
|
|
40
|
+
active = false;
|
|
41
|
+
unsub();
|
|
42
|
+
};
|
|
43
|
+
}, [db, collection, id]);
|
|
44
|
+
return doc;
|
|
45
|
+
}
|
|
46
|
+
/** Imperative mutation hook with built-in pending/error state for forms */
|
|
47
|
+
export function useMutation(db, collection) {
|
|
48
|
+
const [pending, setPending] = useState(false);
|
|
49
|
+
const [error, setError] = useState(null);
|
|
50
|
+
async function run(fn) {
|
|
51
|
+
setPending(true);
|
|
52
|
+
setError(null);
|
|
53
|
+
try {
|
|
54
|
+
return await fn();
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
setError(err);
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
setPending(false);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
pending,
|
|
66
|
+
error,
|
|
67
|
+
set: (doc) => run(() => db.set(collection, doc)),
|
|
68
|
+
update: (id, patch) => run(() => db.update(collection, id, patch)),
|
|
69
|
+
remove: (id) => run(() => db.delete(collection, id)),
|
|
70
|
+
};
|
|
71
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@astriveone/adapter-react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "AstriveOne adapter for react",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"scripts": { "build": "tsc -p tsconfig.json" },
|
|
9
|
+
"dependencies": { "@astriveone/core": "1.0.0" },
|
|
10
|
+
"peerDependencies": { "react": ">=18" },
|
|
11
|
+
"files": ["dist"],
|
|
12
|
+
"license": "MIT"
|
|
13
|
+
}
|