@xata.io/client 0.0.0-alpha.vf231460 → 0.0.0-alpha.vf6d8daa

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
@@ -14,6 +14,13 @@ function isObject(value) {
14
14
  function isString(value) {
15
15
  return value !== void 0 && value !== null && typeof value === "string";
16
16
  }
17
+ function toBase64(value) {
18
+ try {
19
+ return btoa(value);
20
+ } catch (err) {
21
+ return Buffer.from(value).toString("base64");
22
+ }
23
+ }
17
24
 
18
25
  function getEnvVariable(name) {
19
26
  try {
@@ -436,35 +443,35 @@ function isValidBuilder(builder) {
436
443
  return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
437
444
  }
438
445
 
439
- var __accessCheck$6 = (obj, member, msg) => {
446
+ var __accessCheck$7 = (obj, member, msg) => {
440
447
  if (!member.has(obj))
441
448
  throw TypeError("Cannot " + msg);
442
449
  };
443
- var __privateGet$5 = (obj, member, getter) => {
444
- __accessCheck$6(obj, member, "read from private field");
450
+ var __privateGet$6 = (obj, member, getter) => {
451
+ __accessCheck$7(obj, member, "read from private field");
445
452
  return getter ? getter.call(obj) : member.get(obj);
446
453
  };
447
- var __privateAdd$6 = (obj, member, value) => {
454
+ var __privateAdd$7 = (obj, member, value) => {
448
455
  if (member.has(obj))
449
456
  throw TypeError("Cannot add the same private member more than once");
450
457
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
451
458
  };
452
- var __privateSet$4 = (obj, member, value, setter) => {
453
- __accessCheck$6(obj, member, "write to private field");
459
+ var __privateSet$5 = (obj, member, value, setter) => {
460
+ __accessCheck$7(obj, member, "write to private field");
454
461
  setter ? setter.call(obj, value) : member.set(obj, value);
455
462
  return value;
456
463
  };
457
464
  var _extraProps, _namespaces;
458
465
  class XataApiClient {
459
466
  constructor(options = {}) {
460
- __privateAdd$6(this, _extraProps, void 0);
461
- __privateAdd$6(this, _namespaces, {});
467
+ __privateAdd$7(this, _extraProps, void 0);
468
+ __privateAdd$7(this, _namespaces, {});
462
469
  const provider = options.host ?? "production";
463
470
  const apiKey = options?.apiKey ?? getAPIKey();
464
471
  if (!apiKey) {
465
472
  throw new Error("Could not resolve a valid apiKey");
466
473
  }
467
- __privateSet$4(this, _extraProps, {
474
+ __privateSet$5(this, _extraProps, {
468
475
  apiUrl: getHostUrl(provider, "main"),
469
476
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
470
477
  fetchImpl: getFetchImplementation(options.fetch),
@@ -472,34 +479,34 @@ class XataApiClient {
472
479
  });
473
480
  }
474
481
  get user() {
475
- if (!__privateGet$5(this, _namespaces).user)
476
- __privateGet$5(this, _namespaces).user = new UserApi(__privateGet$5(this, _extraProps));
477
- return __privateGet$5(this, _namespaces).user;
482
+ if (!__privateGet$6(this, _namespaces).user)
483
+ __privateGet$6(this, _namespaces).user = new UserApi(__privateGet$6(this, _extraProps));
484
+ return __privateGet$6(this, _namespaces).user;
478
485
  }
479
486
  get workspaces() {
480
- if (!__privateGet$5(this, _namespaces).workspaces)
481
- __privateGet$5(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$5(this, _extraProps));
482
- return __privateGet$5(this, _namespaces).workspaces;
487
+ if (!__privateGet$6(this, _namespaces).workspaces)
488
+ __privateGet$6(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$6(this, _extraProps));
489
+ return __privateGet$6(this, _namespaces).workspaces;
483
490
  }
484
491
  get databases() {
485
- if (!__privateGet$5(this, _namespaces).databases)
486
- __privateGet$5(this, _namespaces).databases = new DatabaseApi(__privateGet$5(this, _extraProps));
487
- return __privateGet$5(this, _namespaces).databases;
492
+ if (!__privateGet$6(this, _namespaces).databases)
493
+ __privateGet$6(this, _namespaces).databases = new DatabaseApi(__privateGet$6(this, _extraProps));
494
+ return __privateGet$6(this, _namespaces).databases;
488
495
  }
489
496
  get branches() {
490
- if (!__privateGet$5(this, _namespaces).branches)
491
- __privateGet$5(this, _namespaces).branches = new BranchApi(__privateGet$5(this, _extraProps));
492
- return __privateGet$5(this, _namespaces).branches;
497
+ if (!__privateGet$6(this, _namespaces).branches)
498
+ __privateGet$6(this, _namespaces).branches = new BranchApi(__privateGet$6(this, _extraProps));
499
+ return __privateGet$6(this, _namespaces).branches;
493
500
  }
494
501
  get tables() {
495
- if (!__privateGet$5(this, _namespaces).tables)
496
- __privateGet$5(this, _namespaces).tables = new TableApi(__privateGet$5(this, _extraProps));
497
- return __privateGet$5(this, _namespaces).tables;
502
+ if (!__privateGet$6(this, _namespaces).tables)
503
+ __privateGet$6(this, _namespaces).tables = new TableApi(__privateGet$6(this, _extraProps));
504
+ return __privateGet$6(this, _namespaces).tables;
498
505
  }
499
506
  get records() {
500
- if (!__privateGet$5(this, _namespaces).records)
501
- __privateGet$5(this, _namespaces).records = new RecordsApi(__privateGet$5(this, _extraProps));
502
- return __privateGet$5(this, _namespaces).records;
507
+ if (!__privateGet$6(this, _namespaces).records)
508
+ __privateGet$6(this, _namespaces).records = new RecordsApi(__privateGet$6(this, _extraProps));
509
+ return __privateGet$6(this, _namespaces).records;
503
510
  }
504
511
  }
505
512
  _extraProps = new WeakMap();
@@ -854,43 +861,43 @@ class XataApiPlugin {
854
861
  class XataPlugin {
855
862
  }
856
863
 
857
- var __accessCheck$5 = (obj, member, msg) => {
864
+ var __accessCheck$6 = (obj, member, msg) => {
858
865
  if (!member.has(obj))
859
866
  throw TypeError("Cannot " + msg);
860
867
  };
861
- var __privateGet$4 = (obj, member, getter) => {
862
- __accessCheck$5(obj, member, "read from private field");
868
+ var __privateGet$5 = (obj, member, getter) => {
869
+ __accessCheck$6(obj, member, "read from private field");
863
870
  return getter ? getter.call(obj) : member.get(obj);
864
871
  };
865
- var __privateAdd$5 = (obj, member, value) => {
872
+ var __privateAdd$6 = (obj, member, value) => {
866
873
  if (member.has(obj))
867
874
  throw TypeError("Cannot add the same private member more than once");
868
875
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
869
876
  };
870
- var __privateSet$3 = (obj, member, value, setter) => {
871
- __accessCheck$5(obj, member, "write to private field");
877
+ var __privateSet$4 = (obj, member, value, setter) => {
878
+ __accessCheck$6(obj, member, "write to private field");
872
879
  setter ? setter.call(obj, value) : member.set(obj, value);
873
880
  return value;
874
881
  };
875
882
  var _query;
876
883
  class Page {
877
884
  constructor(query, meta, records = []) {
878
- __privateAdd$5(this, _query, void 0);
879
- __privateSet$3(this, _query, query);
885
+ __privateAdd$6(this, _query, void 0);
886
+ __privateSet$4(this, _query, query);
880
887
  this.meta = meta;
881
888
  this.records = records;
882
889
  }
883
890
  async nextPage(size, offset) {
884
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
891
+ return __privateGet$5(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
885
892
  }
886
893
  async previousPage(size, offset) {
887
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
894
+ return __privateGet$5(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
888
895
  }
889
896
  async firstPage(size, offset) {
890
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
897
+ return __privateGet$5(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
891
898
  }
892
899
  async lastPage(size, offset) {
893
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
900
+ return __privateGet$5(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
894
901
  }
895
902
  hasNextPage() {
896
903
  return this.meta.page.more;
@@ -902,46 +909,47 @@ const PAGINATION_DEFAULT_SIZE = 200;
902
909
  const PAGINATION_MAX_OFFSET = 800;
903
910
  const PAGINATION_DEFAULT_OFFSET = 0;
904
911
 
905
- var __accessCheck$4 = (obj, member, msg) => {
912
+ var __accessCheck$5 = (obj, member, msg) => {
906
913
  if (!member.has(obj))
907
914
  throw TypeError("Cannot " + msg);
908
915
  };
909
- var __privateGet$3 = (obj, member, getter) => {
910
- __accessCheck$4(obj, member, "read from private field");
916
+ var __privateGet$4 = (obj, member, getter) => {
917
+ __accessCheck$5(obj, member, "read from private field");
911
918
  return getter ? getter.call(obj) : member.get(obj);
912
919
  };
913
- var __privateAdd$4 = (obj, member, value) => {
920
+ var __privateAdd$5 = (obj, member, value) => {
914
921
  if (member.has(obj))
915
922
  throw TypeError("Cannot add the same private member more than once");
916
923
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
917
924
  };
918
- var __privateSet$2 = (obj, member, value, setter) => {
919
- __accessCheck$4(obj, member, "write to private field");
925
+ var __privateSet$3 = (obj, member, value, setter) => {
926
+ __accessCheck$5(obj, member, "write to private field");
920
927
  setter ? setter.call(obj, value) : member.set(obj, value);
921
928
  return value;
922
929
  };
923
930
  var _table$1, _repository, _data;
924
931
  const _Query = class {
925
932
  constructor(repository, table, data, parent) {
926
- __privateAdd$4(this, _table$1, void 0);
927
- __privateAdd$4(this, _repository, void 0);
928
- __privateAdd$4(this, _data, { filter: {} });
933
+ __privateAdd$5(this, _table$1, void 0);
934
+ __privateAdd$5(this, _repository, void 0);
935
+ __privateAdd$5(this, _data, { filter: {} });
929
936
  this.meta = { page: { cursor: "start", more: true } };
930
937
  this.records = [];
931
- __privateSet$2(this, _table$1, table);
938
+ __privateSet$3(this, _table$1, table);
932
939
  if (repository) {
933
- __privateSet$2(this, _repository, repository);
940
+ __privateSet$3(this, _repository, repository);
934
941
  } else {
935
- __privateSet$2(this, _repository, this);
942
+ __privateSet$3(this, _repository, this);
936
943
  }
937
- __privateGet$3(this, _data).filter = data.filter ?? parent?.filter ?? {};
938
- __privateGet$3(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
939
- __privateGet$3(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
940
- __privateGet$3(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
941
- __privateGet$3(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
942
- __privateGet$3(this, _data).sort = data.sort ?? parent?.sort;
943
- __privateGet$3(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
944
- __privateGet$3(this, _data).page = data.page ?? parent?.page;
944
+ __privateGet$4(this, _data).filter = data.filter ?? parent?.filter ?? {};
945
+ __privateGet$4(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
946
+ __privateGet$4(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
947
+ __privateGet$4(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
948
+ __privateGet$4(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
949
+ __privateGet$4(this, _data).sort = data.sort ?? parent?.sort;
950
+ __privateGet$4(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
951
+ __privateGet$4(this, _data).page = data.page ?? parent?.page;
952
+ __privateGet$4(this, _data).ttl = data.ttl ?? parent?.ttl;
945
953
  this.any = this.any.bind(this);
946
954
  this.all = this.all.bind(this);
947
955
  this.not = this.not.bind(this);
@@ -952,45 +960,50 @@ const _Query = class {
952
960
  Object.defineProperty(this, "repository", { enumerable: false });
953
961
  }
954
962
  getQueryOptions() {
955
- return __privateGet$3(this, _data);
963
+ return __privateGet$4(this, _data);
964
+ }
965
+ key() {
966
+ const { columns = [], filter = {}, sort = [], page = {} } = __privateGet$4(this, _data);
967
+ const key = JSON.stringify({ columns, filter, sort, page });
968
+ return toBase64(key);
956
969
  }
957
970
  any(...queries) {
958
971
  const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
959
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $any } }, __privateGet$3(this, _data));
972
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $any } }, __privateGet$4(this, _data));
960
973
  }
961
974
  all(...queries) {
962
975
  const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
963
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
976
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
964
977
  }
965
978
  not(...queries) {
966
979
  const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
967
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $not } }, __privateGet$3(this, _data));
980
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $not } }, __privateGet$4(this, _data));
968
981
  }
969
982
  none(...queries) {
970
983
  const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
971
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $none } }, __privateGet$3(this, _data));
984
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $none } }, __privateGet$4(this, _data));
972
985
  }
973
986
  filter(a, b) {
974
987
  if (arguments.length === 1) {
975
988
  const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
976
- const $all = compact([__privateGet$3(this, _data).filter?.$all].flat().concat(constraints));
977
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
989
+ const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat(constraints));
990
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
978
991
  } else {
979
- const $all = compact([__privateGet$3(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
980
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
992
+ const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
993
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
981
994
  }
982
995
  }
983
996
  sort(column, direction) {
984
- const originalSort = [__privateGet$3(this, _data).sort ?? []].flat();
997
+ const originalSort = [__privateGet$4(this, _data).sort ?? []].flat();
985
998
  const sort = [...originalSort, { column, direction }];
986
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { sort }, __privateGet$3(this, _data));
999
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { sort }, __privateGet$4(this, _data));
987
1000
  }
988
1001
  select(columns) {
989
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { columns }, __privateGet$3(this, _data));
1002
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { columns }, __privateGet$4(this, _data));
990
1003
  }
991
1004
  getPaginated(options = {}) {
992
- const query = new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), options, __privateGet$3(this, _data));
993
- return __privateGet$3(this, _repository).query(query);
1005
+ const query = new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), options, __privateGet$4(this, _data));
1006
+ return __privateGet$4(this, _repository).query(query);
994
1007
  }
995
1008
  async *[Symbol.asyncIterator]() {
996
1009
  for await (const [record] of this.getIterator(1)) {
@@ -1018,7 +1031,7 @@ const _Query = class {
1018
1031
  }
1019
1032
  return results;
1020
1033
  }
1021
- async getOne(options = {}) {
1034
+ async getFirst(options = {}) {
1022
1035
  const records = await this.getMany({ ...options, page: { size: 1 } });
1023
1036
  return records[0] || null;
1024
1037
  }
@@ -1073,75 +1086,92 @@ function buildSortFilter(filter) {
1073
1086
  }
1074
1087
  }
1075
1088
 
1076
- var __accessCheck$3 = (obj, member, msg) => {
1089
+ var __accessCheck$4 = (obj, member, msg) => {
1077
1090
  if (!member.has(obj))
1078
1091
  throw TypeError("Cannot " + msg);
1079
1092
  };
1080
- var __privateGet$2 = (obj, member, getter) => {
1081
- __accessCheck$3(obj, member, "read from private field");
1093
+ var __privateGet$3 = (obj, member, getter) => {
1094
+ __accessCheck$4(obj, member, "read from private field");
1082
1095
  return getter ? getter.call(obj) : member.get(obj);
1083
1096
  };
1084
- var __privateAdd$3 = (obj, member, value) => {
1097
+ var __privateAdd$4 = (obj, member, value) => {
1085
1098
  if (member.has(obj))
1086
1099
  throw TypeError("Cannot add the same private member more than once");
1087
1100
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1088
1101
  };
1089
- var __privateSet$1 = (obj, member, value, setter) => {
1090
- __accessCheck$3(obj, member, "write to private field");
1102
+ var __privateSet$2 = (obj, member, value, setter) => {
1103
+ __accessCheck$4(obj, member, "write to private field");
1091
1104
  setter ? setter.call(obj, value) : member.set(obj, value);
1092
1105
  return value;
1093
1106
  };
1094
1107
  var __privateMethod$2 = (obj, member, method) => {
1095
- __accessCheck$3(obj, member, "access private method");
1108
+ __accessCheck$4(obj, member, "access private method");
1096
1109
  return method;
1097
1110
  };
1098
- var _table, _links, _getFetchProps, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn;
1111
+ var _table, _links, _getFetchProps, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _invalidateCache, invalidateCache_fn, _setCacheRecord, setCacheRecord_fn, _getCacheRecord, getCacheRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn;
1099
1112
  class Repository extends Query {
1100
1113
  }
1101
1114
  class RestRepository extends Query {
1102
1115
  constructor(options) {
1103
1116
  super(null, options.table, {});
1104
- __privateAdd$3(this, _insertRecordWithoutId);
1105
- __privateAdd$3(this, _insertRecordWithId);
1106
- __privateAdd$3(this, _bulkInsertTableRecords);
1107
- __privateAdd$3(this, _updateRecordWithID);
1108
- __privateAdd$3(this, _upsertRecordWithID);
1109
- __privateAdd$3(this, _deleteRecord);
1110
- __privateAdd$3(this, _table, void 0);
1111
- __privateAdd$3(this, _links, void 0);
1112
- __privateAdd$3(this, _getFetchProps, void 0);
1113
- __privateSet$1(this, _table, options.table);
1114
- __privateSet$1(this, _links, options.links ?? {});
1115
- __privateSet$1(this, _getFetchProps, options.getFetchProps);
1117
+ __privateAdd$4(this, _insertRecordWithoutId);
1118
+ __privateAdd$4(this, _insertRecordWithId);
1119
+ __privateAdd$4(this, _bulkInsertTableRecords);
1120
+ __privateAdd$4(this, _updateRecordWithID);
1121
+ __privateAdd$4(this, _upsertRecordWithID);
1122
+ __privateAdd$4(this, _deleteRecord);
1123
+ __privateAdd$4(this, _invalidateCache);
1124
+ __privateAdd$4(this, _setCacheRecord);
1125
+ __privateAdd$4(this, _getCacheRecord);
1126
+ __privateAdd$4(this, _setCacheQuery);
1127
+ __privateAdd$4(this, _getCacheQuery);
1128
+ __privateAdd$4(this, _table, void 0);
1129
+ __privateAdd$4(this, _links, void 0);
1130
+ __privateAdd$4(this, _getFetchProps, void 0);
1131
+ __privateSet$2(this, _table, options.table);
1132
+ __privateSet$2(this, _links, options.links ?? {});
1133
+ __privateSet$2(this, _getFetchProps, options.pluginOptions.getFetchProps);
1116
1134
  this.db = options.db;
1135
+ this.cache = options.pluginOptions.cache;
1117
1136
  }
1118
1137
  async create(a, b) {
1119
1138
  if (Array.isArray(a)) {
1120
- return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1139
+ const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1140
+ await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1141
+ return records;
1121
1142
  }
1122
1143
  if (isString(a) && isObject(b)) {
1123
1144
  if (a === "")
1124
1145
  throw new Error("The id can't be empty");
1125
- return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1146
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1147
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1148
+ return record;
1126
1149
  }
1127
1150
  if (isObject(a) && isString(a.id)) {
1128
1151
  if (a.id === "")
1129
1152
  throw new Error("The id can't be empty");
1130
- return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
1153
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
1154
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1155
+ return record;
1131
1156
  }
1132
1157
  if (isObject(a)) {
1133
- return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1158
+ const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1159
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1160
+ return record;
1134
1161
  }
1135
1162
  throw new Error("Invalid arguments for create method");
1136
1163
  }
1137
1164
  async read(recordId) {
1138
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1165
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1166
+ if (cacheRecord)
1167
+ return cacheRecord;
1168
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1139
1169
  try {
1140
1170
  const response = await getRecord({
1141
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1171
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1142
1172
  ...fetchProps
1143
1173
  });
1144
- return initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), response);
1174
+ return initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), response);
1145
1175
  } catch (e) {
1146
1176
  if (isObject(e) && e.status === 404) {
1147
1177
  return null;
@@ -1157,10 +1187,16 @@ class RestRepository extends Query {
1157
1187
  return Promise.all(a.map((object) => this.update(object)));
1158
1188
  }
1159
1189
  if (isString(a) && isObject(b)) {
1160
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1190
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1191
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1192
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1193
+ return record;
1161
1194
  }
1162
1195
  if (isObject(a) && isString(a.id)) {
1163
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1196
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1197
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1198
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1199
+ return record;
1164
1200
  }
1165
1201
  throw new Error("Invalid arguments for update method");
1166
1202
  }
@@ -1172,41 +1208,52 @@ class RestRepository extends Query {
1172
1208
  return Promise.all(a.map((object) => this.createOrUpdate(object)));
1173
1209
  }
1174
1210
  if (isString(a) && isObject(b)) {
1175
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1211
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1212
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1213
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1214
+ return record;
1176
1215
  }
1177
1216
  if (isObject(a) && isString(a.id)) {
1178
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1217
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1218
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1219
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1220
+ return record;
1179
1221
  }
1180
1222
  throw new Error("Invalid arguments for createOrUpdate method");
1181
1223
  }
1182
- async delete(recordId) {
1183
- if (Array.isArray(recordId)) {
1184
- if (recordId.length > 100) {
1224
+ async delete(a) {
1225
+ if (Array.isArray(a)) {
1226
+ if (a.length > 100) {
1185
1227
  console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1186
1228
  }
1187
- await Promise.all(recordId.map((id) => this.delete(id)));
1229
+ await Promise.all(a.map((id) => this.delete(id)));
1188
1230
  return;
1189
1231
  }
1190
- if (isString(recordId)) {
1191
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId);
1232
+ if (isString(a)) {
1233
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
1234
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1192
1235
  return;
1193
1236
  }
1194
- if (isObject(recordId) && isString(recordId.id)) {
1195
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId.id);
1237
+ if (isObject(a) && isString(a.id)) {
1238
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
1239
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1196
1240
  return;
1197
1241
  }
1198
1242
  throw new Error("Invalid arguments for delete method");
1199
1243
  }
1200
1244
  async search(query, options = {}) {
1201
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1245
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1202
1246
  const { records } = await searchBranch({
1203
1247
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1204
- body: { tables: [__privateGet$2(this, _table)], query, fuzziness: options.fuzziness },
1248
+ body: { tables: [__privateGet$3(this, _table)], query, fuzziness: options.fuzziness },
1205
1249
  ...fetchProps
1206
1250
  });
1207
- return records.map((item) => initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), item));
1251
+ return records.map((item) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), item));
1208
1252
  }
1209
1253
  async query(query) {
1254
+ const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
1255
+ if (cacheQuery)
1256
+ return new Page(query, cacheQuery.meta, cacheQuery.records);
1210
1257
  const data = query.getQueryOptions();
1211
1258
  const body = {
1212
1259
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
@@ -1214,13 +1261,14 @@ class RestRepository extends Query {
1214
1261
  page: data.page,
1215
1262
  columns: data.columns
1216
1263
  };
1217
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1264
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1218
1265
  const { meta, records: objects } = await queryTable({
1219
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1266
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1220
1267
  body,
1221
1268
  ...fetchProps
1222
1269
  });
1223
- const records = objects.map((record) => initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), record));
1270
+ const records = objects.map((record) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), record));
1271
+ await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1224
1272
  return new Page(query, meta, records);
1225
1273
  }
1226
1274
  }
@@ -1229,13 +1277,13 @@ _links = new WeakMap();
1229
1277
  _getFetchProps = new WeakMap();
1230
1278
  _insertRecordWithoutId = new WeakSet();
1231
1279
  insertRecordWithoutId_fn = async function(object) {
1232
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1280
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1233
1281
  const record = transformObjectLinks(object);
1234
1282
  const response = await insertRecord({
1235
1283
  pathParams: {
1236
1284
  workspace: "{workspaceId}",
1237
1285
  dbBranchName: "{dbBranch}",
1238
- tableName: __privateGet$2(this, _table)
1286
+ tableName: __privateGet$3(this, _table)
1239
1287
  },
1240
1288
  body: record,
1241
1289
  ...fetchProps
@@ -1248,13 +1296,13 @@ insertRecordWithoutId_fn = async function(object) {
1248
1296
  };
1249
1297
  _insertRecordWithId = new WeakSet();
1250
1298
  insertRecordWithId_fn = async function(recordId, object) {
1251
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1299
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1252
1300
  const record = transformObjectLinks(object);
1253
1301
  const response = await insertRecordWithID({
1254
1302
  pathParams: {
1255
1303
  workspace: "{workspaceId}",
1256
1304
  dbBranchName: "{dbBranch}",
1257
- tableName: __privateGet$2(this, _table),
1305
+ tableName: __privateGet$3(this, _table),
1258
1306
  recordId
1259
1307
  },
1260
1308
  body: record,
@@ -1269,10 +1317,10 @@ insertRecordWithId_fn = async function(recordId, object) {
1269
1317
  };
1270
1318
  _bulkInsertTableRecords = new WeakSet();
1271
1319
  bulkInsertTableRecords_fn = async function(objects) {
1272
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1320
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1273
1321
  const records = objects.map((object) => transformObjectLinks(object));
1274
1322
  const response = await bulkInsertTableRecords({
1275
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1323
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1276
1324
  body: { records },
1277
1325
  ...fetchProps
1278
1326
  });
@@ -1284,10 +1332,10 @@ bulkInsertTableRecords_fn = async function(objects) {
1284
1332
  };
1285
1333
  _updateRecordWithID = new WeakSet();
1286
1334
  updateRecordWithID_fn = async function(recordId, object) {
1287
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1335
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1288
1336
  const record = transformObjectLinks(object);
1289
1337
  const response = await updateRecordWithID({
1290
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1338
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1291
1339
  body: record,
1292
1340
  ...fetchProps
1293
1341
  });
@@ -1298,9 +1346,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1298
1346
  };
1299
1347
  _upsertRecordWithID = new WeakSet();
1300
1348
  upsertRecordWithID_fn = async function(recordId, object) {
1301
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1349
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1302
1350
  const response = await upsertRecordWithID({
1303
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1351
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1304
1352
  body: object,
1305
1353
  ...fetchProps
1306
1354
  });
@@ -1311,12 +1359,47 @@ upsertRecordWithID_fn = async function(recordId, object) {
1311
1359
  };
1312
1360
  _deleteRecord = new WeakSet();
1313
1361
  deleteRecord_fn = async function(recordId) {
1314
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1362
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1315
1363
  await deleteRecord({
1316
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1364
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1317
1365
  ...fetchProps
1318
1366
  });
1319
1367
  };
1368
+ _invalidateCache = new WeakSet();
1369
+ invalidateCache_fn = async function(recordId) {
1370
+ await this.cache.delete(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1371
+ const cacheItems = await this.cache.getAll();
1372
+ const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1373
+ for (const [key, value] of queries) {
1374
+ const ids = getIds(value);
1375
+ if (ids.includes(recordId))
1376
+ await this.cache.delete(key);
1377
+ }
1378
+ };
1379
+ _setCacheRecord = new WeakSet();
1380
+ setCacheRecord_fn = async function(record) {
1381
+ await this.cache.set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
1382
+ };
1383
+ _getCacheRecord = new WeakSet();
1384
+ getCacheRecord_fn = async function(recordId) {
1385
+ return this.cache.get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1386
+ };
1387
+ _setCacheQuery = new WeakSet();
1388
+ setCacheQuery_fn = async function(query, meta, records) {
1389
+ await this.cache.set(`query_${__privateGet$3(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1390
+ };
1391
+ _getCacheQuery = new WeakSet();
1392
+ getCacheQuery_fn = async function(query) {
1393
+ const key = `query_${__privateGet$3(this, _table)}:${query.key()}`;
1394
+ const result = await this.cache.get(key);
1395
+ if (!result)
1396
+ return null;
1397
+ const { ttl = 0 } = query.getQueryOptions();
1398
+ if (!ttl || ttl < 0)
1399
+ return result;
1400
+ const hasExpired = result.date.getTime() + ttl < Date.now();
1401
+ return hasExpired ? null : result;
1402
+ };
1320
1403
  const transformObjectLinks = (object) => {
1321
1404
  return Object.entries(object).reduce((acc, [key, value]) => {
1322
1405
  if (key === "xata")
@@ -1350,6 +1433,57 @@ const initObject = (db, links, table, object) => {
1350
1433
  Object.freeze(result);
1351
1434
  return result;
1352
1435
  };
1436
+ function getIds(value) {
1437
+ if (Array.isArray(value)) {
1438
+ return value.map((item) => getIds(item)).flat();
1439
+ }
1440
+ if (!isObject(value))
1441
+ return [];
1442
+ const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
1443
+ return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
1444
+ }
1445
+
1446
+ var __accessCheck$3 = (obj, member, msg) => {
1447
+ if (!member.has(obj))
1448
+ throw TypeError("Cannot " + msg);
1449
+ };
1450
+ var __privateGet$2 = (obj, member, getter) => {
1451
+ __accessCheck$3(obj, member, "read from private field");
1452
+ return getter ? getter.call(obj) : member.get(obj);
1453
+ };
1454
+ var __privateAdd$3 = (obj, member, value) => {
1455
+ if (member.has(obj))
1456
+ throw TypeError("Cannot add the same private member more than once");
1457
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1458
+ };
1459
+ var __privateSet$1 = (obj, member, value, setter) => {
1460
+ __accessCheck$3(obj, member, "write to private field");
1461
+ setter ? setter.call(obj, value) : member.set(obj, value);
1462
+ return value;
1463
+ };
1464
+ var _map;
1465
+ class SimpleCache {
1466
+ constructor() {
1467
+ __privateAdd$3(this, _map, void 0);
1468
+ __privateSet$1(this, _map, /* @__PURE__ */ new Map());
1469
+ }
1470
+ async getAll() {
1471
+ return Object.fromEntries(__privateGet$2(this, _map));
1472
+ }
1473
+ async get(key) {
1474
+ return __privateGet$2(this, _map).get(key) ?? null;
1475
+ }
1476
+ async set(key, value) {
1477
+ __privateGet$2(this, _map).set(key, value);
1478
+ }
1479
+ async delete(key) {
1480
+ __privateGet$2(this, _map).delete(key);
1481
+ }
1482
+ async clear() {
1483
+ return __privateGet$2(this, _map).clear();
1484
+ }
1485
+ }
1486
+ _map = new WeakMap();
1353
1487
 
1354
1488
  const gt = (value) => ({ $gt: value });
1355
1489
  const ge = (value) => ({ $ge: value });
@@ -1391,20 +1525,20 @@ class SchemaPlugin extends XataPlugin {
1391
1525
  this.tableNames = tableNames;
1392
1526
  __privateAdd$2(this, _tables, {});
1393
1527
  }
1394
- build(options) {
1395
- const { getFetchProps } = options;
1528
+ build(pluginOptions) {
1396
1529
  const links = this.links;
1397
1530
  const db = new Proxy({}, {
1398
1531
  get: (_target, table) => {
1399
1532
  if (!isString(table))
1400
1533
  throw new Error("Invalid table name");
1401
- if (!__privateGet$1(this, _tables)[table])
1402
- __privateGet$1(this, _tables)[table] = new RestRepository({ db, getFetchProps, table, links });
1534
+ if (!__privateGet$1(this, _tables)[table]) {
1535
+ __privateGet$1(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, links });
1536
+ }
1403
1537
  return __privateGet$1(this, _tables)[table];
1404
1538
  }
1405
1539
  });
1406
1540
  for (const table of this.tableNames ?? []) {
1407
- db[table] = new RestRepository({ db, getFetchProps, table, links });
1541
+ db[table] = new RestRepository({ db, pluginOptions, table, links });
1408
1542
  }
1409
1543
  return db;
1410
1544
  }
@@ -1578,16 +1712,18 @@ const buildClient = (plugins) => {
1578
1712
  __privateAdd(this, _evaluateBranch);
1579
1713
  __privateAdd(this, _branch, void 0);
1580
1714
  const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
1581
- const db = new SchemaPlugin(links, tables).build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1582
- const search = new SearchPlugin(db, links ?? {}).build({
1583
- getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions)
1584
- });
1715
+ const pluginOptions = {
1716
+ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1717
+ cache: safeOptions.cache
1718
+ };
1719
+ const db = new SchemaPlugin(links, tables).build(pluginOptions);
1720
+ const search = new SearchPlugin(db, links ?? {}).build(pluginOptions);
1585
1721
  this.db = db;
1586
1722
  this.search = search;
1587
1723
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1588
1724
  if (!namespace)
1589
1725
  continue;
1590
- const result = namespace.build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1726
+ const result = namespace.build(pluginOptions);
1591
1727
  if (result instanceof Promise) {
1592
1728
  void result.then((namespace2) => {
1593
1729
  this[key] = namespace2;
@@ -1601,11 +1737,12 @@ const buildClient = (plugins) => {
1601
1737
  const fetch = getFetchImplementation(options?.fetch);
1602
1738
  const databaseURL = options?.databaseURL || getDatabaseURL();
1603
1739
  const apiKey = options?.apiKey || getAPIKey();
1740
+ const cache = options?.cache ?? new SimpleCache();
1604
1741
  const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1605
1742
  if (!databaseURL || !apiKey) {
1606
1743
  throw new Error("Options databaseURL and apiKey are required");
1607
1744
  }
1608
- return { fetch, databaseURL, apiKey, branch };
1745
+ return { fetch, databaseURL, apiKey, branch, cache };
1609
1746
  }, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
1610
1747
  fetch,
1611
1748
  apiKey,
@@ -1665,6 +1802,7 @@ exports.Repository = Repository;
1665
1802
  exports.RestRepository = RestRepository;
1666
1803
  exports.SchemaPlugin = SchemaPlugin;
1667
1804
  exports.SearchPlugin = SearchPlugin;
1805
+ exports.SimpleCache = SimpleCache;
1668
1806
  exports.XataApiClient = XataApiClient;
1669
1807
  exports.XataApiPlugin = XataApiPlugin;
1670
1808
  exports.XataError = XataError;