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