@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.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,7 +941,7 @@ 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;
@@ -897,21 +950,21 @@ var _query;
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
955
  this.records = 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;
@@ -922,12 +975,15 @@ const PAGINATION_MAX_SIZE = 200;
922
975
  const PAGINATION_DEFAULT_SIZE = 200;
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
+ }
925
981
 
926
982
  var __accessCheck$5 = (obj, member, msg) => {
927
983
  if (!member.has(obj))
928
984
  throw TypeError("Cannot " + msg);
929
985
  };
930
- var __privateGet$4 = (obj, member, getter) => {
986
+ var __privateGet$5 = (obj, member, getter) => {
931
987
  __accessCheck$5(obj, member, "read from private field");
932
988
  return getter ? getter.call(obj) : member.get(obj);
933
989
  };
@@ -936,34 +992,35 @@ var __privateAdd$5 = (obj, member, value) => {
936
992
  throw TypeError("Cannot add the same private member more than once");
937
993
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
938
994
  };
939
- var __privateSet$3 = (obj, member, value, setter) => {
995
+ var __privateSet$4 = (obj, member, value, setter) => {
940
996
  __accessCheck$5(obj, member, "write to private field");
941
997
  setter ? setter.call(obj, value) : member.set(obj, value);
942
998
  return value;
943
999
  };
944
1000
  var _table$1, _repository, _data;
945
1001
  const _Query = class {
946
- constructor(repository, table, data, parent) {
1002
+ constructor(repository, table, data, rawParent) {
947
1003
  __privateAdd$5(this, _table$1, void 0);
948
1004
  __privateAdd$5(this, _repository, void 0);
949
1005
  __privateAdd$5(this, _data, { filter: {} });
950
1006
  this.meta = { page: { cursor: "start", more: true } };
951
1007
  this.records = [];
952
- __privateSet$3(this, _table$1, table);
1008
+ __privateSet$4(this, _table$1, table);
953
1009
  if (repository) {
954
- __privateSet$3(this, _repository, repository);
1010
+ __privateSet$4(this, _repository, repository);
955
1011
  } else {
956
- __privateSet$3(this, _repository, this);
1012
+ __privateSet$4(this, _repository, this);
957
1013
  }
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;
1014
+ const parent = cleanParent(data, rawParent);
1015
+ __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
1016
+ __privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
1017
+ __privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
1018
+ __privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
1019
+ __privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
1020
+ __privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
1021
+ __privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
1022
+ __privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
1023
+ __privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
967
1024
  this.any = this.any.bind(this);
968
1025
  this.all = this.all.bind(this);
969
1026
  this.not = this.not.bind(this);
@@ -974,83 +1031,85 @@ const _Query = class {
974
1031
  Object.defineProperty(this, "repository", { enumerable: false });
975
1032
  }
976
1033
  getQueryOptions() {
977
- return __privateGet$4(this, _data);
1034
+ return __privateGet$5(this, _data);
978
1035
  }
979
1036
  key() {
980
- const { columns = [], filter = {}, sort = [], page = {} } = __privateGet$4(this, _data);
981
- const key = JSON.stringify({ columns, filter, sort, page });
1037
+ const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
1038
+ const key = JSON.stringify({ columns, filter, sort, pagination });
982
1039
  return toBase64(key);
983
1040
  }
984
1041
  any(...queries) {
985
1042
  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));
1043
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
987
1044
  }
988
1045
  all(...queries) {
989
1046
  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));
1047
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
991
1048
  }
992
1049
  not(...queries) {
993
1050
  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));
1051
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
995
1052
  }
996
1053
  none(...queries) {
997
1054
  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));
1055
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
999
1056
  }
1000
1057
  filter(a, b) {
1001
1058
  if (arguments.length === 1) {
1002
1059
  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));
1060
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
1061
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1005
1062
  } 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));
1063
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1064
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1008
1065
  }
1009
1066
  }
1010
1067
  sort(column, direction) {
1011
- const originalSort = [__privateGet$4(this, _data).sort ?? []].flat();
1068
+ const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
1012
1069
  const sort = [...originalSort, { column, direction }];
1013
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { sort }, __privateGet$4(this, _data));
1070
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
1014
1071
  }
1015
1072
  select(columns) {
1016
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { columns }, __privateGet$4(this, _data));
1073
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
1017
1074
  }
1018
1075
  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);
1076
+ const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
1077
+ return __privateGet$5(this, _repository).query(query);
1021
1078
  }
1022
1079
  async *[Symbol.asyncIterator]() {
1023
- for await (const [record] of this.getIterator(1)) {
1080
+ for await (const [record] of this.getIterator({ batchSize: 1 })) {
1024
1081
  yield record;
1025
1082
  }
1026
1083
  }
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;
1084
+ async *getIterator(options = {}) {
1085
+ const { batchSize = 1 } = options;
1086
+ let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
1087
+ let more = page.hasNextPage();
1088
+ yield page.records;
1089
+ while (more) {
1090
+ page = await page.nextPage();
1091
+ more = page.hasNextPage();
1092
+ yield page.records;
1035
1093
  }
1036
1094
  }
1037
1095
  async getMany(options = {}) {
1038
1096
  const { records } = await this.getPaginated(options);
1039
1097
  return records;
1040
1098
  }
1041
- async getAll(chunk = PAGINATION_MAX_SIZE, options = {}) {
1099
+ async getAll(options = {}) {
1100
+ const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
1042
1101
  const results = [];
1043
- for await (const page of this.getIterator(chunk, options)) {
1102
+ for await (const page of this.getIterator({ ...rest, batchSize })) {
1044
1103
  results.push(...page);
1045
1104
  }
1046
1105
  return results;
1047
1106
  }
1048
1107
  async getFirst(options = {}) {
1049
- const records = await this.getMany({ ...options, page: { size: 1 } });
1050
- return records[0] || null;
1108
+ const records = await this.getMany({ ...options, pagination: { size: 1 } });
1109
+ return records[0] ?? null;
1051
1110
  }
1052
1111
  cache(ttl) {
1053
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { cache: ttl }, __privateGet$4(this, _data));
1112
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
1054
1113
  }
1055
1114
  nextPage(size, offset) {
1056
1115
  return this.firstPage(size, offset);
@@ -1059,10 +1118,10 @@ const _Query = class {
1059
1118
  return this.firstPage(size, offset);
1060
1119
  }
1061
1120
  firstPage(size, offset) {
1062
- return this.getPaginated({ page: { size, offset } });
1121
+ return this.getPaginated({ pagination: { size, offset } });
1063
1122
  }
1064
1123
  lastPage(size, offset) {
1065
- return this.getPaginated({ page: { size, offset, before: "end" } });
1124
+ return this.getPaginated({ pagination: { size, offset, before: "end" } });
1066
1125
  }
1067
1126
  hasNextPage() {
1068
1127
  return this.meta.page.more;
@@ -1072,12 +1131,20 @@ let Query = _Query;
1072
1131
  _table$1 = new WeakMap();
1073
1132
  _repository = new WeakMap();
1074
1133
  _data = new WeakMap();
1134
+ function cleanParent(data, parent) {
1135
+ if (isCursorPaginationOptions(data.pagination)) {
1136
+ return { ...parent, sorting: void 0, filter: void 0 };
1137
+ }
1138
+ return parent;
1139
+ }
1075
1140
 
1076
1141
  function isIdentifiable(x) {
1077
1142
  return isObject(x) && isString(x?.id);
1078
1143
  }
1079
1144
  function isXataRecord(x) {
1080
- return isIdentifiable(x) && typeof x?.xata === "object" && typeof x?.xata?.version === "number";
1145
+ const record = x;
1146
+ const metadata = record?.getMetadata();
1147
+ return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
1081
1148
  }
1082
1149
 
1083
1150
  function isSortFilterString(value) {
@@ -1107,7 +1174,7 @@ var __accessCheck$4 = (obj, member, msg) => {
1107
1174
  if (!member.has(obj))
1108
1175
  throw TypeError("Cannot " + msg);
1109
1176
  };
1110
- var __privateGet$3 = (obj, member, getter) => {
1177
+ var __privateGet$4 = (obj, member, getter) => {
1111
1178
  __accessCheck$4(obj, member, "read from private field");
1112
1179
  return getter ? getter.call(obj) : member.get(obj);
1113
1180
  };
@@ -1116,7 +1183,7 @@ var __privateAdd$4 = (obj, member, value) => {
1116
1183
  throw TypeError("Cannot add the same private member more than once");
1117
1184
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1118
1185
  };
1119
- var __privateSet$2 = (obj, member, value, setter) => {
1186
+ var __privateSet$3 = (obj, member, value, setter) => {
1120
1187
  __accessCheck$4(obj, member, "write to private field");
1121
1188
  setter ? setter.call(obj, value) : member.set(obj, value);
1122
1189
  return value;
@@ -1125,7 +1192,7 @@ var __privateMethod$2 = (obj, member, method) => {
1125
1192
  __accessCheck$4(obj, member, "access private method");
1126
1193
  return method;
1127
1194
  };
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;
1195
+ 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
1196
  class Repository extends Query {
1130
1197
  }
1131
1198
  class RestRepository extends Query {
@@ -1142,18 +1209,20 @@ class RestRepository extends Query {
1142
1209
  __privateAdd$4(this, _getCacheRecord);
1143
1210
  __privateAdd$4(this, _setCacheQuery);
1144
1211
  __privateAdd$4(this, _getCacheQuery);
1212
+ __privateAdd$4(this, _getSchema$1);
1145
1213
  __privateAdd$4(this, _table, void 0);
1146
- __privateAdd$4(this, _links, void 0);
1147
1214
  __privateAdd$4(this, _getFetchProps, void 0);
1148
1215
  __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);
1216
+ __privateAdd$4(this, _schema$1, void 0);
1217
+ __privateSet$3(this, _table, options.table);
1218
+ __privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
1152
1219
  this.db = options.db;
1153
- __privateSet$2(this, _cache, options.pluginOptions.cache);
1220
+ __privateSet$3(this, _cache, options.pluginOptions.cache);
1154
1221
  }
1155
1222
  async create(a, b) {
1156
1223
  if (Array.isArray(a)) {
1224
+ if (a.length === 0)
1225
+ return [];
1157
1226
  const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1158
1227
  await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1159
1228
  return records;
@@ -1179,26 +1248,36 @@ class RestRepository extends Query {
1179
1248
  }
1180
1249
  throw new Error("Invalid arguments for create method");
1181
1250
  }
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;
1251
+ async read(a) {
1252
+ if (Array.isArray(a)) {
1253
+ if (a.length === 0)
1254
+ return [];
1255
+ return this.getAll({ filter: { id: { $any: a } } });
1256
+ }
1257
+ if (isString(a)) {
1258
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, a);
1259
+ if (cacheRecord)
1260
+ return cacheRecord;
1261
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1262
+ try {
1263
+ const response = await getRecord({
1264
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: a },
1265
+ ...fetchProps
1266
+ });
1267
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1268
+ return initObject(this.db, schema, __privateGet$4(this, _table), response);
1269
+ } catch (e) {
1270
+ if (isObject(e) && e.status === 404) {
1271
+ return null;
1272
+ }
1273
+ throw e;
1196
1274
  }
1197
- throw e;
1198
1275
  }
1199
1276
  }
1200
1277
  async update(a, b) {
1201
1278
  if (Array.isArray(a)) {
1279
+ if (a.length === 0)
1280
+ return [];
1202
1281
  if (a.length > 100) {
1203
1282
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1204
1283
  }
@@ -1220,6 +1299,8 @@ class RestRepository extends Query {
1220
1299
  }
1221
1300
  async createOrUpdate(a, b) {
1222
1301
  if (Array.isArray(a)) {
1302
+ if (a.length === 0)
1303
+ return [];
1223
1304
  if (a.length > 100) {
1224
1305
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1225
1306
  }
@@ -1241,6 +1322,8 @@ class RestRepository extends Query {
1241
1322
  }
1242
1323
  async delete(a) {
1243
1324
  if (Array.isArray(a)) {
1325
+ if (a.length === 0)
1326
+ return;
1244
1327
  if (a.length > 100) {
1245
1328
  console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1246
1329
  }
@@ -1260,13 +1343,19 @@ class RestRepository extends Query {
1260
1343
  throw new Error("Invalid arguments for delete method");
1261
1344
  }
1262
1345
  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 },
1346
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1347
+ const { records } = await searchTable({
1348
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1349
+ body: {
1350
+ query,
1351
+ fuzziness: options.fuzziness,
1352
+ highlight: options.highlight,
1353
+ filter: options.filter
1354
+ },
1267
1355
  ...fetchProps
1268
1356
  });
1269
- return records.map((item) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), item));
1357
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1358
+ return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
1270
1359
  }
1271
1360
  async query(query) {
1272
1361
  const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
@@ -1275,34 +1364,35 @@ class RestRepository extends Query {
1275
1364
  const data = query.getQueryOptions();
1276
1365
  const body = {
1277
1366
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1278
- sort: data.sort ? buildSortFilter(data.sort) : void 0,
1279
- page: data.page,
1367
+ sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1368
+ page: data.pagination,
1280
1369
  columns: data.columns
1281
1370
  };
1282
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1371
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1283
1372
  const { meta, records: objects } = await queryTable({
1284
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1373
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1285
1374
  body,
1286
1375
  ...fetchProps
1287
1376
  });
1288
- const records = objects.map((record) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), record));
1377
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1378
+ const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
1289
1379
  await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1290
1380
  return new Page(query, meta, records);
1291
1381
  }
1292
1382
  }
1293
1383
  _table = new WeakMap();
1294
- _links = new WeakMap();
1295
1384
  _getFetchProps = new WeakMap();
1296
1385
  _cache = new WeakMap();
1386
+ _schema$1 = new WeakMap();
1297
1387
  _insertRecordWithoutId = new WeakSet();
1298
1388
  insertRecordWithoutId_fn = async function(object) {
1299
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1389
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1300
1390
  const record = transformObjectLinks(object);
1301
1391
  const response = await insertRecord({
1302
1392
  pathParams: {
1303
1393
  workspace: "{workspaceId}",
1304
1394
  dbBranchName: "{dbBranch}",
1305
- tableName: __privateGet$3(this, _table)
1395
+ tableName: __privateGet$4(this, _table)
1306
1396
  },
1307
1397
  body: record,
1308
1398
  ...fetchProps
@@ -1315,13 +1405,13 @@ insertRecordWithoutId_fn = async function(object) {
1315
1405
  };
1316
1406
  _insertRecordWithId = new WeakSet();
1317
1407
  insertRecordWithId_fn = async function(recordId, object) {
1318
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1408
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1319
1409
  const record = transformObjectLinks(object);
1320
1410
  const response = await insertRecordWithID({
1321
1411
  pathParams: {
1322
1412
  workspace: "{workspaceId}",
1323
1413
  dbBranchName: "{dbBranch}",
1324
- tableName: __privateGet$3(this, _table),
1414
+ tableName: __privateGet$4(this, _table),
1325
1415
  recordId
1326
1416
  },
1327
1417
  body: record,
@@ -1336,10 +1426,10 @@ insertRecordWithId_fn = async function(recordId, object) {
1336
1426
  };
1337
1427
  _bulkInsertTableRecords = new WeakSet();
1338
1428
  bulkInsertTableRecords_fn = async function(objects) {
1339
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1429
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1340
1430
  const records = objects.map((object) => transformObjectLinks(object));
1341
1431
  const response = await bulkInsertTableRecords({
1342
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1432
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1343
1433
  body: { records },
1344
1434
  ...fetchProps
1345
1435
  });
@@ -1351,10 +1441,10 @@ bulkInsertTableRecords_fn = async function(objects) {
1351
1441
  };
1352
1442
  _updateRecordWithID = new WeakSet();
1353
1443
  updateRecordWithID_fn = async function(recordId, object) {
1354
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1444
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1355
1445
  const record = transformObjectLinks(object);
1356
1446
  const response = await updateRecordWithID({
1357
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1447
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1358
1448
  body: record,
1359
1449
  ...fetchProps
1360
1450
  });
@@ -1365,9 +1455,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1365
1455
  };
1366
1456
  _upsertRecordWithID = new WeakSet();
1367
1457
  upsertRecordWithID_fn = async function(recordId, object) {
1368
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1458
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1369
1459
  const response = await upsertRecordWithID({
1370
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1460
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1371
1461
  body: object,
1372
1462
  ...fetchProps
1373
1463
  });
@@ -1378,51 +1468,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
1378
1468
  };
1379
1469
  _deleteRecord = new WeakSet();
1380
1470
  deleteRecord_fn = async function(recordId) {
1381
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1471
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1382
1472
  await deleteRecord({
1383
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1473
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1384
1474
  ...fetchProps
1385
1475
  });
1386
1476
  };
1387
1477
  _invalidateCache = new WeakSet();
1388
1478
  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();
1479
+ await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1480
+ const cacheItems = await __privateGet$4(this, _cache).getAll();
1391
1481
  const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1392
1482
  for (const [key, value] of queries) {
1393
1483
  const ids = getIds(value);
1394
1484
  if (ids.includes(recordId))
1395
- await __privateGet$3(this, _cache).delete(key);
1485
+ await __privateGet$4(this, _cache).delete(key);
1396
1486
  }
1397
1487
  };
1398
1488
  _setCacheRecord = new WeakSet();
1399
1489
  setCacheRecord_fn = async function(record) {
1400
- if (!__privateGet$3(this, _cache).cacheRecords)
1490
+ if (!__privateGet$4(this, _cache).cacheRecords)
1401
1491
  return;
1402
- await __privateGet$3(this, _cache).set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
1492
+ await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
1403
1493
  };
1404
1494
  _getCacheRecord = new WeakSet();
1405
1495
  getCacheRecord_fn = async function(recordId) {
1406
- if (!__privateGet$3(this, _cache).cacheRecords)
1496
+ if (!__privateGet$4(this, _cache).cacheRecords)
1407
1497
  return null;
1408
- return __privateGet$3(this, _cache).get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1498
+ return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1409
1499
  };
1410
1500
  _setCacheQuery = new WeakSet();
1411
1501
  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 });
1502
+ await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1413
1503
  };
1414
1504
  _getCacheQuery = new WeakSet();
1415
1505
  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);
1506
+ const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
1507
+ const result = await __privateGet$4(this, _cache).get(key);
1418
1508
  if (!result)
1419
1509
  return null;
1420
- const { cache: ttl = __privateGet$3(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1421
- if (!ttl || ttl < 0)
1422
- return result;
1510
+ const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1511
+ if (ttl < 0)
1512
+ return null;
1423
1513
  const hasExpired = result.date.getTime() + ttl < Date.now();
1424
1514
  return hasExpired ? null : result;
1425
1515
  };
1516
+ _getSchema$1 = new WeakSet();
1517
+ getSchema_fn$1 = async function() {
1518
+ if (__privateGet$4(this, _schema$1))
1519
+ return __privateGet$4(this, _schema$1);
1520
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1521
+ const { schema } = await getBranchDetails({
1522
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1523
+ ...fetchProps
1524
+ });
1525
+ __privateSet$3(this, _schema$1, schema);
1526
+ return schema;
1527
+ };
1426
1528
  const transformObjectLinks = (object) => {
1427
1529
  return Object.entries(object).reduce((acc, [key, value]) => {
1428
1530
  if (key === "xata")
@@ -1430,15 +1532,34 @@ const transformObjectLinks = (object) => {
1430
1532
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1431
1533
  }, {});
1432
1534
  };
1433
- const initObject = (db, links, table, object) => {
1535
+ const initObject = (db, schema, table, object) => {
1434
1536
  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);
1537
+ const { xata, ...rest } = object ?? {};
1538
+ Object.assign(result, rest);
1539
+ const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
1540
+ if (!columns)
1541
+ console.error(`Table ${table} not found in schema`);
1542
+ for (const column of columns ?? []) {
1543
+ const value = result[column.name];
1544
+ switch (column.type) {
1545
+ case "datetime": {
1546
+ const date = value !== void 0 ? new Date(value) : void 0;
1547
+ if (date && isNaN(date.getTime())) {
1548
+ console.error(`Failed to parse date ${value} for field ${column.name}`);
1549
+ } else if (date) {
1550
+ result[column.name] = date;
1551
+ }
1552
+ break;
1553
+ }
1554
+ case "link": {
1555
+ const linkTable = column.link?.table;
1556
+ if (!linkTable) {
1557
+ console.error(`Failed to parse link for field ${column.name}`);
1558
+ } else if (isObject(value)) {
1559
+ result[column.name] = initObject(db, schema, linkTable, value);
1560
+ }
1561
+ break;
1562
+ }
1442
1563
  }
1443
1564
  }
1444
1565
  result.read = function() {
@@ -1450,7 +1571,10 @@ const initObject = (db, links, table, object) => {
1450
1571
  result.delete = function() {
1451
1572
  return db[table].delete(result["id"]);
1452
1573
  };
1453
- for (const prop of ["read", "update", "delete"]) {
1574
+ result.getMetadata = function() {
1575
+ return xata;
1576
+ };
1577
+ for (const prop of ["read", "update", "delete", "getMetadata"]) {
1454
1578
  Object.defineProperty(result, prop, { enumerable: false });
1455
1579
  }
1456
1580
  Object.freeze(result);
@@ -1470,7 +1594,7 @@ var __accessCheck$3 = (obj, member, msg) => {
1470
1594
  if (!member.has(obj))
1471
1595
  throw TypeError("Cannot " + msg);
1472
1596
  };
1473
- var __privateGet$2 = (obj, member, getter) => {
1597
+ var __privateGet$3 = (obj, member, getter) => {
1474
1598
  __accessCheck$3(obj, member, "read from private field");
1475
1599
  return getter ? getter.call(obj) : member.get(obj);
1476
1600
  };
@@ -1479,7 +1603,7 @@ var __privateAdd$3 = (obj, member, value) => {
1479
1603
  throw TypeError("Cannot add the same private member more than once");
1480
1604
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1481
1605
  };
1482
- var __privateSet$1 = (obj, member, value, setter) => {
1606
+ var __privateSet$2 = (obj, member, value, setter) => {
1483
1607
  __accessCheck$3(obj, member, "write to private field");
1484
1608
  setter ? setter.call(obj, value) : member.set(obj, value);
1485
1609
  return value;
@@ -1488,30 +1612,30 @@ var _map;
1488
1612
  class SimpleCache {
1489
1613
  constructor(options = {}) {
1490
1614
  __privateAdd$3(this, _map, void 0);
1491
- __privateSet$1(this, _map, /* @__PURE__ */ new Map());
1615
+ __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1492
1616
  this.capacity = options.max ?? 500;
1493
1617
  this.cacheRecords = options.cacheRecords ?? true;
1494
1618
  this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1495
1619
  }
1496
1620
  async getAll() {
1497
- return Object.fromEntries(__privateGet$2(this, _map));
1621
+ return Object.fromEntries(__privateGet$3(this, _map));
1498
1622
  }
1499
1623
  async get(key) {
1500
- return __privateGet$2(this, _map).get(key) ?? null;
1624
+ return __privateGet$3(this, _map).get(key) ?? null;
1501
1625
  }
1502
1626
  async set(key, value) {
1503
1627
  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;
1628
+ __privateGet$3(this, _map).set(key, value);
1629
+ if (__privateGet$3(this, _map).size > this.capacity) {
1630
+ const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
1507
1631
  await this.delete(leastRecentlyUsed);
1508
1632
  }
1509
1633
  }
1510
1634
  async delete(key) {
1511
- __privateGet$2(this, _map).delete(key);
1635
+ __privateGet$3(this, _map).delete(key);
1512
1636
  }
1513
1637
  async clear() {
1514
- return __privateGet$2(this, _map).clear();
1638
+ return __privateGet$3(this, _map).clear();
1515
1639
  }
1516
1640
  }
1517
1641
  _map = new WeakMap();
@@ -1539,7 +1663,7 @@ var __accessCheck$2 = (obj, member, msg) => {
1539
1663
  if (!member.has(obj))
1540
1664
  throw TypeError("Cannot " + msg);
1541
1665
  };
1542
- var __privateGet$1 = (obj, member, getter) => {
1666
+ var __privateGet$2 = (obj, member, getter) => {
1543
1667
  __accessCheck$2(obj, member, "read from private field");
1544
1668
  return getter ? getter.call(obj) : member.get(obj);
1545
1669
  };
@@ -1550,26 +1674,24 @@ var __privateAdd$2 = (obj, member, value) => {
1550
1674
  };
1551
1675
  var _tables;
1552
1676
  class SchemaPlugin extends XataPlugin {
1553
- constructor(links, tableNames) {
1677
+ constructor(tableNames) {
1554
1678
  super();
1555
- this.links = links;
1556
1679
  this.tableNames = tableNames;
1557
1680
  __privateAdd$2(this, _tables, {});
1558
1681
  }
1559
1682
  build(pluginOptions) {
1560
- const links = this.links;
1561
1683
  const db = new Proxy({}, {
1562
1684
  get: (_target, table) => {
1563
1685
  if (!isString(table))
1564
1686
  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 });
1687
+ if (__privateGet$2(this, _tables)[table] === void 0) {
1688
+ __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1567
1689
  }
1568
- return __privateGet$1(this, _tables)[table];
1690
+ return __privateGet$2(this, _tables)[table];
1569
1691
  }
1570
1692
  });
1571
1693
  for (const table of this.tableNames ?? []) {
1572
- db[table] = new RestRepository({ db, pluginOptions, table, links });
1694
+ db[table] = new RestRepository({ db, pluginOptions, table });
1573
1695
  }
1574
1696
  return db;
1575
1697
  }
@@ -1580,55 +1702,80 @@ var __accessCheck$1 = (obj, member, msg) => {
1580
1702
  if (!member.has(obj))
1581
1703
  throw TypeError("Cannot " + msg);
1582
1704
  };
1705
+ var __privateGet$1 = (obj, member, getter) => {
1706
+ __accessCheck$1(obj, member, "read from private field");
1707
+ return getter ? getter.call(obj) : member.get(obj);
1708
+ };
1583
1709
  var __privateAdd$1 = (obj, member, value) => {
1584
1710
  if (member.has(obj))
1585
1711
  throw TypeError("Cannot add the same private member more than once");
1586
1712
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1587
1713
  };
1714
+ var __privateSet$1 = (obj, member, value, setter) => {
1715
+ __accessCheck$1(obj, member, "write to private field");
1716
+ setter ? setter.call(obj, value) : member.set(obj, value);
1717
+ return value;
1718
+ };
1588
1719
  var __privateMethod$1 = (obj, member, method) => {
1589
1720
  __accessCheck$1(obj, member, "access private method");
1590
1721
  return method;
1591
1722
  };
1592
- var _search, search_fn;
1723
+ var _schema, _search, search_fn, _getSchema, getSchema_fn;
1593
1724
  class SearchPlugin extends XataPlugin {
1594
- constructor(db, links) {
1725
+ constructor(db) {
1595
1726
  super();
1596
1727
  this.db = db;
1597
- this.links = links;
1598
1728
  __privateAdd$1(this, _search);
1729
+ __privateAdd$1(this, _getSchema);
1730
+ __privateAdd$1(this, _schema, void 0);
1599
1731
  }
1600
1732
  build({ getFetchProps }) {
1601
1733
  return {
1602
1734
  all: async (query, options = {}) => {
1603
1735
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1736
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1604
1737
  return records.map((record) => {
1605
1738
  const { table = "orphan" } = record.xata;
1606
- return { table, record: initObject(this.db, this.links, table, record) };
1739
+ return { table, record: initObject(this.db, schema, table, record) };
1607
1740
  });
1608
1741
  },
1609
1742
  byTable: async (query, options = {}) => {
1610
1743
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1744
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1611
1745
  return records.reduce((acc, record) => {
1612
1746
  const { table = "orphan" } = record.xata;
1613
1747
  const items = acc[table] ?? [];
1614
- const item = initObject(this.db, this.links, table, record);
1748
+ const item = initObject(this.db, schema, table, record);
1615
1749
  return { ...acc, [table]: [...items, item] };
1616
1750
  }, {});
1617
1751
  }
1618
1752
  };
1619
1753
  }
1620
1754
  }
1755
+ _schema = new WeakMap();
1621
1756
  _search = new WeakSet();
1622
1757
  search_fn = async function(query, options, getFetchProps) {
1623
1758
  const fetchProps = await getFetchProps();
1624
- const { tables, fuzziness } = options ?? {};
1759
+ const { tables, fuzziness, highlight } = options ?? {};
1625
1760
  const { records } = await searchBranch({
1626
1761
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1627
- body: { tables, query, fuzziness },
1762
+ body: { tables, query, fuzziness, highlight },
1628
1763
  ...fetchProps
1629
1764
  });
1630
1765
  return records;
1631
1766
  };
1767
+ _getSchema = new WeakSet();
1768
+ getSchema_fn = async function(getFetchProps) {
1769
+ if (__privateGet$1(this, _schema))
1770
+ return __privateGet$1(this, _schema);
1771
+ const fetchProps = await getFetchProps();
1772
+ const { schema } = await getBranchDetails({
1773
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1774
+ ...fetchProps
1775
+ });
1776
+ __privateSet$1(this, _schema, schema);
1777
+ return schema;
1778
+ };
1632
1779
 
1633
1780
  const isBranchStrategyBuilder = (strategy) => {
1634
1781
  return typeof strategy === "function";
@@ -1640,30 +1787,39 @@ const envBranchNames = [
1640
1787
  "CF_PAGES_BRANCH",
1641
1788
  "BRANCH"
1642
1789
  ];
1643
- const defaultBranch = "main";
1644
1790
  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;
1791
+ const env = getBranchByEnvVariable();
1792
+ if (env) {
1793
+ const details = await getDatabaseBranch(env, options);
1794
+ if (details)
1795
+ return env;
1796
+ console.warn(`Branch ${env} not found in Xata. Ignoring...`);
1797
+ }
1798
+ const gitBranch = await getGitBranch();
1799
+ return resolveXataBranch(gitBranch, options);
1655
1800
  }
1656
1801
  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);
1802
+ const branch = await getCurrentBranchName(options);
1803
+ return getDatabaseBranch(branch, options);
1804
+ }
1805
+ async function resolveXataBranch(gitBranch, options) {
1806
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1807
+ const apiKey = options?.apiKey || getAPIKey();
1808
+ if (!databaseURL)
1809
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1810
+ if (!apiKey)
1811
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1812
+ const [protocol, , host, , dbName] = databaseURL.split("/");
1813
+ const [workspace] = host.split(".");
1814
+ const { branch } = await resolveBranch({
1815
+ apiKey,
1816
+ apiUrl: databaseURL,
1817
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1818
+ workspacesApiUrl: `${protocol}//${host}`,
1819
+ pathParams: { dbName, workspace },
1820
+ queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
1821
+ });
1822
+ return branch;
1667
1823
  }
1668
1824
  async function getDatabaseBranch(branch, options) {
1669
1825
  const databaseURL = options?.databaseURL || getDatabaseURL();
@@ -1737,7 +1893,7 @@ var __privateMethod = (obj, member, method) => {
1737
1893
  const buildClient = (plugins) => {
1738
1894
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1739
1895
  return _a = class {
1740
- constructor(options = {}, links, tables) {
1896
+ constructor(options = {}, tables) {
1741
1897
  __privateAdd(this, _parseOptions);
1742
1898
  __privateAdd(this, _getFetchProps);
1743
1899
  __privateAdd(this, _evaluateBranch);
@@ -1747,12 +1903,12 @@ const buildClient = (plugins) => {
1747
1903
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1748
1904
  cache: safeOptions.cache
1749
1905
  };
1750
- const db = new SchemaPlugin(links, tables).build(pluginOptions);
1751
- const search = new SearchPlugin(db, links ?? {}).build(pluginOptions);
1906
+ const db = new SchemaPlugin(tables).build(pluginOptions);
1907
+ const search = new SearchPlugin(db).build(pluginOptions);
1752
1908
  this.db = db;
1753
1909
  this.search = search;
1754
1910
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1755
- if (!namespace)
1911
+ if (namespace === void 0)
1756
1912
  continue;
1757
1913
  const result = namespace.build(pluginOptions);
1758
1914
  if (result instanceof Promise) {
@@ -1769,7 +1925,7 @@ const buildClient = (plugins) => {
1769
1925
  const databaseURL = options?.databaseURL || getDatabaseURL();
1770
1926
  const apiKey = options?.apiKey || getAPIKey();
1771
1927
  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 });
1928
+ 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
1929
  if (!databaseURL || !apiKey) {
1774
1930
  throw new Error("Options databaseURL and apiKey are required");
1775
1931
  }
@@ -1796,7 +1952,7 @@ const buildClient = (plugins) => {
1796
1952
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
1797
1953
  if (__privateGet(this, _branch))
1798
1954
  return __privateGet(this, _branch);
1799
- if (!param)
1955
+ if (param === void 0)
1800
1956
  return void 0;
1801
1957
  const strategies = Array.isArray(param) ? [...param] : [param];
1802
1958
  const evaluateBranch = async (strategy) => {
@@ -1839,11 +1995,11 @@ exports.XataApiPlugin = XataApiPlugin;
1839
1995
  exports.XataError = XataError;
1840
1996
  exports.XataPlugin = XataPlugin;
1841
1997
  exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
1998
+ exports.addGitBranchesEntry = addGitBranchesEntry;
1842
1999
  exports.addTableColumn = addTableColumn;
1843
2000
  exports.buildClient = buildClient;
1844
2001
  exports.bulkInsertTableRecords = bulkInsertTableRecords;
1845
2002
  exports.cancelWorkspaceMemberInvite = cancelWorkspaceMemberInvite;
1846
- exports.compareBranchSchema = compareBranchSchema;
1847
2003
  exports.contains = contains;
1848
2004
  exports.createBranch = createBranch;
1849
2005
  exports.createDatabase = createDatabase;
@@ -1868,13 +2024,13 @@ exports.getBranchList = getBranchList;
1868
2024
  exports.getBranchMetadata = getBranchMetadata;
1869
2025
  exports.getBranchMigrationHistory = getBranchMigrationHistory;
1870
2026
  exports.getBranchMigrationPlan = getBranchMigrationPlan;
1871
- exports.getBranchSchemaHistory = getBranchSchemaHistory;
1872
2027
  exports.getBranchStats = getBranchStats;
1873
2028
  exports.getColumn = getColumn;
1874
2029
  exports.getCurrentBranchDetails = getCurrentBranchDetails;
1875
2030
  exports.getCurrentBranchName = getCurrentBranchName;
1876
2031
  exports.getDatabaseList = getDatabaseList;
1877
2032
  exports.getDatabaseURL = getDatabaseURL;
2033
+ exports.getGitBranchesMapping = getGitBranchesMapping;
1878
2034
  exports.getRecord = getRecord;
1879
2035
  exports.getTableColumns = getTableColumns;
1880
2036
  exports.getTableSchema = getTableSchema;
@@ -1893,6 +2049,7 @@ exports.insertRecord = insertRecord;
1893
2049
  exports.insertRecordWithID = insertRecordWithID;
1894
2050
  exports.inviteWorkspaceMember = inviteWorkspaceMember;
1895
2051
  exports.is = is;
2052
+ exports.isCursorPaginationOptions = isCursorPaginationOptions;
1896
2053
  exports.isIdentifiable = isIdentifiable;
1897
2054
  exports.isNot = isNot;
1898
2055
  exports.isXataRecord = isXataRecord;
@@ -1903,13 +2060,15 @@ exports.notExists = notExists;
1903
2060
  exports.operationsByTag = operationsByTag;
1904
2061
  exports.pattern = pattern;
1905
2062
  exports.queryTable = queryTable;
2063
+ exports.removeGitBranchesEntry = removeGitBranchesEntry;
1906
2064
  exports.removeWorkspaceMember = removeWorkspaceMember;
1907
2065
  exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
2066
+ exports.resolveBranch = resolveBranch;
1908
2067
  exports.searchBranch = searchBranch;
2068
+ exports.searchTable = searchTable;
1909
2069
  exports.setTableSchema = setTableSchema;
1910
2070
  exports.startsWith = startsWith;
1911
2071
  exports.updateBranchMetadata = updateBranchMetadata;
1912
- exports.updateBranchSchema = updateBranchSchema;
1913
2072
  exports.updateColumn = updateColumn;
1914
2073
  exports.updateRecordWithID = updateRecordWithID;
1915
2074
  exports.updateTable = updateTable;