@promakeai/dbreact 1.0.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/README.md +633 -0
- package/SKILL.md +311 -0
- package/dist/adapters/RestAdapter.d.ts.map +1 -0
- package/dist/adapters/SqliteAdapter.d.ts.map +1 -0
- package/dist/adapters/SqliteAdapter.js +543 -0
- package/dist/core/DataManager.d.ts.map +1 -0
- package/dist/hooks/useDataManager.d.ts.map +1 -0
- package/dist/hooks/useDbHooks.d.ts.map +1 -0
- package/dist/hooks/useDbHooks.js +157 -0
- package/dist/hooks/useDbLang.d.ts.map +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +648 -0
- package/dist/providers/DbProvider.d.ts.map +1 -0
- package/dist/providers/DbProvider.js +134 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/utils/whereBuilder.d.ts.map +1 -0
- package/package.json +54 -0
- package/providers/DbProvider.tsx +191 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic Database Hooks
|
|
3
|
+
*
|
|
4
|
+
* Type-safe React hooks for database operations.
|
|
5
|
+
* Works with any table - just pass the table name and type.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* import { useDbList, useDbGet, useDbCreate } from '@promakeai/dbreact';
|
|
10
|
+
* import type { DbProduct } from './db/types';
|
|
11
|
+
*
|
|
12
|
+
* // List all products
|
|
13
|
+
* const { data } = useDbList<DbProduct>('products');
|
|
14
|
+
*
|
|
15
|
+
* // Get single product
|
|
16
|
+
* const { data } = useDbGet<DbProduct>('products', productId);
|
|
17
|
+
*
|
|
18
|
+
* // Create product
|
|
19
|
+
* const mutation = useDbCreate<DbProduct>('products');
|
|
20
|
+
* mutation.mutate({ name: 'New Product' });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
24
|
+
import { useAdapter, useDbLang } from "../providers/DbProvider";
|
|
25
|
+
/**
|
|
26
|
+
* Hook to list all records from a table
|
|
27
|
+
*
|
|
28
|
+
* @param table - Table name
|
|
29
|
+
* @param options - Query options (where, orderBy, limit, offset, enabled)
|
|
30
|
+
* @returns React Query result with data array
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* const { data: products, isLoading } = useDbList<DbProduct>('products', {
|
|
35
|
+
* where: { price: { $gt: 100 } },
|
|
36
|
+
* orderBy: [{ field: 'name', direction: 'ASC' }],
|
|
37
|
+
* limit: 10,
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export function useDbList(table, options) {
|
|
42
|
+
const adapter = useAdapter();
|
|
43
|
+
const { lang, fallbackLang } = useDbLang();
|
|
44
|
+
return useQuery({
|
|
45
|
+
queryKey: [table, "list", options, lang],
|
|
46
|
+
queryFn: () => adapter.list(table, {
|
|
47
|
+
...options,
|
|
48
|
+
lang,
|
|
49
|
+
fallbackLang,
|
|
50
|
+
}),
|
|
51
|
+
enabled: options?.enabled ?? true,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Hook to get a single record by ID or where query
|
|
56
|
+
*
|
|
57
|
+
* @param table - Table name
|
|
58
|
+
* @param idOrOptions - Record ID, or FindOneOptions with where clause
|
|
59
|
+
* @param maybeOptions - Query options when using ID form
|
|
60
|
+
* @returns React Query result with single record or null
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```tsx
|
|
64
|
+
* // By ID
|
|
65
|
+
* const { data: product } = useDbGet<DbProduct>('products', productId);
|
|
66
|
+
*
|
|
67
|
+
* // By where query (findOne style)
|
|
68
|
+
* const { data: product } = useDbGet<DbProduct>('products', {
|
|
69
|
+
* where: { slug: 'my-product' },
|
|
70
|
+
* enabled: !!slug,
|
|
71
|
+
* });
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export function useDbGet(table, idOrOptions, maybeOptions) {
|
|
75
|
+
const adapter = useAdapter();
|
|
76
|
+
const { lang, fallbackLang } = useDbLang();
|
|
77
|
+
const isWhereMode = typeof idOrOptions === "object" && idOrOptions !== null && "where" in idOrOptions;
|
|
78
|
+
const where = isWhereMode ? idOrOptions.where : { id: idOrOptions };
|
|
79
|
+
const enabled = isWhereMode
|
|
80
|
+
? (idOrOptions.enabled ?? true)
|
|
81
|
+
: ((maybeOptions?.enabled ?? true) && idOrOptions !== undefined);
|
|
82
|
+
return useQuery({
|
|
83
|
+
queryKey: [table, "single", where, lang],
|
|
84
|
+
queryFn: () => adapter.findOne(table, { where, lang, fallbackLang }),
|
|
85
|
+
enabled,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Hook to create a new record
|
|
90
|
+
*
|
|
91
|
+
* @param table - Table name
|
|
92
|
+
* @returns React Query mutation for creating records
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```tsx
|
|
96
|
+
* const mutation = useDbCreate<DbProduct>('products');
|
|
97
|
+
*
|
|
98
|
+
* mutation.mutate({ sku: 'ABC', price: 100 });
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export function useDbCreate(table) {
|
|
102
|
+
const adapter = useAdapter();
|
|
103
|
+
const queryClient = useQueryClient();
|
|
104
|
+
return useMutation({
|
|
105
|
+
mutationFn: (data) => adapter.create(table, data),
|
|
106
|
+
onSuccess: () => {
|
|
107
|
+
queryClient.invalidateQueries({ queryKey: [table] });
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Hook to update an existing record
|
|
113
|
+
*
|
|
114
|
+
* @param table - Table name
|
|
115
|
+
* @returns React Query mutation for updating records
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```tsx
|
|
119
|
+
* const mutation = useDbUpdate<DbProduct>('products');
|
|
120
|
+
*
|
|
121
|
+
* mutation.mutate({ id: 1, data: { price: 150 } });
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
export function useDbUpdate(table) {
|
|
125
|
+
const adapter = useAdapter();
|
|
126
|
+
const queryClient = useQueryClient();
|
|
127
|
+
return useMutation({
|
|
128
|
+
mutationFn: ({ id, data }) => adapter.update(table, id, data),
|
|
129
|
+
onSuccess: (_, { id }) => {
|
|
130
|
+
queryClient.invalidateQueries({ queryKey: [table] });
|
|
131
|
+
queryClient.invalidateQueries({ queryKey: [table, "single", id] });
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Hook to delete a record
|
|
137
|
+
*
|
|
138
|
+
* @param table - Table name
|
|
139
|
+
* @returns React Query mutation for deleting records
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```tsx
|
|
143
|
+
* const mutation = useDbDelete('products');
|
|
144
|
+
*
|
|
145
|
+
* mutation.mutate(productId);
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
export function useDbDelete(table) {
|
|
149
|
+
const adapter = useAdapter();
|
|
150
|
+
const queryClient = useQueryClient();
|
|
151
|
+
return useMutation({
|
|
152
|
+
mutationFn: (id) => adapter.delete(table, id),
|
|
153
|
+
onSuccess: () => {
|
|
154
|
+
queryClient.invalidateQueries({ queryKey: [table] });
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDbLang.d.ts","sourceRoot":"","sources":["../../hooks/useDbLang.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EACL,GAAG,EACH,YAAY,EACZ,CAAC,EACD,gBAAgB,EAChB,eAAe,EACf,oBAAoB,EACpB,gBAAgB,EAChB,eAAe,GAChB,MAAM,gBAAgB,CAAC;AAExB,YAAY,EACV,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,cAAc,EACd,cAAc,EACd,SAAS,EACT,YAAY,EACZ,eAAe,EACf,YAAY,EACZ,WAAW,GACZ,MAAM,gBAAgB,CAAC;AAGxB,YAAY,EACV,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,GACf,MAAM,SAAS,CAAC;AAGjB,OAAO,EACL,UAAU,EACV,KAAK,EACL,UAAU,EACV,SAAS,GACV,MAAM,wBAAwB,CAAC;AAGhC,OAAO,EACL,SAAS,EACT,QAAQ,EACR,WAAW,EACX,WAAW,EACX,WAAW,EACX,KAAK,WAAW,EAChB,KAAK,cAAc,GACpB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EAAE,aAAa,EAAE,KAAK,mBAAmB,EAAE,MAAM,0BAA0B,CAAC"}
|