@taruvi/refine-providers 1.1.6 → 1.1.7
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 +101 -39
- package/dist/index.cjs +61 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +62 -16
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Database, Storage, Functions, App, User, Analytics, Auth, Policy } from '@taruvi/sdk';
|
|
1
|
+
import { Graph, Database, Storage, Functions, App, User, Analytics, Auth, Policy } from '@taruvi/sdk';
|
|
2
2
|
import DataLoader2 from 'dataloader';
|
|
3
3
|
|
|
4
4
|
// src/dataProvider.ts
|
|
@@ -80,6 +80,18 @@ function applyPopulate(query, meta) {
|
|
|
80
80
|
const populateArray = Array.isArray(meta.populate) ? meta.populate : meta.populate.split(",").map((s) => s.trim());
|
|
81
81
|
return query.populate(populateArray);
|
|
82
82
|
}
|
|
83
|
+
function isGraphQuery(meta) {
|
|
84
|
+
return !!(meta?.format || meta?.graph_types || meta?.include || meta?.depth);
|
|
85
|
+
}
|
|
86
|
+
function buildGraphQuery(client, tableName, meta, recordId) {
|
|
87
|
+
let query = new Graph(client).from(tableName);
|
|
88
|
+
if (recordId) query = query.get(recordId);
|
|
89
|
+
if (meta?.format) query = query.format(meta.format);
|
|
90
|
+
if (meta?.include) query = query.include(meta.include);
|
|
91
|
+
if (meta?.depth) query = query.depth(meta.depth);
|
|
92
|
+
if (meta?.graph_types) query = query.types(meta.graph_types);
|
|
93
|
+
return query;
|
|
94
|
+
}
|
|
83
95
|
function dataProvider(client) {
|
|
84
96
|
const config = client.getConfig();
|
|
85
97
|
const baseApiUrl = `${config.baseUrl}/api/apps/${config.appSlug}`;
|
|
@@ -90,6 +102,12 @@ function dataProvider(client) {
|
|
|
90
102
|
const { resource, pagination, filters, sorters, meta } = params;
|
|
91
103
|
const taruviMeta = meta;
|
|
92
104
|
const tableName = getTableName(resource, taruviMeta);
|
|
105
|
+
if (isGraphQuery(taruviMeta)) {
|
|
106
|
+
const response2 = await buildGraphQuery(client, tableName, taruviMeta).execute();
|
|
107
|
+
const data = Array.isArray(response2) ? response2 : response2?.data ?? [];
|
|
108
|
+
const total = response2?.total ?? data.length;
|
|
109
|
+
return { data, total };
|
|
110
|
+
}
|
|
93
111
|
let query = new Database(client).from(tableName);
|
|
94
112
|
query = applyFilters(query, filters);
|
|
95
113
|
query = applySorters(query, sorters);
|
|
@@ -102,6 +120,11 @@ function dataProvider(client) {
|
|
|
102
120
|
const { resource, id, meta } = params;
|
|
103
121
|
const taruviMeta = meta;
|
|
104
122
|
const tableName = getTableName(resource, taruviMeta);
|
|
123
|
+
if (isGraphQuery(taruviMeta)) {
|
|
124
|
+
const response2 = await buildGraphQuery(client, tableName, taruviMeta, String(id)).execute();
|
|
125
|
+
const data = Array.isArray(response2) ? response2[0] : response2?.data?.[0] ?? response2;
|
|
126
|
+
return { data };
|
|
127
|
+
}
|
|
105
128
|
let query = new Database(client).from(tableName);
|
|
106
129
|
query = applyPopulate(query, taruviMeta);
|
|
107
130
|
const response = await query.get(String(id)).execute();
|
|
@@ -111,6 +134,15 @@ function dataProvider(client) {
|
|
|
111
134
|
const { resource, ids, meta } = params;
|
|
112
135
|
const taruviMeta = meta;
|
|
113
136
|
const tableName = getTableName(resource, taruviMeta);
|
|
137
|
+
if (isGraphQuery(taruviMeta)) {
|
|
138
|
+
const data = await Promise.all(
|
|
139
|
+
ids.map(async (id) => {
|
|
140
|
+
const response2 = await buildGraphQuery(client, tableName, taruviMeta, String(id)).execute();
|
|
141
|
+
return Array.isArray(response2) ? response2[0] : response2?.data?.[0] ?? response2;
|
|
142
|
+
})
|
|
143
|
+
);
|
|
144
|
+
return { data };
|
|
145
|
+
}
|
|
114
146
|
const idColumn = getIdColumn(taruviMeta);
|
|
115
147
|
let query = new Database(client).from(tableName);
|
|
116
148
|
query = query.filter(idColumn, "in", ids.map(String));
|
|
@@ -122,6 +154,10 @@ function dataProvider(client) {
|
|
|
122
154
|
const { resource, variables, meta } = params;
|
|
123
155
|
const taruviMeta = meta;
|
|
124
156
|
const tableName = getTableName(resource, taruviMeta);
|
|
157
|
+
if (isGraphQuery(taruviMeta)) {
|
|
158
|
+
const data2 = await new Graph(client).from(tableName).createEdge(variables).execute();
|
|
159
|
+
return { data: data2 };
|
|
160
|
+
}
|
|
125
161
|
const data = await new Database(client).from(tableName).create(variables).execute();
|
|
126
162
|
return { data };
|
|
127
163
|
},
|
|
@@ -140,6 +176,10 @@ function dataProvider(client) {
|
|
|
140
176
|
const { resource, id, variables, meta } = params;
|
|
141
177
|
const taruviMeta = meta;
|
|
142
178
|
const tableName = getTableName(resource, taruviMeta);
|
|
179
|
+
if (isGraphQuery(taruviMeta)) {
|
|
180
|
+
const data2 = await new Graph(client).from(tableName).updateEdge(String(id), variables).execute();
|
|
181
|
+
return { data: data2 };
|
|
182
|
+
}
|
|
143
183
|
const data = await new Database(client).from(tableName).get(String(id)).update(variables).execute();
|
|
144
184
|
return { data };
|
|
145
185
|
},
|
|
@@ -158,6 +198,10 @@ function dataProvider(client) {
|
|
|
158
198
|
const { resource, id, meta } = params;
|
|
159
199
|
const taruviMeta = meta;
|
|
160
200
|
const tableName = getTableName(resource, taruviMeta);
|
|
201
|
+
if (isGraphQuery(taruviMeta)) {
|
|
202
|
+
const data2 = await new Graph(client).from(tableName).deleteEdge([String(id)]).execute();
|
|
203
|
+
return { data: data2 };
|
|
204
|
+
}
|
|
161
205
|
const data = await new Database(client).from(tableName).delete(String(id)).execute();
|
|
162
206
|
return { data };
|
|
163
207
|
},
|
|
@@ -165,34 +209,36 @@ function dataProvider(client) {
|
|
|
165
209
|
const { resource, ids, meta } = params;
|
|
166
210
|
const taruviMeta = meta;
|
|
167
211
|
const tableName = getTableName(resource, taruviMeta);
|
|
212
|
+
if (isGraphQuery(taruviMeta)) {
|
|
213
|
+
const data2 = await new Graph(client).from(tableName).deleteEdge(ids.map(String)).execute();
|
|
214
|
+
return { data: [data2] };
|
|
215
|
+
}
|
|
168
216
|
const data = await Promise.all(
|
|
169
|
-
ids.map(
|
|
170
|
-
return new Database(client).from(tableName).delete(String(id)).execute();
|
|
171
|
-
})
|
|
217
|
+
ids.map((id) => new Database(client).from(tableName).delete(String(id)).execute())
|
|
172
218
|
);
|
|
173
219
|
return { data };
|
|
174
220
|
},
|
|
175
221
|
custom: async (params) => {
|
|
176
222
|
const { url, method, payload, query } = params;
|
|
223
|
+
let fullUrl = `api/apps/${config.appSlug}/datatables/${url}`;
|
|
177
224
|
let data;
|
|
178
225
|
switch (method.toLowerCase()) {
|
|
179
226
|
case "get": {
|
|
180
|
-
let requestUrl = url;
|
|
181
227
|
if (query && Object.keys(query).length > 0) {
|
|
182
228
|
const queryString = new URLSearchParams(query).toString();
|
|
183
|
-
|
|
229
|
+
fullUrl = `${fullUrl}${fullUrl.includes("?") ? "&" : "?"}${queryString}`;
|
|
184
230
|
}
|
|
185
|
-
data = await client.httpClient.get(
|
|
231
|
+
data = await client.httpClient.get(fullUrl);
|
|
186
232
|
break;
|
|
187
233
|
}
|
|
188
234
|
case "post":
|
|
189
|
-
data = await client.httpClient.post(
|
|
235
|
+
data = await client.httpClient.post(fullUrl, payload);
|
|
190
236
|
break;
|
|
191
237
|
case "put":
|
|
192
|
-
data = await client.httpClient.put(
|
|
238
|
+
data = await client.httpClient.put(fullUrl, payload);
|
|
193
239
|
break;
|
|
194
240
|
case "delete":
|
|
195
|
-
data = await client.httpClient.delete(
|
|
241
|
+
data = await client.httpClient.delete(fullUrl);
|
|
196
242
|
break;
|
|
197
243
|
default:
|
|
198
244
|
throw new Error(`Unsupported HTTP method: ${method}`);
|
|
@@ -401,25 +447,25 @@ function storageDataProvider(client) {
|
|
|
401
447
|
},
|
|
402
448
|
custom: async (params) => {
|
|
403
449
|
const { url, method, payload, query } = params;
|
|
450
|
+
let fullUrl = `api/apps/${config.appSlug}/storage/buckets/${url}`;
|
|
404
451
|
let data;
|
|
405
452
|
switch (method.toLowerCase()) {
|
|
406
453
|
case "get": {
|
|
407
|
-
let requestUrl = url;
|
|
408
454
|
if (query && Object.keys(query).length > 0) {
|
|
409
455
|
const queryString = new URLSearchParams(query).toString();
|
|
410
|
-
|
|
456
|
+
fullUrl = `${fullUrl}${fullUrl.includes("?") ? "&" : "?"}${queryString}`;
|
|
411
457
|
}
|
|
412
|
-
data = await client.httpClient.get(
|
|
458
|
+
data = await client.httpClient.get(fullUrl);
|
|
413
459
|
break;
|
|
414
460
|
}
|
|
415
461
|
case "post":
|
|
416
|
-
data = await client.httpClient.post(
|
|
462
|
+
data = await client.httpClient.post(fullUrl, payload);
|
|
417
463
|
break;
|
|
418
464
|
case "put":
|
|
419
|
-
data = await client.httpClient.put(
|
|
465
|
+
data = await client.httpClient.put(fullUrl, payload);
|
|
420
466
|
break;
|
|
421
467
|
case "delete":
|
|
422
|
-
data = await client.httpClient.delete(
|
|
468
|
+
data = await client.httpClient.delete(fullUrl);
|
|
423
469
|
break;
|
|
424
470
|
default:
|
|
425
471
|
throw new Error(`Unsupported HTTP method: ${method}`);
|