@xata.io/client 0.9.1 → 0.10.2

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