@xata.io/client 0.0.0-alpha.ved00a04 → 0.0.0-alpha.vedd7251

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 {
@@ -34,7 +37,10 @@ function getEnvVariable(name) {
34
37
  }
35
38
  async function getGitBranch() {
36
39
  try {
37
- return require("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
40
+ if (typeof require === "function") {
41
+ const req = require;
42
+ return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
43
+ }
38
44
  } catch (err) {
39
45
  }
40
46
  try {
@@ -67,7 +73,12 @@ function getFetchImplementation(userFetch) {
67
73
  return fetchImpl;
68
74
  }
69
75
 
70
- class FetcherError extends Error {
76
+ class ErrorWithCause extends Error {
77
+ constructor(message, options) {
78
+ super(message, options);
79
+ }
80
+ }
81
+ class FetcherError extends ErrorWithCause {
71
82
  constructor(status, data) {
72
83
  super(getMessage(data));
73
84
  this.status = status;
@@ -248,6 +259,14 @@ const deleteDatabase = (variables) => fetch$1({
248
259
  method: "delete",
249
260
  ...variables
250
261
  });
262
+ const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
263
+ const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
264
+ const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
265
+ const resolveBranch = (variables) => fetch$1({
266
+ url: "/dbs/{dbName}/resolveBranch",
267
+ method: "get",
268
+ ...variables
269
+ });
251
270
  const getBranchDetails = (variables) => fetch$1({
252
271
  url: "/db/{dbBranchName}",
253
272
  method: "get",
@@ -276,17 +295,6 @@ const getBranchMetadata = (variables) => fetch$1({
276
295
  const getBranchMigrationHistory = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations", method: "get", ...variables });
277
296
  const executeBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/execute", method: "post", ...variables });
278
297
  const getBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/plan", method: "post", ...variables });
279
- const compareBranchSchema = (variables) => fetch$1({
280
- url: "/db/{dbBranchName}/schema/compare/{branchName}",
281
- method: "post",
282
- ...variables
283
- });
284
- const updateBranchSchema = (variables) => fetch$1({
285
- url: "/db/{dbBranchName}/schema/update",
286
- method: "post",
287
- ...variables
288
- });
289
- const getBranchSchemaHistory = (variables) => fetch$1({ url: "/db/{dbBranchName}/schema/history", method: "post", ...variables });
290
298
  const getBranchStats = (variables) => fetch$1({
291
299
  url: "/db/{dbBranchName}/stats",
292
300
  method: "get",
@@ -366,6 +374,11 @@ const queryTable = (variables) => fetch$1({
366
374
  method: "post",
367
375
  ...variables
368
376
  });
377
+ const searchTable = (variables) => fetch$1({
378
+ url: "/db/{dbBranchName}/tables/{tableName}/search",
379
+ method: "post",
380
+ ...variables
381
+ });
369
382
  const searchBranch = (variables) => fetch$1({
370
383
  url: "/db/{dbBranchName}/search",
371
384
  method: "post",
@@ -387,7 +400,15 @@ const operationsByTag = {
387
400
  resendWorkspaceMemberInvite,
388
401
  acceptWorkspaceMemberInvite
389
402
  },
390
- database: { getDatabaseList, createDatabase, deleteDatabase },
403
+ database: {
404
+ getDatabaseList,
405
+ createDatabase,
406
+ deleteDatabase,
407
+ getGitBranchesMapping,
408
+ addGitBranchesEntry,
409
+ removeGitBranchesEntry,
410
+ resolveBranch
411
+ },
391
412
  branch: {
392
413
  getBranchList,
393
414
  getBranchDetails,
@@ -398,9 +419,6 @@ const operationsByTag = {
398
419
  getBranchMigrationHistory,
399
420
  executeBranchMigrationPlan,
400
421
  getBranchMigrationPlan,
401
- compareBranchSchema,
402
- updateBranchSchema,
403
- getBranchSchemaHistory,
404
422
  getBranchStats
405
423
  },
406
424
  table: {
@@ -424,6 +442,7 @@ const operationsByTag = {
424
442
  getRecord,
425
443
  bulkInsertTableRecords,
426
444
  queryTable,
445
+ searchTable,
427
446
  searchBranch
428
447
  }
429
448
  };
@@ -457,7 +476,7 @@ var __accessCheck$7 = (obj, member, msg) => {
457
476
  if (!member.has(obj))
458
477
  throw TypeError("Cannot " + msg);
459
478
  };
460
- var __privateGet$6 = (obj, member, getter) => {
479
+ var __privateGet$7 = (obj, member, getter) => {
461
480
  __accessCheck$7(obj, member, "read from private field");
462
481
  return getter ? getter.call(obj) : member.get(obj);
463
482
  };
@@ -466,7 +485,7 @@ var __privateAdd$7 = (obj, member, value) => {
466
485
  throw TypeError("Cannot add the same private member more than once");
467
486
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
468
487
  };
469
- var __privateSet$5 = (obj, member, value, setter) => {
488
+ var __privateSet$6 = (obj, member, value, setter) => {
470
489
  __accessCheck$7(obj, member, "write to private field");
471
490
  setter ? setter.call(obj, value) : member.set(obj, value);
472
491
  return value;
@@ -481,7 +500,7 @@ class XataApiClient {
481
500
  if (!apiKey) {
482
501
  throw new Error("Could not resolve a valid apiKey");
483
502
  }
484
- __privateSet$5(this, _extraProps, {
503
+ __privateSet$6(this, _extraProps, {
485
504
  apiUrl: getHostUrl(provider, "main"),
486
505
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
487
506
  fetchImpl: getFetchImplementation(options.fetch),
@@ -489,34 +508,34 @@ class XataApiClient {
489
508
  });
490
509
  }
491
510
  get user() {
492
- if (!__privateGet$6(this, _namespaces).user)
493
- __privateGet$6(this, _namespaces).user = new UserApi(__privateGet$6(this, _extraProps));
494
- return __privateGet$6(this, _namespaces).user;
511
+ if (!__privateGet$7(this, _namespaces).user)
512
+ __privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
513
+ return __privateGet$7(this, _namespaces).user;
495
514
  }
496
515
  get workspaces() {
497
- if (!__privateGet$6(this, _namespaces).workspaces)
498
- __privateGet$6(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$6(this, _extraProps));
499
- return __privateGet$6(this, _namespaces).workspaces;
516
+ if (!__privateGet$7(this, _namespaces).workspaces)
517
+ __privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
518
+ return __privateGet$7(this, _namespaces).workspaces;
500
519
  }
501
520
  get databases() {
502
- if (!__privateGet$6(this, _namespaces).databases)
503
- __privateGet$6(this, _namespaces).databases = new DatabaseApi(__privateGet$6(this, _extraProps));
504
- return __privateGet$6(this, _namespaces).databases;
521
+ if (!__privateGet$7(this, _namespaces).databases)
522
+ __privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
523
+ return __privateGet$7(this, _namespaces).databases;
505
524
  }
506
525
  get branches() {
507
- if (!__privateGet$6(this, _namespaces).branches)
508
- __privateGet$6(this, _namespaces).branches = new BranchApi(__privateGet$6(this, _extraProps));
509
- return __privateGet$6(this, _namespaces).branches;
526
+ if (!__privateGet$7(this, _namespaces).branches)
527
+ __privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
528
+ return __privateGet$7(this, _namespaces).branches;
510
529
  }
511
530
  get tables() {
512
- if (!__privateGet$6(this, _namespaces).tables)
513
- __privateGet$6(this, _namespaces).tables = new TableApi(__privateGet$6(this, _extraProps));
514
- return __privateGet$6(this, _namespaces).tables;
531
+ if (!__privateGet$7(this, _namespaces).tables)
532
+ __privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
533
+ return __privateGet$7(this, _namespaces).tables;
515
534
  }
516
535
  get records() {
517
- if (!__privateGet$6(this, _namespaces).records)
518
- __privateGet$6(this, _namespaces).records = new RecordsApi(__privateGet$6(this, _extraProps));
519
- return __privateGet$6(this, _namespaces).records;
536
+ if (!__privateGet$7(this, _namespaces).records)
537
+ __privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
538
+ return __privateGet$7(this, _namespaces).records;
520
539
  }
521
540
  }
522
541
  _extraProps = new WeakMap();
@@ -650,6 +669,33 @@ class DatabaseApi {
650
669
  ...this.extraProps
651
670
  });
652
671
  }
672
+ getGitBranchesMapping(workspace, dbName) {
673
+ return operationsByTag.database.getGitBranchesMapping({
674
+ pathParams: { workspace, dbName },
675
+ ...this.extraProps
676
+ });
677
+ }
678
+ addGitBranchesEntry(workspace, dbName, body) {
679
+ return operationsByTag.database.addGitBranchesEntry({
680
+ pathParams: { workspace, dbName },
681
+ body,
682
+ ...this.extraProps
683
+ });
684
+ }
685
+ removeGitBranchesEntry(workspace, dbName, gitBranch) {
686
+ return operationsByTag.database.removeGitBranchesEntry({
687
+ pathParams: { workspace, dbName },
688
+ queryParams: { gitBranch },
689
+ ...this.extraProps
690
+ });
691
+ }
692
+ resolveBranch(workspace, dbName, gitBranch) {
693
+ return operationsByTag.database.resolveBranch({
694
+ pathParams: { workspace, dbName },
695
+ queryParams: { gitBranch },
696
+ ...this.extraProps
697
+ });
698
+ }
653
699
  }
654
700
  class BranchApi {
655
701
  constructor(extraProps) {
@@ -667,10 +713,10 @@ class BranchApi {
667
713
  ...this.extraProps
668
714
  });
669
715
  }
670
- createBranch(workspace, database, branch, from = "", options = {}) {
716
+ createBranch(workspace, database, branch, from, options = {}) {
671
717
  return operationsByTag.branch.createBranch({
672
718
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
673
- queryParams: { from },
719
+ queryParams: isString(from) ? { from } : void 0,
674
720
  body: options,
675
721
  ...this.extraProps
676
722
  });
@@ -852,6 +898,13 @@ class RecordsApi {
852
898
  ...this.extraProps
853
899
  });
854
900
  }
901
+ searchTable(workspace, database, branch, tableName, query) {
902
+ return operationsByTag.records.searchTable({
903
+ pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
904
+ body: query,
905
+ ...this.extraProps
906
+ });
907
+ }
855
908
  searchBranch(workspace, database, branch, query) {
856
909
  return operationsByTag.records.searchBranch({
857
910
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
@@ -875,7 +928,7 @@ var __accessCheck$6 = (obj, member, msg) => {
875
928
  if (!member.has(obj))
876
929
  throw TypeError("Cannot " + msg);
877
930
  };
878
- var __privateGet$5 = (obj, member, getter) => {
931
+ var __privateGet$6 = (obj, member, getter) => {
879
932
  __accessCheck$6(obj, member, "read from private field");
880
933
  return getter ? getter.call(obj) : member.get(obj);
881
934
  };
@@ -884,7 +937,7 @@ var __privateAdd$6 = (obj, member, value) => {
884
937
  throw TypeError("Cannot add the same private member more than once");
885
938
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
886
939
  };
887
- var __privateSet$4 = (obj, member, value, setter) => {
940
+ var __privateSet$5 = (obj, member, value, setter) => {
888
941
  __accessCheck$6(obj, member, "write to private field");
889
942
  setter ? setter.call(obj, value) : member.set(obj, value);
890
943
  return value;
@@ -893,21 +946,21 @@ var _query;
893
946
  class Page {
894
947
  constructor(query, meta, records = []) {
895
948
  __privateAdd$6(this, _query, void 0);
896
- __privateSet$4(this, _query, query);
949
+ __privateSet$5(this, _query, query);
897
950
  this.meta = meta;
898
951
  this.records = records;
899
952
  }
900
953
  async nextPage(size, offset) {
901
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
954
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
902
955
  }
903
956
  async previousPage(size, offset) {
904
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
957
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
905
958
  }
906
959
  async firstPage(size, offset) {
907
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
960
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
908
961
  }
909
962
  async lastPage(size, offset) {
910
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
963
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
911
964
  }
912
965
  hasNextPage() {
913
966
  return this.meta.page.more;
@@ -918,12 +971,15 @@ const PAGINATION_MAX_SIZE = 200;
918
971
  const PAGINATION_DEFAULT_SIZE = 200;
919
972
  const PAGINATION_MAX_OFFSET = 800;
920
973
  const PAGINATION_DEFAULT_OFFSET = 0;
974
+ function isCursorPaginationOptions(options) {
975
+ return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
976
+ }
921
977
 
922
978
  var __accessCheck$5 = (obj, member, msg) => {
923
979
  if (!member.has(obj))
924
980
  throw TypeError("Cannot " + msg);
925
981
  };
926
- var __privateGet$4 = (obj, member, getter) => {
982
+ var __privateGet$5 = (obj, member, getter) => {
927
983
  __accessCheck$5(obj, member, "read from private field");
928
984
  return getter ? getter.call(obj) : member.get(obj);
929
985
  };
@@ -932,34 +988,35 @@ var __privateAdd$5 = (obj, member, value) => {
932
988
  throw TypeError("Cannot add the same private member more than once");
933
989
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
934
990
  };
935
- var __privateSet$3 = (obj, member, value, setter) => {
991
+ var __privateSet$4 = (obj, member, value, setter) => {
936
992
  __accessCheck$5(obj, member, "write to private field");
937
993
  setter ? setter.call(obj, value) : member.set(obj, value);
938
994
  return value;
939
995
  };
940
996
  var _table$1, _repository, _data;
941
997
  const _Query = class {
942
- constructor(repository, table, data, parent) {
998
+ constructor(repository, table, data, rawParent) {
943
999
  __privateAdd$5(this, _table$1, void 0);
944
1000
  __privateAdd$5(this, _repository, void 0);
945
1001
  __privateAdd$5(this, _data, { filter: {} });
946
1002
  this.meta = { page: { cursor: "start", more: true } };
947
1003
  this.records = [];
948
- __privateSet$3(this, _table$1, table);
1004
+ __privateSet$4(this, _table$1, table);
949
1005
  if (repository) {
950
- __privateSet$3(this, _repository, repository);
1006
+ __privateSet$4(this, _repository, repository);
951
1007
  } else {
952
- __privateSet$3(this, _repository, this);
1008
+ __privateSet$4(this, _repository, this);
953
1009
  }
954
- __privateGet$4(this, _data).filter = data.filter ?? parent?.filter ?? {};
955
- __privateGet$4(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
956
- __privateGet$4(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
957
- __privateGet$4(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
958
- __privateGet$4(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
959
- __privateGet$4(this, _data).sort = data.sort ?? parent?.sort;
960
- __privateGet$4(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
961
- __privateGet$4(this, _data).page = data.page ?? parent?.page;
962
- __privateGet$4(this, _data).cache = data.cache ?? parent?.cache;
1010
+ const parent = cleanParent(data, rawParent);
1011
+ __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
1012
+ __privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
1013
+ __privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
1014
+ __privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
1015
+ __privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
1016
+ __privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
1017
+ __privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
1018
+ __privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
1019
+ __privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
963
1020
  this.any = this.any.bind(this);
964
1021
  this.all = this.all.bind(this);
965
1022
  this.not = this.not.bind(this);
@@ -970,83 +1027,85 @@ const _Query = class {
970
1027
  Object.defineProperty(this, "repository", { enumerable: false });
971
1028
  }
972
1029
  getQueryOptions() {
973
- return __privateGet$4(this, _data);
1030
+ return __privateGet$5(this, _data);
974
1031
  }
975
1032
  key() {
976
- const { columns = [], filter = {}, sort = [], page = {} } = __privateGet$4(this, _data);
977
- const key = JSON.stringify({ columns, filter, sort, page });
1033
+ const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
1034
+ const key = JSON.stringify({ columns, filter, sort, pagination });
978
1035
  return toBase64(key);
979
1036
  }
980
1037
  any(...queries) {
981
1038
  const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
982
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $any } }, __privateGet$4(this, _data));
1039
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
983
1040
  }
984
1041
  all(...queries) {
985
1042
  const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
986
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1043
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
987
1044
  }
988
1045
  not(...queries) {
989
1046
  const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
990
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $not } }, __privateGet$4(this, _data));
1047
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
991
1048
  }
992
1049
  none(...queries) {
993
1050
  const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
994
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $none } }, __privateGet$4(this, _data));
1051
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
995
1052
  }
996
1053
  filter(a, b) {
997
1054
  if (arguments.length === 1) {
998
1055
  const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
999
- const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat(constraints));
1000
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1056
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
1057
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1001
1058
  } else {
1002
- const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1003
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1059
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1060
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1004
1061
  }
1005
1062
  }
1006
1063
  sort(column, direction) {
1007
- const originalSort = [__privateGet$4(this, _data).sort ?? []].flat();
1064
+ const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
1008
1065
  const sort = [...originalSort, { column, direction }];
1009
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { sort }, __privateGet$4(this, _data));
1066
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
1010
1067
  }
1011
1068
  select(columns) {
1012
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { columns }, __privateGet$4(this, _data));
1069
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
1013
1070
  }
1014
1071
  getPaginated(options = {}) {
1015
- const query = new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), options, __privateGet$4(this, _data));
1016
- return __privateGet$4(this, _repository).query(query);
1072
+ const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
1073
+ return __privateGet$5(this, _repository).query(query);
1017
1074
  }
1018
1075
  async *[Symbol.asyncIterator]() {
1019
- for await (const [record] of this.getIterator(1)) {
1076
+ for await (const [record] of this.getIterator({ batchSize: 1 })) {
1020
1077
  yield record;
1021
1078
  }
1022
1079
  }
1023
- async *getIterator(chunk, options = {}) {
1024
- let offset = 0;
1025
- let end = false;
1026
- while (!end) {
1027
- const { records, meta } = await this.getPaginated({ ...options, page: { size: chunk, offset } });
1028
- yield records;
1029
- offset += chunk;
1030
- end = !meta.page.more;
1080
+ async *getIterator(options = {}) {
1081
+ const { batchSize = 1 } = options;
1082
+ let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
1083
+ let more = page.hasNextPage();
1084
+ yield page.records;
1085
+ while (more) {
1086
+ page = await page.nextPage();
1087
+ more = page.hasNextPage();
1088
+ yield page.records;
1031
1089
  }
1032
1090
  }
1033
1091
  async getMany(options = {}) {
1034
1092
  const { records } = await this.getPaginated(options);
1035
1093
  return records;
1036
1094
  }
1037
- async getAll(chunk = PAGINATION_MAX_SIZE, options = {}) {
1095
+ async getAll(options = {}) {
1096
+ const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
1038
1097
  const results = [];
1039
- for await (const page of this.getIterator(chunk, options)) {
1098
+ for await (const page of this.getIterator({ ...rest, batchSize })) {
1040
1099
  results.push(...page);
1041
1100
  }
1042
1101
  return results;
1043
1102
  }
1044
1103
  async getFirst(options = {}) {
1045
- const records = await this.getMany({ ...options, page: { size: 1 } });
1046
- return records[0] || null;
1104
+ const records = await this.getMany({ ...options, pagination: { size: 1 } });
1105
+ return records[0] ?? null;
1047
1106
  }
1048
1107
  cache(ttl) {
1049
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { cache: ttl }, __privateGet$4(this, _data));
1108
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
1050
1109
  }
1051
1110
  nextPage(size, offset) {
1052
1111
  return this.firstPage(size, offset);
@@ -1055,10 +1114,10 @@ const _Query = class {
1055
1114
  return this.firstPage(size, offset);
1056
1115
  }
1057
1116
  firstPage(size, offset) {
1058
- return this.getPaginated({ page: { size, offset } });
1117
+ return this.getPaginated({ pagination: { size, offset } });
1059
1118
  }
1060
1119
  lastPage(size, offset) {
1061
- return this.getPaginated({ page: { size, offset, before: "end" } });
1120
+ return this.getPaginated({ pagination: { size, offset, before: "end" } });
1062
1121
  }
1063
1122
  hasNextPage() {
1064
1123
  return this.meta.page.more;
@@ -1068,12 +1127,20 @@ let Query = _Query;
1068
1127
  _table$1 = new WeakMap();
1069
1128
  _repository = new WeakMap();
1070
1129
  _data = new WeakMap();
1130
+ function cleanParent(data, parent) {
1131
+ if (isCursorPaginationOptions(data.pagination)) {
1132
+ return { ...parent, sorting: void 0, filter: void 0 };
1133
+ }
1134
+ return parent;
1135
+ }
1071
1136
 
1072
1137
  function isIdentifiable(x) {
1073
1138
  return isObject(x) && isString(x?.id);
1074
1139
  }
1075
1140
  function isXataRecord(x) {
1076
- return isIdentifiable(x) && typeof x?.xata === "object" && typeof x?.xata?.version === "number";
1141
+ const record = x;
1142
+ const metadata = record?.getMetadata();
1143
+ return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
1077
1144
  }
1078
1145
 
1079
1146
  function isSortFilterString(value) {
@@ -1103,7 +1170,7 @@ var __accessCheck$4 = (obj, member, msg) => {
1103
1170
  if (!member.has(obj))
1104
1171
  throw TypeError("Cannot " + msg);
1105
1172
  };
1106
- var __privateGet$3 = (obj, member, getter) => {
1173
+ var __privateGet$4 = (obj, member, getter) => {
1107
1174
  __accessCheck$4(obj, member, "read from private field");
1108
1175
  return getter ? getter.call(obj) : member.get(obj);
1109
1176
  };
@@ -1112,7 +1179,7 @@ var __privateAdd$4 = (obj, member, value) => {
1112
1179
  throw TypeError("Cannot add the same private member more than once");
1113
1180
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1114
1181
  };
1115
- var __privateSet$2 = (obj, member, value, setter) => {
1182
+ var __privateSet$3 = (obj, member, value, setter) => {
1116
1183
  __accessCheck$4(obj, member, "write to private field");
1117
1184
  setter ? setter.call(obj, value) : member.set(obj, value);
1118
1185
  return value;
@@ -1121,7 +1188,7 @@ var __privateMethod$2 = (obj, member, method) => {
1121
1188
  __accessCheck$4(obj, member, "access private method");
1122
1189
  return method;
1123
1190
  };
1124
- 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;
1191
+ 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;
1125
1192
  class Repository extends Query {
1126
1193
  }
1127
1194
  class RestRepository extends Query {
@@ -1138,18 +1205,20 @@ class RestRepository extends Query {
1138
1205
  __privateAdd$4(this, _getCacheRecord);
1139
1206
  __privateAdd$4(this, _setCacheQuery);
1140
1207
  __privateAdd$4(this, _getCacheQuery);
1208
+ __privateAdd$4(this, _getSchema$1);
1141
1209
  __privateAdd$4(this, _table, void 0);
1142
- __privateAdd$4(this, _links, void 0);
1143
1210
  __privateAdd$4(this, _getFetchProps, void 0);
1144
1211
  __privateAdd$4(this, _cache, void 0);
1145
- __privateSet$2(this, _table, options.table);
1146
- __privateSet$2(this, _links, options.links ?? {});
1147
- __privateSet$2(this, _getFetchProps, options.pluginOptions.getFetchProps);
1212
+ __privateAdd$4(this, _schema$1, void 0);
1213
+ __privateSet$3(this, _table, options.table);
1214
+ __privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
1148
1215
  this.db = options.db;
1149
- __privateSet$2(this, _cache, options.pluginOptions.cache);
1216
+ __privateSet$3(this, _cache, options.pluginOptions.cache);
1150
1217
  }
1151
1218
  async create(a, b) {
1152
1219
  if (Array.isArray(a)) {
1220
+ if (a.length === 0)
1221
+ return [];
1153
1222
  const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1154
1223
  await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1155
1224
  return records;
@@ -1175,26 +1244,36 @@ class RestRepository extends Query {
1175
1244
  }
1176
1245
  throw new Error("Invalid arguments for create method");
1177
1246
  }
1178
- async read(recordId) {
1179
- const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1180
- if (cacheRecord)
1181
- return cacheRecord;
1182
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1183
- try {
1184
- const response = await getRecord({
1185
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1186
- ...fetchProps
1187
- });
1188
- return initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), response);
1189
- } catch (e) {
1190
- if (isObject(e) && e.status === 404) {
1191
- return null;
1247
+ async read(a) {
1248
+ if (Array.isArray(a)) {
1249
+ if (a.length === 0)
1250
+ return [];
1251
+ return this.getAll({ filter: { id: { $any: a } } });
1252
+ }
1253
+ if (isString(a)) {
1254
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, a);
1255
+ if (cacheRecord)
1256
+ return cacheRecord;
1257
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1258
+ try {
1259
+ const response = await getRecord({
1260
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: a },
1261
+ ...fetchProps
1262
+ });
1263
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1264
+ return initObject(this.db, schema, __privateGet$4(this, _table), response);
1265
+ } catch (e) {
1266
+ if (isObject(e) && e.status === 404) {
1267
+ return null;
1268
+ }
1269
+ throw e;
1192
1270
  }
1193
- throw e;
1194
1271
  }
1195
1272
  }
1196
1273
  async update(a, b) {
1197
1274
  if (Array.isArray(a)) {
1275
+ if (a.length === 0)
1276
+ return [];
1198
1277
  if (a.length > 100) {
1199
1278
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1200
1279
  }
@@ -1216,6 +1295,8 @@ class RestRepository extends Query {
1216
1295
  }
1217
1296
  async createOrUpdate(a, b) {
1218
1297
  if (Array.isArray(a)) {
1298
+ if (a.length === 0)
1299
+ return [];
1219
1300
  if (a.length > 100) {
1220
1301
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1221
1302
  }
@@ -1237,6 +1318,8 @@ class RestRepository extends Query {
1237
1318
  }
1238
1319
  async delete(a) {
1239
1320
  if (Array.isArray(a)) {
1321
+ if (a.length === 0)
1322
+ return;
1240
1323
  if (a.length > 100) {
1241
1324
  console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1242
1325
  }
@@ -1256,13 +1339,19 @@ class RestRepository extends Query {
1256
1339
  throw new Error("Invalid arguments for delete method");
1257
1340
  }
1258
1341
  async search(query, options = {}) {
1259
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1260
- const { records } = await searchBranch({
1261
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1262
- body: { tables: [__privateGet$3(this, _table)], query, fuzziness: options.fuzziness },
1342
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1343
+ const { records } = await searchTable({
1344
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1345
+ body: {
1346
+ query,
1347
+ fuzziness: options.fuzziness,
1348
+ highlight: options.highlight,
1349
+ filter: options.filter
1350
+ },
1263
1351
  ...fetchProps
1264
1352
  });
1265
- return records.map((item) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), item));
1353
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1354
+ return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
1266
1355
  }
1267
1356
  async query(query) {
1268
1357
  const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
@@ -1271,34 +1360,35 @@ class RestRepository extends Query {
1271
1360
  const data = query.getQueryOptions();
1272
1361
  const body = {
1273
1362
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1274
- sort: data.sort ? buildSortFilter(data.sort) : void 0,
1275
- page: data.page,
1363
+ sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1364
+ page: data.pagination,
1276
1365
  columns: data.columns
1277
1366
  };
1278
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1367
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1279
1368
  const { meta, records: objects } = await queryTable({
1280
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1369
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1281
1370
  body,
1282
1371
  ...fetchProps
1283
1372
  });
1284
- const records = objects.map((record) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), record));
1373
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1374
+ const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
1285
1375
  await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1286
1376
  return new Page(query, meta, records);
1287
1377
  }
1288
1378
  }
1289
1379
  _table = new WeakMap();
1290
- _links = new WeakMap();
1291
1380
  _getFetchProps = new WeakMap();
1292
1381
  _cache = new WeakMap();
1382
+ _schema$1 = new WeakMap();
1293
1383
  _insertRecordWithoutId = new WeakSet();
1294
1384
  insertRecordWithoutId_fn = async function(object) {
1295
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1385
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1296
1386
  const record = transformObjectLinks(object);
1297
1387
  const response = await insertRecord({
1298
1388
  pathParams: {
1299
1389
  workspace: "{workspaceId}",
1300
1390
  dbBranchName: "{dbBranch}",
1301
- tableName: __privateGet$3(this, _table)
1391
+ tableName: __privateGet$4(this, _table)
1302
1392
  },
1303
1393
  body: record,
1304
1394
  ...fetchProps
@@ -1311,13 +1401,13 @@ insertRecordWithoutId_fn = async function(object) {
1311
1401
  };
1312
1402
  _insertRecordWithId = new WeakSet();
1313
1403
  insertRecordWithId_fn = async function(recordId, object) {
1314
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1404
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1315
1405
  const record = transformObjectLinks(object);
1316
1406
  const response = await insertRecordWithID({
1317
1407
  pathParams: {
1318
1408
  workspace: "{workspaceId}",
1319
1409
  dbBranchName: "{dbBranch}",
1320
- tableName: __privateGet$3(this, _table),
1410
+ tableName: __privateGet$4(this, _table),
1321
1411
  recordId
1322
1412
  },
1323
1413
  body: record,
@@ -1332,10 +1422,10 @@ insertRecordWithId_fn = async function(recordId, object) {
1332
1422
  };
1333
1423
  _bulkInsertTableRecords = new WeakSet();
1334
1424
  bulkInsertTableRecords_fn = async function(objects) {
1335
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1425
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1336
1426
  const records = objects.map((object) => transformObjectLinks(object));
1337
1427
  const response = await bulkInsertTableRecords({
1338
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1428
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1339
1429
  body: { records },
1340
1430
  ...fetchProps
1341
1431
  });
@@ -1347,10 +1437,10 @@ bulkInsertTableRecords_fn = async function(objects) {
1347
1437
  };
1348
1438
  _updateRecordWithID = new WeakSet();
1349
1439
  updateRecordWithID_fn = async function(recordId, object) {
1350
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1440
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1351
1441
  const record = transformObjectLinks(object);
1352
1442
  const response = await updateRecordWithID({
1353
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1443
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1354
1444
  body: record,
1355
1445
  ...fetchProps
1356
1446
  });
@@ -1361,9 +1451,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1361
1451
  };
1362
1452
  _upsertRecordWithID = new WeakSet();
1363
1453
  upsertRecordWithID_fn = async function(recordId, object) {
1364
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1454
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1365
1455
  const response = await upsertRecordWithID({
1366
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1456
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1367
1457
  body: object,
1368
1458
  ...fetchProps
1369
1459
  });
@@ -1374,51 +1464,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
1374
1464
  };
1375
1465
  _deleteRecord = new WeakSet();
1376
1466
  deleteRecord_fn = async function(recordId) {
1377
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1467
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1378
1468
  await deleteRecord({
1379
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1469
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1380
1470
  ...fetchProps
1381
1471
  });
1382
1472
  };
1383
1473
  _invalidateCache = new WeakSet();
1384
1474
  invalidateCache_fn = async function(recordId) {
1385
- await __privateGet$3(this, _cache).delete(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1386
- const cacheItems = await __privateGet$3(this, _cache).getAll();
1475
+ await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1476
+ const cacheItems = await __privateGet$4(this, _cache).getAll();
1387
1477
  const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1388
1478
  for (const [key, value] of queries) {
1389
1479
  const ids = getIds(value);
1390
1480
  if (ids.includes(recordId))
1391
- await __privateGet$3(this, _cache).delete(key);
1481
+ await __privateGet$4(this, _cache).delete(key);
1392
1482
  }
1393
1483
  };
1394
1484
  _setCacheRecord = new WeakSet();
1395
1485
  setCacheRecord_fn = async function(record) {
1396
- if (!__privateGet$3(this, _cache).cacheRecords)
1486
+ if (!__privateGet$4(this, _cache).cacheRecords)
1397
1487
  return;
1398
- await __privateGet$3(this, _cache).set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
1488
+ await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
1399
1489
  };
1400
1490
  _getCacheRecord = new WeakSet();
1401
1491
  getCacheRecord_fn = async function(recordId) {
1402
- if (!__privateGet$3(this, _cache).cacheRecords)
1492
+ if (!__privateGet$4(this, _cache).cacheRecords)
1403
1493
  return null;
1404
- return __privateGet$3(this, _cache).get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1494
+ return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1405
1495
  };
1406
1496
  _setCacheQuery = new WeakSet();
1407
1497
  setCacheQuery_fn = async function(query, meta, records) {
1408
- await __privateGet$3(this, _cache).set(`query_${__privateGet$3(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1498
+ await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1409
1499
  };
1410
1500
  _getCacheQuery = new WeakSet();
1411
1501
  getCacheQuery_fn = async function(query) {
1412
- const key = `query_${__privateGet$3(this, _table)}:${query.key()}`;
1413
- const result = await __privateGet$3(this, _cache).get(key);
1502
+ const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
1503
+ const result = await __privateGet$4(this, _cache).get(key);
1414
1504
  if (!result)
1415
1505
  return null;
1416
- const { cache: ttl = __privateGet$3(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1417
- if (!ttl || ttl < 0)
1418
- return result;
1506
+ const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1507
+ if (ttl < 0)
1508
+ return null;
1419
1509
  const hasExpired = result.date.getTime() + ttl < Date.now();
1420
1510
  return hasExpired ? null : result;
1421
1511
  };
1512
+ _getSchema$1 = new WeakSet();
1513
+ getSchema_fn$1 = async function() {
1514
+ if (__privateGet$4(this, _schema$1))
1515
+ return __privateGet$4(this, _schema$1);
1516
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1517
+ const { schema } = await getBranchDetails({
1518
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1519
+ ...fetchProps
1520
+ });
1521
+ __privateSet$3(this, _schema$1, schema);
1522
+ return schema;
1523
+ };
1422
1524
  const transformObjectLinks = (object) => {
1423
1525
  return Object.entries(object).reduce((acc, [key, value]) => {
1424
1526
  if (key === "xata")
@@ -1426,15 +1528,34 @@ const transformObjectLinks = (object) => {
1426
1528
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1427
1529
  }, {});
1428
1530
  };
1429
- const initObject = (db, links, table, object) => {
1531
+ const initObject = (db, schema, table, object) => {
1430
1532
  const result = {};
1431
- Object.assign(result, object);
1432
- const tableLinks = links[table] || [];
1433
- for (const link of tableLinks) {
1434
- const [field, linkTable] = link;
1435
- const value = result[field];
1436
- if (value && isObject(value)) {
1437
- result[field] = initObject(db, links, linkTable, value);
1533
+ const { xata, ...rest } = object ?? {};
1534
+ Object.assign(result, rest);
1535
+ const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
1536
+ if (!columns)
1537
+ console.error(`Table ${table} not found in schema`);
1538
+ for (const column of columns ?? []) {
1539
+ const value = result[column.name];
1540
+ switch (column.type) {
1541
+ case "datetime": {
1542
+ const date = value !== void 0 ? new Date(value) : void 0;
1543
+ if (date && isNaN(date.getTime())) {
1544
+ console.error(`Failed to parse date ${value} for field ${column.name}`);
1545
+ } else if (date) {
1546
+ result[column.name] = date;
1547
+ }
1548
+ break;
1549
+ }
1550
+ case "link": {
1551
+ const linkTable = column.link?.table;
1552
+ if (!linkTable) {
1553
+ console.error(`Failed to parse link for field ${column.name}`);
1554
+ } else if (isObject(value)) {
1555
+ result[column.name] = initObject(db, schema, linkTable, value);
1556
+ }
1557
+ break;
1558
+ }
1438
1559
  }
1439
1560
  }
1440
1561
  result.read = function() {
@@ -1446,7 +1567,10 @@ const initObject = (db, links, table, object) => {
1446
1567
  result.delete = function() {
1447
1568
  return db[table].delete(result["id"]);
1448
1569
  };
1449
- for (const prop of ["read", "update", "delete"]) {
1570
+ result.getMetadata = function() {
1571
+ return xata;
1572
+ };
1573
+ for (const prop of ["read", "update", "delete", "getMetadata"]) {
1450
1574
  Object.defineProperty(result, prop, { enumerable: false });
1451
1575
  }
1452
1576
  Object.freeze(result);
@@ -1466,7 +1590,7 @@ var __accessCheck$3 = (obj, member, msg) => {
1466
1590
  if (!member.has(obj))
1467
1591
  throw TypeError("Cannot " + msg);
1468
1592
  };
1469
- var __privateGet$2 = (obj, member, getter) => {
1593
+ var __privateGet$3 = (obj, member, getter) => {
1470
1594
  __accessCheck$3(obj, member, "read from private field");
1471
1595
  return getter ? getter.call(obj) : member.get(obj);
1472
1596
  };
@@ -1475,7 +1599,7 @@ var __privateAdd$3 = (obj, member, value) => {
1475
1599
  throw TypeError("Cannot add the same private member more than once");
1476
1600
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1477
1601
  };
1478
- var __privateSet$1 = (obj, member, value, setter) => {
1602
+ var __privateSet$2 = (obj, member, value, setter) => {
1479
1603
  __accessCheck$3(obj, member, "write to private field");
1480
1604
  setter ? setter.call(obj, value) : member.set(obj, value);
1481
1605
  return value;
@@ -1484,30 +1608,30 @@ var _map;
1484
1608
  class SimpleCache {
1485
1609
  constructor(options = {}) {
1486
1610
  __privateAdd$3(this, _map, void 0);
1487
- __privateSet$1(this, _map, /* @__PURE__ */ new Map());
1611
+ __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1488
1612
  this.capacity = options.max ?? 500;
1489
1613
  this.cacheRecords = options.cacheRecords ?? true;
1490
1614
  this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1491
1615
  }
1492
1616
  async getAll() {
1493
- return Object.fromEntries(__privateGet$2(this, _map));
1617
+ return Object.fromEntries(__privateGet$3(this, _map));
1494
1618
  }
1495
1619
  async get(key) {
1496
- return __privateGet$2(this, _map).get(key) ?? null;
1620
+ return __privateGet$3(this, _map).get(key) ?? null;
1497
1621
  }
1498
1622
  async set(key, value) {
1499
1623
  await this.delete(key);
1500
- __privateGet$2(this, _map).set(key, value);
1501
- if (__privateGet$2(this, _map).size > this.capacity) {
1502
- const leastRecentlyUsed = __privateGet$2(this, _map).keys().next().value;
1624
+ __privateGet$3(this, _map).set(key, value);
1625
+ if (__privateGet$3(this, _map).size > this.capacity) {
1626
+ const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
1503
1627
  await this.delete(leastRecentlyUsed);
1504
1628
  }
1505
1629
  }
1506
1630
  async delete(key) {
1507
- __privateGet$2(this, _map).delete(key);
1631
+ __privateGet$3(this, _map).delete(key);
1508
1632
  }
1509
1633
  async clear() {
1510
- return __privateGet$2(this, _map).clear();
1634
+ return __privateGet$3(this, _map).clear();
1511
1635
  }
1512
1636
  }
1513
1637
  _map = new WeakMap();
@@ -1535,7 +1659,7 @@ var __accessCheck$2 = (obj, member, msg) => {
1535
1659
  if (!member.has(obj))
1536
1660
  throw TypeError("Cannot " + msg);
1537
1661
  };
1538
- var __privateGet$1 = (obj, member, getter) => {
1662
+ var __privateGet$2 = (obj, member, getter) => {
1539
1663
  __accessCheck$2(obj, member, "read from private field");
1540
1664
  return getter ? getter.call(obj) : member.get(obj);
1541
1665
  };
@@ -1546,26 +1670,24 @@ var __privateAdd$2 = (obj, member, value) => {
1546
1670
  };
1547
1671
  var _tables;
1548
1672
  class SchemaPlugin extends XataPlugin {
1549
- constructor(links, tableNames) {
1673
+ constructor(tableNames) {
1550
1674
  super();
1551
- this.links = links;
1552
1675
  this.tableNames = tableNames;
1553
1676
  __privateAdd$2(this, _tables, {});
1554
1677
  }
1555
1678
  build(pluginOptions) {
1556
- const links = this.links;
1557
1679
  const db = new Proxy({}, {
1558
1680
  get: (_target, table) => {
1559
1681
  if (!isString(table))
1560
1682
  throw new Error("Invalid table name");
1561
- if (!__privateGet$1(this, _tables)[table]) {
1562
- __privateGet$1(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, links });
1683
+ if (__privateGet$2(this, _tables)[table] === void 0) {
1684
+ __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1563
1685
  }
1564
- return __privateGet$1(this, _tables)[table];
1686
+ return __privateGet$2(this, _tables)[table];
1565
1687
  }
1566
1688
  });
1567
1689
  for (const table of this.tableNames ?? []) {
1568
- db[table] = new RestRepository({ db, pluginOptions, table, links });
1690
+ db[table] = new RestRepository({ db, pluginOptions, table });
1569
1691
  }
1570
1692
  return db;
1571
1693
  }
@@ -1576,55 +1698,80 @@ var __accessCheck$1 = (obj, member, msg) => {
1576
1698
  if (!member.has(obj))
1577
1699
  throw TypeError("Cannot " + msg);
1578
1700
  };
1701
+ var __privateGet$1 = (obj, member, getter) => {
1702
+ __accessCheck$1(obj, member, "read from private field");
1703
+ return getter ? getter.call(obj) : member.get(obj);
1704
+ };
1579
1705
  var __privateAdd$1 = (obj, member, value) => {
1580
1706
  if (member.has(obj))
1581
1707
  throw TypeError("Cannot add the same private member more than once");
1582
1708
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1583
1709
  };
1710
+ var __privateSet$1 = (obj, member, value, setter) => {
1711
+ __accessCheck$1(obj, member, "write to private field");
1712
+ setter ? setter.call(obj, value) : member.set(obj, value);
1713
+ return value;
1714
+ };
1584
1715
  var __privateMethod$1 = (obj, member, method) => {
1585
1716
  __accessCheck$1(obj, member, "access private method");
1586
1717
  return method;
1587
1718
  };
1588
- var _search, search_fn;
1719
+ var _schema, _search, search_fn, _getSchema, getSchema_fn;
1589
1720
  class SearchPlugin extends XataPlugin {
1590
- constructor(db, links) {
1721
+ constructor(db) {
1591
1722
  super();
1592
1723
  this.db = db;
1593
- this.links = links;
1594
1724
  __privateAdd$1(this, _search);
1725
+ __privateAdd$1(this, _getSchema);
1726
+ __privateAdd$1(this, _schema, void 0);
1595
1727
  }
1596
1728
  build({ getFetchProps }) {
1597
1729
  return {
1598
1730
  all: async (query, options = {}) => {
1599
1731
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1732
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1600
1733
  return records.map((record) => {
1601
1734
  const { table = "orphan" } = record.xata;
1602
- return { table, record: initObject(this.db, this.links, table, record) };
1735
+ return { table, record: initObject(this.db, schema, table, record) };
1603
1736
  });
1604
1737
  },
1605
1738
  byTable: async (query, options = {}) => {
1606
1739
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1740
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1607
1741
  return records.reduce((acc, record) => {
1608
1742
  const { table = "orphan" } = record.xata;
1609
1743
  const items = acc[table] ?? [];
1610
- const item = initObject(this.db, this.links, table, record);
1744
+ const item = initObject(this.db, schema, table, record);
1611
1745
  return { ...acc, [table]: [...items, item] };
1612
1746
  }, {});
1613
1747
  }
1614
1748
  };
1615
1749
  }
1616
1750
  }
1751
+ _schema = new WeakMap();
1617
1752
  _search = new WeakSet();
1618
1753
  search_fn = async function(query, options, getFetchProps) {
1619
1754
  const fetchProps = await getFetchProps();
1620
- const { tables, fuzziness } = options ?? {};
1755
+ const { tables, fuzziness, highlight } = options ?? {};
1621
1756
  const { records } = await searchBranch({
1622
1757
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1623
- body: { tables, query, fuzziness },
1758
+ body: { tables, query, fuzziness, highlight },
1624
1759
  ...fetchProps
1625
1760
  });
1626
1761
  return records;
1627
1762
  };
1763
+ _getSchema = new WeakSet();
1764
+ getSchema_fn = async function(getFetchProps) {
1765
+ if (__privateGet$1(this, _schema))
1766
+ return __privateGet$1(this, _schema);
1767
+ const fetchProps = await getFetchProps();
1768
+ const { schema } = await getBranchDetails({
1769
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1770
+ ...fetchProps
1771
+ });
1772
+ __privateSet$1(this, _schema, schema);
1773
+ return schema;
1774
+ };
1628
1775
 
1629
1776
  const isBranchStrategyBuilder = (strategy) => {
1630
1777
  return typeof strategy === "function";
@@ -1636,30 +1783,39 @@ const envBranchNames = [
1636
1783
  "CF_PAGES_BRANCH",
1637
1784
  "BRANCH"
1638
1785
  ];
1639
- const defaultBranch = "main";
1640
1786
  async function getCurrentBranchName(options) {
1641
- const env = await getBranchByEnvVariable();
1642
- if (env)
1643
- return env;
1644
- const branch = await getGitBranch();
1645
- if (!branch)
1646
- return defaultBranch;
1647
- const details = await getDatabaseBranch(branch, options);
1648
- if (details)
1649
- return branch;
1650
- return defaultBranch;
1787
+ const env = getBranchByEnvVariable();
1788
+ if (env) {
1789
+ const details = await getDatabaseBranch(env, options);
1790
+ if (details)
1791
+ return env;
1792
+ console.warn(`Branch ${env} not found in Xata. Ignoring...`);
1793
+ }
1794
+ const gitBranch = await getGitBranch();
1795
+ return resolveXataBranch(gitBranch, options);
1651
1796
  }
1652
1797
  async function getCurrentBranchDetails(options) {
1653
- const env = await getBranchByEnvVariable();
1654
- if (env)
1655
- return getDatabaseBranch(env, options);
1656
- const branch = await getGitBranch();
1657
- if (!branch)
1658
- return getDatabaseBranch(defaultBranch, options);
1659
- const details = await getDatabaseBranch(branch, options);
1660
- if (details)
1661
- return details;
1662
- return getDatabaseBranch(defaultBranch, options);
1798
+ const branch = await getCurrentBranchName(options);
1799
+ return getDatabaseBranch(branch, options);
1800
+ }
1801
+ async function resolveXataBranch(gitBranch, options) {
1802
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1803
+ const apiKey = options?.apiKey || getAPIKey();
1804
+ if (!databaseURL)
1805
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1806
+ if (!apiKey)
1807
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1808
+ const [protocol, , host, , dbName] = databaseURL.split("/");
1809
+ const [workspace] = host.split(".");
1810
+ const { branch } = await resolveBranch({
1811
+ apiKey,
1812
+ apiUrl: databaseURL,
1813
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1814
+ workspacesApiUrl: `${protocol}//${host}`,
1815
+ pathParams: { dbName, workspace },
1816
+ queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
1817
+ });
1818
+ return branch;
1663
1819
  }
1664
1820
  async function getDatabaseBranch(branch, options) {
1665
1821
  const databaseURL = options?.databaseURL || getDatabaseURL();
@@ -1733,7 +1889,7 @@ var __privateMethod = (obj, member, method) => {
1733
1889
  const buildClient = (plugins) => {
1734
1890
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1735
1891
  return _a = class {
1736
- constructor(options = {}, links, tables) {
1892
+ constructor(options = {}, tables) {
1737
1893
  __privateAdd(this, _parseOptions);
1738
1894
  __privateAdd(this, _getFetchProps);
1739
1895
  __privateAdd(this, _evaluateBranch);
@@ -1743,12 +1899,12 @@ const buildClient = (plugins) => {
1743
1899
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1744
1900
  cache: safeOptions.cache
1745
1901
  };
1746
- const db = new SchemaPlugin(links, tables).build(pluginOptions);
1747
- const search = new SearchPlugin(db, links ?? {}).build(pluginOptions);
1902
+ const db = new SchemaPlugin(tables).build(pluginOptions);
1903
+ const search = new SearchPlugin(db).build(pluginOptions);
1748
1904
  this.db = db;
1749
1905
  this.search = search;
1750
1906
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1751
- if (!namespace)
1907
+ if (namespace === void 0)
1752
1908
  continue;
1753
1909
  const result = namespace.build(pluginOptions);
1754
1910
  if (result instanceof Promise) {
@@ -1765,7 +1921,7 @@ const buildClient = (plugins) => {
1765
1921
  const databaseURL = options?.databaseURL || getDatabaseURL();
1766
1922
  const apiKey = options?.apiKey || getAPIKey();
1767
1923
  const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1768
- const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1924
+ const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1769
1925
  if (!databaseURL || !apiKey) {
1770
1926
  throw new Error("Options databaseURL and apiKey are required");
1771
1927
  }
@@ -1792,7 +1948,7 @@ const buildClient = (plugins) => {
1792
1948
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
1793
1949
  if (__privateGet(this, _branch))
1794
1950
  return __privateGet(this, _branch);
1795
- if (!param)
1951
+ if (param === void 0)
1796
1952
  return void 0;
1797
1953
  const strategies = Array.isArray(param) ? [...param] : [param];
1798
1954
  const evaluateBranch = async (strategy) => {
@@ -1817,5 +1973,5 @@ class XataError extends Error {
1817
1973
  }
1818
1974
  }
1819
1975
 
1820
- 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, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, compareBranchSchema, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchSchemaHistory, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, 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, removeWorkspaceMember, resendWorkspaceMemberInvite, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateBranchSchema, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
1976
+ 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 };
1821
1977
  //# sourceMappingURL=index.mjs.map