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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -11,8 +11,11 @@ function compact(arr) {
11
11
  function isObject(value) {
12
12
  return Boolean(value) && typeof value === "object" && !Array.isArray(value);
13
13
  }
14
+ function isDefined(value) {
15
+ return value !== null && value !== void 0;
16
+ }
14
17
  function isString(value) {
15
- return value !== void 0 && value !== null && typeof value === "string";
18
+ return isDefined(value) && typeof value === "string";
16
19
  }
17
20
  function toBase64(value) {
18
21
  try {
@@ -38,7 +41,10 @@ function getEnvVariable(name) {
38
41
  }
39
42
  async function getGitBranch() {
40
43
  try {
41
- return require("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
44
+ if (typeof require === "function") {
45
+ const req = require;
46
+ return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
47
+ }
42
48
  } catch (err) {
43
49
  }
44
50
  try {
@@ -71,7 +77,12 @@ function getFetchImplementation(userFetch) {
71
77
  return fetchImpl;
72
78
  }
73
79
 
74
- class FetcherError extends Error {
80
+ class ErrorWithCause extends Error {
81
+ constructor(message, options) {
82
+ super(message, options);
83
+ }
84
+ }
85
+ class FetcherError extends ErrorWithCause {
75
86
  constructor(status, data) {
76
87
  super(getMessage(data));
77
88
  this.status = status;
@@ -252,6 +263,14 @@ const deleteDatabase = (variables) => fetch$1({
252
263
  method: "delete",
253
264
  ...variables
254
265
  });
266
+ const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
267
+ const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
268
+ const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
269
+ const resolveBranch = (variables) => fetch$1({
270
+ url: "/dbs/{dbName}/resolveBranch",
271
+ method: "get",
272
+ ...variables
273
+ });
255
274
  const getBranchDetails = (variables) => fetch$1({
256
275
  url: "/db/{dbBranchName}",
257
276
  method: "get",
@@ -280,17 +299,6 @@ const getBranchMetadata = (variables) => fetch$1({
280
299
  const getBranchMigrationHistory = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations", method: "get", ...variables });
281
300
  const executeBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/execute", method: "post", ...variables });
282
301
  const getBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/plan", method: "post", ...variables });
283
- const compareBranchSchema = (variables) => fetch$1({
284
- url: "/db/{dbBranchName}/schema/compare/{branchName}",
285
- method: "post",
286
- ...variables
287
- });
288
- const updateBranchSchema = (variables) => fetch$1({
289
- url: "/db/{dbBranchName}/schema/update",
290
- method: "post",
291
- ...variables
292
- });
293
- const getBranchSchemaHistory = (variables) => fetch$1({ url: "/db/{dbBranchName}/schema/history", method: "post", ...variables });
294
302
  const getBranchStats = (variables) => fetch$1({
295
303
  url: "/db/{dbBranchName}/stats",
296
304
  method: "get",
@@ -370,6 +378,11 @@ const queryTable = (variables) => fetch$1({
370
378
  method: "post",
371
379
  ...variables
372
380
  });
381
+ const searchTable = (variables) => fetch$1({
382
+ url: "/db/{dbBranchName}/tables/{tableName}/search",
383
+ method: "post",
384
+ ...variables
385
+ });
373
386
  const searchBranch = (variables) => fetch$1({
374
387
  url: "/db/{dbBranchName}/search",
375
388
  method: "post",
@@ -391,7 +404,15 @@ const operationsByTag = {
391
404
  resendWorkspaceMemberInvite,
392
405
  acceptWorkspaceMemberInvite
393
406
  },
394
- database: { getDatabaseList, createDatabase, deleteDatabase },
407
+ database: {
408
+ getDatabaseList,
409
+ createDatabase,
410
+ deleteDatabase,
411
+ getGitBranchesMapping,
412
+ addGitBranchesEntry,
413
+ removeGitBranchesEntry,
414
+ resolveBranch
415
+ },
395
416
  branch: {
396
417
  getBranchList,
397
418
  getBranchDetails,
@@ -402,9 +423,6 @@ const operationsByTag = {
402
423
  getBranchMigrationHistory,
403
424
  executeBranchMigrationPlan,
404
425
  getBranchMigrationPlan,
405
- compareBranchSchema,
406
- updateBranchSchema,
407
- getBranchSchemaHistory,
408
426
  getBranchStats
409
427
  },
410
428
  table: {
@@ -428,6 +446,7 @@ const operationsByTag = {
428
446
  getRecord,
429
447
  bulkInsertTableRecords,
430
448
  queryTable,
449
+ searchTable,
431
450
  searchBranch
432
451
  }
433
452
  };
@@ -461,7 +480,7 @@ var __accessCheck$7 = (obj, member, msg) => {
461
480
  if (!member.has(obj))
462
481
  throw TypeError("Cannot " + msg);
463
482
  };
464
- var __privateGet$6 = (obj, member, getter) => {
483
+ var __privateGet$7 = (obj, member, getter) => {
465
484
  __accessCheck$7(obj, member, "read from private field");
466
485
  return getter ? getter.call(obj) : member.get(obj);
467
486
  };
@@ -470,7 +489,7 @@ var __privateAdd$7 = (obj, member, value) => {
470
489
  throw TypeError("Cannot add the same private member more than once");
471
490
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
472
491
  };
473
- var __privateSet$5 = (obj, member, value, setter) => {
492
+ var __privateSet$6 = (obj, member, value, setter) => {
474
493
  __accessCheck$7(obj, member, "write to private field");
475
494
  setter ? setter.call(obj, value) : member.set(obj, value);
476
495
  return value;
@@ -485,7 +504,7 @@ class XataApiClient {
485
504
  if (!apiKey) {
486
505
  throw new Error("Could not resolve a valid apiKey");
487
506
  }
488
- __privateSet$5(this, _extraProps, {
507
+ __privateSet$6(this, _extraProps, {
489
508
  apiUrl: getHostUrl(provider, "main"),
490
509
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
491
510
  fetchImpl: getFetchImplementation(options.fetch),
@@ -493,34 +512,34 @@ class XataApiClient {
493
512
  });
494
513
  }
495
514
  get user() {
496
- if (!__privateGet$6(this, _namespaces).user)
497
- __privateGet$6(this, _namespaces).user = new UserApi(__privateGet$6(this, _extraProps));
498
- return __privateGet$6(this, _namespaces).user;
515
+ if (!__privateGet$7(this, _namespaces).user)
516
+ __privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
517
+ return __privateGet$7(this, _namespaces).user;
499
518
  }
500
519
  get workspaces() {
501
- if (!__privateGet$6(this, _namespaces).workspaces)
502
- __privateGet$6(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$6(this, _extraProps));
503
- return __privateGet$6(this, _namespaces).workspaces;
520
+ if (!__privateGet$7(this, _namespaces).workspaces)
521
+ __privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
522
+ return __privateGet$7(this, _namespaces).workspaces;
504
523
  }
505
524
  get databases() {
506
- if (!__privateGet$6(this, _namespaces).databases)
507
- __privateGet$6(this, _namespaces).databases = new DatabaseApi(__privateGet$6(this, _extraProps));
508
- return __privateGet$6(this, _namespaces).databases;
525
+ if (!__privateGet$7(this, _namespaces).databases)
526
+ __privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
527
+ return __privateGet$7(this, _namespaces).databases;
509
528
  }
510
529
  get branches() {
511
- if (!__privateGet$6(this, _namespaces).branches)
512
- __privateGet$6(this, _namespaces).branches = new BranchApi(__privateGet$6(this, _extraProps));
513
- return __privateGet$6(this, _namespaces).branches;
530
+ if (!__privateGet$7(this, _namespaces).branches)
531
+ __privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
532
+ return __privateGet$7(this, _namespaces).branches;
514
533
  }
515
534
  get tables() {
516
- if (!__privateGet$6(this, _namespaces).tables)
517
- __privateGet$6(this, _namespaces).tables = new TableApi(__privateGet$6(this, _extraProps));
518
- return __privateGet$6(this, _namespaces).tables;
535
+ if (!__privateGet$7(this, _namespaces).tables)
536
+ __privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
537
+ return __privateGet$7(this, _namespaces).tables;
519
538
  }
520
539
  get records() {
521
- if (!__privateGet$6(this, _namespaces).records)
522
- __privateGet$6(this, _namespaces).records = new RecordsApi(__privateGet$6(this, _extraProps));
523
- return __privateGet$6(this, _namespaces).records;
540
+ if (!__privateGet$7(this, _namespaces).records)
541
+ __privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
542
+ return __privateGet$7(this, _namespaces).records;
524
543
  }
525
544
  }
526
545
  _extraProps = new WeakMap();
@@ -654,6 +673,33 @@ class DatabaseApi {
654
673
  ...this.extraProps
655
674
  });
656
675
  }
676
+ getGitBranchesMapping(workspace, dbName) {
677
+ return operationsByTag.database.getGitBranchesMapping({
678
+ pathParams: { workspace, dbName },
679
+ ...this.extraProps
680
+ });
681
+ }
682
+ addGitBranchesEntry(workspace, dbName, body) {
683
+ return operationsByTag.database.addGitBranchesEntry({
684
+ pathParams: { workspace, dbName },
685
+ body,
686
+ ...this.extraProps
687
+ });
688
+ }
689
+ removeGitBranchesEntry(workspace, dbName, gitBranch) {
690
+ return operationsByTag.database.removeGitBranchesEntry({
691
+ pathParams: { workspace, dbName },
692
+ queryParams: { gitBranch },
693
+ ...this.extraProps
694
+ });
695
+ }
696
+ resolveBranch(workspace, dbName, gitBranch) {
697
+ return operationsByTag.database.resolveBranch({
698
+ pathParams: { workspace, dbName },
699
+ queryParams: { gitBranch },
700
+ ...this.extraProps
701
+ });
702
+ }
657
703
  }
658
704
  class BranchApi {
659
705
  constructor(extraProps) {
@@ -671,10 +717,10 @@ class BranchApi {
671
717
  ...this.extraProps
672
718
  });
673
719
  }
674
- createBranch(workspace, database, branch, from = "", options = {}) {
720
+ createBranch(workspace, database, branch, from, options = {}) {
675
721
  return operationsByTag.branch.createBranch({
676
722
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
677
- queryParams: { from },
723
+ queryParams: isString(from) ? { from } : void 0,
678
724
  body: options,
679
725
  ...this.extraProps
680
726
  });
@@ -856,6 +902,13 @@ class RecordsApi {
856
902
  ...this.extraProps
857
903
  });
858
904
  }
905
+ searchTable(workspace, database, branch, tableName, query) {
906
+ return operationsByTag.records.searchTable({
907
+ pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
908
+ body: query,
909
+ ...this.extraProps
910
+ });
911
+ }
859
912
  searchBranch(workspace, database, branch, query) {
860
913
  return operationsByTag.records.searchBranch({
861
914
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
@@ -879,7 +932,7 @@ var __accessCheck$6 = (obj, member, msg) => {
879
932
  if (!member.has(obj))
880
933
  throw TypeError("Cannot " + msg);
881
934
  };
882
- var __privateGet$5 = (obj, member, getter) => {
935
+ var __privateGet$6 = (obj, member, getter) => {
883
936
  __accessCheck$6(obj, member, "read from private field");
884
937
  return getter ? getter.call(obj) : member.get(obj);
885
938
  };
@@ -888,30 +941,30 @@ var __privateAdd$6 = (obj, member, value) => {
888
941
  throw TypeError("Cannot add the same private member more than once");
889
942
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
890
943
  };
891
- var __privateSet$4 = (obj, member, value, setter) => {
944
+ var __privateSet$5 = (obj, member, value, setter) => {
892
945
  __accessCheck$6(obj, member, "write to private field");
893
946
  setter ? setter.call(obj, value) : member.set(obj, value);
894
947
  return value;
895
948
  };
896
- var _query;
949
+ var _query, _page;
897
950
  class Page {
898
951
  constructor(query, meta, records = []) {
899
952
  __privateAdd$6(this, _query, void 0);
900
- __privateSet$4(this, _query, query);
953
+ __privateSet$5(this, _query, query);
901
954
  this.meta = meta;
902
- this.records = records;
955
+ this.records = new RecordArray(this, records);
903
956
  }
904
957
  async nextPage(size, offset) {
905
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
958
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
906
959
  }
907
960
  async previousPage(size, offset) {
908
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
961
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
909
962
  }
910
963
  async firstPage(size, offset) {
911
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
964
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
912
965
  }
913
966
  async lastPage(size, offset) {
914
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
967
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
915
968
  }
916
969
  hasNextPage() {
917
970
  return this.meta.page.more;
@@ -919,15 +972,46 @@ class Page {
919
972
  }
920
973
  _query = new WeakMap();
921
974
  const PAGINATION_MAX_SIZE = 200;
922
- const PAGINATION_DEFAULT_SIZE = 200;
975
+ const PAGINATION_DEFAULT_SIZE = 20;
923
976
  const PAGINATION_MAX_OFFSET = 800;
924
977
  const PAGINATION_DEFAULT_OFFSET = 0;
978
+ function isCursorPaginationOptions(options) {
979
+ return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
980
+ }
981
+ const _RecordArray = class extends Array {
982
+ constructor(page, overrideRecords) {
983
+ super(...overrideRecords ?? page.records);
984
+ __privateAdd$6(this, _page, void 0);
985
+ __privateSet$5(this, _page, page);
986
+ }
987
+ async nextPage(size, offset) {
988
+ const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
989
+ return new _RecordArray(newPage);
990
+ }
991
+ async previousPage(size, offset) {
992
+ const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
993
+ return new _RecordArray(newPage);
994
+ }
995
+ async firstPage(size, offset) {
996
+ const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
997
+ return new _RecordArray(newPage);
998
+ }
999
+ async lastPage(size, offset) {
1000
+ const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
1001
+ return new _RecordArray(newPage);
1002
+ }
1003
+ hasNextPage() {
1004
+ return __privateGet$6(this, _page).meta.page.more;
1005
+ }
1006
+ };
1007
+ let RecordArray = _RecordArray;
1008
+ _page = new WeakMap();
925
1009
 
926
1010
  var __accessCheck$5 = (obj, member, msg) => {
927
1011
  if (!member.has(obj))
928
1012
  throw TypeError("Cannot " + msg);
929
1013
  };
930
- var __privateGet$4 = (obj, member, getter) => {
1014
+ var __privateGet$5 = (obj, member, getter) => {
931
1015
  __accessCheck$5(obj, member, "read from private field");
932
1016
  return getter ? getter.call(obj) : member.get(obj);
933
1017
  };
@@ -936,34 +1020,35 @@ var __privateAdd$5 = (obj, member, value) => {
936
1020
  throw TypeError("Cannot add the same private member more than once");
937
1021
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
938
1022
  };
939
- var __privateSet$3 = (obj, member, value, setter) => {
1023
+ var __privateSet$4 = (obj, member, value, setter) => {
940
1024
  __accessCheck$5(obj, member, "write to private field");
941
1025
  setter ? setter.call(obj, value) : member.set(obj, value);
942
1026
  return value;
943
1027
  };
944
1028
  var _table$1, _repository, _data;
945
1029
  const _Query = class {
946
- constructor(repository, table, data, parent) {
1030
+ constructor(repository, table, data, rawParent) {
947
1031
  __privateAdd$5(this, _table$1, void 0);
948
1032
  __privateAdd$5(this, _repository, void 0);
949
1033
  __privateAdd$5(this, _data, { filter: {} });
950
1034
  this.meta = { page: { cursor: "start", more: true } };
951
- this.records = [];
952
- __privateSet$3(this, _table$1, table);
1035
+ this.records = new RecordArray(this, []);
1036
+ __privateSet$4(this, _table$1, table);
953
1037
  if (repository) {
954
- __privateSet$3(this, _repository, repository);
1038
+ __privateSet$4(this, _repository, repository);
955
1039
  } else {
956
- __privateSet$3(this, _repository, this);
1040
+ __privateSet$4(this, _repository, this);
957
1041
  }
958
- __privateGet$4(this, _data).filter = data.filter ?? parent?.filter ?? {};
959
- __privateGet$4(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
960
- __privateGet$4(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
961
- __privateGet$4(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
962
- __privateGet$4(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
963
- __privateGet$4(this, _data).sort = data.sort ?? parent?.sort;
964
- __privateGet$4(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
965
- __privateGet$4(this, _data).page = data.page ?? parent?.page;
966
- __privateGet$4(this, _data).cache = data.cache ?? parent?.cache;
1042
+ const parent = cleanParent(data, rawParent);
1043
+ __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
1044
+ __privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
1045
+ __privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
1046
+ __privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
1047
+ __privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
1048
+ __privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
1049
+ __privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
1050
+ __privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
1051
+ __privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
967
1052
  this.any = this.any.bind(this);
968
1053
  this.all = this.all.bind(this);
969
1054
  this.not = this.not.bind(this);
@@ -974,83 +1059,88 @@ const _Query = class {
974
1059
  Object.defineProperty(this, "repository", { enumerable: false });
975
1060
  }
976
1061
  getQueryOptions() {
977
- return __privateGet$4(this, _data);
1062
+ return __privateGet$5(this, _data);
978
1063
  }
979
1064
  key() {
980
- const { columns = [], filter = {}, sort = [], page = {} } = __privateGet$4(this, _data);
981
- const key = JSON.stringify({ columns, filter, sort, page });
1065
+ const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
1066
+ const key = JSON.stringify({ columns, filter, sort, pagination });
982
1067
  return toBase64(key);
983
1068
  }
984
1069
  any(...queries) {
985
1070
  const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
986
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $any } }, __privateGet$4(this, _data));
1071
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
987
1072
  }
988
1073
  all(...queries) {
989
1074
  const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
990
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1075
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
991
1076
  }
992
1077
  not(...queries) {
993
1078
  const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
994
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $not } }, __privateGet$4(this, _data));
1079
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
995
1080
  }
996
1081
  none(...queries) {
997
1082
  const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
998
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $none } }, __privateGet$4(this, _data));
1083
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
999
1084
  }
1000
1085
  filter(a, b) {
1001
1086
  if (arguments.length === 1) {
1002
1087
  const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
1003
- const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat(constraints));
1004
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1088
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
1089
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1005
1090
  } else {
1006
- const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1007
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1091
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1092
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1008
1093
  }
1009
1094
  }
1010
1095
  sort(column, direction) {
1011
- const originalSort = [__privateGet$4(this, _data).sort ?? []].flat();
1096
+ const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
1012
1097
  const sort = [...originalSort, { column, direction }];
1013
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { sort }, __privateGet$4(this, _data));
1098
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
1014
1099
  }
1015
1100
  select(columns) {
1016
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { columns }, __privateGet$4(this, _data));
1101
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
1017
1102
  }
1018
1103
  getPaginated(options = {}) {
1019
- const query = new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), options, __privateGet$4(this, _data));
1020
- return __privateGet$4(this, _repository).query(query);
1104
+ const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
1105
+ return __privateGet$5(this, _repository).query(query);
1021
1106
  }
1022
1107
  async *[Symbol.asyncIterator]() {
1023
- for await (const [record] of this.getIterator(1)) {
1108
+ for await (const [record] of this.getIterator({ batchSize: 1 })) {
1024
1109
  yield record;
1025
1110
  }
1026
1111
  }
1027
- async *getIterator(chunk, options = {}) {
1028
- let offset = 0;
1029
- let end = false;
1030
- while (!end) {
1031
- const { records, meta } = await this.getPaginated({ ...options, page: { size: chunk, offset } });
1032
- yield records;
1033
- offset += chunk;
1034
- end = !meta.page.more;
1112
+ async *getIterator(options = {}) {
1113
+ const { batchSize = 1 } = options;
1114
+ let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
1115
+ let more = page.hasNextPage();
1116
+ yield page.records;
1117
+ while (more) {
1118
+ page = await page.nextPage();
1119
+ more = page.hasNextPage();
1120
+ yield page.records;
1035
1121
  }
1036
1122
  }
1037
1123
  async getMany(options = {}) {
1038
- const { records } = await this.getPaginated(options);
1039
- return records;
1124
+ const page = await this.getPaginated(options);
1125
+ if (page.hasNextPage() && options.pagination?.size === void 0) {
1126
+ console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
1127
+ }
1128
+ return page.records;
1040
1129
  }
1041
- async getAll(chunk = PAGINATION_MAX_SIZE, options = {}) {
1130
+ async getAll(options = {}) {
1131
+ const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
1042
1132
  const results = [];
1043
- for await (const page of this.getIterator(chunk, options)) {
1133
+ for await (const page of this.getIterator({ ...rest, batchSize })) {
1044
1134
  results.push(...page);
1045
1135
  }
1046
1136
  return results;
1047
1137
  }
1048
1138
  async getFirst(options = {}) {
1049
- const records = await this.getMany({ ...options, page: { size: 1 } });
1050
- return records[0] || null;
1139
+ const records = await this.getMany({ ...options, pagination: { size: 1 } });
1140
+ return records[0] ?? null;
1051
1141
  }
1052
1142
  cache(ttl) {
1053
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { cache: ttl }, __privateGet$4(this, _data));
1143
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
1054
1144
  }
1055
1145
  nextPage(size, offset) {
1056
1146
  return this.firstPage(size, offset);
@@ -1059,10 +1149,10 @@ const _Query = class {
1059
1149
  return this.firstPage(size, offset);
1060
1150
  }
1061
1151
  firstPage(size, offset) {
1062
- return this.getPaginated({ page: { size, offset } });
1152
+ return this.getPaginated({ pagination: { size, offset } });
1063
1153
  }
1064
1154
  lastPage(size, offset) {
1065
- return this.getPaginated({ page: { size, offset, before: "end" } });
1155
+ return this.getPaginated({ pagination: { size, offset, before: "end" } });
1066
1156
  }
1067
1157
  hasNextPage() {
1068
1158
  return this.meta.page.more;
@@ -1072,12 +1162,20 @@ let Query = _Query;
1072
1162
  _table$1 = new WeakMap();
1073
1163
  _repository = new WeakMap();
1074
1164
  _data = new WeakMap();
1165
+ function cleanParent(data, parent) {
1166
+ if (isCursorPaginationOptions(data.pagination)) {
1167
+ return { ...parent, sorting: void 0, filter: void 0 };
1168
+ }
1169
+ return parent;
1170
+ }
1075
1171
 
1076
1172
  function isIdentifiable(x) {
1077
1173
  return isObject(x) && isString(x?.id);
1078
1174
  }
1079
1175
  function isXataRecord(x) {
1080
- return isIdentifiable(x) && typeof x?.xata === "object" && typeof x?.xata?.version === "number";
1176
+ const record = x;
1177
+ const metadata = record?.getMetadata();
1178
+ return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
1081
1179
  }
1082
1180
 
1083
1181
  function isSortFilterString(value) {
@@ -1107,7 +1205,7 @@ var __accessCheck$4 = (obj, member, msg) => {
1107
1205
  if (!member.has(obj))
1108
1206
  throw TypeError("Cannot " + msg);
1109
1207
  };
1110
- var __privateGet$3 = (obj, member, getter) => {
1208
+ var __privateGet$4 = (obj, member, getter) => {
1111
1209
  __accessCheck$4(obj, member, "read from private field");
1112
1210
  return getter ? getter.call(obj) : member.get(obj);
1113
1211
  };
@@ -1116,7 +1214,7 @@ var __privateAdd$4 = (obj, member, value) => {
1116
1214
  throw TypeError("Cannot add the same private member more than once");
1117
1215
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1118
1216
  };
1119
- var __privateSet$2 = (obj, member, value, setter) => {
1217
+ var __privateSet$3 = (obj, member, value, setter) => {
1120
1218
  __accessCheck$4(obj, member, "write to private field");
1121
1219
  setter ? setter.call(obj, value) : member.set(obj, value);
1122
1220
  return value;
@@ -1125,7 +1223,7 @@ var __privateMethod$2 = (obj, member, method) => {
1125
1223
  __accessCheck$4(obj, member, "access private method");
1126
1224
  return method;
1127
1225
  };
1128
- 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;
1226
+ 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;
1129
1227
  class Repository extends Query {
1130
1228
  }
1131
1229
  class RestRepository extends Query {
@@ -1142,18 +1240,20 @@ class RestRepository extends Query {
1142
1240
  __privateAdd$4(this, _getCacheRecord);
1143
1241
  __privateAdd$4(this, _setCacheQuery);
1144
1242
  __privateAdd$4(this, _getCacheQuery);
1243
+ __privateAdd$4(this, _getSchema$1);
1145
1244
  __privateAdd$4(this, _table, void 0);
1146
- __privateAdd$4(this, _links, void 0);
1147
1245
  __privateAdd$4(this, _getFetchProps, void 0);
1148
1246
  __privateAdd$4(this, _cache, void 0);
1149
- __privateSet$2(this, _table, options.table);
1150
- __privateSet$2(this, _links, options.links ?? {});
1151
- __privateSet$2(this, _getFetchProps, options.pluginOptions.getFetchProps);
1247
+ __privateAdd$4(this, _schema$1, void 0);
1248
+ __privateSet$3(this, _table, options.table);
1249
+ __privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
1152
1250
  this.db = options.db;
1153
- __privateSet$2(this, _cache, options.pluginOptions.cache);
1251
+ __privateSet$3(this, _cache, options.pluginOptions.cache);
1154
1252
  }
1155
1253
  async create(a, b) {
1156
1254
  if (Array.isArray(a)) {
1255
+ if (a.length === 0)
1256
+ return [];
1157
1257
  const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1158
1258
  await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1159
1259
  return records;
@@ -1179,26 +1279,36 @@ class RestRepository extends Query {
1179
1279
  }
1180
1280
  throw new Error("Invalid arguments for create method");
1181
1281
  }
1182
- async read(recordId) {
1183
- const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1184
- if (cacheRecord)
1185
- return cacheRecord;
1186
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1187
- try {
1188
- const response = await getRecord({
1189
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1190
- ...fetchProps
1191
- });
1192
- return initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), response);
1193
- } catch (e) {
1194
- if (isObject(e) && e.status === 404) {
1195
- return null;
1282
+ async read(a) {
1283
+ if (Array.isArray(a)) {
1284
+ if (a.length === 0)
1285
+ return [];
1286
+ return this.getAll({ filter: { id: { $any: a } } });
1287
+ }
1288
+ if (isString(a)) {
1289
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, a);
1290
+ if (cacheRecord)
1291
+ return cacheRecord;
1292
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1293
+ try {
1294
+ const response = await getRecord({
1295
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: a },
1296
+ ...fetchProps
1297
+ });
1298
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1299
+ return initObject(this.db, schema, __privateGet$4(this, _table), response);
1300
+ } catch (e) {
1301
+ if (isObject(e) && e.status === 404) {
1302
+ return null;
1303
+ }
1304
+ throw e;
1196
1305
  }
1197
- throw e;
1198
1306
  }
1199
1307
  }
1200
1308
  async update(a, b) {
1201
1309
  if (Array.isArray(a)) {
1310
+ if (a.length === 0)
1311
+ return [];
1202
1312
  if (a.length > 100) {
1203
1313
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1204
1314
  }
@@ -1220,6 +1330,8 @@ class RestRepository extends Query {
1220
1330
  }
1221
1331
  async createOrUpdate(a, b) {
1222
1332
  if (Array.isArray(a)) {
1333
+ if (a.length === 0)
1334
+ return [];
1223
1335
  if (a.length > 100) {
1224
1336
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1225
1337
  }
@@ -1241,6 +1353,8 @@ class RestRepository extends Query {
1241
1353
  }
1242
1354
  async delete(a) {
1243
1355
  if (Array.isArray(a)) {
1356
+ if (a.length === 0)
1357
+ return;
1244
1358
  if (a.length > 100) {
1245
1359
  console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1246
1360
  }
@@ -1260,13 +1374,19 @@ class RestRepository extends Query {
1260
1374
  throw new Error("Invalid arguments for delete method");
1261
1375
  }
1262
1376
  async search(query, options = {}) {
1263
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1264
- const { records } = await searchBranch({
1265
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1266
- body: { tables: [__privateGet$3(this, _table)], query, fuzziness: options.fuzziness },
1377
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1378
+ const { records } = await searchTable({
1379
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1380
+ body: {
1381
+ query,
1382
+ fuzziness: options.fuzziness,
1383
+ highlight: options.highlight,
1384
+ filter: options.filter
1385
+ },
1267
1386
  ...fetchProps
1268
1387
  });
1269
- return records.map((item) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), item));
1388
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1389
+ return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
1270
1390
  }
1271
1391
  async query(query) {
1272
1392
  const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
@@ -1275,34 +1395,35 @@ class RestRepository extends Query {
1275
1395
  const data = query.getQueryOptions();
1276
1396
  const body = {
1277
1397
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1278
- sort: data.sort ? buildSortFilter(data.sort) : void 0,
1279
- page: data.page,
1398
+ sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1399
+ page: data.pagination,
1280
1400
  columns: data.columns
1281
1401
  };
1282
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1402
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1283
1403
  const { meta, records: objects } = await queryTable({
1284
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1404
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1285
1405
  body,
1286
1406
  ...fetchProps
1287
1407
  });
1288
- const records = objects.map((record) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), record));
1408
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1409
+ const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
1289
1410
  await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1290
1411
  return new Page(query, meta, records);
1291
1412
  }
1292
1413
  }
1293
1414
  _table = new WeakMap();
1294
- _links = new WeakMap();
1295
1415
  _getFetchProps = new WeakMap();
1296
1416
  _cache = new WeakMap();
1417
+ _schema$1 = new WeakMap();
1297
1418
  _insertRecordWithoutId = new WeakSet();
1298
1419
  insertRecordWithoutId_fn = async function(object) {
1299
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1420
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1300
1421
  const record = transformObjectLinks(object);
1301
1422
  const response = await insertRecord({
1302
1423
  pathParams: {
1303
1424
  workspace: "{workspaceId}",
1304
1425
  dbBranchName: "{dbBranch}",
1305
- tableName: __privateGet$3(this, _table)
1426
+ tableName: __privateGet$4(this, _table)
1306
1427
  },
1307
1428
  body: record,
1308
1429
  ...fetchProps
@@ -1315,13 +1436,13 @@ insertRecordWithoutId_fn = async function(object) {
1315
1436
  };
1316
1437
  _insertRecordWithId = new WeakSet();
1317
1438
  insertRecordWithId_fn = async function(recordId, object) {
1318
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1439
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1319
1440
  const record = transformObjectLinks(object);
1320
1441
  const response = await insertRecordWithID({
1321
1442
  pathParams: {
1322
1443
  workspace: "{workspaceId}",
1323
1444
  dbBranchName: "{dbBranch}",
1324
- tableName: __privateGet$3(this, _table),
1445
+ tableName: __privateGet$4(this, _table),
1325
1446
  recordId
1326
1447
  },
1327
1448
  body: record,
@@ -1336,10 +1457,10 @@ insertRecordWithId_fn = async function(recordId, object) {
1336
1457
  };
1337
1458
  _bulkInsertTableRecords = new WeakSet();
1338
1459
  bulkInsertTableRecords_fn = async function(objects) {
1339
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1460
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1340
1461
  const records = objects.map((object) => transformObjectLinks(object));
1341
1462
  const response = await bulkInsertTableRecords({
1342
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1463
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1343
1464
  body: { records },
1344
1465
  ...fetchProps
1345
1466
  });
@@ -1351,10 +1472,10 @@ bulkInsertTableRecords_fn = async function(objects) {
1351
1472
  };
1352
1473
  _updateRecordWithID = new WeakSet();
1353
1474
  updateRecordWithID_fn = async function(recordId, object) {
1354
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1475
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1355
1476
  const record = transformObjectLinks(object);
1356
1477
  const response = await updateRecordWithID({
1357
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1478
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1358
1479
  body: record,
1359
1480
  ...fetchProps
1360
1481
  });
@@ -1365,9 +1486,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1365
1486
  };
1366
1487
  _upsertRecordWithID = new WeakSet();
1367
1488
  upsertRecordWithID_fn = async function(recordId, object) {
1368
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1489
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1369
1490
  const response = await upsertRecordWithID({
1370
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1491
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1371
1492
  body: object,
1372
1493
  ...fetchProps
1373
1494
  });
@@ -1378,51 +1499,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
1378
1499
  };
1379
1500
  _deleteRecord = new WeakSet();
1380
1501
  deleteRecord_fn = async function(recordId) {
1381
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1502
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1382
1503
  await deleteRecord({
1383
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1504
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1384
1505
  ...fetchProps
1385
1506
  });
1386
1507
  };
1387
1508
  _invalidateCache = new WeakSet();
1388
1509
  invalidateCache_fn = async function(recordId) {
1389
- await __privateGet$3(this, _cache).delete(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1390
- const cacheItems = await __privateGet$3(this, _cache).getAll();
1510
+ await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1511
+ const cacheItems = await __privateGet$4(this, _cache).getAll();
1391
1512
  const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1392
1513
  for (const [key, value] of queries) {
1393
1514
  const ids = getIds(value);
1394
1515
  if (ids.includes(recordId))
1395
- await __privateGet$3(this, _cache).delete(key);
1516
+ await __privateGet$4(this, _cache).delete(key);
1396
1517
  }
1397
1518
  };
1398
1519
  _setCacheRecord = new WeakSet();
1399
1520
  setCacheRecord_fn = async function(record) {
1400
- if (!__privateGet$3(this, _cache).cacheRecords)
1521
+ if (!__privateGet$4(this, _cache).cacheRecords)
1401
1522
  return;
1402
- await __privateGet$3(this, _cache).set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
1523
+ await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
1403
1524
  };
1404
1525
  _getCacheRecord = new WeakSet();
1405
1526
  getCacheRecord_fn = async function(recordId) {
1406
- if (!__privateGet$3(this, _cache).cacheRecords)
1527
+ if (!__privateGet$4(this, _cache).cacheRecords)
1407
1528
  return null;
1408
- return __privateGet$3(this, _cache).get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1529
+ return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1409
1530
  };
1410
1531
  _setCacheQuery = new WeakSet();
1411
1532
  setCacheQuery_fn = async function(query, meta, records) {
1412
- await __privateGet$3(this, _cache).set(`query_${__privateGet$3(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1533
+ await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1413
1534
  };
1414
1535
  _getCacheQuery = new WeakSet();
1415
1536
  getCacheQuery_fn = async function(query) {
1416
- const key = `query_${__privateGet$3(this, _table)}:${query.key()}`;
1417
- const result = await __privateGet$3(this, _cache).get(key);
1537
+ const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
1538
+ const result = await __privateGet$4(this, _cache).get(key);
1418
1539
  if (!result)
1419
1540
  return null;
1420
- const { cache: ttl = __privateGet$3(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1421
- if (!ttl || ttl < 0)
1422
- return result;
1541
+ const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1542
+ if (ttl < 0)
1543
+ return null;
1423
1544
  const hasExpired = result.date.getTime() + ttl < Date.now();
1424
1545
  return hasExpired ? null : result;
1425
1546
  };
1547
+ _getSchema$1 = new WeakSet();
1548
+ getSchema_fn$1 = async function() {
1549
+ if (__privateGet$4(this, _schema$1))
1550
+ return __privateGet$4(this, _schema$1);
1551
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1552
+ const { schema } = await getBranchDetails({
1553
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1554
+ ...fetchProps
1555
+ });
1556
+ __privateSet$3(this, _schema$1, schema);
1557
+ return schema;
1558
+ };
1426
1559
  const transformObjectLinks = (object) => {
1427
1560
  return Object.entries(object).reduce((acc, [key, value]) => {
1428
1561
  if (key === "xata")
@@ -1430,15 +1563,34 @@ const transformObjectLinks = (object) => {
1430
1563
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1431
1564
  }, {});
1432
1565
  };
1433
- const initObject = (db, links, table, object) => {
1566
+ const initObject = (db, schema, table, object) => {
1434
1567
  const result = {};
1435
- Object.assign(result, object);
1436
- const tableLinks = links[table] || [];
1437
- for (const link of tableLinks) {
1438
- const [field, linkTable] = link;
1439
- const value = result[field];
1440
- if (value && isObject(value)) {
1441
- result[field] = initObject(db, links, linkTable, value);
1568
+ const { xata, ...rest } = object ?? {};
1569
+ Object.assign(result, rest);
1570
+ const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
1571
+ if (!columns)
1572
+ console.error(`Table ${table} not found in schema`);
1573
+ for (const column of columns ?? []) {
1574
+ const value = result[column.name];
1575
+ switch (column.type) {
1576
+ case "datetime": {
1577
+ const date = value !== void 0 ? new Date(value) : void 0;
1578
+ if (date && isNaN(date.getTime())) {
1579
+ console.error(`Failed to parse date ${value} for field ${column.name}`);
1580
+ } else if (date) {
1581
+ result[column.name] = date;
1582
+ }
1583
+ break;
1584
+ }
1585
+ case "link": {
1586
+ const linkTable = column.link?.table;
1587
+ if (!linkTable) {
1588
+ console.error(`Failed to parse link for field ${column.name}`);
1589
+ } else if (isObject(value)) {
1590
+ result[column.name] = initObject(db, schema, linkTable, value);
1591
+ }
1592
+ break;
1593
+ }
1442
1594
  }
1443
1595
  }
1444
1596
  result.read = function() {
@@ -1450,7 +1602,10 @@ const initObject = (db, links, table, object) => {
1450
1602
  result.delete = function() {
1451
1603
  return db[table].delete(result["id"]);
1452
1604
  };
1453
- for (const prop of ["read", "update", "delete"]) {
1605
+ result.getMetadata = function() {
1606
+ return xata;
1607
+ };
1608
+ for (const prop of ["read", "update", "delete", "getMetadata"]) {
1454
1609
  Object.defineProperty(result, prop, { enumerable: false });
1455
1610
  }
1456
1611
  Object.freeze(result);
@@ -1470,7 +1625,7 @@ var __accessCheck$3 = (obj, member, msg) => {
1470
1625
  if (!member.has(obj))
1471
1626
  throw TypeError("Cannot " + msg);
1472
1627
  };
1473
- var __privateGet$2 = (obj, member, getter) => {
1628
+ var __privateGet$3 = (obj, member, getter) => {
1474
1629
  __accessCheck$3(obj, member, "read from private field");
1475
1630
  return getter ? getter.call(obj) : member.get(obj);
1476
1631
  };
@@ -1479,7 +1634,7 @@ var __privateAdd$3 = (obj, member, value) => {
1479
1634
  throw TypeError("Cannot add the same private member more than once");
1480
1635
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1481
1636
  };
1482
- var __privateSet$1 = (obj, member, value, setter) => {
1637
+ var __privateSet$2 = (obj, member, value, setter) => {
1483
1638
  __accessCheck$3(obj, member, "write to private field");
1484
1639
  setter ? setter.call(obj, value) : member.set(obj, value);
1485
1640
  return value;
@@ -1488,30 +1643,30 @@ var _map;
1488
1643
  class SimpleCache {
1489
1644
  constructor(options = {}) {
1490
1645
  __privateAdd$3(this, _map, void 0);
1491
- __privateSet$1(this, _map, /* @__PURE__ */ new Map());
1646
+ __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1492
1647
  this.capacity = options.max ?? 500;
1493
1648
  this.cacheRecords = options.cacheRecords ?? true;
1494
1649
  this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1495
1650
  }
1496
1651
  async getAll() {
1497
- return Object.fromEntries(__privateGet$2(this, _map));
1652
+ return Object.fromEntries(__privateGet$3(this, _map));
1498
1653
  }
1499
1654
  async get(key) {
1500
- return __privateGet$2(this, _map).get(key) ?? null;
1655
+ return __privateGet$3(this, _map).get(key) ?? null;
1501
1656
  }
1502
1657
  async set(key, value) {
1503
1658
  await this.delete(key);
1504
- __privateGet$2(this, _map).set(key, value);
1505
- if (__privateGet$2(this, _map).size > this.capacity) {
1506
- const leastRecentlyUsed = __privateGet$2(this, _map).keys().next().value;
1659
+ __privateGet$3(this, _map).set(key, value);
1660
+ if (__privateGet$3(this, _map).size > this.capacity) {
1661
+ const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
1507
1662
  await this.delete(leastRecentlyUsed);
1508
1663
  }
1509
1664
  }
1510
1665
  async delete(key) {
1511
- __privateGet$2(this, _map).delete(key);
1666
+ __privateGet$3(this, _map).delete(key);
1512
1667
  }
1513
1668
  async clear() {
1514
- return __privateGet$2(this, _map).clear();
1669
+ return __privateGet$3(this, _map).clear();
1515
1670
  }
1516
1671
  }
1517
1672
  _map = new WeakMap();
@@ -1539,7 +1694,7 @@ var __accessCheck$2 = (obj, member, msg) => {
1539
1694
  if (!member.has(obj))
1540
1695
  throw TypeError("Cannot " + msg);
1541
1696
  };
1542
- var __privateGet$1 = (obj, member, getter) => {
1697
+ var __privateGet$2 = (obj, member, getter) => {
1543
1698
  __accessCheck$2(obj, member, "read from private field");
1544
1699
  return getter ? getter.call(obj) : member.get(obj);
1545
1700
  };
@@ -1550,26 +1705,24 @@ var __privateAdd$2 = (obj, member, value) => {
1550
1705
  };
1551
1706
  var _tables;
1552
1707
  class SchemaPlugin extends XataPlugin {
1553
- constructor(links, tableNames) {
1708
+ constructor(tableNames) {
1554
1709
  super();
1555
- this.links = links;
1556
1710
  this.tableNames = tableNames;
1557
1711
  __privateAdd$2(this, _tables, {});
1558
1712
  }
1559
1713
  build(pluginOptions) {
1560
- const links = this.links;
1561
1714
  const db = new Proxy({}, {
1562
1715
  get: (_target, table) => {
1563
1716
  if (!isString(table))
1564
1717
  throw new Error("Invalid table name");
1565
- if (!__privateGet$1(this, _tables)[table]) {
1566
- __privateGet$1(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, links });
1718
+ if (__privateGet$2(this, _tables)[table] === void 0) {
1719
+ __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1567
1720
  }
1568
- return __privateGet$1(this, _tables)[table];
1721
+ return __privateGet$2(this, _tables)[table];
1569
1722
  }
1570
1723
  });
1571
1724
  for (const table of this.tableNames ?? []) {
1572
- db[table] = new RestRepository({ db, pluginOptions, table, links });
1725
+ db[table] = new RestRepository({ db, pluginOptions, table });
1573
1726
  }
1574
1727
  return db;
1575
1728
  }
@@ -1580,55 +1733,80 @@ var __accessCheck$1 = (obj, member, msg) => {
1580
1733
  if (!member.has(obj))
1581
1734
  throw TypeError("Cannot " + msg);
1582
1735
  };
1736
+ var __privateGet$1 = (obj, member, getter) => {
1737
+ __accessCheck$1(obj, member, "read from private field");
1738
+ return getter ? getter.call(obj) : member.get(obj);
1739
+ };
1583
1740
  var __privateAdd$1 = (obj, member, value) => {
1584
1741
  if (member.has(obj))
1585
1742
  throw TypeError("Cannot add the same private member more than once");
1586
1743
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1587
1744
  };
1745
+ var __privateSet$1 = (obj, member, value, setter) => {
1746
+ __accessCheck$1(obj, member, "write to private field");
1747
+ setter ? setter.call(obj, value) : member.set(obj, value);
1748
+ return value;
1749
+ };
1588
1750
  var __privateMethod$1 = (obj, member, method) => {
1589
1751
  __accessCheck$1(obj, member, "access private method");
1590
1752
  return method;
1591
1753
  };
1592
- var _search, search_fn;
1754
+ var _schema, _search, search_fn, _getSchema, getSchema_fn;
1593
1755
  class SearchPlugin extends XataPlugin {
1594
- constructor(db, links) {
1756
+ constructor(db) {
1595
1757
  super();
1596
1758
  this.db = db;
1597
- this.links = links;
1598
1759
  __privateAdd$1(this, _search);
1760
+ __privateAdd$1(this, _getSchema);
1761
+ __privateAdd$1(this, _schema, void 0);
1599
1762
  }
1600
1763
  build({ getFetchProps }) {
1601
1764
  return {
1602
1765
  all: async (query, options = {}) => {
1603
1766
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1767
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1604
1768
  return records.map((record) => {
1605
1769
  const { table = "orphan" } = record.xata;
1606
- return { table, record: initObject(this.db, this.links, table, record) };
1770
+ return { table, record: initObject(this.db, schema, table, record) };
1607
1771
  });
1608
1772
  },
1609
1773
  byTable: async (query, options = {}) => {
1610
1774
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1775
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1611
1776
  return records.reduce((acc, record) => {
1612
1777
  const { table = "orphan" } = record.xata;
1613
1778
  const items = acc[table] ?? [];
1614
- const item = initObject(this.db, this.links, table, record);
1779
+ const item = initObject(this.db, schema, table, record);
1615
1780
  return { ...acc, [table]: [...items, item] };
1616
1781
  }, {});
1617
1782
  }
1618
1783
  };
1619
1784
  }
1620
1785
  }
1786
+ _schema = new WeakMap();
1621
1787
  _search = new WeakSet();
1622
1788
  search_fn = async function(query, options, getFetchProps) {
1623
1789
  const fetchProps = await getFetchProps();
1624
- const { tables, fuzziness } = options ?? {};
1790
+ const { tables, fuzziness, highlight } = options ?? {};
1625
1791
  const { records } = await searchBranch({
1626
1792
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1627
- body: { tables, query, fuzziness },
1793
+ body: { tables, query, fuzziness, highlight },
1628
1794
  ...fetchProps
1629
1795
  });
1630
1796
  return records;
1631
1797
  };
1798
+ _getSchema = new WeakSet();
1799
+ getSchema_fn = async function(getFetchProps) {
1800
+ if (__privateGet$1(this, _schema))
1801
+ return __privateGet$1(this, _schema);
1802
+ const fetchProps = await getFetchProps();
1803
+ const { schema } = await getBranchDetails({
1804
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1805
+ ...fetchProps
1806
+ });
1807
+ __privateSet$1(this, _schema, schema);
1808
+ return schema;
1809
+ };
1632
1810
 
1633
1811
  const isBranchStrategyBuilder = (strategy) => {
1634
1812
  return typeof strategy === "function";
@@ -1640,30 +1818,39 @@ const envBranchNames = [
1640
1818
  "CF_PAGES_BRANCH",
1641
1819
  "BRANCH"
1642
1820
  ];
1643
- const defaultBranch = "main";
1644
1821
  async function getCurrentBranchName(options) {
1645
- const env = await getBranchByEnvVariable();
1646
- if (env)
1647
- return env;
1648
- const branch = await getGitBranch();
1649
- if (!branch)
1650
- return defaultBranch;
1651
- const details = await getDatabaseBranch(branch, options);
1652
- if (details)
1653
- return branch;
1654
- return defaultBranch;
1822
+ const env = getBranchByEnvVariable();
1823
+ if (env) {
1824
+ const details = await getDatabaseBranch(env, options);
1825
+ if (details)
1826
+ return env;
1827
+ console.warn(`Branch ${env} not found in Xata. Ignoring...`);
1828
+ }
1829
+ const gitBranch = await getGitBranch();
1830
+ return resolveXataBranch(gitBranch, options);
1655
1831
  }
1656
1832
  async function getCurrentBranchDetails(options) {
1657
- const env = await getBranchByEnvVariable();
1658
- if (env)
1659
- return getDatabaseBranch(env, options);
1660
- const branch = await getGitBranch();
1661
- if (!branch)
1662
- return getDatabaseBranch(defaultBranch, options);
1663
- const details = await getDatabaseBranch(branch, options);
1664
- if (details)
1665
- return details;
1666
- return getDatabaseBranch(defaultBranch, options);
1833
+ const branch = await getCurrentBranchName(options);
1834
+ return getDatabaseBranch(branch, options);
1835
+ }
1836
+ async function resolveXataBranch(gitBranch, options) {
1837
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1838
+ const apiKey = options?.apiKey || getAPIKey();
1839
+ if (!databaseURL)
1840
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1841
+ if (!apiKey)
1842
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1843
+ const [protocol, , host, , dbName] = databaseURL.split("/");
1844
+ const [workspace] = host.split(".");
1845
+ const { branch } = await resolveBranch({
1846
+ apiKey,
1847
+ apiUrl: databaseURL,
1848
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1849
+ workspacesApiUrl: `${protocol}//${host}`,
1850
+ pathParams: { dbName, workspace },
1851
+ queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
1852
+ });
1853
+ return branch;
1667
1854
  }
1668
1855
  async function getDatabaseBranch(branch, options) {
1669
1856
  const databaseURL = options?.databaseURL || getDatabaseURL();
@@ -1737,7 +1924,7 @@ var __privateMethod = (obj, member, method) => {
1737
1924
  const buildClient = (plugins) => {
1738
1925
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1739
1926
  return _a = class {
1740
- constructor(options = {}, links, tables) {
1927
+ constructor(options = {}, tables) {
1741
1928
  __privateAdd(this, _parseOptions);
1742
1929
  __privateAdd(this, _getFetchProps);
1743
1930
  __privateAdd(this, _evaluateBranch);
@@ -1747,12 +1934,12 @@ const buildClient = (plugins) => {
1747
1934
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1748
1935
  cache: safeOptions.cache
1749
1936
  };
1750
- const db = new SchemaPlugin(links, tables).build(pluginOptions);
1751
- const search = new SearchPlugin(db, links ?? {}).build(pluginOptions);
1937
+ const db = new SchemaPlugin(tables).build(pluginOptions);
1938
+ const search = new SearchPlugin(db).build(pluginOptions);
1752
1939
  this.db = db;
1753
1940
  this.search = search;
1754
1941
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1755
- if (!namespace)
1942
+ if (namespace === void 0)
1756
1943
  continue;
1757
1944
  const result = namespace.build(pluginOptions);
1758
1945
  if (result instanceof Promise) {
@@ -1769,7 +1956,7 @@ const buildClient = (plugins) => {
1769
1956
  const databaseURL = options?.databaseURL || getDatabaseURL();
1770
1957
  const apiKey = options?.apiKey || getAPIKey();
1771
1958
  const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1772
- const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1959
+ const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1773
1960
  if (!databaseURL || !apiKey) {
1774
1961
  throw new Error("Options databaseURL and apiKey are required");
1775
1962
  }
@@ -1796,7 +1983,7 @@ const buildClient = (plugins) => {
1796
1983
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
1797
1984
  if (__privateGet(this, _branch))
1798
1985
  return __privateGet(this, _branch);
1799
- if (!param)
1986
+ if (param === void 0)
1800
1987
  return void 0;
1801
1988
  const strategies = Array.isArray(param) ? [...param] : [param];
1802
1989
  const evaluateBranch = async (strategy) => {
@@ -1829,6 +2016,7 @@ exports.PAGINATION_MAX_OFFSET = PAGINATION_MAX_OFFSET;
1829
2016
  exports.PAGINATION_MAX_SIZE = PAGINATION_MAX_SIZE;
1830
2017
  exports.Page = Page;
1831
2018
  exports.Query = Query;
2019
+ exports.RecordArray = RecordArray;
1832
2020
  exports.Repository = Repository;
1833
2021
  exports.RestRepository = RestRepository;
1834
2022
  exports.SchemaPlugin = SchemaPlugin;
@@ -1839,11 +2027,11 @@ exports.XataApiPlugin = XataApiPlugin;
1839
2027
  exports.XataError = XataError;
1840
2028
  exports.XataPlugin = XataPlugin;
1841
2029
  exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
2030
+ exports.addGitBranchesEntry = addGitBranchesEntry;
1842
2031
  exports.addTableColumn = addTableColumn;
1843
2032
  exports.buildClient = buildClient;
1844
2033
  exports.bulkInsertTableRecords = bulkInsertTableRecords;
1845
2034
  exports.cancelWorkspaceMemberInvite = cancelWorkspaceMemberInvite;
1846
- exports.compareBranchSchema = compareBranchSchema;
1847
2035
  exports.contains = contains;
1848
2036
  exports.createBranch = createBranch;
1849
2037
  exports.createDatabase = createDatabase;
@@ -1868,13 +2056,13 @@ exports.getBranchList = getBranchList;
1868
2056
  exports.getBranchMetadata = getBranchMetadata;
1869
2057
  exports.getBranchMigrationHistory = getBranchMigrationHistory;
1870
2058
  exports.getBranchMigrationPlan = getBranchMigrationPlan;
1871
- exports.getBranchSchemaHistory = getBranchSchemaHistory;
1872
2059
  exports.getBranchStats = getBranchStats;
1873
2060
  exports.getColumn = getColumn;
1874
2061
  exports.getCurrentBranchDetails = getCurrentBranchDetails;
1875
2062
  exports.getCurrentBranchName = getCurrentBranchName;
1876
2063
  exports.getDatabaseList = getDatabaseList;
1877
2064
  exports.getDatabaseURL = getDatabaseURL;
2065
+ exports.getGitBranchesMapping = getGitBranchesMapping;
1878
2066
  exports.getRecord = getRecord;
1879
2067
  exports.getTableColumns = getTableColumns;
1880
2068
  exports.getTableSchema = getTableSchema;
@@ -1893,6 +2081,7 @@ exports.insertRecord = insertRecord;
1893
2081
  exports.insertRecordWithID = insertRecordWithID;
1894
2082
  exports.inviteWorkspaceMember = inviteWorkspaceMember;
1895
2083
  exports.is = is;
2084
+ exports.isCursorPaginationOptions = isCursorPaginationOptions;
1896
2085
  exports.isIdentifiable = isIdentifiable;
1897
2086
  exports.isNot = isNot;
1898
2087
  exports.isXataRecord = isXataRecord;
@@ -1903,13 +2092,15 @@ exports.notExists = notExists;
1903
2092
  exports.operationsByTag = operationsByTag;
1904
2093
  exports.pattern = pattern;
1905
2094
  exports.queryTable = queryTable;
2095
+ exports.removeGitBranchesEntry = removeGitBranchesEntry;
1906
2096
  exports.removeWorkspaceMember = removeWorkspaceMember;
1907
2097
  exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
2098
+ exports.resolveBranch = resolveBranch;
1908
2099
  exports.searchBranch = searchBranch;
2100
+ exports.searchTable = searchTable;
1909
2101
  exports.setTableSchema = setTableSchema;
1910
2102
  exports.startsWith = startsWith;
1911
2103
  exports.updateBranchMetadata = updateBranchMetadata;
1912
- exports.updateBranchSchema = updateBranchSchema;
1913
2104
  exports.updateColumn = updateColumn;
1914
2105
  exports.updateRecordWithID = updateRecordWithID;
1915
2106
  exports.updateTable = updateTable;