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