@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.cjs CHANGED
@@ -186,7 +186,7 @@ function isGraphQuery(meta) {
186
186
  return !!(meta?.format || meta?.graph_types || meta?.include || meta?.depth);
187
187
  }
188
188
  function buildGraphQuery(client, tableName, meta, recordId) {
189
- let query = new sdk.Graph(client).from(tableName);
189
+ let query = new sdk.Database(client).from(tableName);
190
190
  if (recordId) query = query.get(recordId);
191
191
  if (meta?.format) query = query.format(meta.format);
192
192
  if (meta?.include) query = query.include(meta.include);
@@ -257,10 +257,12 @@ function dataProvider(client) {
257
257
  const taruviMeta = meta;
258
258
  const tableName = getTableName(resource, taruviMeta);
259
259
  if (isGraphQuery(taruviMeta)) {
260
- const response2 = await new sdk.Graph(client).from(tableName).create(variables).execute();
260
+ const response2 = await new sdk.Database(client).from(tableName).edges().create(variables).execute();
261
261
  return { data: response2.data };
262
262
  }
263
- const response = await new sdk.Database(client).from(tableName).create(variables).execute();
263
+ const db = new sdk.Database(client).from(tableName);
264
+ const query = taruviMeta?.upsert ? db.upsert(variables) : db.create(variables);
265
+ const response = await query.execute();
264
266
  return { data: response.data };
265
267
  },
266
268
  createMany: async (params) => {
@@ -280,7 +282,7 @@ function dataProvider(client) {
280
282
  const taruviMeta = meta;
281
283
  const tableName = getTableName(resource, taruviMeta);
282
284
  if (isGraphQuery(taruviMeta)) {
283
- const response2 = await new sdk.Graph(client).from(tableName).update(String(id), variables).execute();
285
+ const response2 = await new sdk.Database(client).from(tableName).edges().get(String(id)).update(variables).execute();
284
286
  return { data: response2.data };
285
287
  }
286
288
  const response = await new sdk.Database(client).from(tableName).get(String(id)).update(variables).execute();
@@ -290,20 +292,17 @@ function dataProvider(client) {
290
292
  const { resource, ids, variables, meta } = params;
291
293
  const taruviMeta = meta;
292
294
  const tableName = getTableName(resource, taruviMeta);
293
- const data = await Promise.all(
294
- ids.map(async (id) => {
295
- const response = await new sdk.Database(client).from(tableName).get(String(id)).update(variables).execute();
296
- return response.data;
297
- })
298
- );
299
- return { data };
295
+ const idColumn = getIdColumn(taruviMeta);
296
+ const body = ids.map((id) => ({ [idColumn]: id, ...variables }));
297
+ const response = await new sdk.Database(client).from(tableName).bulkUpdate(body).execute();
298
+ return { data: response.data };
300
299
  },
301
300
  deleteOne: async (params) => {
302
301
  const { resource, id, meta } = params;
303
302
  const taruviMeta = meta;
304
303
  const tableName = getTableName(resource, taruviMeta);
305
304
  if (isGraphQuery(taruviMeta)) {
306
- const response = await new sdk.Graph(client).from(tableName).delete([Number(id)]).execute();
305
+ const response = await new sdk.Database(client).from(tableName).edges().delete([Number(id)]).execute();
307
306
  return { data: response.data };
308
307
  }
309
308
  await new sdk.Database(client).from(tableName).delete(String(id)).execute();
@@ -314,14 +313,16 @@ function dataProvider(client) {
314
313
  const taruviMeta = meta;
315
314
  const tableName = getTableName(resource, taruviMeta);
316
315
  if (isGraphQuery(taruviMeta)) {
317
- const response = await new sdk.Graph(client).from(tableName).delete(ids.map(Number)).execute();
316
+ const response = await new sdk.Database(client).from(tableName).edges().delete(ids.map(Number)).execute();
318
317
  return { data: [response.data] };
319
318
  }
320
- await Promise.all(
321
- ids.map(async (id) => {
322
- await new sdk.Database(client).from(tableName).delete(String(id)).execute();
323
- })
324
- );
319
+ if (taruviMeta?.deleteByFilter && taruviMeta?.filters) {
320
+ let query = new sdk.Database(client).from(tableName);
321
+ query = applyFilters(query, taruviMeta.filters);
322
+ await query.deleteFiltered().execute();
323
+ return { data: ids.map((id) => ({ id })) };
324
+ }
325
+ await new sdk.Database(client).from(tableName).bulkDelete(ids.map(String)).execute();
325
326
  return { data: ids.map((id) => ({ id })) };
326
327
  },
327
328
  custom: async (params) => {
@@ -896,59 +897,23 @@ function authProvider(client) {
896
897
  },
897
898
  check: async () => {
898
899
  if (auth.isUserAuthenticated()) {
899
- if (auth.isTokenExpired()) {
900
- const refreshed = await auth.refreshAccessToken();
901
- if (refreshed) {
902
- return {
903
- authenticated: true
904
- };
905
- }
906
- return {
907
- authenticated: false,
908
- logout: true,
909
- redirectTo: "/login",
910
- error: {
911
- name: "SessionExpired",
912
- message: "Your session has expired. Please log in again."
913
- }
914
- };
915
- }
916
- return {
917
- authenticated: true
918
- };
900
+ return { authenticated: true };
919
901
  }
920
902
  return {
921
903
  authenticated: false,
922
904
  redirectTo: "/login"
923
905
  };
924
906
  },
925
- //TODO need to check if max retries logic is needed
926
907
  onError: async (error) => {
927
908
  const status = error?.statusCode || error?.status || error?.response?.status;
928
- if (status === 401) {
929
- if (auth.isUserAuthenticated()) {
930
- const refreshed = await auth.refreshAccessToken();
931
- if (refreshed) {
932
- return {
933
- error
934
- };
935
- }
936
- }
909
+ if (status === 401 || status === 403) {
937
910
  return {
938
911
  logout: true,
939
912
  redirectTo: "/login",
940
913
  error
941
914
  };
942
915
  }
943
- if (status === 403) {
944
- return {
945
- error
946
- // Don't logout for 403, user is authenticated but not authorized
947
- };
948
- }
949
- return {
950
- error
951
- };
916
+ return { error };
952
917
  },
953
918
  register: async (params = {}) => {
954
919
  const { callbackUrl } = params;