@taruvi/refine-providers 1.2.2 → 1.2.4

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.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Graph, Database, Storage, Functions, Analytics, App, Secrets, User, Auth, Policy } from '@taruvi/sdk';
1
+ import { Database, Storage, Functions, Analytics, App, Secrets, User, Auth, Policy } from '@taruvi/sdk';
2
2
  import DataLoader from 'dataloader';
3
3
 
4
4
  // src/dataProvider.ts
@@ -180,7 +180,7 @@ function isGraphQuery(meta) {
180
180
  return !!(meta?.format || meta?.graph_types || meta?.include || meta?.depth);
181
181
  }
182
182
  function buildGraphQuery(client, tableName, meta, recordId) {
183
- let query = new Graph(client).from(tableName);
183
+ let query = new Database(client).from(tableName);
184
184
  if (recordId) query = query.get(recordId);
185
185
  if (meta?.format) query = query.format(meta.format);
186
186
  if (meta?.include) query = query.include(meta.include);
@@ -251,10 +251,12 @@ function dataProvider(client) {
251
251
  const taruviMeta = meta;
252
252
  const tableName = getTableName(resource, taruviMeta);
253
253
  if (isGraphQuery(taruviMeta)) {
254
- const response2 = await new Graph(client).from(tableName).create(variables).execute();
254
+ const response2 = await new Database(client).from(tableName).edges().create(variables).execute();
255
255
  return { data: response2.data };
256
256
  }
257
- const response = await new Database(client).from(tableName).create(variables).execute();
257
+ const db = new Database(client).from(tableName);
258
+ const query = taruviMeta?.upsert ? db.upsert(variables) : db.create(variables);
259
+ const response = await query.execute();
258
260
  return { data: response.data };
259
261
  },
260
262
  createMany: async (params) => {
@@ -274,7 +276,7 @@ function dataProvider(client) {
274
276
  const taruviMeta = meta;
275
277
  const tableName = getTableName(resource, taruviMeta);
276
278
  if (isGraphQuery(taruviMeta)) {
277
- const response2 = await new Graph(client).from(tableName).update(String(id), variables).execute();
279
+ const response2 = await new Database(client).from(tableName).edges().get(String(id)).update(variables).execute();
278
280
  return { data: response2.data };
279
281
  }
280
282
  const response = await new Database(client).from(tableName).get(String(id)).update(variables).execute();
@@ -284,20 +286,17 @@ function dataProvider(client) {
284
286
  const { resource, ids, variables, meta } = params;
285
287
  const taruviMeta = meta;
286
288
  const tableName = getTableName(resource, taruviMeta);
287
- const data = await Promise.all(
288
- ids.map(async (id) => {
289
- const response = await new Database(client).from(tableName).get(String(id)).update(variables).execute();
290
- return response.data;
291
- })
292
- );
293
- return { data };
289
+ const idColumn = getIdColumn(taruviMeta);
290
+ const body = ids.map((id) => ({ [idColumn]: id, ...variables }));
291
+ const response = await new Database(client).from(tableName).bulkUpdate(body).execute();
292
+ return { data: response.data };
294
293
  },
295
294
  deleteOne: async (params) => {
296
295
  const { resource, id, meta } = params;
297
296
  const taruviMeta = meta;
298
297
  const tableName = getTableName(resource, taruviMeta);
299
298
  if (isGraphQuery(taruviMeta)) {
300
- const response = await new Graph(client).from(tableName).delete([Number(id)]).execute();
299
+ const response = await new Database(client).from(tableName).edges().delete([Number(id)]).execute();
301
300
  return { data: response.data };
302
301
  }
303
302
  await new Database(client).from(tableName).delete(String(id)).execute();
@@ -308,14 +307,16 @@ function dataProvider(client) {
308
307
  const taruviMeta = meta;
309
308
  const tableName = getTableName(resource, taruviMeta);
310
309
  if (isGraphQuery(taruviMeta)) {
311
- const response = await new Graph(client).from(tableName).delete(ids.map(Number)).execute();
310
+ const response = await new Database(client).from(tableName).edges().delete(ids.map(Number)).execute();
312
311
  return { data: [response.data] };
313
312
  }
314
- await Promise.all(
315
- ids.map(async (id) => {
316
- await new Database(client).from(tableName).delete(String(id)).execute();
317
- })
318
- );
313
+ if (taruviMeta?.deleteByFilter && taruviMeta?.filters) {
314
+ let query = new Database(client).from(tableName);
315
+ query = applyFilters(query, taruviMeta.filters);
316
+ await query.deleteFiltered().execute();
317
+ return { data: ids.map((id) => ({ id })) };
318
+ }
319
+ await new Database(client).from(tableName).bulkDelete(ids.map(String)).execute();
319
320
  return { data: ids.map((id) => ({ id })) };
320
321
  },
321
322
  custom: async (params) => {
@@ -890,59 +891,23 @@ function authProvider(client) {
890
891
  },
891
892
  check: async () => {
892
893
  if (auth.isUserAuthenticated()) {
893
- if (auth.isTokenExpired()) {
894
- const refreshed = await auth.refreshAccessToken();
895
- if (refreshed) {
896
- return {
897
- authenticated: true
898
- };
899
- }
900
- return {
901
- authenticated: false,
902
- logout: true,
903
- redirectTo: "/login",
904
- error: {
905
- name: "SessionExpired",
906
- message: "Your session has expired. Please log in again."
907
- }
908
- };
909
- }
910
- return {
911
- authenticated: true
912
- };
894
+ return { authenticated: true };
913
895
  }
914
896
  return {
915
897
  authenticated: false,
916
898
  redirectTo: "/login"
917
899
  };
918
900
  },
919
- //TODO need to check if max retries logic is needed
920
901
  onError: async (error) => {
921
902
  const status = error?.statusCode || error?.status || error?.response?.status;
922
- if (status === 401) {
923
- if (auth.isUserAuthenticated()) {
924
- const refreshed = await auth.refreshAccessToken();
925
- if (refreshed) {
926
- return {
927
- error
928
- };
929
- }
930
- }
903
+ if (status === 401 || status === 403) {
931
904
  return {
932
905
  logout: true,
933
906
  redirectTo: "/login",
934
907
  error
935
908
  };
936
909
  }
937
- if (status === 403) {
938
- return {
939
- error
940
- // Don't logout for 403, user is authenticated but not authorized
941
- };
942
- }
943
- return {
944
- error
945
- };
910
+ return { error };
946
911
  },
947
912
  register: async (params = {}) => {
948
913
  const { callbackUrl } = params;