@xata.io/client 0.9.0 → 0.10.1

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
@@ -38,7 +38,10 @@ function getEnvVariable(name) {
38
38
  }
39
39
  async function getGitBranch() {
40
40
  try {
41
- 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
+ }
42
45
  } catch (err) {
43
46
  }
44
47
  try {
@@ -252,6 +255,14 @@ const deleteDatabase = (variables) => fetch$1({
252
255
  method: "delete",
253
256
  ...variables
254
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
+ });
255
266
  const getBranchDetails = (variables) => fetch$1({
256
267
  url: "/db/{dbBranchName}",
257
268
  method: "get",
@@ -380,7 +391,15 @@ const operationsByTag = {
380
391
  resendWorkspaceMemberInvite,
381
392
  acceptWorkspaceMemberInvite
382
393
  },
383
- database: { getDatabaseList, createDatabase, deleteDatabase },
394
+ database: {
395
+ getDatabaseList,
396
+ createDatabase,
397
+ deleteDatabase,
398
+ getGitBranchesMapping,
399
+ addGitBranchesEntry,
400
+ removeGitBranchesEntry,
401
+ resolveBranch
402
+ },
384
403
  branch: {
385
404
  getBranchList,
386
405
  getBranchDetails,
@@ -447,7 +466,7 @@ var __accessCheck$7 = (obj, member, msg) => {
447
466
  if (!member.has(obj))
448
467
  throw TypeError("Cannot " + msg);
449
468
  };
450
- var __privateGet$6 = (obj, member, getter) => {
469
+ var __privateGet$7 = (obj, member, getter) => {
451
470
  __accessCheck$7(obj, member, "read from private field");
452
471
  return getter ? getter.call(obj) : member.get(obj);
453
472
  };
@@ -456,7 +475,7 @@ var __privateAdd$7 = (obj, member, value) => {
456
475
  throw TypeError("Cannot add the same private member more than once");
457
476
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
458
477
  };
459
- var __privateSet$5 = (obj, member, value, setter) => {
478
+ var __privateSet$6 = (obj, member, value, setter) => {
460
479
  __accessCheck$7(obj, member, "write to private field");
461
480
  setter ? setter.call(obj, value) : member.set(obj, value);
462
481
  return value;
@@ -471,7 +490,7 @@ class XataApiClient {
471
490
  if (!apiKey) {
472
491
  throw new Error("Could not resolve a valid apiKey");
473
492
  }
474
- __privateSet$5(this, _extraProps, {
493
+ __privateSet$6(this, _extraProps, {
475
494
  apiUrl: getHostUrl(provider, "main"),
476
495
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
477
496
  fetchImpl: getFetchImplementation(options.fetch),
@@ -479,34 +498,34 @@ class XataApiClient {
479
498
  });
480
499
  }
481
500
  get 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;
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;
485
504
  }
486
505
  get 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;
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;
490
509
  }
491
510
  get 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;
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;
495
514
  }
496
515
  get 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;
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;
500
519
  }
501
520
  get 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;
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;
505
524
  }
506
525
  get 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;
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;
510
529
  }
511
530
  }
512
531
  _extraProps = new WeakMap();
@@ -640,6 +659,33 @@ class DatabaseApi {
640
659
  ...this.extraProps
641
660
  });
642
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
+ }
643
689
  }
644
690
  class BranchApi {
645
691
  constructor(extraProps) {
@@ -657,10 +703,10 @@ class BranchApi {
657
703
  ...this.extraProps
658
704
  });
659
705
  }
660
- createBranch(workspace, database, branch, from = "", options = {}) {
706
+ createBranch(workspace, database, branch, from, options = {}) {
661
707
  return operationsByTag.branch.createBranch({
662
708
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
663
- queryParams: { from },
709
+ queryParams: isString(from) ? { from } : void 0,
664
710
  body: options,
665
711
  ...this.extraProps
666
712
  });
@@ -865,7 +911,7 @@ var __accessCheck$6 = (obj, member, msg) => {
865
911
  if (!member.has(obj))
866
912
  throw TypeError("Cannot " + msg);
867
913
  };
868
- var __privateGet$5 = (obj, member, getter) => {
914
+ var __privateGet$6 = (obj, member, getter) => {
869
915
  __accessCheck$6(obj, member, "read from private field");
870
916
  return getter ? getter.call(obj) : member.get(obj);
871
917
  };
@@ -874,7 +920,7 @@ var __privateAdd$6 = (obj, member, value) => {
874
920
  throw TypeError("Cannot add the same private member more than once");
875
921
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
876
922
  };
877
- var __privateSet$4 = (obj, member, value, setter) => {
923
+ var __privateSet$5 = (obj, member, value, setter) => {
878
924
  __accessCheck$6(obj, member, "write to private field");
879
925
  setter ? setter.call(obj, value) : member.set(obj, value);
880
926
  return value;
@@ -883,21 +929,21 @@ var _query;
883
929
  class Page {
884
930
  constructor(query, meta, records = []) {
885
931
  __privateAdd$6(this, _query, void 0);
886
- __privateSet$4(this, _query, query);
932
+ __privateSet$5(this, _query, query);
887
933
  this.meta = meta;
888
934
  this.records = records;
889
935
  }
890
936
  async nextPage(size, offset) {
891
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
937
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
892
938
  }
893
939
  async previousPage(size, offset) {
894
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
940
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
895
941
  }
896
942
  async firstPage(size, offset) {
897
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
943
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
898
944
  }
899
945
  async lastPage(size, offset) {
900
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
946
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
901
947
  }
902
948
  hasNextPage() {
903
949
  return this.meta.page.more;
@@ -913,7 +959,7 @@ var __accessCheck$5 = (obj, member, msg) => {
913
959
  if (!member.has(obj))
914
960
  throw TypeError("Cannot " + msg);
915
961
  };
916
- var __privateGet$4 = (obj, member, getter) => {
962
+ var __privateGet$5 = (obj, member, getter) => {
917
963
  __accessCheck$5(obj, member, "read from private field");
918
964
  return getter ? getter.call(obj) : member.get(obj);
919
965
  };
@@ -922,7 +968,7 @@ var __privateAdd$5 = (obj, member, value) => {
922
968
  throw TypeError("Cannot add the same private member more than once");
923
969
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
924
970
  };
925
- var __privateSet$3 = (obj, member, value, setter) => {
971
+ var __privateSet$4 = (obj, member, value, setter) => {
926
972
  __accessCheck$5(obj, member, "write to private field");
927
973
  setter ? setter.call(obj, value) : member.set(obj, value);
928
974
  return value;
@@ -935,21 +981,21 @@ const _Query = class {
935
981
  __privateAdd$5(this, _data, { filter: {} });
936
982
  this.meta = { page: { cursor: "start", more: true } };
937
983
  this.records = [];
938
- __privateSet$3(this, _table$1, table);
984
+ __privateSet$4(this, _table$1, table);
939
985
  if (repository) {
940
- __privateSet$3(this, _repository, repository);
986
+ __privateSet$4(this, _repository, repository);
941
987
  } else {
942
- __privateSet$3(this, _repository, this);
988
+ __privateSet$4(this, _repository, this);
943
989
  }
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).cache = data.cache ?? parent?.cache;
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).pagination = data.pagination ?? parent?.pagination;
998
+ __privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
953
999
  this.any = this.any.bind(this);
954
1000
  this.all = this.all.bind(this);
955
1001
  this.not = this.not.bind(this);
@@ -960,63 +1006,64 @@ const _Query = class {
960
1006
  Object.defineProperty(this, "repository", { enumerable: false });
961
1007
  }
962
1008
  getQueryOptions() {
963
- return __privateGet$4(this, _data);
1009
+ return __privateGet$5(this, _data);
964
1010
  }
965
1011
  key() {
966
- const { columns = [], filter = {}, sort = [], page = {} } = __privateGet$4(this, _data);
967
- const key = JSON.stringify({ columns, filter, sort, page });
1012
+ const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
1013
+ const key = JSON.stringify({ columns, filter, sort, pagination });
968
1014
  return toBase64(key);
969
1015
  }
970
1016
  any(...queries) {
971
1017
  const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
972
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $any } }, __privateGet$4(this, _data));
1018
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
973
1019
  }
974
1020
  all(...queries) {
975
1021
  const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
976
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1022
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
977
1023
  }
978
1024
  not(...queries) {
979
1025
  const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
980
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $not } }, __privateGet$4(this, _data));
1026
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
981
1027
  }
982
1028
  none(...queries) {
983
1029
  const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
984
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $none } }, __privateGet$4(this, _data));
1030
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
985
1031
  }
986
1032
  filter(a, b) {
987
1033
  if (arguments.length === 1) {
988
1034
  const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
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));
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));
991
1037
  } else {
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));
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));
994
1040
  }
995
1041
  }
996
1042
  sort(column, direction) {
997
- const originalSort = [__privateGet$4(this, _data).sort ?? []].flat();
1043
+ const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
998
1044
  const sort = [...originalSort, { column, direction }];
999
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { sort }, __privateGet$4(this, _data));
1045
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
1000
1046
  }
1001
1047
  select(columns) {
1002
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { columns }, __privateGet$4(this, _data));
1048
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
1003
1049
  }
1004
1050
  getPaginated(options = {}) {
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);
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);
1007
1053
  }
1008
1054
  async *[Symbol.asyncIterator]() {
1009
- for await (const [record] of this.getIterator(1)) {
1055
+ for await (const [record] of this.getIterator({ batchSize: 1 })) {
1010
1056
  yield record;
1011
1057
  }
1012
1058
  }
1013
- async *getIterator(chunk, options = {}) {
1059
+ async *getIterator(options = {}) {
1060
+ const { batchSize = 1 } = options;
1014
1061
  let offset = 0;
1015
1062
  let end = false;
1016
1063
  while (!end) {
1017
- const { records, meta } = await this.getPaginated({ ...options, page: { size: chunk, offset } });
1064
+ const { records, meta } = await this.getPaginated({ ...options, pagination: { size: batchSize, offset } });
1018
1065
  yield records;
1019
- offset += chunk;
1066
+ offset += batchSize;
1020
1067
  end = !meta.page.more;
1021
1068
  }
1022
1069
  }
@@ -1024,19 +1071,20 @@ const _Query = class {
1024
1071
  const { records } = await this.getPaginated(options);
1025
1072
  return records;
1026
1073
  }
1027
- async getAll(chunk = PAGINATION_MAX_SIZE, options = {}) {
1074
+ async getAll(options = {}) {
1075
+ const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
1028
1076
  const results = [];
1029
- for await (const page of this.getIterator(chunk, options)) {
1077
+ for await (const page of this.getIterator({ ...rest, batchSize })) {
1030
1078
  results.push(...page);
1031
1079
  }
1032
1080
  return results;
1033
1081
  }
1034
1082
  async getFirst(options = {}) {
1035
- const records = await this.getMany({ ...options, page: { size: 1 } });
1036
- return records[0] || null;
1083
+ const records = await this.getMany({ ...options, pagination: { size: 1 } });
1084
+ return records[0] ?? null;
1037
1085
  }
1038
1086
  cache(ttl) {
1039
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { cache: ttl }, __privateGet$4(this, _data));
1087
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
1040
1088
  }
1041
1089
  nextPage(size, offset) {
1042
1090
  return this.firstPage(size, offset);
@@ -1045,10 +1093,10 @@ const _Query = class {
1045
1093
  return this.firstPage(size, offset);
1046
1094
  }
1047
1095
  firstPage(size, offset) {
1048
- return this.getPaginated({ page: { size, offset } });
1096
+ return this.getPaginated({ pagination: { size, offset } });
1049
1097
  }
1050
1098
  lastPage(size, offset) {
1051
- return this.getPaginated({ page: { size, offset, before: "end" } });
1099
+ return this.getPaginated({ pagination: { size, offset, before: "end" } });
1052
1100
  }
1053
1101
  hasNextPage() {
1054
1102
  return this.meta.page.more;
@@ -1093,7 +1141,7 @@ var __accessCheck$4 = (obj, member, msg) => {
1093
1141
  if (!member.has(obj))
1094
1142
  throw TypeError("Cannot " + msg);
1095
1143
  };
1096
- var __privateGet$3 = (obj, member, getter) => {
1144
+ var __privateGet$4 = (obj, member, getter) => {
1097
1145
  __accessCheck$4(obj, member, "read from private field");
1098
1146
  return getter ? getter.call(obj) : member.get(obj);
1099
1147
  };
@@ -1102,7 +1150,7 @@ var __privateAdd$4 = (obj, member, value) => {
1102
1150
  throw TypeError("Cannot add the same private member more than once");
1103
1151
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1104
1152
  };
1105
- var __privateSet$2 = (obj, member, value, setter) => {
1153
+ var __privateSet$3 = (obj, member, value, setter) => {
1106
1154
  __accessCheck$4(obj, member, "write to private field");
1107
1155
  setter ? setter.call(obj, value) : member.set(obj, value);
1108
1156
  return value;
@@ -1111,7 +1159,7 @@ var __privateMethod$2 = (obj, member, method) => {
1111
1159
  __accessCheck$4(obj, member, "access private method");
1112
1160
  return method;
1113
1161
  };
1114
- var _table, _links, _getFetchProps, _cache, _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;
1162
+ var _table, _getFetchProps, _cache, _schema$1, _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, _getSchema$1, getSchema_fn$1;
1115
1163
  class Repository extends Query {
1116
1164
  }
1117
1165
  class RestRepository extends Query {
@@ -1128,15 +1176,15 @@ class RestRepository extends Query {
1128
1176
  __privateAdd$4(this, _getCacheRecord);
1129
1177
  __privateAdd$4(this, _setCacheQuery);
1130
1178
  __privateAdd$4(this, _getCacheQuery);
1179
+ __privateAdd$4(this, _getSchema$1);
1131
1180
  __privateAdd$4(this, _table, void 0);
1132
- __privateAdd$4(this, _links, void 0);
1133
1181
  __privateAdd$4(this, _getFetchProps, void 0);
1134
1182
  __privateAdd$4(this, _cache, void 0);
1135
- __privateSet$2(this, _table, options.table);
1136
- __privateSet$2(this, _links, options.links ?? {});
1137
- __privateSet$2(this, _getFetchProps, options.pluginOptions.getFetchProps);
1183
+ __privateAdd$4(this, _schema$1, void 0);
1184
+ __privateSet$3(this, _table, options.table);
1185
+ __privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
1138
1186
  this.db = options.db;
1139
- __privateSet$2(this, _cache, options.pluginOptions.cache);
1187
+ __privateSet$3(this, _cache, options.pluginOptions.cache);
1140
1188
  }
1141
1189
  async create(a, b) {
1142
1190
  if (Array.isArray(a)) {
@@ -1169,13 +1217,14 @@ class RestRepository extends Query {
1169
1217
  const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1170
1218
  if (cacheRecord)
1171
1219
  return cacheRecord;
1172
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1220
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1173
1221
  try {
1174
1222
  const response = await getRecord({
1175
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1223
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1176
1224
  ...fetchProps
1177
1225
  });
1178
- return initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), response);
1226
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1227
+ return initObject(this.db, schema, __privateGet$4(this, _table), response);
1179
1228
  } catch (e) {
1180
1229
  if (isObject(e) && e.status === 404) {
1181
1230
  return null;
@@ -1246,13 +1295,14 @@ class RestRepository extends Query {
1246
1295
  throw new Error("Invalid arguments for delete method");
1247
1296
  }
1248
1297
  async search(query, options = {}) {
1249
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1298
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1250
1299
  const { records } = await searchBranch({
1251
1300
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1252
- body: { tables: [__privateGet$3(this, _table)], query, fuzziness: options.fuzziness },
1301
+ body: { tables: [__privateGet$4(this, _table)], query, fuzziness: options.fuzziness },
1253
1302
  ...fetchProps
1254
1303
  });
1255
- return records.map((item) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), item));
1304
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1305
+ return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
1256
1306
  }
1257
1307
  async query(query) {
1258
1308
  const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
@@ -1261,34 +1311,35 @@ class RestRepository extends Query {
1261
1311
  const data = query.getQueryOptions();
1262
1312
  const body = {
1263
1313
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1264
- sort: data.sort ? buildSortFilter(data.sort) : void 0,
1265
- page: data.page,
1314
+ sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1315
+ page: data.pagination,
1266
1316
  columns: data.columns
1267
1317
  };
1268
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1318
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1269
1319
  const { meta, records: objects } = await queryTable({
1270
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1320
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1271
1321
  body,
1272
1322
  ...fetchProps
1273
1323
  });
1274
- const records = objects.map((record) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), record));
1324
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1325
+ const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
1275
1326
  await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1276
1327
  return new Page(query, meta, records);
1277
1328
  }
1278
1329
  }
1279
1330
  _table = new WeakMap();
1280
- _links = new WeakMap();
1281
1331
  _getFetchProps = new WeakMap();
1282
1332
  _cache = new WeakMap();
1333
+ _schema$1 = new WeakMap();
1283
1334
  _insertRecordWithoutId = new WeakSet();
1284
1335
  insertRecordWithoutId_fn = async function(object) {
1285
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1336
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1286
1337
  const record = transformObjectLinks(object);
1287
1338
  const response = await insertRecord({
1288
1339
  pathParams: {
1289
1340
  workspace: "{workspaceId}",
1290
1341
  dbBranchName: "{dbBranch}",
1291
- tableName: __privateGet$3(this, _table)
1342
+ tableName: __privateGet$4(this, _table)
1292
1343
  },
1293
1344
  body: record,
1294
1345
  ...fetchProps
@@ -1301,13 +1352,13 @@ insertRecordWithoutId_fn = async function(object) {
1301
1352
  };
1302
1353
  _insertRecordWithId = new WeakSet();
1303
1354
  insertRecordWithId_fn = async function(recordId, object) {
1304
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1355
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1305
1356
  const record = transformObjectLinks(object);
1306
1357
  const response = await insertRecordWithID({
1307
1358
  pathParams: {
1308
1359
  workspace: "{workspaceId}",
1309
1360
  dbBranchName: "{dbBranch}",
1310
- tableName: __privateGet$3(this, _table),
1361
+ tableName: __privateGet$4(this, _table),
1311
1362
  recordId
1312
1363
  },
1313
1364
  body: record,
@@ -1322,10 +1373,10 @@ insertRecordWithId_fn = async function(recordId, object) {
1322
1373
  };
1323
1374
  _bulkInsertTableRecords = new WeakSet();
1324
1375
  bulkInsertTableRecords_fn = async function(objects) {
1325
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1376
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1326
1377
  const records = objects.map((object) => transformObjectLinks(object));
1327
1378
  const response = await bulkInsertTableRecords({
1328
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1379
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1329
1380
  body: { records },
1330
1381
  ...fetchProps
1331
1382
  });
@@ -1337,10 +1388,10 @@ bulkInsertTableRecords_fn = async function(objects) {
1337
1388
  };
1338
1389
  _updateRecordWithID = new WeakSet();
1339
1390
  updateRecordWithID_fn = async function(recordId, object) {
1340
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1391
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1341
1392
  const record = transformObjectLinks(object);
1342
1393
  const response = await updateRecordWithID({
1343
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1394
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1344
1395
  body: record,
1345
1396
  ...fetchProps
1346
1397
  });
@@ -1351,9 +1402,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1351
1402
  };
1352
1403
  _upsertRecordWithID = new WeakSet();
1353
1404
  upsertRecordWithID_fn = async function(recordId, object) {
1354
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1405
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1355
1406
  const response = await upsertRecordWithID({
1356
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1407
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1357
1408
  body: object,
1358
1409
  ...fetchProps
1359
1410
  });
@@ -1364,51 +1415,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
1364
1415
  };
1365
1416
  _deleteRecord = new WeakSet();
1366
1417
  deleteRecord_fn = async function(recordId) {
1367
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1418
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1368
1419
  await deleteRecord({
1369
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1420
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1370
1421
  ...fetchProps
1371
1422
  });
1372
1423
  };
1373
1424
  _invalidateCache = new WeakSet();
1374
1425
  invalidateCache_fn = async function(recordId) {
1375
- await __privateGet$3(this, _cache).delete(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1376
- const cacheItems = await __privateGet$3(this, _cache).getAll();
1426
+ await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1427
+ const cacheItems = await __privateGet$4(this, _cache).getAll();
1377
1428
  const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1378
1429
  for (const [key, value] of queries) {
1379
1430
  const ids = getIds(value);
1380
1431
  if (ids.includes(recordId))
1381
- await __privateGet$3(this, _cache).delete(key);
1432
+ await __privateGet$4(this, _cache).delete(key);
1382
1433
  }
1383
1434
  };
1384
1435
  _setCacheRecord = new WeakSet();
1385
1436
  setCacheRecord_fn = async function(record) {
1386
- if (!__privateGet$3(this, _cache).cacheRecords)
1437
+ if (!__privateGet$4(this, _cache).cacheRecords)
1387
1438
  return;
1388
- await __privateGet$3(this, _cache).set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
1439
+ await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
1389
1440
  };
1390
1441
  _getCacheRecord = new WeakSet();
1391
1442
  getCacheRecord_fn = async function(recordId) {
1392
- if (!__privateGet$3(this, _cache).cacheRecords)
1443
+ if (!__privateGet$4(this, _cache).cacheRecords)
1393
1444
  return null;
1394
- return __privateGet$3(this, _cache).get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1445
+ return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1395
1446
  };
1396
1447
  _setCacheQuery = new WeakSet();
1397
1448
  setCacheQuery_fn = async function(query, meta, records) {
1398
- await __privateGet$3(this, _cache).set(`query_${__privateGet$3(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1449
+ await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1399
1450
  };
1400
1451
  _getCacheQuery = new WeakSet();
1401
1452
  getCacheQuery_fn = async function(query) {
1402
- const key = `query_${__privateGet$3(this, _table)}:${query.key()}`;
1403
- const result = await __privateGet$3(this, _cache).get(key);
1453
+ const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
1454
+ const result = await __privateGet$4(this, _cache).get(key);
1404
1455
  if (!result)
1405
1456
  return null;
1406
- const { cache: ttl = __privateGet$3(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1407
- if (!ttl || ttl < 0)
1408
- return result;
1457
+ const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1458
+ if (ttl < 0)
1459
+ return null;
1409
1460
  const hasExpired = result.date.getTime() + ttl < Date.now();
1410
1461
  return hasExpired ? null : result;
1411
1462
  };
1463
+ _getSchema$1 = new WeakSet();
1464
+ getSchema_fn$1 = async function() {
1465
+ if (__privateGet$4(this, _schema$1))
1466
+ return __privateGet$4(this, _schema$1);
1467
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1468
+ const { schema } = await getBranchDetails({
1469
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1470
+ ...fetchProps
1471
+ });
1472
+ __privateSet$3(this, _schema$1, schema);
1473
+ return schema;
1474
+ };
1412
1475
  const transformObjectLinks = (object) => {
1413
1476
  return Object.entries(object).reduce((acc, [key, value]) => {
1414
1477
  if (key === "xata")
@@ -1416,15 +1479,33 @@ const transformObjectLinks = (object) => {
1416
1479
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1417
1480
  }, {});
1418
1481
  };
1419
- const initObject = (db, links, table, object) => {
1482
+ const initObject = (db, schema, table, object) => {
1420
1483
  const result = {};
1421
1484
  Object.assign(result, object);
1422
- const tableLinks = links[table] || [];
1423
- for (const link of tableLinks) {
1424
- const [field, linkTable] = link;
1425
- const value = result[field];
1426
- if (value && isObject(value)) {
1427
- result[field] = initObject(db, links, linkTable, value);
1485
+ const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
1486
+ if (!columns)
1487
+ console.error(`Table ${table} not found in schema`);
1488
+ for (const column of columns ?? []) {
1489
+ const value = result[column.name];
1490
+ switch (column.type) {
1491
+ case "datetime": {
1492
+ const date = new Date(value);
1493
+ if (isNaN(date.getTime())) {
1494
+ console.error(`Failed to parse date ${value} for field ${column.name}`);
1495
+ } else {
1496
+ result[column.name] = date;
1497
+ }
1498
+ break;
1499
+ }
1500
+ case "link": {
1501
+ const linkTable = column.link?.table;
1502
+ if (!linkTable) {
1503
+ console.error(`Failed to parse link for field ${column.name}`);
1504
+ } else if (isObject(value)) {
1505
+ result[column.name] = initObject(db, schema, linkTable, value);
1506
+ }
1507
+ break;
1508
+ }
1428
1509
  }
1429
1510
  }
1430
1511
  result.read = function() {
@@ -1456,7 +1537,7 @@ var __accessCheck$3 = (obj, member, msg) => {
1456
1537
  if (!member.has(obj))
1457
1538
  throw TypeError("Cannot " + msg);
1458
1539
  };
1459
- var __privateGet$2 = (obj, member, getter) => {
1540
+ var __privateGet$3 = (obj, member, getter) => {
1460
1541
  __accessCheck$3(obj, member, "read from private field");
1461
1542
  return getter ? getter.call(obj) : member.get(obj);
1462
1543
  };
@@ -1465,7 +1546,7 @@ var __privateAdd$3 = (obj, member, value) => {
1465
1546
  throw TypeError("Cannot add the same private member more than once");
1466
1547
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1467
1548
  };
1468
- var __privateSet$1 = (obj, member, value, setter) => {
1549
+ var __privateSet$2 = (obj, member, value, setter) => {
1469
1550
  __accessCheck$3(obj, member, "write to private field");
1470
1551
  setter ? setter.call(obj, value) : member.set(obj, value);
1471
1552
  return value;
@@ -1474,30 +1555,30 @@ var _map;
1474
1555
  class SimpleCache {
1475
1556
  constructor(options = {}) {
1476
1557
  __privateAdd$3(this, _map, void 0);
1477
- __privateSet$1(this, _map, /* @__PURE__ */ new Map());
1558
+ __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1478
1559
  this.capacity = options.max ?? 500;
1479
1560
  this.cacheRecords = options.cacheRecords ?? true;
1480
1561
  this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1481
1562
  }
1482
1563
  async getAll() {
1483
- return Object.fromEntries(__privateGet$2(this, _map));
1564
+ return Object.fromEntries(__privateGet$3(this, _map));
1484
1565
  }
1485
1566
  async get(key) {
1486
- return __privateGet$2(this, _map).get(key) ?? null;
1567
+ return __privateGet$3(this, _map).get(key) ?? null;
1487
1568
  }
1488
1569
  async set(key, value) {
1489
1570
  await this.delete(key);
1490
- __privateGet$2(this, _map).set(key, value);
1491
- if (__privateGet$2(this, _map).size > this.capacity) {
1492
- const leastRecentlyUsed = __privateGet$2(this, _map).keys().next().value;
1571
+ __privateGet$3(this, _map).set(key, value);
1572
+ if (__privateGet$3(this, _map).size > this.capacity) {
1573
+ const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
1493
1574
  await this.delete(leastRecentlyUsed);
1494
1575
  }
1495
1576
  }
1496
1577
  async delete(key) {
1497
- __privateGet$2(this, _map).delete(key);
1578
+ __privateGet$3(this, _map).delete(key);
1498
1579
  }
1499
1580
  async clear() {
1500
- return __privateGet$2(this, _map).clear();
1581
+ return __privateGet$3(this, _map).clear();
1501
1582
  }
1502
1583
  }
1503
1584
  _map = new WeakMap();
@@ -1525,7 +1606,7 @@ var __accessCheck$2 = (obj, member, msg) => {
1525
1606
  if (!member.has(obj))
1526
1607
  throw TypeError("Cannot " + msg);
1527
1608
  };
1528
- var __privateGet$1 = (obj, member, getter) => {
1609
+ var __privateGet$2 = (obj, member, getter) => {
1529
1610
  __accessCheck$2(obj, member, "read from private field");
1530
1611
  return getter ? getter.call(obj) : member.get(obj);
1531
1612
  };
@@ -1536,26 +1617,24 @@ var __privateAdd$2 = (obj, member, value) => {
1536
1617
  };
1537
1618
  var _tables;
1538
1619
  class SchemaPlugin extends XataPlugin {
1539
- constructor(links, tableNames) {
1620
+ constructor(tableNames) {
1540
1621
  super();
1541
- this.links = links;
1542
1622
  this.tableNames = tableNames;
1543
1623
  __privateAdd$2(this, _tables, {});
1544
1624
  }
1545
1625
  build(pluginOptions) {
1546
- const links = this.links;
1547
1626
  const db = new Proxy({}, {
1548
1627
  get: (_target, table) => {
1549
1628
  if (!isString(table))
1550
1629
  throw new Error("Invalid table name");
1551
- if (!__privateGet$1(this, _tables)[table]) {
1552
- __privateGet$1(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, links });
1630
+ if (__privateGet$2(this, _tables)[table] === void 0) {
1631
+ __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1553
1632
  }
1554
- return __privateGet$1(this, _tables)[table];
1633
+ return __privateGet$2(this, _tables)[table];
1555
1634
  }
1556
1635
  });
1557
1636
  for (const table of this.tableNames ?? []) {
1558
- db[table] = new RestRepository({ db, pluginOptions, table, links });
1637
+ db[table] = new RestRepository({ db, pluginOptions, table });
1559
1638
  }
1560
1639
  return db;
1561
1640
  }
@@ -1566,44 +1645,57 @@ var __accessCheck$1 = (obj, member, msg) => {
1566
1645
  if (!member.has(obj))
1567
1646
  throw TypeError("Cannot " + msg);
1568
1647
  };
1648
+ var __privateGet$1 = (obj, member, getter) => {
1649
+ __accessCheck$1(obj, member, "read from private field");
1650
+ return getter ? getter.call(obj) : member.get(obj);
1651
+ };
1569
1652
  var __privateAdd$1 = (obj, member, value) => {
1570
1653
  if (member.has(obj))
1571
1654
  throw TypeError("Cannot add the same private member more than once");
1572
1655
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1573
1656
  };
1657
+ var __privateSet$1 = (obj, member, value, setter) => {
1658
+ __accessCheck$1(obj, member, "write to private field");
1659
+ setter ? setter.call(obj, value) : member.set(obj, value);
1660
+ return value;
1661
+ };
1574
1662
  var __privateMethod$1 = (obj, member, method) => {
1575
1663
  __accessCheck$1(obj, member, "access private method");
1576
1664
  return method;
1577
1665
  };
1578
- var _search, search_fn;
1666
+ var _schema, _search, search_fn, _getSchema, getSchema_fn;
1579
1667
  class SearchPlugin extends XataPlugin {
1580
- constructor(db, links) {
1668
+ constructor(db) {
1581
1669
  super();
1582
1670
  this.db = db;
1583
- this.links = links;
1584
1671
  __privateAdd$1(this, _search);
1672
+ __privateAdd$1(this, _getSchema);
1673
+ __privateAdd$1(this, _schema, void 0);
1585
1674
  }
1586
1675
  build({ getFetchProps }) {
1587
1676
  return {
1588
1677
  all: async (query, options = {}) => {
1589
1678
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1679
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1590
1680
  return records.map((record) => {
1591
1681
  const { table = "orphan" } = record.xata;
1592
- return { table, record: initObject(this.db, this.links, table, record) };
1682
+ return { table, record: initObject(this.db, schema, table, record) };
1593
1683
  });
1594
1684
  },
1595
1685
  byTable: async (query, options = {}) => {
1596
1686
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1687
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1597
1688
  return records.reduce((acc, record) => {
1598
1689
  const { table = "orphan" } = record.xata;
1599
1690
  const items = acc[table] ?? [];
1600
- const item = initObject(this.db, this.links, table, record);
1691
+ const item = initObject(this.db, schema, table, record);
1601
1692
  return { ...acc, [table]: [...items, item] };
1602
1693
  }, {});
1603
1694
  }
1604
1695
  };
1605
1696
  }
1606
1697
  }
1698
+ _schema = new WeakMap();
1607
1699
  _search = new WeakSet();
1608
1700
  search_fn = async function(query, options, getFetchProps) {
1609
1701
  const fetchProps = await getFetchProps();
@@ -1615,6 +1707,18 @@ search_fn = async function(query, options, getFetchProps) {
1615
1707
  });
1616
1708
  return records;
1617
1709
  };
1710
+ _getSchema = new WeakSet();
1711
+ getSchema_fn = async function(getFetchProps) {
1712
+ if (__privateGet$1(this, _schema))
1713
+ return __privateGet$1(this, _schema);
1714
+ const fetchProps = await getFetchProps();
1715
+ const { schema } = await getBranchDetails({
1716
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1717
+ ...fetchProps
1718
+ });
1719
+ __privateSet$1(this, _schema, schema);
1720
+ return schema;
1721
+ };
1618
1722
 
1619
1723
  const isBranchStrategyBuilder = (strategy) => {
1620
1724
  return typeof strategy === "function";
@@ -1626,30 +1730,39 @@ const envBranchNames = [
1626
1730
  "CF_PAGES_BRANCH",
1627
1731
  "BRANCH"
1628
1732
  ];
1629
- const defaultBranch = "main";
1630
1733
  async function getCurrentBranchName(options) {
1631
- const env = await getBranchByEnvVariable();
1632
- if (env)
1633
- return env;
1634
- const branch = await getGitBranch();
1635
- if (!branch)
1636
- return defaultBranch;
1637
- const details = await getDatabaseBranch(branch, options);
1638
- if (details)
1639
- return branch;
1640
- return defaultBranch;
1734
+ const env = getBranchByEnvVariable();
1735
+ if (env) {
1736
+ const details = await getDatabaseBranch(env, options);
1737
+ if (details)
1738
+ return env;
1739
+ console.warn(`Branch ${env} not found in Xata. Ignoring...`);
1740
+ }
1741
+ const gitBranch = await getGitBranch();
1742
+ return resolveXataBranch(gitBranch, options);
1641
1743
  }
1642
1744
  async function getCurrentBranchDetails(options) {
1643
- const env = await getBranchByEnvVariable();
1644
- if (env)
1645
- return getDatabaseBranch(env, options);
1646
- const branch = await getGitBranch();
1647
- if (!branch)
1648
- return getDatabaseBranch(defaultBranch, options);
1649
- const details = await getDatabaseBranch(branch, options);
1650
- if (details)
1651
- return details;
1652
- return getDatabaseBranch(defaultBranch, options);
1745
+ const branch = await getCurrentBranchName(options);
1746
+ return getDatabaseBranch(branch, options);
1747
+ }
1748
+ async function resolveXataBranch(gitBranch, options) {
1749
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1750
+ const apiKey = options?.apiKey || getAPIKey();
1751
+ if (!databaseURL)
1752
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1753
+ if (!apiKey)
1754
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1755
+ const [protocol, , host, , dbName] = databaseURL.split("/");
1756
+ const [workspace] = host.split(".");
1757
+ const { branch } = await resolveBranch({
1758
+ apiKey,
1759
+ apiUrl: databaseURL,
1760
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1761
+ workspacesApiUrl: `${protocol}//${host}`,
1762
+ pathParams: { dbName, workspace },
1763
+ queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
1764
+ });
1765
+ return branch;
1653
1766
  }
1654
1767
  async function getDatabaseBranch(branch, options) {
1655
1768
  const databaseURL = options?.databaseURL || getDatabaseURL();
@@ -1723,7 +1836,7 @@ var __privateMethod = (obj, member, method) => {
1723
1836
  const buildClient = (plugins) => {
1724
1837
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1725
1838
  return _a = class {
1726
- constructor(options = {}, links, tables) {
1839
+ constructor(options = {}, tables) {
1727
1840
  __privateAdd(this, _parseOptions);
1728
1841
  __privateAdd(this, _getFetchProps);
1729
1842
  __privateAdd(this, _evaluateBranch);
@@ -1733,12 +1846,12 @@ const buildClient = (plugins) => {
1733
1846
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1734
1847
  cache: safeOptions.cache
1735
1848
  };
1736
- const db = new SchemaPlugin(links, tables).build(pluginOptions);
1737
- const search = new SearchPlugin(db, links ?? {}).build(pluginOptions);
1849
+ const db = new SchemaPlugin(tables).build(pluginOptions);
1850
+ const search = new SearchPlugin(db).build(pluginOptions);
1738
1851
  this.db = db;
1739
1852
  this.search = search;
1740
1853
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1741
- if (!namespace)
1854
+ if (namespace === void 0)
1742
1855
  continue;
1743
1856
  const result = namespace.build(pluginOptions);
1744
1857
  if (result instanceof Promise) {
@@ -1755,7 +1868,7 @@ const buildClient = (plugins) => {
1755
1868
  const databaseURL = options?.databaseURL || getDatabaseURL();
1756
1869
  const apiKey = options?.apiKey || getAPIKey();
1757
1870
  const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1758
- const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1871
+ const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1759
1872
  if (!databaseURL || !apiKey) {
1760
1873
  throw new Error("Options databaseURL and apiKey are required");
1761
1874
  }
@@ -1782,7 +1895,7 @@ const buildClient = (plugins) => {
1782
1895
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
1783
1896
  if (__privateGet(this, _branch))
1784
1897
  return __privateGet(this, _branch);
1785
- if (!param)
1898
+ if (param === void 0)
1786
1899
  return void 0;
1787
1900
  const strategies = Array.isArray(param) ? [...param] : [param];
1788
1901
  const evaluateBranch = async (strategy) => {
@@ -1825,6 +1938,7 @@ exports.XataApiPlugin = XataApiPlugin;
1825
1938
  exports.XataError = XataError;
1826
1939
  exports.XataPlugin = XataPlugin;
1827
1940
  exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
1941
+ exports.addGitBranchesEntry = addGitBranchesEntry;
1828
1942
  exports.addTableColumn = addTableColumn;
1829
1943
  exports.buildClient = buildClient;
1830
1944
  exports.bulkInsertTableRecords = bulkInsertTableRecords;
@@ -1859,6 +1973,7 @@ exports.getCurrentBranchDetails = getCurrentBranchDetails;
1859
1973
  exports.getCurrentBranchName = getCurrentBranchName;
1860
1974
  exports.getDatabaseList = getDatabaseList;
1861
1975
  exports.getDatabaseURL = getDatabaseURL;
1976
+ exports.getGitBranchesMapping = getGitBranchesMapping;
1862
1977
  exports.getRecord = getRecord;
1863
1978
  exports.getTableColumns = getTableColumns;
1864
1979
  exports.getTableSchema = getTableSchema;
@@ -1887,8 +2002,10 @@ exports.notExists = notExists;
1887
2002
  exports.operationsByTag = operationsByTag;
1888
2003
  exports.pattern = pattern;
1889
2004
  exports.queryTable = queryTable;
2005
+ exports.removeGitBranchesEntry = removeGitBranchesEntry;
1890
2006
  exports.removeWorkspaceMember = removeWorkspaceMember;
1891
2007
  exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
2008
+ exports.resolveBranch = resolveBranch;
1892
2009
  exports.searchBranch = searchBranch;
1893
2010
  exports.setTableSchema = setTableSchema;
1894
2011
  exports.startsWith = startsWith;