@xata.io/client 0.0.0-alpha.vfd071d9 → 0.0.0-alpha.vfde9dcf

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.mjs CHANGED
@@ -7,8 +7,11 @@ function compact(arr) {
7
7
  function isObject(value) {
8
8
  return Boolean(value) && typeof value === "object" && !Array.isArray(value);
9
9
  }
10
+ function isDefined(value) {
11
+ return value !== null && value !== void 0;
12
+ }
10
13
  function isString(value) {
11
- return value !== void 0 && value !== null && typeof value === "string";
14
+ return isDefined(value) && typeof value === "string";
12
15
  }
13
16
  function toBase64(value) {
14
17
  try {
@@ -366,6 +369,11 @@ const queryTable = (variables) => fetch$1({
366
369
  method: "post",
367
370
  ...variables
368
371
  });
372
+ const searchTable = (variables) => fetch$1({
373
+ url: "/db/{dbBranchName}/tables/{tableName}/search",
374
+ method: "post",
375
+ ...variables
376
+ });
369
377
  const searchBranch = (variables) => fetch$1({
370
378
  url: "/db/{dbBranchName}/search",
371
379
  method: "post",
@@ -429,6 +437,7 @@ const operationsByTag = {
429
437
  getRecord,
430
438
  bulkInsertTableRecords,
431
439
  queryTable,
440
+ searchTable,
432
441
  searchBranch
433
442
  }
434
443
  };
@@ -462,7 +471,7 @@ var __accessCheck$7 = (obj, member, msg) => {
462
471
  if (!member.has(obj))
463
472
  throw TypeError("Cannot " + msg);
464
473
  };
465
- var __privateGet$6 = (obj, member, getter) => {
474
+ var __privateGet$7 = (obj, member, getter) => {
466
475
  __accessCheck$7(obj, member, "read from private field");
467
476
  return getter ? getter.call(obj) : member.get(obj);
468
477
  };
@@ -471,7 +480,7 @@ var __privateAdd$7 = (obj, member, value) => {
471
480
  throw TypeError("Cannot add the same private member more than once");
472
481
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
473
482
  };
474
- var __privateSet$5 = (obj, member, value, setter) => {
483
+ var __privateSet$6 = (obj, member, value, setter) => {
475
484
  __accessCheck$7(obj, member, "write to private field");
476
485
  setter ? setter.call(obj, value) : member.set(obj, value);
477
486
  return value;
@@ -486,7 +495,7 @@ class XataApiClient {
486
495
  if (!apiKey) {
487
496
  throw new Error("Could not resolve a valid apiKey");
488
497
  }
489
- __privateSet$5(this, _extraProps, {
498
+ __privateSet$6(this, _extraProps, {
490
499
  apiUrl: getHostUrl(provider, "main"),
491
500
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
492
501
  fetchImpl: getFetchImplementation(options.fetch),
@@ -494,34 +503,34 @@ class XataApiClient {
494
503
  });
495
504
  }
496
505
  get user() {
497
- if (!__privateGet$6(this, _namespaces).user)
498
- __privateGet$6(this, _namespaces).user = new UserApi(__privateGet$6(this, _extraProps));
499
- return __privateGet$6(this, _namespaces).user;
506
+ if (!__privateGet$7(this, _namespaces).user)
507
+ __privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
508
+ return __privateGet$7(this, _namespaces).user;
500
509
  }
501
510
  get workspaces() {
502
- if (!__privateGet$6(this, _namespaces).workspaces)
503
- __privateGet$6(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$6(this, _extraProps));
504
- return __privateGet$6(this, _namespaces).workspaces;
511
+ if (!__privateGet$7(this, _namespaces).workspaces)
512
+ __privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
513
+ return __privateGet$7(this, _namespaces).workspaces;
505
514
  }
506
515
  get databases() {
507
- if (!__privateGet$6(this, _namespaces).databases)
508
- __privateGet$6(this, _namespaces).databases = new DatabaseApi(__privateGet$6(this, _extraProps));
509
- return __privateGet$6(this, _namespaces).databases;
516
+ if (!__privateGet$7(this, _namespaces).databases)
517
+ __privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
518
+ return __privateGet$7(this, _namespaces).databases;
510
519
  }
511
520
  get branches() {
512
- if (!__privateGet$6(this, _namespaces).branches)
513
- __privateGet$6(this, _namespaces).branches = new BranchApi(__privateGet$6(this, _extraProps));
514
- return __privateGet$6(this, _namespaces).branches;
521
+ if (!__privateGet$7(this, _namespaces).branches)
522
+ __privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
523
+ return __privateGet$7(this, _namespaces).branches;
515
524
  }
516
525
  get tables() {
517
- if (!__privateGet$6(this, _namespaces).tables)
518
- __privateGet$6(this, _namespaces).tables = new TableApi(__privateGet$6(this, _extraProps));
519
- return __privateGet$6(this, _namespaces).tables;
526
+ if (!__privateGet$7(this, _namespaces).tables)
527
+ __privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
528
+ return __privateGet$7(this, _namespaces).tables;
520
529
  }
521
530
  get records() {
522
- if (!__privateGet$6(this, _namespaces).records)
523
- __privateGet$6(this, _namespaces).records = new RecordsApi(__privateGet$6(this, _extraProps));
524
- return __privateGet$6(this, _namespaces).records;
531
+ if (!__privateGet$7(this, _namespaces).records)
532
+ __privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
533
+ return __privateGet$7(this, _namespaces).records;
525
534
  }
526
535
  }
527
536
  _extraProps = new WeakMap();
@@ -699,10 +708,10 @@ class BranchApi {
699
708
  ...this.extraProps
700
709
  });
701
710
  }
702
- createBranch(workspace, database, branch, from = "", options = {}) {
711
+ createBranch(workspace, database, branch, from, options = {}) {
703
712
  return operationsByTag.branch.createBranch({
704
713
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
705
- queryParams: { from },
714
+ queryParams: isString(from) ? { from } : void 0,
706
715
  body: options,
707
716
  ...this.extraProps
708
717
  });
@@ -884,6 +893,13 @@ class RecordsApi {
884
893
  ...this.extraProps
885
894
  });
886
895
  }
896
+ searchTable(workspace, database, branch, tableName, query) {
897
+ return operationsByTag.records.searchTable({
898
+ pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
899
+ body: query,
900
+ ...this.extraProps
901
+ });
902
+ }
887
903
  searchBranch(workspace, database, branch, query) {
888
904
  return operationsByTag.records.searchBranch({
889
905
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
@@ -907,7 +923,7 @@ var __accessCheck$6 = (obj, member, msg) => {
907
923
  if (!member.has(obj))
908
924
  throw TypeError("Cannot " + msg);
909
925
  };
910
- var __privateGet$5 = (obj, member, getter) => {
926
+ var __privateGet$6 = (obj, member, getter) => {
911
927
  __accessCheck$6(obj, member, "read from private field");
912
928
  return getter ? getter.call(obj) : member.get(obj);
913
929
  };
@@ -916,7 +932,7 @@ var __privateAdd$6 = (obj, member, value) => {
916
932
  throw TypeError("Cannot add the same private member more than once");
917
933
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
918
934
  };
919
- var __privateSet$4 = (obj, member, value, setter) => {
935
+ var __privateSet$5 = (obj, member, value, setter) => {
920
936
  __accessCheck$6(obj, member, "write to private field");
921
937
  setter ? setter.call(obj, value) : member.set(obj, value);
922
938
  return value;
@@ -925,21 +941,21 @@ var _query;
925
941
  class Page {
926
942
  constructor(query, meta, records = []) {
927
943
  __privateAdd$6(this, _query, void 0);
928
- __privateSet$4(this, _query, query);
944
+ __privateSet$5(this, _query, query);
929
945
  this.meta = meta;
930
946
  this.records = records;
931
947
  }
932
948
  async nextPage(size, offset) {
933
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
949
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
934
950
  }
935
951
  async previousPage(size, offset) {
936
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
952
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
937
953
  }
938
954
  async firstPage(size, offset) {
939
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
955
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
940
956
  }
941
957
  async lastPage(size, offset) {
942
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
958
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
943
959
  }
944
960
  hasNextPage() {
945
961
  return this.meta.page.more;
@@ -950,12 +966,15 @@ const PAGINATION_MAX_SIZE = 200;
950
966
  const PAGINATION_DEFAULT_SIZE = 200;
951
967
  const PAGINATION_MAX_OFFSET = 800;
952
968
  const PAGINATION_DEFAULT_OFFSET = 0;
969
+ function isCursorPaginationOptions(options) {
970
+ return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
971
+ }
953
972
 
954
973
  var __accessCheck$5 = (obj, member, msg) => {
955
974
  if (!member.has(obj))
956
975
  throw TypeError("Cannot " + msg);
957
976
  };
958
- var __privateGet$4 = (obj, member, getter) => {
977
+ var __privateGet$5 = (obj, member, getter) => {
959
978
  __accessCheck$5(obj, member, "read from private field");
960
979
  return getter ? getter.call(obj) : member.get(obj);
961
980
  };
@@ -964,7 +983,7 @@ var __privateAdd$5 = (obj, member, value) => {
964
983
  throw TypeError("Cannot add the same private member more than once");
965
984
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
966
985
  };
967
- var __privateSet$3 = (obj, member, value, setter) => {
986
+ var __privateSet$4 = (obj, member, value, setter) => {
968
987
  __accessCheck$5(obj, member, "write to private field");
969
988
  setter ? setter.call(obj, value) : member.set(obj, value);
970
989
  return value;
@@ -977,21 +996,21 @@ const _Query = class {
977
996
  __privateAdd$5(this, _data, { filter: {} });
978
997
  this.meta = { page: { cursor: "start", more: true } };
979
998
  this.records = [];
980
- __privateSet$3(this, _table$1, table);
999
+ __privateSet$4(this, _table$1, table);
981
1000
  if (repository) {
982
- __privateSet$3(this, _repository, repository);
1001
+ __privateSet$4(this, _repository, repository);
983
1002
  } else {
984
- __privateSet$3(this, _repository, this);
1003
+ __privateSet$4(this, _repository, this);
985
1004
  }
986
- __privateGet$4(this, _data).filter = data.filter ?? parent?.filter ?? {};
987
- __privateGet$4(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
988
- __privateGet$4(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
989
- __privateGet$4(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
990
- __privateGet$4(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
991
- __privateGet$4(this, _data).sort = data.sort ?? parent?.sort;
992
- __privateGet$4(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
993
- __privateGet$4(this, _data).page = data.page ?? parent?.page;
994
- __privateGet$4(this, _data).cache = data.cache ?? parent?.cache;
1005
+ __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
1006
+ __privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
1007
+ __privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
1008
+ __privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
1009
+ __privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
1010
+ __privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
1011
+ __privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
1012
+ __privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
1013
+ __privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
995
1014
  this.any = this.any.bind(this);
996
1015
  this.all = this.all.bind(this);
997
1016
  this.not = this.not.bind(this);
@@ -1002,83 +1021,85 @@ const _Query = class {
1002
1021
  Object.defineProperty(this, "repository", { enumerable: false });
1003
1022
  }
1004
1023
  getQueryOptions() {
1005
- return __privateGet$4(this, _data);
1024
+ return __privateGet$5(this, _data);
1006
1025
  }
1007
1026
  key() {
1008
- const { columns = [], filter = {}, sort = [], page = {} } = __privateGet$4(this, _data);
1009
- const key = JSON.stringify({ columns, filter, sort, page });
1027
+ const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
1028
+ const key = JSON.stringify({ columns, filter, sort, pagination });
1010
1029
  return toBase64(key);
1011
1030
  }
1012
1031
  any(...queries) {
1013
1032
  const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
1014
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $any } }, __privateGet$4(this, _data));
1033
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
1015
1034
  }
1016
1035
  all(...queries) {
1017
1036
  const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
1018
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1037
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1019
1038
  }
1020
1039
  not(...queries) {
1021
1040
  const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
1022
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $not } }, __privateGet$4(this, _data));
1041
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
1023
1042
  }
1024
1043
  none(...queries) {
1025
1044
  const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
1026
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $none } }, __privateGet$4(this, _data));
1045
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
1027
1046
  }
1028
1047
  filter(a, b) {
1029
1048
  if (arguments.length === 1) {
1030
1049
  const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
1031
- const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat(constraints));
1032
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1050
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
1051
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1033
1052
  } else {
1034
- const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1035
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1053
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1054
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1036
1055
  }
1037
1056
  }
1038
1057
  sort(column, direction) {
1039
- const originalSort = [__privateGet$4(this, _data).sort ?? []].flat();
1058
+ const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
1040
1059
  const sort = [...originalSort, { column, direction }];
1041
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { sort }, __privateGet$4(this, _data));
1060
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
1042
1061
  }
1043
1062
  select(columns) {
1044
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { columns }, __privateGet$4(this, _data));
1063
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
1045
1064
  }
1046
1065
  getPaginated(options = {}) {
1047
- const query = new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), options, __privateGet$4(this, _data));
1048
- return __privateGet$4(this, _repository).query(query);
1066
+ const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
1067
+ return __privateGet$5(this, _repository).query(query);
1049
1068
  }
1050
1069
  async *[Symbol.asyncIterator]() {
1051
- for await (const [record] of this.getIterator(1)) {
1070
+ for await (const [record] of this.getIterator({ batchSize: 1 })) {
1052
1071
  yield record;
1053
1072
  }
1054
1073
  }
1055
- async *getIterator(chunk, options = {}) {
1056
- let offset = 0;
1057
- let end = false;
1058
- while (!end) {
1059
- const { records, meta } = await this.getPaginated({ ...options, page: { size: chunk, offset } });
1060
- yield records;
1061
- offset += chunk;
1062
- end = !meta.page.more;
1074
+ async *getIterator(options = {}) {
1075
+ const { batchSize = 1 } = options;
1076
+ let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
1077
+ let more = page.hasNextPage();
1078
+ yield page.records;
1079
+ while (more) {
1080
+ page = await page.nextPage();
1081
+ more = page.hasNextPage();
1082
+ yield page.records;
1063
1083
  }
1064
1084
  }
1065
1085
  async getMany(options = {}) {
1066
1086
  const { records } = await this.getPaginated(options);
1067
1087
  return records;
1068
1088
  }
1069
- async getAll(chunk = PAGINATION_MAX_SIZE, options = {}) {
1089
+ async getAll(options = {}) {
1090
+ const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
1070
1091
  const results = [];
1071
- for await (const page of this.getIterator(chunk, options)) {
1092
+ for await (const page of this.getIterator({ ...rest, batchSize })) {
1072
1093
  results.push(...page);
1073
1094
  }
1074
1095
  return results;
1075
1096
  }
1076
1097
  async getFirst(options = {}) {
1077
- const records = await this.getMany({ ...options, page: { size: 1 } });
1078
- return records[0] || null;
1098
+ const records = await this.getMany({ ...options, pagination: { size: 1 } });
1099
+ return records[0] ?? null;
1079
1100
  }
1080
1101
  cache(ttl) {
1081
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { cache: ttl }, __privateGet$4(this, _data));
1102
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
1082
1103
  }
1083
1104
  nextPage(size, offset) {
1084
1105
  return this.firstPage(size, offset);
@@ -1087,10 +1108,10 @@ const _Query = class {
1087
1108
  return this.firstPage(size, offset);
1088
1109
  }
1089
1110
  firstPage(size, offset) {
1090
- return this.getPaginated({ page: { size, offset } });
1111
+ return this.getPaginated({ pagination: { size, offset } });
1091
1112
  }
1092
1113
  lastPage(size, offset) {
1093
- return this.getPaginated({ page: { size, offset, before: "end" } });
1114
+ return this.getPaginated({ pagination: { size, offset, before: "end" } });
1094
1115
  }
1095
1116
  hasNextPage() {
1096
1117
  return this.meta.page.more;
@@ -1135,7 +1156,7 @@ var __accessCheck$4 = (obj, member, msg) => {
1135
1156
  if (!member.has(obj))
1136
1157
  throw TypeError("Cannot " + msg);
1137
1158
  };
1138
- var __privateGet$3 = (obj, member, getter) => {
1159
+ var __privateGet$4 = (obj, member, getter) => {
1139
1160
  __accessCheck$4(obj, member, "read from private field");
1140
1161
  return getter ? getter.call(obj) : member.get(obj);
1141
1162
  };
@@ -1144,7 +1165,7 @@ var __privateAdd$4 = (obj, member, value) => {
1144
1165
  throw TypeError("Cannot add the same private member more than once");
1145
1166
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1146
1167
  };
1147
- var __privateSet$2 = (obj, member, value, setter) => {
1168
+ var __privateSet$3 = (obj, member, value, setter) => {
1148
1169
  __accessCheck$4(obj, member, "write to private field");
1149
1170
  setter ? setter.call(obj, value) : member.set(obj, value);
1150
1171
  return value;
@@ -1153,7 +1174,7 @@ var __privateMethod$2 = (obj, member, method) => {
1153
1174
  __accessCheck$4(obj, member, "access private method");
1154
1175
  return method;
1155
1176
  };
1156
- 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;
1177
+ 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;
1157
1178
  class Repository extends Query {
1158
1179
  }
1159
1180
  class RestRepository extends Query {
@@ -1170,15 +1191,15 @@ class RestRepository extends Query {
1170
1191
  __privateAdd$4(this, _getCacheRecord);
1171
1192
  __privateAdd$4(this, _setCacheQuery);
1172
1193
  __privateAdd$4(this, _getCacheQuery);
1194
+ __privateAdd$4(this, _getSchema$1);
1173
1195
  __privateAdd$4(this, _table, void 0);
1174
- __privateAdd$4(this, _links, void 0);
1175
1196
  __privateAdd$4(this, _getFetchProps, void 0);
1176
1197
  __privateAdd$4(this, _cache, void 0);
1177
- __privateSet$2(this, _table, options.table);
1178
- __privateSet$2(this, _links, options.links ?? {});
1179
- __privateSet$2(this, _getFetchProps, options.pluginOptions.getFetchProps);
1198
+ __privateAdd$4(this, _schema$1, void 0);
1199
+ __privateSet$3(this, _table, options.table);
1200
+ __privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
1180
1201
  this.db = options.db;
1181
- __privateSet$2(this, _cache, options.pluginOptions.cache);
1202
+ __privateSet$3(this, _cache, options.pluginOptions.cache);
1182
1203
  }
1183
1204
  async create(a, b) {
1184
1205
  if (Array.isArray(a)) {
@@ -1211,13 +1232,14 @@ class RestRepository extends Query {
1211
1232
  const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1212
1233
  if (cacheRecord)
1213
1234
  return cacheRecord;
1214
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1235
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1215
1236
  try {
1216
1237
  const response = await getRecord({
1217
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1238
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1218
1239
  ...fetchProps
1219
1240
  });
1220
- return initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), response);
1241
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1242
+ return initObject(this.db, schema, __privateGet$4(this, _table), response);
1221
1243
  } catch (e) {
1222
1244
  if (isObject(e) && e.status === 404) {
1223
1245
  return null;
@@ -1288,49 +1310,58 @@ class RestRepository extends Query {
1288
1310
  throw new Error("Invalid arguments for delete method");
1289
1311
  }
1290
1312
  async search(query, options = {}) {
1291
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1292
- const { records } = await searchBranch({
1293
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1294
- body: { tables: [__privateGet$3(this, _table)], query, fuzziness: options.fuzziness },
1313
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1314
+ const { records } = await searchTable({
1315
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1316
+ body: {
1317
+ query,
1318
+ fuzziness: options.fuzziness,
1319
+ filter: options.filter
1320
+ },
1295
1321
  ...fetchProps
1296
1322
  });
1297
- return records.map((item) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), item));
1323
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1324
+ return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
1298
1325
  }
1299
1326
  async query(query) {
1300
1327
  const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
1301
1328
  if (cacheQuery)
1302
1329
  return new Page(query, cacheQuery.meta, cacheQuery.records);
1303
1330
  const data = query.getQueryOptions();
1331
+ const filter = Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0;
1332
+ const sort = data.sort !== void 0 ? buildSortFilter(data.sort) : void 0;
1333
+ const isCursorPagination = isCursorPaginationOptions(data.pagination);
1304
1334
  const body = {
1305
- filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1306
- sort: data.sort ? buildSortFilter(data.sort) : void 0,
1307
- page: data.page,
1335
+ filter: isCursorPagination ? void 0 : filter,
1336
+ sort: isCursorPagination ? void 0 : sort,
1337
+ page: data.pagination,
1308
1338
  columns: data.columns
1309
1339
  };
1310
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1340
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1311
1341
  const { meta, records: objects } = await queryTable({
1312
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1342
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1313
1343
  body,
1314
1344
  ...fetchProps
1315
1345
  });
1316
- const records = objects.map((record) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), record));
1346
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1347
+ const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
1317
1348
  await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1318
1349
  return new Page(query, meta, records);
1319
1350
  }
1320
1351
  }
1321
1352
  _table = new WeakMap();
1322
- _links = new WeakMap();
1323
1353
  _getFetchProps = new WeakMap();
1324
1354
  _cache = new WeakMap();
1355
+ _schema$1 = new WeakMap();
1325
1356
  _insertRecordWithoutId = new WeakSet();
1326
1357
  insertRecordWithoutId_fn = async function(object) {
1327
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1358
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1328
1359
  const record = transformObjectLinks(object);
1329
1360
  const response = await insertRecord({
1330
1361
  pathParams: {
1331
1362
  workspace: "{workspaceId}",
1332
1363
  dbBranchName: "{dbBranch}",
1333
- tableName: __privateGet$3(this, _table)
1364
+ tableName: __privateGet$4(this, _table)
1334
1365
  },
1335
1366
  body: record,
1336
1367
  ...fetchProps
@@ -1343,13 +1374,13 @@ insertRecordWithoutId_fn = async function(object) {
1343
1374
  };
1344
1375
  _insertRecordWithId = new WeakSet();
1345
1376
  insertRecordWithId_fn = async function(recordId, object) {
1346
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1377
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1347
1378
  const record = transformObjectLinks(object);
1348
1379
  const response = await insertRecordWithID({
1349
1380
  pathParams: {
1350
1381
  workspace: "{workspaceId}",
1351
1382
  dbBranchName: "{dbBranch}",
1352
- tableName: __privateGet$3(this, _table),
1383
+ tableName: __privateGet$4(this, _table),
1353
1384
  recordId
1354
1385
  },
1355
1386
  body: record,
@@ -1364,10 +1395,10 @@ insertRecordWithId_fn = async function(recordId, object) {
1364
1395
  };
1365
1396
  _bulkInsertTableRecords = new WeakSet();
1366
1397
  bulkInsertTableRecords_fn = async function(objects) {
1367
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1398
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1368
1399
  const records = objects.map((object) => transformObjectLinks(object));
1369
1400
  const response = await bulkInsertTableRecords({
1370
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1401
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1371
1402
  body: { records },
1372
1403
  ...fetchProps
1373
1404
  });
@@ -1379,10 +1410,10 @@ bulkInsertTableRecords_fn = async function(objects) {
1379
1410
  };
1380
1411
  _updateRecordWithID = new WeakSet();
1381
1412
  updateRecordWithID_fn = async function(recordId, object) {
1382
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1413
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1383
1414
  const record = transformObjectLinks(object);
1384
1415
  const response = await updateRecordWithID({
1385
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1416
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1386
1417
  body: record,
1387
1418
  ...fetchProps
1388
1419
  });
@@ -1393,9 +1424,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1393
1424
  };
1394
1425
  _upsertRecordWithID = new WeakSet();
1395
1426
  upsertRecordWithID_fn = async function(recordId, object) {
1396
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1427
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1397
1428
  const response = await upsertRecordWithID({
1398
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1429
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1399
1430
  body: object,
1400
1431
  ...fetchProps
1401
1432
  });
@@ -1406,51 +1437,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
1406
1437
  };
1407
1438
  _deleteRecord = new WeakSet();
1408
1439
  deleteRecord_fn = async function(recordId) {
1409
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1440
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1410
1441
  await deleteRecord({
1411
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1442
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1412
1443
  ...fetchProps
1413
1444
  });
1414
1445
  };
1415
1446
  _invalidateCache = new WeakSet();
1416
1447
  invalidateCache_fn = async function(recordId) {
1417
- await __privateGet$3(this, _cache).delete(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1418
- const cacheItems = await __privateGet$3(this, _cache).getAll();
1448
+ await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1449
+ const cacheItems = await __privateGet$4(this, _cache).getAll();
1419
1450
  const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1420
1451
  for (const [key, value] of queries) {
1421
1452
  const ids = getIds(value);
1422
1453
  if (ids.includes(recordId))
1423
- await __privateGet$3(this, _cache).delete(key);
1454
+ await __privateGet$4(this, _cache).delete(key);
1424
1455
  }
1425
1456
  };
1426
1457
  _setCacheRecord = new WeakSet();
1427
1458
  setCacheRecord_fn = async function(record) {
1428
- if (!__privateGet$3(this, _cache).cacheRecords)
1459
+ if (!__privateGet$4(this, _cache).cacheRecords)
1429
1460
  return;
1430
- await __privateGet$3(this, _cache).set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
1461
+ await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
1431
1462
  };
1432
1463
  _getCacheRecord = new WeakSet();
1433
1464
  getCacheRecord_fn = async function(recordId) {
1434
- if (!__privateGet$3(this, _cache).cacheRecords)
1465
+ if (!__privateGet$4(this, _cache).cacheRecords)
1435
1466
  return null;
1436
- return __privateGet$3(this, _cache).get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1467
+ return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1437
1468
  };
1438
1469
  _setCacheQuery = new WeakSet();
1439
1470
  setCacheQuery_fn = async function(query, meta, records) {
1440
- await __privateGet$3(this, _cache).set(`query_${__privateGet$3(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1471
+ await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1441
1472
  };
1442
1473
  _getCacheQuery = new WeakSet();
1443
1474
  getCacheQuery_fn = async function(query) {
1444
- const key = `query_${__privateGet$3(this, _table)}:${query.key()}`;
1445
- const result = await __privateGet$3(this, _cache).get(key);
1475
+ const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
1476
+ const result = await __privateGet$4(this, _cache).get(key);
1446
1477
  if (!result)
1447
1478
  return null;
1448
- const { cache: ttl = __privateGet$3(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1449
- if (!ttl || ttl < 0)
1450
- return result;
1479
+ const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1480
+ if (ttl < 0)
1481
+ return null;
1451
1482
  const hasExpired = result.date.getTime() + ttl < Date.now();
1452
1483
  return hasExpired ? null : result;
1453
1484
  };
1485
+ _getSchema$1 = new WeakSet();
1486
+ getSchema_fn$1 = async function() {
1487
+ if (__privateGet$4(this, _schema$1))
1488
+ return __privateGet$4(this, _schema$1);
1489
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1490
+ const { schema } = await getBranchDetails({
1491
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1492
+ ...fetchProps
1493
+ });
1494
+ __privateSet$3(this, _schema$1, schema);
1495
+ return schema;
1496
+ };
1454
1497
  const transformObjectLinks = (object) => {
1455
1498
  return Object.entries(object).reduce((acc, [key, value]) => {
1456
1499
  if (key === "xata")
@@ -1458,15 +1501,33 @@ const transformObjectLinks = (object) => {
1458
1501
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1459
1502
  }, {});
1460
1503
  };
1461
- const initObject = (db, links, table, object) => {
1504
+ const initObject = (db, schema, table, object) => {
1462
1505
  const result = {};
1463
1506
  Object.assign(result, object);
1464
- const tableLinks = links[table] || [];
1465
- for (const link of tableLinks) {
1466
- const [field, linkTable] = link;
1467
- const value = result[field];
1468
- if (value && isObject(value)) {
1469
- result[field] = initObject(db, links, linkTable, value);
1507
+ const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
1508
+ if (!columns)
1509
+ console.error(`Table ${table} not found in schema`);
1510
+ for (const column of columns ?? []) {
1511
+ const value = result[column.name];
1512
+ switch (column.type) {
1513
+ case "datetime": {
1514
+ const date = new Date(value);
1515
+ if (isNaN(date.getTime())) {
1516
+ console.error(`Failed to parse date ${value} for field ${column.name}`);
1517
+ } else {
1518
+ result[column.name] = date;
1519
+ }
1520
+ break;
1521
+ }
1522
+ case "link": {
1523
+ const linkTable = column.link?.table;
1524
+ if (!linkTable) {
1525
+ console.error(`Failed to parse link for field ${column.name}`);
1526
+ } else if (isObject(value)) {
1527
+ result[column.name] = initObject(db, schema, linkTable, value);
1528
+ }
1529
+ break;
1530
+ }
1470
1531
  }
1471
1532
  }
1472
1533
  result.read = function() {
@@ -1498,7 +1559,7 @@ var __accessCheck$3 = (obj, member, msg) => {
1498
1559
  if (!member.has(obj))
1499
1560
  throw TypeError("Cannot " + msg);
1500
1561
  };
1501
- var __privateGet$2 = (obj, member, getter) => {
1562
+ var __privateGet$3 = (obj, member, getter) => {
1502
1563
  __accessCheck$3(obj, member, "read from private field");
1503
1564
  return getter ? getter.call(obj) : member.get(obj);
1504
1565
  };
@@ -1507,7 +1568,7 @@ var __privateAdd$3 = (obj, member, value) => {
1507
1568
  throw TypeError("Cannot add the same private member more than once");
1508
1569
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1509
1570
  };
1510
- var __privateSet$1 = (obj, member, value, setter) => {
1571
+ var __privateSet$2 = (obj, member, value, setter) => {
1511
1572
  __accessCheck$3(obj, member, "write to private field");
1512
1573
  setter ? setter.call(obj, value) : member.set(obj, value);
1513
1574
  return value;
@@ -1516,30 +1577,30 @@ var _map;
1516
1577
  class SimpleCache {
1517
1578
  constructor(options = {}) {
1518
1579
  __privateAdd$3(this, _map, void 0);
1519
- __privateSet$1(this, _map, /* @__PURE__ */ new Map());
1580
+ __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1520
1581
  this.capacity = options.max ?? 500;
1521
1582
  this.cacheRecords = options.cacheRecords ?? true;
1522
1583
  this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1523
1584
  }
1524
1585
  async getAll() {
1525
- return Object.fromEntries(__privateGet$2(this, _map));
1586
+ return Object.fromEntries(__privateGet$3(this, _map));
1526
1587
  }
1527
1588
  async get(key) {
1528
- return __privateGet$2(this, _map).get(key) ?? null;
1589
+ return __privateGet$3(this, _map).get(key) ?? null;
1529
1590
  }
1530
1591
  async set(key, value) {
1531
1592
  await this.delete(key);
1532
- __privateGet$2(this, _map).set(key, value);
1533
- if (__privateGet$2(this, _map).size > this.capacity) {
1534
- const leastRecentlyUsed = __privateGet$2(this, _map).keys().next().value;
1593
+ __privateGet$3(this, _map).set(key, value);
1594
+ if (__privateGet$3(this, _map).size > this.capacity) {
1595
+ const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
1535
1596
  await this.delete(leastRecentlyUsed);
1536
1597
  }
1537
1598
  }
1538
1599
  async delete(key) {
1539
- __privateGet$2(this, _map).delete(key);
1600
+ __privateGet$3(this, _map).delete(key);
1540
1601
  }
1541
1602
  async clear() {
1542
- return __privateGet$2(this, _map).clear();
1603
+ return __privateGet$3(this, _map).clear();
1543
1604
  }
1544
1605
  }
1545
1606
  _map = new WeakMap();
@@ -1567,7 +1628,7 @@ var __accessCheck$2 = (obj, member, msg) => {
1567
1628
  if (!member.has(obj))
1568
1629
  throw TypeError("Cannot " + msg);
1569
1630
  };
1570
- var __privateGet$1 = (obj, member, getter) => {
1631
+ var __privateGet$2 = (obj, member, getter) => {
1571
1632
  __accessCheck$2(obj, member, "read from private field");
1572
1633
  return getter ? getter.call(obj) : member.get(obj);
1573
1634
  };
@@ -1578,26 +1639,24 @@ var __privateAdd$2 = (obj, member, value) => {
1578
1639
  };
1579
1640
  var _tables;
1580
1641
  class SchemaPlugin extends XataPlugin {
1581
- constructor(links, tableNames) {
1642
+ constructor(tableNames) {
1582
1643
  super();
1583
- this.links = links;
1584
1644
  this.tableNames = tableNames;
1585
1645
  __privateAdd$2(this, _tables, {});
1586
1646
  }
1587
1647
  build(pluginOptions) {
1588
- const links = this.links;
1589
1648
  const db = new Proxy({}, {
1590
1649
  get: (_target, table) => {
1591
1650
  if (!isString(table))
1592
1651
  throw new Error("Invalid table name");
1593
- if (!__privateGet$1(this, _tables)[table]) {
1594
- __privateGet$1(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, links });
1652
+ if (__privateGet$2(this, _tables)[table] === void 0) {
1653
+ __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1595
1654
  }
1596
- return __privateGet$1(this, _tables)[table];
1655
+ return __privateGet$2(this, _tables)[table];
1597
1656
  }
1598
1657
  });
1599
1658
  for (const table of this.tableNames ?? []) {
1600
- db[table] = new RestRepository({ db, pluginOptions, table, links });
1659
+ db[table] = new RestRepository({ db, pluginOptions, table });
1601
1660
  }
1602
1661
  return db;
1603
1662
  }
@@ -1608,44 +1667,57 @@ var __accessCheck$1 = (obj, member, msg) => {
1608
1667
  if (!member.has(obj))
1609
1668
  throw TypeError("Cannot " + msg);
1610
1669
  };
1670
+ var __privateGet$1 = (obj, member, getter) => {
1671
+ __accessCheck$1(obj, member, "read from private field");
1672
+ return getter ? getter.call(obj) : member.get(obj);
1673
+ };
1611
1674
  var __privateAdd$1 = (obj, member, value) => {
1612
1675
  if (member.has(obj))
1613
1676
  throw TypeError("Cannot add the same private member more than once");
1614
1677
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1615
1678
  };
1679
+ var __privateSet$1 = (obj, member, value, setter) => {
1680
+ __accessCheck$1(obj, member, "write to private field");
1681
+ setter ? setter.call(obj, value) : member.set(obj, value);
1682
+ return value;
1683
+ };
1616
1684
  var __privateMethod$1 = (obj, member, method) => {
1617
1685
  __accessCheck$1(obj, member, "access private method");
1618
1686
  return method;
1619
1687
  };
1620
- var _search, search_fn;
1688
+ var _schema, _search, search_fn, _getSchema, getSchema_fn;
1621
1689
  class SearchPlugin extends XataPlugin {
1622
- constructor(db, links) {
1690
+ constructor(db) {
1623
1691
  super();
1624
1692
  this.db = db;
1625
- this.links = links;
1626
1693
  __privateAdd$1(this, _search);
1694
+ __privateAdd$1(this, _getSchema);
1695
+ __privateAdd$1(this, _schema, void 0);
1627
1696
  }
1628
1697
  build({ getFetchProps }) {
1629
1698
  return {
1630
1699
  all: async (query, options = {}) => {
1631
1700
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1701
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1632
1702
  return records.map((record) => {
1633
1703
  const { table = "orphan" } = record.xata;
1634
- return { table, record: initObject(this.db, this.links, table, record) };
1704
+ return { table, record: initObject(this.db, schema, table, record) };
1635
1705
  });
1636
1706
  },
1637
1707
  byTable: async (query, options = {}) => {
1638
1708
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1709
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1639
1710
  return records.reduce((acc, record) => {
1640
1711
  const { table = "orphan" } = record.xata;
1641
1712
  const items = acc[table] ?? [];
1642
- const item = initObject(this.db, this.links, table, record);
1713
+ const item = initObject(this.db, schema, table, record);
1643
1714
  return { ...acc, [table]: [...items, item] };
1644
1715
  }, {});
1645
1716
  }
1646
1717
  };
1647
1718
  }
1648
1719
  }
1720
+ _schema = new WeakMap();
1649
1721
  _search = new WeakSet();
1650
1722
  search_fn = async function(query, options, getFetchProps) {
1651
1723
  const fetchProps = await getFetchProps();
@@ -1657,6 +1729,18 @@ search_fn = async function(query, options, getFetchProps) {
1657
1729
  });
1658
1730
  return records;
1659
1731
  };
1732
+ _getSchema = new WeakSet();
1733
+ getSchema_fn = async function(getFetchProps) {
1734
+ if (__privateGet$1(this, _schema))
1735
+ return __privateGet$1(this, _schema);
1736
+ const fetchProps = await getFetchProps();
1737
+ const { schema } = await getBranchDetails({
1738
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1739
+ ...fetchProps
1740
+ });
1741
+ __privateSet$1(this, _schema, schema);
1742
+ return schema;
1743
+ };
1660
1744
 
1661
1745
  const isBranchStrategyBuilder = (strategy) => {
1662
1746
  return typeof strategy === "function";
@@ -1668,30 +1752,39 @@ const envBranchNames = [
1668
1752
  "CF_PAGES_BRANCH",
1669
1753
  "BRANCH"
1670
1754
  ];
1671
- const defaultBranch = "main";
1672
1755
  async function getCurrentBranchName(options) {
1673
- const env = await getBranchByEnvVariable();
1674
- if (env)
1675
- return env;
1676
- const branch = await getGitBranch();
1677
- if (!branch)
1678
- return defaultBranch;
1679
- const details = await getDatabaseBranch(branch, options);
1680
- if (details)
1681
- return branch;
1682
- return defaultBranch;
1756
+ const env = getBranchByEnvVariable();
1757
+ if (env) {
1758
+ const details = await getDatabaseBranch(env, options);
1759
+ if (details)
1760
+ return env;
1761
+ console.warn(`Branch ${env} not found in Xata. Ignoring...`);
1762
+ }
1763
+ const gitBranch = await getGitBranch();
1764
+ return resolveXataBranch(gitBranch, options);
1683
1765
  }
1684
1766
  async function getCurrentBranchDetails(options) {
1685
- const env = await getBranchByEnvVariable();
1686
- if (env)
1687
- return getDatabaseBranch(env, options);
1688
- const branch = await getGitBranch();
1689
- if (!branch)
1690
- return getDatabaseBranch(defaultBranch, options);
1691
- const details = await getDatabaseBranch(branch, options);
1692
- if (details)
1693
- return details;
1694
- return getDatabaseBranch(defaultBranch, options);
1767
+ const branch = await getCurrentBranchName(options);
1768
+ return getDatabaseBranch(branch, options);
1769
+ }
1770
+ async function resolveXataBranch(gitBranch, options) {
1771
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1772
+ const apiKey = options?.apiKey || getAPIKey();
1773
+ if (!databaseURL)
1774
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1775
+ if (!apiKey)
1776
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1777
+ const [protocol, , host, , dbName] = databaseURL.split("/");
1778
+ const [workspace] = host.split(".");
1779
+ const { branch } = await resolveBranch({
1780
+ apiKey,
1781
+ apiUrl: databaseURL,
1782
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1783
+ workspacesApiUrl: `${protocol}//${host}`,
1784
+ pathParams: { dbName, workspace },
1785
+ queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
1786
+ });
1787
+ return branch;
1695
1788
  }
1696
1789
  async function getDatabaseBranch(branch, options) {
1697
1790
  const databaseURL = options?.databaseURL || getDatabaseURL();
@@ -1765,7 +1858,7 @@ var __privateMethod = (obj, member, method) => {
1765
1858
  const buildClient = (plugins) => {
1766
1859
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1767
1860
  return _a = class {
1768
- constructor(options = {}, links, tables) {
1861
+ constructor(options = {}, tables) {
1769
1862
  __privateAdd(this, _parseOptions);
1770
1863
  __privateAdd(this, _getFetchProps);
1771
1864
  __privateAdd(this, _evaluateBranch);
@@ -1775,12 +1868,12 @@ const buildClient = (plugins) => {
1775
1868
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1776
1869
  cache: safeOptions.cache
1777
1870
  };
1778
- const db = new SchemaPlugin(links, tables).build(pluginOptions);
1779
- const search = new SearchPlugin(db, links ?? {}).build(pluginOptions);
1871
+ const db = new SchemaPlugin(tables).build(pluginOptions);
1872
+ const search = new SearchPlugin(db).build(pluginOptions);
1780
1873
  this.db = db;
1781
1874
  this.search = search;
1782
1875
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1783
- if (!namespace)
1876
+ if (namespace === void 0)
1784
1877
  continue;
1785
1878
  const result = namespace.build(pluginOptions);
1786
1879
  if (result instanceof Promise) {
@@ -1797,7 +1890,7 @@ const buildClient = (plugins) => {
1797
1890
  const databaseURL = options?.databaseURL || getDatabaseURL();
1798
1891
  const apiKey = options?.apiKey || getAPIKey();
1799
1892
  const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1800
- const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1893
+ const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1801
1894
  if (!databaseURL || !apiKey) {
1802
1895
  throw new Error("Options databaseURL and apiKey are required");
1803
1896
  }
@@ -1824,7 +1917,7 @@ const buildClient = (plugins) => {
1824
1917
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
1825
1918
  if (__privateGet(this, _branch))
1826
1919
  return __privateGet(this, _branch);
1827
- if (!param)
1920
+ if (param === void 0)
1828
1921
  return void 0;
1829
1922
  const strategies = Array.isArray(param) ? [...param] : [param];
1830
1923
  const evaluateBranch = async (strategy) => {
@@ -1849,5 +1942,5 @@ class XataError extends Error {
1849
1942
  }
1850
1943
  }
1851
1944
 
1852
- export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
1945
+ export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
1853
1946
  //# sourceMappingURL=index.mjs.map