-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
297 lines (267 loc) · 7.05 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/** @format */
import {
useQuery,
QueryClient,
QueryClientProvider as QueryClientProviderBase,
} from "react-query";
import supabase from "./supabase";
// React Query client
const client = new QueryClient();
/**** USERS ****/
// Fetch user data
// Note: This is called automatically in `auth.js` and data is merged into `auth.user`
export function useUser(uid) {
// Manage data fetching with React Query: https://react-query.tanstack.com/overview
return useQuery(
// Unique query key: https://react-query.tanstack.com/guides/query-keys
["user", { uid }],
// Query function that fetches data
() =>
supabase
.from("users")
.select(`*, customers ( * )`)
.eq("id", uid)
.single()
.then(handle),
// Only call query function if we have a `uid`
{ enabled: !!uid }
);
}
// Update an existing user
export async function updateUser(uid, data) {
const response = await supabase
.from("users")
.update(data)
.eq("id", uid)
.then(handle);
// Invalidate and refetch queries that could have old data
await client.invalidateQueries(["user", { uid }]);
return response;
}
/**** PROPERTIES ****/
// Fetch property data
export function useProperty(id) {
return useQuery(
["property", { id }],
() =>
supabase.from("properties").select().eq("id", id).single().then(handle),
{ enabled: !!id }
);
}
// Fetch property data (non-hook)
// Useful if you need to fetch data from outside of a component
export function getProperty(id) {
return supabase
.from("properties")
.select()
.eq("id", id)
.single()
.then(handle);
}
// Fetch all properties by user
export function usePropertiesByUser(user_id) {
return useQuery(
["properties", { user_id }],
() =>
supabase
.from("properties")
.select()
.eq("user_id", user_id)
.order("address", { ascending: false })
.then(handle),
{ enabled: !!user_id }
);
}
// Create a new property
export async function createProperty(data) {
const response = await supabase
.from("properties")
.insert([data])
.then(handle);
// Invalidate and refetch queries that could have old data
await client.invalidateQueries(["properties"]);
return response;
}
// Update an property
export async function updateProperty(id, data) {
const response = await supabase
.from("properties")
.update(data)
.eq("id", id)
.then(handle);
// Invalidate and refetch queries that could have old data
await Promise.all([
client.invalidateQueries(["property", { id }]),
client.invalidateQueries(["properties"]),
]);
return response;
}
// Delete an property
export async function deleteProperty(id) {
const response = await supabase
.from("properties")
.delete()
.eq("id", id)
.then(handle);
// Invalidate and refetch queries that could have old data
await Promise.all([
client.invalidateQueries(["property", { id }]),
client.invalidateQueries(["properties"]),
]);
return response;
}
/*** UNITS ****/
// Fetch unit data
export function useUnit(id) {
return useQuery(
["unit", { id }],
() => supabase.from("units").select().eq("id", id).single().then(handle),
{ enabled: !!id }
);
}
// Fetch all units by user
export function useUnitsByUser(user_id) {
return useQuery(
["units", { user_id }],
() =>
supabase
.from("units")
.select()
.eq("user_id", user_id)
.then(handle),
{ enabled: !!user_id }
);
}
// Fetch units by property and user, if no property_id return null
export function useUnitsByProperty(property_id, user_id) {
return useQuery(
["units", { property_id, user_id }],
() =>
supabase
.from("units")
.select()
.eq("property_id", property_id)
.eq("user_id", user_id)
.then(handle),
{ enabled: !!property_id }
);
}
// Fetch units for property
export function useUnitsForProperty(property_id) {
return useQuery(
["units", { property_id }],
() =>
supabase
.from("units")
.select()
.eq("property_id", property_id)
.then(handle),
{ enabled: !!property_id }
);
}
// Create a unit
export async function createUnit(data) {
const response = await supabase.from("units").insert([data]).then(handle);
// Invalidate and refetch queries that could have old data
await client.invalidateQueries(["units"]);
return response;
}
// Update a unit
export async function updateUnit(id, data) {
const response = await supabase
.from("units")
.update(data)
.eq("id", id)
.then(handle);
// Invalidate and refetch queries that could have old data
await client.invalidateQueries(["units"]);
return response;
}
// Delete a unit
export async function deleteUnit(id) {
const response = await supabase
.from("units")
.delete()
.eq("id", id)
.then(handle);
// Invalidate and refetch queries that could have old data
await client.invalidateQueries(["units"]);
return response;
}
/**** ITEMS ****/
// Fetch item data
export function useItem(id) {
return useQuery(
["item", { id }],
() => supabase.from("items").select().eq("id", id).single().then(handle),
{ enabled: !!id }
);
}
// Fetch item data (non-hook)
// Useful if you need to fetch data from outside of a component
export function getItem(id) {
return supabase.from("items").select().eq("id", id).single().then(handle);
}
// Fetch all items by owner
export function useItemsByOwner(owner) {
return useQuery(
["items", { owner }],
() =>
supabase
.from("items")
.select()
.eq("owner", owner)
.order("created_at", { ascending: false })
.then(handle),
{ enabled: !!owner }
);
}
// Create a new item
export async function createItem(data) {
const response = await supabase.from("items").insert([data]).then(handle);
// Invalidate and refetch queries that could have old data
await client.invalidateQueries(["items"]);
return response;
}
// Update an item
export async function updateItem(id, data) {
const response = await supabase
.from("items")
.update(data)
.eq("id", id)
.then(handle);
// Invalidate and refetch queries that could have old data
await Promise.all([
client.invalidateQueries(["item", { id }]),
client.invalidateQueries(["items"]),
]);
return response;
}
// Delete an item
export async function deleteItem(id) {
const response = await supabase
.from("items")
.delete()
.eq("id", id)
.then(handle);
// Invalidate and refetch queries that could have old data
await Promise.all([
client.invalidateQueries(["item", { id }]),
client.invalidateQueries(["items"]),
]);
return response;
}
/**** HELPERS ****/
// Get response data or throw error if there is one
function handle(response) {
if (response.error) throw response.error;
return response.data;
}
// React Query context provider that wraps our app
export function QueryClientProvider(props) {
return (
<QueryClientProviderBase client={client}>
{props.children}
</QueryClientProviderBase>
);
}