@xata.io/client 0.0.0-alpha.vf2043e7 → 0.0.0-alpha.vf350c0a

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 {
@@ -31,7 +38,10 @@ function getEnvVariable(name) {
31
38
  }
32
39
  async function getGitBranch() {
33
40
  try {
34
- return require("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
41
+ if (typeof require === "function") {
42
+ const req = require;
43
+ return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
44
+ }
35
45
  } catch (err) {
36
46
  }
37
47
  try {
@@ -245,6 +255,14 @@ const deleteDatabase = (variables) => fetch$1({
245
255
  method: "delete",
246
256
  ...variables
247
257
  });
258
+ const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
259
+ const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
260
+ const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
261
+ const resolveBranch = (variables) => fetch$1({
262
+ url: "/dbs/{dbName}/resolveBranch",
263
+ method: "get",
264
+ ...variables
265
+ });
248
266
  const getBranchDetails = (variables) => fetch$1({
249
267
  url: "/db/{dbBranchName}",
250
268
  method: "get",
@@ -373,7 +391,15 @@ const operationsByTag = {
373
391
  resendWorkspaceMemberInvite,
374
392
  acceptWorkspaceMemberInvite
375
393
  },
376
- database: { getDatabaseList, createDatabase, deleteDatabase },
394
+ database: {
395
+ getDatabaseList,
396
+ createDatabase,
397
+ deleteDatabase,
398
+ getGitBranchesMapping,
399
+ addGitBranchesEntry,
400
+ removeGitBranchesEntry,
401
+ resolveBranch
402
+ },
377
403
  branch: {
378
404
  getBranchList,
379
405
  getBranchDetails,
@@ -436,35 +462,35 @@ function isValidBuilder(builder) {
436
462
  return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
437
463
  }
438
464
 
439
- var __accessCheck$6 = (obj, member, msg) => {
465
+ var __accessCheck$7 = (obj, member, msg) => {
440
466
  if (!member.has(obj))
441
467
  throw TypeError("Cannot " + msg);
442
468
  };
443
- var __privateGet$5 = (obj, member, getter) => {
444
- __accessCheck$6(obj, member, "read from private field");
469
+ var __privateGet$7 = (obj, member, getter) => {
470
+ __accessCheck$7(obj, member, "read from private field");
445
471
  return getter ? getter.call(obj) : member.get(obj);
446
472
  };
447
- var __privateAdd$6 = (obj, member, value) => {
473
+ var __privateAdd$7 = (obj, member, value) => {
448
474
  if (member.has(obj))
449
475
  throw TypeError("Cannot add the same private member more than once");
450
476
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
451
477
  };
452
- var __privateSet$4 = (obj, member, value, setter) => {
453
- __accessCheck$6(obj, member, "write to private field");
478
+ var __privateSet$6 = (obj, member, value, setter) => {
479
+ __accessCheck$7(obj, member, "write to private field");
454
480
  setter ? setter.call(obj, value) : member.set(obj, value);
455
481
  return value;
456
482
  };
457
483
  var _extraProps, _namespaces;
458
484
  class XataApiClient {
459
485
  constructor(options = {}) {
460
- __privateAdd$6(this, _extraProps, void 0);
461
- __privateAdd$6(this, _namespaces, {});
486
+ __privateAdd$7(this, _extraProps, void 0);
487
+ __privateAdd$7(this, _namespaces, {});
462
488
  const provider = options.host ?? "production";
463
489
  const apiKey = options?.apiKey ?? getAPIKey();
464
490
  if (!apiKey) {
465
491
  throw new Error("Could not resolve a valid apiKey");
466
492
  }
467
- __privateSet$4(this, _extraProps, {
493
+ __privateSet$6(this, _extraProps, {
468
494
  apiUrl: getHostUrl(provider, "main"),
469
495
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
470
496
  fetchImpl: getFetchImplementation(options.fetch),
@@ -472,34 +498,34 @@ class XataApiClient {
472
498
  });
473
499
  }
474
500
  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;
501
+ if (!__privateGet$7(this, _namespaces).user)
502
+ __privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
503
+ return __privateGet$7(this, _namespaces).user;
478
504
  }
479
505
  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;
506
+ if (!__privateGet$7(this, _namespaces).workspaces)
507
+ __privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
508
+ return __privateGet$7(this, _namespaces).workspaces;
483
509
  }
484
510
  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;
511
+ if (!__privateGet$7(this, _namespaces).databases)
512
+ __privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
513
+ return __privateGet$7(this, _namespaces).databases;
488
514
  }
489
515
  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;
516
+ if (!__privateGet$7(this, _namespaces).branches)
517
+ __privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
518
+ return __privateGet$7(this, _namespaces).branches;
493
519
  }
494
520
  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;
521
+ if (!__privateGet$7(this, _namespaces).tables)
522
+ __privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
523
+ return __privateGet$7(this, _namespaces).tables;
498
524
  }
499
525
  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;
526
+ if (!__privateGet$7(this, _namespaces).records)
527
+ __privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
528
+ return __privateGet$7(this, _namespaces).records;
503
529
  }
504
530
  }
505
531
  _extraProps = new WeakMap();
@@ -633,6 +659,33 @@ class DatabaseApi {
633
659
  ...this.extraProps
634
660
  });
635
661
  }
662
+ getGitBranchesMapping(workspace, dbName) {
663
+ return operationsByTag.database.getGitBranchesMapping({
664
+ pathParams: { workspace, dbName },
665
+ ...this.extraProps
666
+ });
667
+ }
668
+ addGitBranchesEntry(workspace, dbName, body) {
669
+ return operationsByTag.database.addGitBranchesEntry({
670
+ pathParams: { workspace, dbName },
671
+ body,
672
+ ...this.extraProps
673
+ });
674
+ }
675
+ removeGitBranchesEntry(workspace, dbName, gitBranch) {
676
+ return operationsByTag.database.removeGitBranchesEntry({
677
+ pathParams: { workspace, dbName },
678
+ queryParams: { gitBranch },
679
+ ...this.extraProps
680
+ });
681
+ }
682
+ resolveBranch(workspace, dbName, gitBranch) {
683
+ return operationsByTag.database.resolveBranch({
684
+ pathParams: { workspace, dbName },
685
+ queryParams: { gitBranch },
686
+ ...this.extraProps
687
+ });
688
+ }
636
689
  }
637
690
  class BranchApi {
638
691
  constructor(extraProps) {
@@ -854,43 +907,43 @@ class XataApiPlugin {
854
907
  class XataPlugin {
855
908
  }
856
909
 
857
- var __accessCheck$5 = (obj, member, msg) => {
910
+ var __accessCheck$6 = (obj, member, msg) => {
858
911
  if (!member.has(obj))
859
912
  throw TypeError("Cannot " + msg);
860
913
  };
861
- var __privateGet$4 = (obj, member, getter) => {
862
- __accessCheck$5(obj, member, "read from private field");
914
+ var __privateGet$6 = (obj, member, getter) => {
915
+ __accessCheck$6(obj, member, "read from private field");
863
916
  return getter ? getter.call(obj) : member.get(obj);
864
917
  };
865
- var __privateAdd$5 = (obj, member, value) => {
918
+ var __privateAdd$6 = (obj, member, value) => {
866
919
  if (member.has(obj))
867
920
  throw TypeError("Cannot add the same private member more than once");
868
921
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
869
922
  };
870
- var __privateSet$3 = (obj, member, value, setter) => {
871
- __accessCheck$5(obj, member, "write to private field");
923
+ var __privateSet$5 = (obj, member, value, setter) => {
924
+ __accessCheck$6(obj, member, "write to private field");
872
925
  setter ? setter.call(obj, value) : member.set(obj, value);
873
926
  return value;
874
927
  };
875
928
  var _query;
876
929
  class Page {
877
930
  constructor(query, meta, records = []) {
878
- __privateAdd$5(this, _query, void 0);
879
- __privateSet$3(this, _query, query);
931
+ __privateAdd$6(this, _query, void 0);
932
+ __privateSet$5(this, _query, query);
880
933
  this.meta = meta;
881
934
  this.records = records;
882
935
  }
883
936
  async nextPage(size, offset) {
884
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
937
+ return __privateGet$6(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
885
938
  }
886
939
  async previousPage(size, offset) {
887
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
940
+ return __privateGet$6(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
888
941
  }
889
942
  async firstPage(size, offset) {
890
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
943
+ return __privateGet$6(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
891
944
  }
892
945
  async lastPage(size, offset) {
893
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
946
+ return __privateGet$6(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
894
947
  }
895
948
  hasNextPage() {
896
949
  return this.meta.page.more;
@@ -902,46 +955,47 @@ const PAGINATION_DEFAULT_SIZE = 200;
902
955
  const PAGINATION_MAX_OFFSET = 800;
903
956
  const PAGINATION_DEFAULT_OFFSET = 0;
904
957
 
905
- var __accessCheck$4 = (obj, member, msg) => {
958
+ var __accessCheck$5 = (obj, member, msg) => {
906
959
  if (!member.has(obj))
907
960
  throw TypeError("Cannot " + msg);
908
961
  };
909
- var __privateGet$3 = (obj, member, getter) => {
910
- __accessCheck$4(obj, member, "read from private field");
962
+ var __privateGet$5 = (obj, member, getter) => {
963
+ __accessCheck$5(obj, member, "read from private field");
911
964
  return getter ? getter.call(obj) : member.get(obj);
912
965
  };
913
- var __privateAdd$4 = (obj, member, value) => {
966
+ var __privateAdd$5 = (obj, member, value) => {
914
967
  if (member.has(obj))
915
968
  throw TypeError("Cannot add the same private member more than once");
916
969
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
917
970
  };
918
- var __privateSet$2 = (obj, member, value, setter) => {
919
- __accessCheck$4(obj, member, "write to private field");
971
+ var __privateSet$4 = (obj, member, value, setter) => {
972
+ __accessCheck$5(obj, member, "write to private field");
920
973
  setter ? setter.call(obj, value) : member.set(obj, value);
921
974
  return value;
922
975
  };
923
976
  var _table$1, _repository, _data;
924
977
  const _Query = class {
925
978
  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: {} });
979
+ __privateAdd$5(this, _table$1, void 0);
980
+ __privateAdd$5(this, _repository, void 0);
981
+ __privateAdd$5(this, _data, { filter: {} });
929
982
  this.meta = { page: { cursor: "start", more: true } };
930
983
  this.records = [];
931
- __privateSet$2(this, _table$1, table);
984
+ __privateSet$4(this, _table$1, table);
932
985
  if (repository) {
933
- __privateSet$2(this, _repository, repository);
986
+ __privateSet$4(this, _repository, repository);
934
987
  } else {
935
- __privateSet$2(this, _repository, this);
988
+ __privateSet$4(this, _repository, this);
936
989
  }
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;
990
+ __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
991
+ __privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
992
+ __privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
993
+ __privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
994
+ __privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
995
+ __privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
996
+ __privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
997
+ __privateGet$5(this, _data).page = data.page ?? parent?.page;
998
+ __privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
945
999
  this.any = this.any.bind(this);
946
1000
  this.all = this.all.bind(this);
947
1001
  this.not = this.not.bind(this);
@@ -952,45 +1006,50 @@ const _Query = class {
952
1006
  Object.defineProperty(this, "repository", { enumerable: false });
953
1007
  }
954
1008
  getQueryOptions() {
955
- return __privateGet$3(this, _data);
1009
+ return __privateGet$5(this, _data);
1010
+ }
1011
+ key() {
1012
+ const { columns = [], filter = {}, sort = [], page = {} } = __privateGet$5(this, _data);
1013
+ const key = JSON.stringify({ columns, filter, sort, page });
1014
+ return toBase64(key);
956
1015
  }
957
1016
  any(...queries) {
958
1017
  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));
1018
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
960
1019
  }
961
1020
  all(...queries) {
962
1021
  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));
1022
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
964
1023
  }
965
1024
  not(...queries) {
966
1025
  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));
1026
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
968
1027
  }
969
1028
  none(...queries) {
970
1029
  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));
1030
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
972
1031
  }
973
1032
  filter(a, b) {
974
1033
  if (arguments.length === 1) {
975
1034
  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));
1035
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
1036
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
978
1037
  } 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));
1038
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1039
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
981
1040
  }
982
1041
  }
983
1042
  sort(column, direction) {
984
- const originalSort = [__privateGet$3(this, _data).sort ?? []].flat();
1043
+ const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
985
1044
  const sort = [...originalSort, { column, direction }];
986
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { sort }, __privateGet$3(this, _data));
1045
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
987
1046
  }
988
1047
  select(columns) {
989
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { columns }, __privateGet$3(this, _data));
1048
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
990
1049
  }
991
1050
  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);
1051
+ const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
1052
+ return __privateGet$5(this, _repository).query(query);
994
1053
  }
995
1054
  async *[Symbol.asyncIterator]() {
996
1055
  for await (const [record] of this.getIterator(1)) {
@@ -1018,10 +1077,13 @@ const _Query = class {
1018
1077
  }
1019
1078
  return results;
1020
1079
  }
1021
- async getOne(options = {}) {
1080
+ async getFirst(options = {}) {
1022
1081
  const records = await this.getMany({ ...options, page: { size: 1 } });
1023
1082
  return records[0] || null;
1024
1083
  }
1084
+ cache(ttl) {
1085
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
1086
+ }
1025
1087
  nextPage(size, offset) {
1026
1088
  return this.firstPage(size, offset);
1027
1089
  }
@@ -1073,75 +1135,94 @@ function buildSortFilter(filter) {
1073
1135
  }
1074
1136
  }
1075
1137
 
1076
- var __accessCheck$3 = (obj, member, msg) => {
1138
+ var __accessCheck$4 = (obj, member, msg) => {
1077
1139
  if (!member.has(obj))
1078
1140
  throw TypeError("Cannot " + msg);
1079
1141
  };
1080
- var __privateGet$2 = (obj, member, getter) => {
1081
- __accessCheck$3(obj, member, "read from private field");
1142
+ var __privateGet$4 = (obj, member, getter) => {
1143
+ __accessCheck$4(obj, member, "read from private field");
1082
1144
  return getter ? getter.call(obj) : member.get(obj);
1083
1145
  };
1084
- var __privateAdd$3 = (obj, member, value) => {
1146
+ var __privateAdd$4 = (obj, member, value) => {
1085
1147
  if (member.has(obj))
1086
1148
  throw TypeError("Cannot add the same private member more than once");
1087
1149
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1088
1150
  };
1089
- var __privateSet$1 = (obj, member, value, setter) => {
1090
- __accessCheck$3(obj, member, "write to private field");
1151
+ var __privateSet$3 = (obj, member, value, setter) => {
1152
+ __accessCheck$4(obj, member, "write to private field");
1091
1153
  setter ? setter.call(obj, value) : member.set(obj, value);
1092
1154
  return value;
1093
1155
  };
1094
1156
  var __privateMethod$2 = (obj, member, method) => {
1095
- __accessCheck$3(obj, member, "access private method");
1157
+ __accessCheck$4(obj, member, "access private method");
1096
1158
  return method;
1097
1159
  };
1098
- var _table, _links, _getFetchProps, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn;
1160
+ var _table, _getFetchProps, _cache, _schemaColumns, _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, _getSchemaColumns, getSchemaColumns_fn;
1099
1161
  class Repository extends Query {
1100
1162
  }
1101
1163
  class RestRepository extends Query {
1102
1164
  constructor(options) {
1103
1165
  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);
1166
+ __privateAdd$4(this, _insertRecordWithoutId);
1167
+ __privateAdd$4(this, _insertRecordWithId);
1168
+ __privateAdd$4(this, _bulkInsertTableRecords);
1169
+ __privateAdd$4(this, _updateRecordWithID);
1170
+ __privateAdd$4(this, _upsertRecordWithID);
1171
+ __privateAdd$4(this, _deleteRecord);
1172
+ __privateAdd$4(this, _invalidateCache);
1173
+ __privateAdd$4(this, _setCacheRecord);
1174
+ __privateAdd$4(this, _getCacheRecord);
1175
+ __privateAdd$4(this, _setCacheQuery);
1176
+ __privateAdd$4(this, _getCacheQuery);
1177
+ __privateAdd$4(this, _getSchemaColumns);
1178
+ __privateAdd$4(this, _table, void 0);
1179
+ __privateAdd$4(this, _getFetchProps, void 0);
1180
+ __privateAdd$4(this, _cache, void 0);
1181
+ __privateAdd$4(this, _schemaColumns, void 0);
1182
+ __privateSet$3(this, _table, options.table);
1183
+ __privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
1116
1184
  this.db = options.db;
1185
+ __privateSet$3(this, _cache, options.pluginOptions.cache);
1117
1186
  }
1118
1187
  async create(a, b) {
1119
1188
  if (Array.isArray(a)) {
1120
- return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1189
+ const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1190
+ await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1191
+ return records;
1121
1192
  }
1122
1193
  if (isString(a) && isObject(b)) {
1123
1194
  if (a === "")
1124
1195
  throw new Error("The id can't be empty");
1125
- return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1196
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1197
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1198
+ return record;
1126
1199
  }
1127
1200
  if (isObject(a) && isString(a.id)) {
1128
1201
  if (a.id === "")
1129
1202
  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 });
1203
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
1204
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1205
+ return record;
1131
1206
  }
1132
1207
  if (isObject(a)) {
1133
- return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1208
+ const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1209
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1210
+ return record;
1134
1211
  }
1135
1212
  throw new Error("Invalid arguments for create method");
1136
1213
  }
1137
1214
  async read(recordId) {
1138
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1215
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1216
+ if (cacheRecord)
1217
+ return cacheRecord;
1218
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1139
1219
  try {
1140
1220
  const response = await getRecord({
1141
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1221
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1142
1222
  ...fetchProps
1143
1223
  });
1144
- return initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), response);
1224
+ const columns = await __privateMethod$2(this, _getSchemaColumns, getSchemaColumns_fn).call(this);
1225
+ return initObject(this.db, columns, __privateGet$4(this, _table), response);
1145
1226
  } catch (e) {
1146
1227
  if (isObject(e) && e.status === 404) {
1147
1228
  return null;
@@ -1157,10 +1238,16 @@ class RestRepository extends Query {
1157
1238
  return Promise.all(a.map((object) => this.update(object)));
1158
1239
  }
1159
1240
  if (isString(a) && isObject(b)) {
1160
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1241
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1242
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1243
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1244
+ return record;
1161
1245
  }
1162
1246
  if (isObject(a) && isString(a.id)) {
1163
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1247
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1248
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1249
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1250
+ return record;
1164
1251
  }
1165
1252
  throw new Error("Invalid arguments for update method");
1166
1253
  }
@@ -1172,41 +1259,53 @@ class RestRepository extends Query {
1172
1259
  return Promise.all(a.map((object) => this.createOrUpdate(object)));
1173
1260
  }
1174
1261
  if (isString(a) && isObject(b)) {
1175
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1262
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1263
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1264
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1265
+ return record;
1176
1266
  }
1177
1267
  if (isObject(a) && isString(a.id)) {
1178
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1268
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1269
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1270
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1271
+ return record;
1179
1272
  }
1180
1273
  throw new Error("Invalid arguments for createOrUpdate method");
1181
1274
  }
1182
- async delete(recordId) {
1183
- if (Array.isArray(recordId)) {
1184
- if (recordId.length > 100) {
1275
+ async delete(a) {
1276
+ if (Array.isArray(a)) {
1277
+ if (a.length > 100) {
1185
1278
  console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1186
1279
  }
1187
- await Promise.all(recordId.map((id) => this.delete(id)));
1280
+ await Promise.all(a.map((id) => this.delete(id)));
1188
1281
  return;
1189
1282
  }
1190
- if (isString(recordId)) {
1191
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId);
1283
+ if (isString(a)) {
1284
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
1285
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1192
1286
  return;
1193
1287
  }
1194
- if (isObject(recordId) && isString(recordId.id)) {
1195
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId.id);
1288
+ if (isObject(a) && isString(a.id)) {
1289
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
1290
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1196
1291
  return;
1197
1292
  }
1198
1293
  throw new Error("Invalid arguments for delete method");
1199
1294
  }
1200
1295
  async search(query, options = {}) {
1201
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1296
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1202
1297
  const { records } = await searchBranch({
1203
1298
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1204
- body: { tables: [__privateGet$2(this, _table)], query, fuzziness: options.fuzziness },
1299
+ body: { tables: [__privateGet$4(this, _table)], query, fuzziness: options.fuzziness },
1205
1300
  ...fetchProps
1206
1301
  });
1207
- return records.map((item) => initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), item));
1302
+ const columns = await __privateMethod$2(this, _getSchemaColumns, getSchemaColumns_fn).call(this);
1303
+ return records.map((item) => initObject(this.db, columns, __privateGet$4(this, _table), item));
1208
1304
  }
1209
1305
  async query(query) {
1306
+ const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
1307
+ if (cacheQuery)
1308
+ return new Page(query, cacheQuery.meta, cacheQuery.records);
1210
1309
  const data = query.getQueryOptions();
1211
1310
  const body = {
1212
1311
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
@@ -1214,28 +1313,31 @@ class RestRepository extends Query {
1214
1313
  page: data.page,
1215
1314
  columns: data.columns
1216
1315
  };
1217
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1316
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1218
1317
  const { meta, records: objects } = await queryTable({
1219
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1318
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1220
1319
  body,
1221
1320
  ...fetchProps
1222
1321
  });
1223
- const records = objects.map((record) => initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), record));
1322
+ const columns = await __privateMethod$2(this, _getSchemaColumns, getSchemaColumns_fn).call(this);
1323
+ const records = objects.map((record) => initObject(this.db, columns, __privateGet$4(this, _table), record));
1324
+ await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1224
1325
  return new Page(query, meta, records);
1225
1326
  }
1226
1327
  }
1227
1328
  _table = new WeakMap();
1228
- _links = new WeakMap();
1229
1329
  _getFetchProps = new WeakMap();
1330
+ _cache = new WeakMap();
1331
+ _schemaColumns = new WeakMap();
1230
1332
  _insertRecordWithoutId = new WeakSet();
1231
1333
  insertRecordWithoutId_fn = async function(object) {
1232
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1334
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1233
1335
  const record = transformObjectLinks(object);
1234
1336
  const response = await insertRecord({
1235
1337
  pathParams: {
1236
1338
  workspace: "{workspaceId}",
1237
1339
  dbBranchName: "{dbBranch}",
1238
- tableName: __privateGet$2(this, _table)
1340
+ tableName: __privateGet$4(this, _table)
1239
1341
  },
1240
1342
  body: record,
1241
1343
  ...fetchProps
@@ -1248,13 +1350,13 @@ insertRecordWithoutId_fn = async function(object) {
1248
1350
  };
1249
1351
  _insertRecordWithId = new WeakSet();
1250
1352
  insertRecordWithId_fn = async function(recordId, object) {
1251
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1353
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1252
1354
  const record = transformObjectLinks(object);
1253
1355
  const response = await insertRecordWithID({
1254
1356
  pathParams: {
1255
1357
  workspace: "{workspaceId}",
1256
1358
  dbBranchName: "{dbBranch}",
1257
- tableName: __privateGet$2(this, _table),
1359
+ tableName: __privateGet$4(this, _table),
1258
1360
  recordId
1259
1361
  },
1260
1362
  body: record,
@@ -1269,10 +1371,10 @@ insertRecordWithId_fn = async function(recordId, object) {
1269
1371
  };
1270
1372
  _bulkInsertTableRecords = new WeakSet();
1271
1373
  bulkInsertTableRecords_fn = async function(objects) {
1272
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1374
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1273
1375
  const records = objects.map((object) => transformObjectLinks(object));
1274
1376
  const response = await bulkInsertTableRecords({
1275
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1377
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1276
1378
  body: { records },
1277
1379
  ...fetchProps
1278
1380
  });
@@ -1284,10 +1386,10 @@ bulkInsertTableRecords_fn = async function(objects) {
1284
1386
  };
1285
1387
  _updateRecordWithID = new WeakSet();
1286
1388
  updateRecordWithID_fn = async function(recordId, object) {
1287
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1389
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1288
1390
  const record = transformObjectLinks(object);
1289
1391
  const response = await updateRecordWithID({
1290
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1392
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1291
1393
  body: record,
1292
1394
  ...fetchProps
1293
1395
  });
@@ -1298,9 +1400,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1298
1400
  };
1299
1401
  _upsertRecordWithID = new WeakSet();
1300
1402
  upsertRecordWithID_fn = async function(recordId, object) {
1301
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1403
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1302
1404
  const response = await upsertRecordWithID({
1303
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1405
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1304
1406
  body: object,
1305
1407
  ...fetchProps
1306
1408
  });
@@ -1311,12 +1413,62 @@ upsertRecordWithID_fn = async function(recordId, object) {
1311
1413
  };
1312
1414
  _deleteRecord = new WeakSet();
1313
1415
  deleteRecord_fn = async function(recordId) {
1314
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1416
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1315
1417
  await deleteRecord({
1316
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1418
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1317
1419
  ...fetchProps
1318
1420
  });
1319
1421
  };
1422
+ _invalidateCache = new WeakSet();
1423
+ invalidateCache_fn = async function(recordId) {
1424
+ await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1425
+ const cacheItems = await __privateGet$4(this, _cache).getAll();
1426
+ const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1427
+ for (const [key, value] of queries) {
1428
+ const ids = getIds(value);
1429
+ if (ids.includes(recordId))
1430
+ await __privateGet$4(this, _cache).delete(key);
1431
+ }
1432
+ };
1433
+ _setCacheRecord = new WeakSet();
1434
+ setCacheRecord_fn = async function(record) {
1435
+ if (!__privateGet$4(this, _cache).cacheRecords)
1436
+ return;
1437
+ await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
1438
+ };
1439
+ _getCacheRecord = new WeakSet();
1440
+ getCacheRecord_fn = async function(recordId) {
1441
+ if (!__privateGet$4(this, _cache).cacheRecords)
1442
+ return null;
1443
+ return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1444
+ };
1445
+ _setCacheQuery = new WeakSet();
1446
+ setCacheQuery_fn = async function(query, meta, records) {
1447
+ await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1448
+ };
1449
+ _getCacheQuery = new WeakSet();
1450
+ getCacheQuery_fn = async function(query) {
1451
+ const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
1452
+ const result = await __privateGet$4(this, _cache).get(key);
1453
+ if (!result)
1454
+ return null;
1455
+ const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1456
+ if (!ttl || ttl < 0)
1457
+ return result;
1458
+ const hasExpired = result.date.getTime() + ttl < Date.now();
1459
+ return hasExpired ? null : result;
1460
+ };
1461
+ _getSchemaColumns = new WeakSet();
1462
+ getSchemaColumns_fn = async function() {
1463
+ if (__privateGet$4(this, _schemaColumns))
1464
+ return __privateGet$4(this, _schemaColumns);
1465
+ const { columns } = await getTableSchema({
1466
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1467
+ ...await __privateGet$4(this, _getFetchProps).call(this)
1468
+ });
1469
+ __privateSet$3(this, _schemaColumns, columns);
1470
+ return columns;
1471
+ };
1320
1472
  const transformObjectLinks = (object) => {
1321
1473
  return Object.entries(object).reduce((acc, [key, value]) => {
1322
1474
  if (key === "xata")
@@ -1324,15 +1476,30 @@ const transformObjectLinks = (object) => {
1324
1476
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1325
1477
  }, {});
1326
1478
  };
1327
- const initObject = (db, links, table, object) => {
1479
+ const initObject = (db, columns, table, object) => {
1328
1480
  const result = {};
1329
1481
  Object.assign(result, object);
1330
- const tableLinks = links[table] || [];
1331
- for (const link of tableLinks) {
1332
- const [field, linkTable] = link;
1333
- const value = result[field];
1334
- if (value && isObject(value)) {
1335
- result[field] = initObject(db, links, linkTable, value);
1482
+ for (const column of columns) {
1483
+ const value = result[column.name];
1484
+ switch (column.type) {
1485
+ case "datetime": {
1486
+ const date = new Date(value);
1487
+ if (isNaN(date.getTime())) {
1488
+ console.error(`Failed to parse date ${value} for field ${column.name}`);
1489
+ } else {
1490
+ result[column.name] = date;
1491
+ }
1492
+ break;
1493
+ }
1494
+ case "link": {
1495
+ const linkTable = column.link?.table;
1496
+ if (!linkTable) {
1497
+ console.error(`Failed to parse link for field ${column.name}`);
1498
+ } else if (value && isObject(value)) {
1499
+ result[column.name] = initObject(db, columns, linkTable, value);
1500
+ }
1501
+ break;
1502
+ }
1336
1503
  }
1337
1504
  }
1338
1505
  result.read = function() {
@@ -1350,6 +1517,65 @@ const initObject = (db, links, table, object) => {
1350
1517
  Object.freeze(result);
1351
1518
  return result;
1352
1519
  };
1520
+ function getIds(value) {
1521
+ if (Array.isArray(value)) {
1522
+ return value.map((item) => getIds(item)).flat();
1523
+ }
1524
+ if (!isObject(value))
1525
+ return [];
1526
+ const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
1527
+ return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
1528
+ }
1529
+
1530
+ var __accessCheck$3 = (obj, member, msg) => {
1531
+ if (!member.has(obj))
1532
+ throw TypeError("Cannot " + msg);
1533
+ };
1534
+ var __privateGet$3 = (obj, member, getter) => {
1535
+ __accessCheck$3(obj, member, "read from private field");
1536
+ return getter ? getter.call(obj) : member.get(obj);
1537
+ };
1538
+ var __privateAdd$3 = (obj, member, value) => {
1539
+ if (member.has(obj))
1540
+ throw TypeError("Cannot add the same private member more than once");
1541
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1542
+ };
1543
+ var __privateSet$2 = (obj, member, value, setter) => {
1544
+ __accessCheck$3(obj, member, "write to private field");
1545
+ setter ? setter.call(obj, value) : member.set(obj, value);
1546
+ return value;
1547
+ };
1548
+ var _map;
1549
+ class SimpleCache {
1550
+ constructor(options = {}) {
1551
+ __privateAdd$3(this, _map, void 0);
1552
+ __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1553
+ this.capacity = options.max ?? 500;
1554
+ this.cacheRecords = options.cacheRecords ?? true;
1555
+ this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1556
+ }
1557
+ async getAll() {
1558
+ return Object.fromEntries(__privateGet$3(this, _map));
1559
+ }
1560
+ async get(key) {
1561
+ return __privateGet$3(this, _map).get(key) ?? null;
1562
+ }
1563
+ async set(key, value) {
1564
+ await this.delete(key);
1565
+ __privateGet$3(this, _map).set(key, value);
1566
+ if (__privateGet$3(this, _map).size > this.capacity) {
1567
+ const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
1568
+ await this.delete(leastRecentlyUsed);
1569
+ }
1570
+ }
1571
+ async delete(key) {
1572
+ __privateGet$3(this, _map).delete(key);
1573
+ }
1574
+ async clear() {
1575
+ return __privateGet$3(this, _map).clear();
1576
+ }
1577
+ }
1578
+ _map = new WeakMap();
1353
1579
 
1354
1580
  const gt = (value) => ({ $gt: value });
1355
1581
  const ge = (value) => ({ $ge: value });
@@ -1374,7 +1600,7 @@ var __accessCheck$2 = (obj, member, msg) => {
1374
1600
  if (!member.has(obj))
1375
1601
  throw TypeError("Cannot " + msg);
1376
1602
  };
1377
- var __privateGet$1 = (obj, member, getter) => {
1603
+ var __privateGet$2 = (obj, member, getter) => {
1378
1604
  __accessCheck$2(obj, member, "read from private field");
1379
1605
  return getter ? getter.call(obj) : member.get(obj);
1380
1606
  };
@@ -1385,26 +1611,24 @@ var __privateAdd$2 = (obj, member, value) => {
1385
1611
  };
1386
1612
  var _tables;
1387
1613
  class SchemaPlugin extends XataPlugin {
1388
- constructor(links, tableNames) {
1614
+ constructor(tableNames) {
1389
1615
  super();
1390
- this.links = links;
1391
1616
  this.tableNames = tableNames;
1392
1617
  __privateAdd$2(this, _tables, {});
1393
1618
  }
1394
- build(options) {
1395
- const { getFetchProps } = options;
1396
- const links = this.links;
1619
+ build(pluginOptions) {
1397
1620
  const db = new Proxy({}, {
1398
1621
  get: (_target, table) => {
1399
1622
  if (!isString(table))
1400
1623
  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 });
1403
- return __privateGet$1(this, _tables)[table];
1624
+ if (!__privateGet$2(this, _tables)[table]) {
1625
+ __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1626
+ }
1627
+ return __privateGet$2(this, _tables)[table];
1404
1628
  }
1405
1629
  });
1406
1630
  for (const table of this.tableNames ?? []) {
1407
- db[table] = new RestRepository({ db, getFetchProps, table, links });
1631
+ db[table] = new RestRepository({ db, pluginOptions, table });
1408
1632
  }
1409
1633
  return db;
1410
1634
  }
@@ -1415,44 +1639,65 @@ var __accessCheck$1 = (obj, member, msg) => {
1415
1639
  if (!member.has(obj))
1416
1640
  throw TypeError("Cannot " + msg);
1417
1641
  };
1642
+ var __privateGet$1 = (obj, member, getter) => {
1643
+ __accessCheck$1(obj, member, "read from private field");
1644
+ return getter ? getter.call(obj) : member.get(obj);
1645
+ };
1418
1646
  var __privateAdd$1 = (obj, member, value) => {
1419
1647
  if (member.has(obj))
1420
1648
  throw TypeError("Cannot add the same private member more than once");
1421
1649
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1422
1650
  };
1651
+ var __privateSet$1 = (obj, member, value, setter) => {
1652
+ __accessCheck$1(obj, member, "write to private field");
1653
+ setter ? setter.call(obj, value) : member.set(obj, value);
1654
+ return value;
1655
+ };
1423
1656
  var __privateMethod$1 = (obj, member, method) => {
1424
1657
  __accessCheck$1(obj, member, "access private method");
1425
1658
  return method;
1426
1659
  };
1427
- var _search, search_fn;
1660
+ var _schema, _search, search_fn, _getSchema, getSchema_fn;
1428
1661
  class SearchPlugin extends XataPlugin {
1429
- constructor(db, links) {
1662
+ constructor(db) {
1430
1663
  super();
1431
1664
  this.db = db;
1432
- this.links = links;
1433
1665
  __privateAdd$1(this, _search);
1666
+ __privateAdd$1(this, _getSchema);
1667
+ __privateAdd$1(this, _schema, void 0);
1434
1668
  }
1435
1669
  build({ getFetchProps }) {
1436
1670
  return {
1437
1671
  all: async (query, options = {}) => {
1438
1672
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1673
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1439
1674
  return records.map((record) => {
1440
1675
  const { table = "orphan" } = record.xata;
1441
- return { table, record: initObject(this.db, this.links, table, record) };
1676
+ const columns = schema.tables.find((t) => t.name === table)?.columns;
1677
+ if (!columns) {
1678
+ console.error(`No schema columns found for table ${table}`);
1679
+ }
1680
+ return { table, record: initObject(this.db, columns ?? [], table, record) };
1442
1681
  });
1443
1682
  },
1444
1683
  byTable: async (query, options = {}) => {
1445
1684
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1685
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1446
1686
  return records.reduce((acc, record) => {
1447
1687
  const { table = "orphan" } = record.xata;
1688
+ const columns = schema.tables.find((t) => t.name === table)?.columns;
1689
+ if (!columns) {
1690
+ console.error(`No schema columns found for table ${table}`);
1691
+ }
1448
1692
  const items = acc[table] ?? [];
1449
- const item = initObject(this.db, this.links, table, record);
1693
+ const item = initObject(this.db, columns ?? [], table, record);
1450
1694
  return { ...acc, [table]: [...items, item] };
1451
1695
  }, {});
1452
1696
  }
1453
1697
  };
1454
1698
  }
1455
1699
  }
1700
+ _schema = new WeakMap();
1456
1701
  _search = new WeakSet();
1457
1702
  search_fn = async function(query, options, getFetchProps) {
1458
1703
  const fetchProps = await getFetchProps();
@@ -1464,6 +1709,18 @@ search_fn = async function(query, options, getFetchProps) {
1464
1709
  });
1465
1710
  return records;
1466
1711
  };
1712
+ _getSchema = new WeakSet();
1713
+ getSchema_fn = async function(getFetchProps) {
1714
+ if (__privateGet$1(this, _schema))
1715
+ return __privateGet$1(this, _schema);
1716
+ const fetchProps = await getFetchProps();
1717
+ const { schema } = await getBranchDetails({
1718
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1719
+ ...fetchProps
1720
+ });
1721
+ __privateSet$1(this, _schema, schema);
1722
+ return schema;
1723
+ };
1467
1724
 
1468
1725
  const isBranchStrategyBuilder = (strategy) => {
1469
1726
  return typeof strategy === "function";
@@ -1572,22 +1829,24 @@ var __privateMethod = (obj, member, method) => {
1572
1829
  const buildClient = (plugins) => {
1573
1830
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1574
1831
  return _a = class {
1575
- constructor(options = {}, links, tables) {
1832
+ constructor(options = {}, tables) {
1576
1833
  __privateAdd(this, _parseOptions);
1577
1834
  __privateAdd(this, _getFetchProps);
1578
1835
  __privateAdd(this, _evaluateBranch);
1579
1836
  __privateAdd(this, _branch, void 0);
1580
1837
  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
- });
1838
+ const pluginOptions = {
1839
+ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1840
+ cache: safeOptions.cache
1841
+ };
1842
+ const db = new SchemaPlugin(tables).build(pluginOptions);
1843
+ const search = new SearchPlugin(db).build(pluginOptions);
1585
1844
  this.db = db;
1586
1845
  this.search = search;
1587
1846
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1588
1847
  if (!namespace)
1589
1848
  continue;
1590
- const result = namespace.build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1849
+ const result = namespace.build(pluginOptions);
1591
1850
  if (result instanceof Promise) {
1592
1851
  void result.then((namespace2) => {
1593
1852
  this[key] = namespace2;
@@ -1601,11 +1860,12 @@ const buildClient = (plugins) => {
1601
1860
  const fetch = getFetchImplementation(options?.fetch);
1602
1861
  const databaseURL = options?.databaseURL || getDatabaseURL();
1603
1862
  const apiKey = options?.apiKey || getAPIKey();
1863
+ const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1604
1864
  const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1605
1865
  if (!databaseURL || !apiKey) {
1606
1866
  throw new Error("Options databaseURL and apiKey are required");
1607
1867
  }
1608
- return { fetch, databaseURL, apiKey, branch };
1868
+ return { fetch, databaseURL, apiKey, branch, cache };
1609
1869
  }, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
1610
1870
  fetch,
1611
1871
  apiKey,
@@ -1665,11 +1925,13 @@ exports.Repository = Repository;
1665
1925
  exports.RestRepository = RestRepository;
1666
1926
  exports.SchemaPlugin = SchemaPlugin;
1667
1927
  exports.SearchPlugin = SearchPlugin;
1928
+ exports.SimpleCache = SimpleCache;
1668
1929
  exports.XataApiClient = XataApiClient;
1669
1930
  exports.XataApiPlugin = XataApiPlugin;
1670
1931
  exports.XataError = XataError;
1671
1932
  exports.XataPlugin = XataPlugin;
1672
1933
  exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
1934
+ exports.addGitBranchesEntry = addGitBranchesEntry;
1673
1935
  exports.addTableColumn = addTableColumn;
1674
1936
  exports.buildClient = buildClient;
1675
1937
  exports.bulkInsertTableRecords = bulkInsertTableRecords;
@@ -1704,6 +1966,7 @@ exports.getCurrentBranchDetails = getCurrentBranchDetails;
1704
1966
  exports.getCurrentBranchName = getCurrentBranchName;
1705
1967
  exports.getDatabaseList = getDatabaseList;
1706
1968
  exports.getDatabaseURL = getDatabaseURL;
1969
+ exports.getGitBranchesMapping = getGitBranchesMapping;
1707
1970
  exports.getRecord = getRecord;
1708
1971
  exports.getTableColumns = getTableColumns;
1709
1972
  exports.getTableSchema = getTableSchema;
@@ -1732,8 +1995,10 @@ exports.notExists = notExists;
1732
1995
  exports.operationsByTag = operationsByTag;
1733
1996
  exports.pattern = pattern;
1734
1997
  exports.queryTable = queryTable;
1998
+ exports.removeGitBranchesEntry = removeGitBranchesEntry;
1735
1999
  exports.removeWorkspaceMember = removeWorkspaceMember;
1736
2000
  exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
2001
+ exports.resolveBranch = resolveBranch;
1737
2002
  exports.searchBranch = searchBranch;
1738
2003
  exports.setTableSchema = setTableSchema;
1739
2004
  exports.startsWith = startsWith;