@xata.io/client 0.0.0-alpha.vfb4a018 → 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,50 +1025,50 @@ 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
1074
  for await (const [record] of this.getIterator({ batchSize: 1 })) {
@@ -1058,13 +1077,13 @@ const _Query = class {
1058
1077
  }
1059
1078
  async *getIterator(options = {}) {
1060
1079
  const { batchSize = 1 } = options;
1061
- let offset = 0;
1062
- let end = false;
1063
- while (!end) {
1064
- const { records, meta } = await this.getPaginated({ ...options, page: { size: batchSize, offset } });
1065
- yield records;
1066
- offset += batchSize;
1067
- end = !meta.page.more;
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;
1068
1087
  }
1069
1088
  }
1070
1089
  async getMany(options = {}) {
@@ -1080,11 +1099,11 @@ const _Query = class {
1080
1099
  return results;
1081
1100
  }
1082
1101
  async getFirst(options = {}) {
1083
- const records = await this.getMany({ ...options, page: { size: 1 } });
1084
- return records[0] || null;
1102
+ const records = await this.getMany({ ...options, pagination: { size: 1 } });
1103
+ return records[0] ?? null;
1085
1104
  }
1086
1105
  cache(ttl) {
1087
- 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));
1088
1107
  }
1089
1108
  nextPage(size, offset) {
1090
1109
  return this.firstPage(size, offset);
@@ -1093,10 +1112,10 @@ const _Query = class {
1093
1112
  return this.firstPage(size, offset);
1094
1113
  }
1095
1114
  firstPage(size, offset) {
1096
- return this.getPaginated({ page: { size, offset } });
1115
+ return this.getPaginated({ pagination: { size, offset } });
1097
1116
  }
1098
1117
  lastPage(size, offset) {
1099
- return this.getPaginated({ page: { size, offset, before: "end" } });
1118
+ return this.getPaginated({ pagination: { size, offset, before: "end" } });
1100
1119
  }
1101
1120
  hasNextPage() {
1102
1121
  return this.meta.page.more;
@@ -1141,7 +1160,7 @@ var __accessCheck$4 = (obj, member, msg) => {
1141
1160
  if (!member.has(obj))
1142
1161
  throw TypeError("Cannot " + msg);
1143
1162
  };
1144
- var __privateGet$3 = (obj, member, getter) => {
1163
+ var __privateGet$4 = (obj, member, getter) => {
1145
1164
  __accessCheck$4(obj, member, "read from private field");
1146
1165
  return getter ? getter.call(obj) : member.get(obj);
1147
1166
  };
@@ -1150,7 +1169,7 @@ var __privateAdd$4 = (obj, member, value) => {
1150
1169
  throw TypeError("Cannot add the same private member more than once");
1151
1170
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1152
1171
  };
1153
- var __privateSet$2 = (obj, member, value, setter) => {
1172
+ var __privateSet$3 = (obj, member, value, setter) => {
1154
1173
  __accessCheck$4(obj, member, "write to private field");
1155
1174
  setter ? setter.call(obj, value) : member.set(obj, value);
1156
1175
  return value;
@@ -1159,7 +1178,7 @@ var __privateMethod$2 = (obj, member, method) => {
1159
1178
  __accessCheck$4(obj, member, "access private method");
1160
1179
  return method;
1161
1180
  };
1162
- 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;
1163
1182
  class Repository extends Query {
1164
1183
  }
1165
1184
  class RestRepository extends Query {
@@ -1176,15 +1195,15 @@ class RestRepository extends Query {
1176
1195
  __privateAdd$4(this, _getCacheRecord);
1177
1196
  __privateAdd$4(this, _setCacheQuery);
1178
1197
  __privateAdd$4(this, _getCacheQuery);
1198
+ __privateAdd$4(this, _getSchema$1);
1179
1199
  __privateAdd$4(this, _table, void 0);
1180
- __privateAdd$4(this, _links, void 0);
1181
1200
  __privateAdd$4(this, _getFetchProps, void 0);
1182
1201
  __privateAdd$4(this, _cache, void 0);
1183
- __privateSet$2(this, _table, options.table);
1184
- __privateSet$2(this, _links, options.links ?? {});
1185
- __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);
1186
1205
  this.db = options.db;
1187
- __privateSet$2(this, _cache, options.pluginOptions.cache);
1206
+ __privateSet$3(this, _cache, options.pluginOptions.cache);
1188
1207
  }
1189
1208
  async create(a, b) {
1190
1209
  if (Array.isArray(a)) {
@@ -1217,13 +1236,14 @@ class RestRepository extends Query {
1217
1236
  const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1218
1237
  if (cacheRecord)
1219
1238
  return cacheRecord;
1220
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1239
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1221
1240
  try {
1222
1241
  const response = await getRecord({
1223
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1242
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1224
1243
  ...fetchProps
1225
1244
  });
1226
- 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);
1227
1247
  } catch (e) {
1228
1248
  if (isObject(e) && e.status === 404) {
1229
1249
  return null;
@@ -1294,49 +1314,58 @@ class RestRepository extends Query {
1294
1314
  throw new Error("Invalid arguments for delete method");
1295
1315
  }
1296
1316
  async search(query, options = {}) {
1297
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1298
- const { records } = await searchBranch({
1299
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1300
- 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
+ },
1301
1325
  ...fetchProps
1302
1326
  });
1303
- 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));
1304
1329
  }
1305
1330
  async query(query) {
1306
1331
  const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
1307
1332
  if (cacheQuery)
1308
1333
  return new Page(query, cacheQuery.meta, cacheQuery.records);
1309
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);
1310
1338
  const body = {
1311
- filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1312
- sort: data.sort ? buildSortFilter(data.sort) : void 0,
1313
- page: data.page,
1339
+ filter: isCursorPagination ? void 0 : filter,
1340
+ sort: isCursorPagination ? void 0 : sort,
1341
+ page: data.pagination,
1314
1342
  columns: data.columns
1315
1343
  };
1316
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1344
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1317
1345
  const { meta, records: objects } = await queryTable({
1318
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1346
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1319
1347
  body,
1320
1348
  ...fetchProps
1321
1349
  });
1322
- 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));
1323
1352
  await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1324
1353
  return new Page(query, meta, records);
1325
1354
  }
1326
1355
  }
1327
1356
  _table = new WeakMap();
1328
- _links = new WeakMap();
1329
1357
  _getFetchProps = new WeakMap();
1330
1358
  _cache = new WeakMap();
1359
+ _schema$1 = new WeakMap();
1331
1360
  _insertRecordWithoutId = new WeakSet();
1332
1361
  insertRecordWithoutId_fn = async function(object) {
1333
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1362
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1334
1363
  const record = transformObjectLinks(object);
1335
1364
  const response = await insertRecord({
1336
1365
  pathParams: {
1337
1366
  workspace: "{workspaceId}",
1338
1367
  dbBranchName: "{dbBranch}",
1339
- tableName: __privateGet$3(this, _table)
1368
+ tableName: __privateGet$4(this, _table)
1340
1369
  },
1341
1370
  body: record,
1342
1371
  ...fetchProps
@@ -1349,13 +1378,13 @@ insertRecordWithoutId_fn = async function(object) {
1349
1378
  };
1350
1379
  _insertRecordWithId = new WeakSet();
1351
1380
  insertRecordWithId_fn = async function(recordId, object) {
1352
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1381
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1353
1382
  const record = transformObjectLinks(object);
1354
1383
  const response = await insertRecordWithID({
1355
1384
  pathParams: {
1356
1385
  workspace: "{workspaceId}",
1357
1386
  dbBranchName: "{dbBranch}",
1358
- tableName: __privateGet$3(this, _table),
1387
+ tableName: __privateGet$4(this, _table),
1359
1388
  recordId
1360
1389
  },
1361
1390
  body: record,
@@ -1370,10 +1399,10 @@ insertRecordWithId_fn = async function(recordId, object) {
1370
1399
  };
1371
1400
  _bulkInsertTableRecords = new WeakSet();
1372
1401
  bulkInsertTableRecords_fn = async function(objects) {
1373
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1402
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1374
1403
  const records = objects.map((object) => transformObjectLinks(object));
1375
1404
  const response = await bulkInsertTableRecords({
1376
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1405
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1377
1406
  body: { records },
1378
1407
  ...fetchProps
1379
1408
  });
@@ -1385,10 +1414,10 @@ bulkInsertTableRecords_fn = async function(objects) {
1385
1414
  };
1386
1415
  _updateRecordWithID = new WeakSet();
1387
1416
  updateRecordWithID_fn = async function(recordId, object) {
1388
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1417
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1389
1418
  const record = transformObjectLinks(object);
1390
1419
  const response = await updateRecordWithID({
1391
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1420
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1392
1421
  body: record,
1393
1422
  ...fetchProps
1394
1423
  });
@@ -1399,9 +1428,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1399
1428
  };
1400
1429
  _upsertRecordWithID = new WeakSet();
1401
1430
  upsertRecordWithID_fn = async function(recordId, object) {
1402
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1431
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1403
1432
  const response = await upsertRecordWithID({
1404
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1433
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1405
1434
  body: object,
1406
1435
  ...fetchProps
1407
1436
  });
@@ -1412,51 +1441,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
1412
1441
  };
1413
1442
  _deleteRecord = new WeakSet();
1414
1443
  deleteRecord_fn = async function(recordId) {
1415
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1444
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1416
1445
  await deleteRecord({
1417
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1446
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1418
1447
  ...fetchProps
1419
1448
  });
1420
1449
  };
1421
1450
  _invalidateCache = new WeakSet();
1422
1451
  invalidateCache_fn = async function(recordId) {
1423
- await __privateGet$3(this, _cache).delete(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1424
- 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();
1425
1454
  const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1426
1455
  for (const [key, value] of queries) {
1427
1456
  const ids = getIds(value);
1428
1457
  if (ids.includes(recordId))
1429
- await __privateGet$3(this, _cache).delete(key);
1458
+ await __privateGet$4(this, _cache).delete(key);
1430
1459
  }
1431
1460
  };
1432
1461
  _setCacheRecord = new WeakSet();
1433
1462
  setCacheRecord_fn = async function(record) {
1434
- if (!__privateGet$3(this, _cache).cacheRecords)
1463
+ if (!__privateGet$4(this, _cache).cacheRecords)
1435
1464
  return;
1436
- 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);
1437
1466
  };
1438
1467
  _getCacheRecord = new WeakSet();
1439
1468
  getCacheRecord_fn = async function(recordId) {
1440
- if (!__privateGet$3(this, _cache).cacheRecords)
1469
+ if (!__privateGet$4(this, _cache).cacheRecords)
1441
1470
  return null;
1442
- 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}`);
1443
1472
  };
1444
1473
  _setCacheQuery = new WeakSet();
1445
1474
  setCacheQuery_fn = async function(query, meta, records) {
1446
- 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 });
1447
1476
  };
1448
1477
  _getCacheQuery = new WeakSet();
1449
1478
  getCacheQuery_fn = async function(query) {
1450
- const key = `query_${__privateGet$3(this, _table)}:${query.key()}`;
1451
- 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);
1452
1481
  if (!result)
1453
1482
  return null;
1454
- const { cache: ttl = __privateGet$3(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1455
- if (!ttl || ttl < 0)
1456
- return result;
1483
+ const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1484
+ if (ttl < 0)
1485
+ return null;
1457
1486
  const hasExpired = result.date.getTime() + ttl < Date.now();
1458
1487
  return hasExpired ? null : result;
1459
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
+ };
1460
1501
  const transformObjectLinks = (object) => {
1461
1502
  return Object.entries(object).reduce((acc, [key, value]) => {
1462
1503
  if (key === "xata")
@@ -1464,15 +1505,33 @@ const transformObjectLinks = (object) => {
1464
1505
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1465
1506
  }, {});
1466
1507
  };
1467
- const initObject = (db, links, table, object) => {
1508
+ const initObject = (db, schema, table, object) => {
1468
1509
  const result = {};
1469
1510
  Object.assign(result, object);
1470
- const tableLinks = links[table] || [];
1471
- for (const link of tableLinks) {
1472
- const [field, linkTable] = link;
1473
- const value = result[field];
1474
- if (value && isObject(value)) {
1475
- 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
+ }
1476
1535
  }
1477
1536
  }
1478
1537
  result.read = function() {
@@ -1504,7 +1563,7 @@ var __accessCheck$3 = (obj, member, msg) => {
1504
1563
  if (!member.has(obj))
1505
1564
  throw TypeError("Cannot " + msg);
1506
1565
  };
1507
- var __privateGet$2 = (obj, member, getter) => {
1566
+ var __privateGet$3 = (obj, member, getter) => {
1508
1567
  __accessCheck$3(obj, member, "read from private field");
1509
1568
  return getter ? getter.call(obj) : member.get(obj);
1510
1569
  };
@@ -1513,7 +1572,7 @@ var __privateAdd$3 = (obj, member, value) => {
1513
1572
  throw TypeError("Cannot add the same private member more than once");
1514
1573
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1515
1574
  };
1516
- var __privateSet$1 = (obj, member, value, setter) => {
1575
+ var __privateSet$2 = (obj, member, value, setter) => {
1517
1576
  __accessCheck$3(obj, member, "write to private field");
1518
1577
  setter ? setter.call(obj, value) : member.set(obj, value);
1519
1578
  return value;
@@ -1522,30 +1581,30 @@ var _map;
1522
1581
  class SimpleCache {
1523
1582
  constructor(options = {}) {
1524
1583
  __privateAdd$3(this, _map, void 0);
1525
- __privateSet$1(this, _map, /* @__PURE__ */ new Map());
1584
+ __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1526
1585
  this.capacity = options.max ?? 500;
1527
1586
  this.cacheRecords = options.cacheRecords ?? true;
1528
1587
  this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1529
1588
  }
1530
1589
  async getAll() {
1531
- return Object.fromEntries(__privateGet$2(this, _map));
1590
+ return Object.fromEntries(__privateGet$3(this, _map));
1532
1591
  }
1533
1592
  async get(key) {
1534
- return __privateGet$2(this, _map).get(key) ?? null;
1593
+ return __privateGet$3(this, _map).get(key) ?? null;
1535
1594
  }
1536
1595
  async set(key, value) {
1537
1596
  await this.delete(key);
1538
- __privateGet$2(this, _map).set(key, value);
1539
- if (__privateGet$2(this, _map).size > this.capacity) {
1540
- 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;
1541
1600
  await this.delete(leastRecentlyUsed);
1542
1601
  }
1543
1602
  }
1544
1603
  async delete(key) {
1545
- __privateGet$2(this, _map).delete(key);
1604
+ __privateGet$3(this, _map).delete(key);
1546
1605
  }
1547
1606
  async clear() {
1548
- return __privateGet$2(this, _map).clear();
1607
+ return __privateGet$3(this, _map).clear();
1549
1608
  }
1550
1609
  }
1551
1610
  _map = new WeakMap();
@@ -1573,7 +1632,7 @@ var __accessCheck$2 = (obj, member, msg) => {
1573
1632
  if (!member.has(obj))
1574
1633
  throw TypeError("Cannot " + msg);
1575
1634
  };
1576
- var __privateGet$1 = (obj, member, getter) => {
1635
+ var __privateGet$2 = (obj, member, getter) => {
1577
1636
  __accessCheck$2(obj, member, "read from private field");
1578
1637
  return getter ? getter.call(obj) : member.get(obj);
1579
1638
  };
@@ -1584,26 +1643,24 @@ var __privateAdd$2 = (obj, member, value) => {
1584
1643
  };
1585
1644
  var _tables;
1586
1645
  class SchemaPlugin extends XataPlugin {
1587
- constructor(links, tableNames) {
1646
+ constructor(tableNames) {
1588
1647
  super();
1589
- this.links = links;
1590
1648
  this.tableNames = tableNames;
1591
1649
  __privateAdd$2(this, _tables, {});
1592
1650
  }
1593
1651
  build(pluginOptions) {
1594
- const links = this.links;
1595
1652
  const db = new Proxy({}, {
1596
1653
  get: (_target, table) => {
1597
1654
  if (!isString(table))
1598
1655
  throw new Error("Invalid table name");
1599
- if (!__privateGet$1(this, _tables)[table]) {
1600
- __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 });
1601
1658
  }
1602
- return __privateGet$1(this, _tables)[table];
1659
+ return __privateGet$2(this, _tables)[table];
1603
1660
  }
1604
1661
  });
1605
1662
  for (const table of this.tableNames ?? []) {
1606
- db[table] = new RestRepository({ db, pluginOptions, table, links });
1663
+ db[table] = new RestRepository({ db, pluginOptions, table });
1607
1664
  }
1608
1665
  return db;
1609
1666
  }
@@ -1614,44 +1671,57 @@ var __accessCheck$1 = (obj, member, msg) => {
1614
1671
  if (!member.has(obj))
1615
1672
  throw TypeError("Cannot " + msg);
1616
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
+ };
1617
1678
  var __privateAdd$1 = (obj, member, value) => {
1618
1679
  if (member.has(obj))
1619
1680
  throw TypeError("Cannot add the same private member more than once");
1620
1681
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1621
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
+ };
1622
1688
  var __privateMethod$1 = (obj, member, method) => {
1623
1689
  __accessCheck$1(obj, member, "access private method");
1624
1690
  return method;
1625
1691
  };
1626
- var _search, search_fn;
1692
+ var _schema, _search, search_fn, _getSchema, getSchema_fn;
1627
1693
  class SearchPlugin extends XataPlugin {
1628
- constructor(db, links) {
1694
+ constructor(db) {
1629
1695
  super();
1630
1696
  this.db = db;
1631
- this.links = links;
1632
1697
  __privateAdd$1(this, _search);
1698
+ __privateAdd$1(this, _getSchema);
1699
+ __privateAdd$1(this, _schema, void 0);
1633
1700
  }
1634
1701
  build({ getFetchProps }) {
1635
1702
  return {
1636
1703
  all: async (query, options = {}) => {
1637
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);
1638
1706
  return records.map((record) => {
1639
1707
  const { table = "orphan" } = record.xata;
1640
- return { table, record: initObject(this.db, this.links, table, record) };
1708
+ return { table, record: initObject(this.db, schema, table, record) };
1641
1709
  });
1642
1710
  },
1643
1711
  byTable: async (query, options = {}) => {
1644
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);
1645
1714
  return records.reduce((acc, record) => {
1646
1715
  const { table = "orphan" } = record.xata;
1647
1716
  const items = acc[table] ?? [];
1648
- const item = initObject(this.db, this.links, table, record);
1717
+ const item = initObject(this.db, schema, table, record);
1649
1718
  return { ...acc, [table]: [...items, item] };
1650
1719
  }, {});
1651
1720
  }
1652
1721
  };
1653
1722
  }
1654
1723
  }
1724
+ _schema = new WeakMap();
1655
1725
  _search = new WeakSet();
1656
1726
  search_fn = async function(query, options, getFetchProps) {
1657
1727
  const fetchProps = await getFetchProps();
@@ -1663,6 +1733,18 @@ search_fn = async function(query, options, getFetchProps) {
1663
1733
  });
1664
1734
  return records;
1665
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
+ };
1666
1748
 
1667
1749
  const isBranchStrategyBuilder = (strategy) => {
1668
1750
  return typeof strategy === "function";
@@ -1674,30 +1756,39 @@ const envBranchNames = [
1674
1756
  "CF_PAGES_BRANCH",
1675
1757
  "BRANCH"
1676
1758
  ];
1677
- const defaultBranch = "main";
1678
1759
  async function getCurrentBranchName(options) {
1679
- const env = await getBranchByEnvVariable();
1680
- if (env)
1681
- return env;
1682
- const branch = await getGitBranch();
1683
- if (!branch)
1684
- return defaultBranch;
1685
- const details = await getDatabaseBranch(branch, options);
1686
- if (details)
1687
- return branch;
1688
- 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);
1689
1769
  }
1690
1770
  async function getCurrentBranchDetails(options) {
1691
- const env = await getBranchByEnvVariable();
1692
- if (env)
1693
- return getDatabaseBranch(env, options);
1694
- const branch = await getGitBranch();
1695
- if (!branch)
1696
- return getDatabaseBranch(defaultBranch, options);
1697
- const details = await getDatabaseBranch(branch, options);
1698
- if (details)
1699
- return details;
1700
- 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;
1701
1792
  }
1702
1793
  async function getDatabaseBranch(branch, options) {
1703
1794
  const databaseURL = options?.databaseURL || getDatabaseURL();
@@ -1771,7 +1862,7 @@ var __privateMethod = (obj, member, method) => {
1771
1862
  const buildClient = (plugins) => {
1772
1863
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1773
1864
  return _a = class {
1774
- constructor(options = {}, links, tables) {
1865
+ constructor(options = {}, tables) {
1775
1866
  __privateAdd(this, _parseOptions);
1776
1867
  __privateAdd(this, _getFetchProps);
1777
1868
  __privateAdd(this, _evaluateBranch);
@@ -1781,12 +1872,12 @@ const buildClient = (plugins) => {
1781
1872
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1782
1873
  cache: safeOptions.cache
1783
1874
  };
1784
- const db = new SchemaPlugin(links, tables).build(pluginOptions);
1785
- const search = new SearchPlugin(db, links ?? {}).build(pluginOptions);
1875
+ const db = new SchemaPlugin(tables).build(pluginOptions);
1876
+ const search = new SearchPlugin(db).build(pluginOptions);
1786
1877
  this.db = db;
1787
1878
  this.search = search;
1788
1879
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1789
- if (!namespace)
1880
+ if (namespace === void 0)
1790
1881
  continue;
1791
1882
  const result = namespace.build(pluginOptions);
1792
1883
  if (result instanceof Promise) {
@@ -1803,7 +1894,7 @@ const buildClient = (plugins) => {
1803
1894
  const databaseURL = options?.databaseURL || getDatabaseURL();
1804
1895
  const apiKey = options?.apiKey || getAPIKey();
1805
1896
  const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1806
- 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 });
1807
1898
  if (!databaseURL || !apiKey) {
1808
1899
  throw new Error("Options databaseURL and apiKey are required");
1809
1900
  }
@@ -1830,7 +1921,7 @@ const buildClient = (plugins) => {
1830
1921
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
1831
1922
  if (__privateGet(this, _branch))
1832
1923
  return __privateGet(this, _branch);
1833
- if (!param)
1924
+ if (param === void 0)
1834
1925
  return void 0;
1835
1926
  const strategies = Array.isArray(param) ? [...param] : [param];
1836
1927
  const evaluateBranch = async (strategy) => {
@@ -1927,6 +2018,7 @@ exports.insertRecord = insertRecord;
1927
2018
  exports.insertRecordWithID = insertRecordWithID;
1928
2019
  exports.inviteWorkspaceMember = inviteWorkspaceMember;
1929
2020
  exports.is = is;
2021
+ exports.isCursorPaginationOptions = isCursorPaginationOptions;
1930
2022
  exports.isIdentifiable = isIdentifiable;
1931
2023
  exports.isNot = isNot;
1932
2024
  exports.isXataRecord = isXataRecord;
@@ -1942,6 +2034,7 @@ exports.removeWorkspaceMember = removeWorkspaceMember;
1942
2034
  exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
1943
2035
  exports.resolveBranch = resolveBranch;
1944
2036
  exports.searchBranch = searchBranch;
2037
+ exports.searchTable = searchTable;
1945
2038
  exports.setTableSchema = setTableSchema;
1946
2039
  exports.startsWith = startsWith;
1947
2040
  exports.updateBranchMetadata = updateBranchMetadata;