@xata.io/client 0.0.0-alpha.vf2043e7 → 0.0.0-alpha.vf221157

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
@@ -14,6 +14,13 @@ function isObject(value) {
14
14
  function isString(value) {
15
15
  return value !== void 0 && value !== null && typeof value === "string";
16
16
  }
17
+ function toBase64(value) {
18
+ try {
19
+ return btoa(value);
20
+ } catch (err) {
21
+ return Buffer.from(value).toString("base64");
22
+ }
23
+ }
17
24
 
18
25
  function getEnvVariable(name) {
19
26
  try {
@@ -31,7 +38,10 @@ function getEnvVariable(name) {
31
38
  }
32
39
  async function getGitBranch() {
33
40
  try {
34
- return require("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
41
+ if (typeof require === "function") {
42
+ const req = require;
43
+ return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
44
+ }
35
45
  } catch (err) {
36
46
  }
37
47
  try {
@@ -245,6 +255,14 @@ const deleteDatabase = (variables) => fetch$1({
245
255
  method: "delete",
246
256
  ...variables
247
257
  });
258
+ const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
259
+ const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
260
+ const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
261
+ const resolveBranch = (variables) => fetch$1({
262
+ url: "/dbs/{dbName}/resolveBranch",
263
+ method: "get",
264
+ ...variables
265
+ });
248
266
  const getBranchDetails = (variables) => fetch$1({
249
267
  url: "/db/{dbBranchName}",
250
268
  method: "get",
@@ -352,6 +370,11 @@ const queryTable = (variables) => fetch$1({
352
370
  method: "post",
353
371
  ...variables
354
372
  });
373
+ const searchTable = (variables) => fetch$1({
374
+ url: "/db/{dbBranchName}/tables/{tableName}/search",
375
+ method: "post",
376
+ ...variables
377
+ });
355
378
  const searchBranch = (variables) => fetch$1({
356
379
  url: "/db/{dbBranchName}/search",
357
380
  method: "post",
@@ -373,7 +396,15 @@ const operationsByTag = {
373
396
  resendWorkspaceMemberInvite,
374
397
  acceptWorkspaceMemberInvite
375
398
  },
376
- database: { getDatabaseList, createDatabase, deleteDatabase },
399
+ database: {
400
+ getDatabaseList,
401
+ createDatabase,
402
+ deleteDatabase,
403
+ getGitBranchesMapping,
404
+ addGitBranchesEntry,
405
+ removeGitBranchesEntry,
406
+ resolveBranch
407
+ },
377
408
  branch: {
378
409
  getBranchList,
379
410
  getBranchDetails,
@@ -407,6 +438,7 @@ const operationsByTag = {
407
438
  getRecord,
408
439
  bulkInsertTableRecords,
409
440
  queryTable,
441
+ searchTable,
410
442
  searchBranch
411
443
  }
412
444
  };
@@ -436,35 +468,35 @@ function isValidBuilder(builder) {
436
468
  return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
437
469
  }
438
470
 
439
- var __accessCheck$6 = (obj, member, msg) => {
471
+ var __accessCheck$7 = (obj, member, msg) => {
440
472
  if (!member.has(obj))
441
473
  throw TypeError("Cannot " + msg);
442
474
  };
443
- var __privateGet$5 = (obj, member, getter) => {
444
- __accessCheck$6(obj, member, "read from private field");
475
+ var __privateGet$7 = (obj, member, getter) => {
476
+ __accessCheck$7(obj, member, "read from private field");
445
477
  return getter ? getter.call(obj) : member.get(obj);
446
478
  };
447
- var __privateAdd$6 = (obj, member, value) => {
479
+ var __privateAdd$7 = (obj, member, value) => {
448
480
  if (member.has(obj))
449
481
  throw TypeError("Cannot add the same private member more than once");
450
482
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
451
483
  };
452
- var __privateSet$4 = (obj, member, value, setter) => {
453
- __accessCheck$6(obj, member, "write to private field");
484
+ var __privateSet$6 = (obj, member, value, setter) => {
485
+ __accessCheck$7(obj, member, "write to private field");
454
486
  setter ? setter.call(obj, value) : member.set(obj, value);
455
487
  return value;
456
488
  };
457
489
  var _extraProps, _namespaces;
458
490
  class XataApiClient {
459
491
  constructor(options = {}) {
460
- __privateAdd$6(this, _extraProps, void 0);
461
- __privateAdd$6(this, _namespaces, {});
492
+ __privateAdd$7(this, _extraProps, void 0);
493
+ __privateAdd$7(this, _namespaces, {});
462
494
  const provider = options.host ?? "production";
463
495
  const apiKey = options?.apiKey ?? getAPIKey();
464
496
  if (!apiKey) {
465
497
  throw new Error("Could not resolve a valid apiKey");
466
498
  }
467
- __privateSet$4(this, _extraProps, {
499
+ __privateSet$6(this, _extraProps, {
468
500
  apiUrl: getHostUrl(provider, "main"),
469
501
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
470
502
  fetchImpl: getFetchImplementation(options.fetch),
@@ -472,34 +504,34 @@ class XataApiClient {
472
504
  });
473
505
  }
474
506
  get user() {
475
- if (!__privateGet$5(this, _namespaces).user)
476
- __privateGet$5(this, _namespaces).user = new UserApi(__privateGet$5(this, _extraProps));
477
- return __privateGet$5(this, _namespaces).user;
507
+ if (!__privateGet$7(this, _namespaces).user)
508
+ __privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
509
+ return __privateGet$7(this, _namespaces).user;
478
510
  }
479
511
  get workspaces() {
480
- if (!__privateGet$5(this, _namespaces).workspaces)
481
- __privateGet$5(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$5(this, _extraProps));
482
- return __privateGet$5(this, _namespaces).workspaces;
512
+ if (!__privateGet$7(this, _namespaces).workspaces)
513
+ __privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
514
+ return __privateGet$7(this, _namespaces).workspaces;
483
515
  }
484
516
  get databases() {
485
- if (!__privateGet$5(this, _namespaces).databases)
486
- __privateGet$5(this, _namespaces).databases = new DatabaseApi(__privateGet$5(this, _extraProps));
487
- return __privateGet$5(this, _namespaces).databases;
517
+ if (!__privateGet$7(this, _namespaces).databases)
518
+ __privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
519
+ return __privateGet$7(this, _namespaces).databases;
488
520
  }
489
521
  get branches() {
490
- if (!__privateGet$5(this, _namespaces).branches)
491
- __privateGet$5(this, _namespaces).branches = new BranchApi(__privateGet$5(this, _extraProps));
492
- return __privateGet$5(this, _namespaces).branches;
522
+ if (!__privateGet$7(this, _namespaces).branches)
523
+ __privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
524
+ return __privateGet$7(this, _namespaces).branches;
493
525
  }
494
526
  get tables() {
495
- if (!__privateGet$5(this, _namespaces).tables)
496
- __privateGet$5(this, _namespaces).tables = new TableApi(__privateGet$5(this, _extraProps));
497
- return __privateGet$5(this, _namespaces).tables;
527
+ if (!__privateGet$7(this, _namespaces).tables)
528
+ __privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
529
+ return __privateGet$7(this, _namespaces).tables;
498
530
  }
499
531
  get records() {
500
- if (!__privateGet$5(this, _namespaces).records)
501
- __privateGet$5(this, _namespaces).records = new RecordsApi(__privateGet$5(this, _extraProps));
502
- return __privateGet$5(this, _namespaces).records;
532
+ if (!__privateGet$7(this, _namespaces).records)
533
+ __privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
534
+ return __privateGet$7(this, _namespaces).records;
503
535
  }
504
536
  }
505
537
  _extraProps = new WeakMap();
@@ -633,6 +665,33 @@ class DatabaseApi {
633
665
  ...this.extraProps
634
666
  });
635
667
  }
668
+ getGitBranchesMapping(workspace, dbName) {
669
+ return operationsByTag.database.getGitBranchesMapping({
670
+ pathParams: { workspace, dbName },
671
+ ...this.extraProps
672
+ });
673
+ }
674
+ addGitBranchesEntry(workspace, dbName, body) {
675
+ return operationsByTag.database.addGitBranchesEntry({
676
+ pathParams: { workspace, dbName },
677
+ body,
678
+ ...this.extraProps
679
+ });
680
+ }
681
+ removeGitBranchesEntry(workspace, dbName, gitBranch) {
682
+ return operationsByTag.database.removeGitBranchesEntry({
683
+ pathParams: { workspace, dbName },
684
+ queryParams: { gitBranch },
685
+ ...this.extraProps
686
+ });
687
+ }
688
+ resolveBranch(workspace, dbName, gitBranch) {
689
+ return operationsByTag.database.resolveBranch({
690
+ pathParams: { workspace, dbName },
691
+ queryParams: { gitBranch },
692
+ ...this.extraProps
693
+ });
694
+ }
636
695
  }
637
696
  class BranchApi {
638
697
  constructor(extraProps) {
@@ -650,10 +709,10 @@ class BranchApi {
650
709
  ...this.extraProps
651
710
  });
652
711
  }
653
- createBranch(workspace, database, branch, from = "", options = {}) {
712
+ createBranch(workspace, database, branch, from, options = {}) {
654
713
  return operationsByTag.branch.createBranch({
655
714
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
656
- queryParams: { from },
715
+ queryParams: isString(from) ? { from } : void 0,
657
716
  body: options,
658
717
  ...this.extraProps
659
718
  });
@@ -835,6 +894,13 @@ class RecordsApi {
835
894
  ...this.extraProps
836
895
  });
837
896
  }
897
+ searchTable(workspace, database, branch, tableName, query) {
898
+ return operationsByTag.records.searchTable({
899
+ pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
900
+ body: query,
901
+ ...this.extraProps
902
+ });
903
+ }
838
904
  searchBranch(workspace, database, branch, query) {
839
905
  return operationsByTag.records.searchBranch({
840
906
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
@@ -854,43 +920,43 @@ class XataApiPlugin {
854
920
  class XataPlugin {
855
921
  }
856
922
 
857
- var __accessCheck$5 = (obj, member, msg) => {
923
+ var __accessCheck$6 = (obj, member, msg) => {
858
924
  if (!member.has(obj))
859
925
  throw TypeError("Cannot " + msg);
860
926
  };
861
- var __privateGet$4 = (obj, member, getter) => {
862
- __accessCheck$5(obj, member, "read from private field");
927
+ var __privateGet$6 = (obj, member, getter) => {
928
+ __accessCheck$6(obj, member, "read from private field");
863
929
  return getter ? getter.call(obj) : member.get(obj);
864
930
  };
865
- var __privateAdd$5 = (obj, member, value) => {
931
+ var __privateAdd$6 = (obj, member, value) => {
866
932
  if (member.has(obj))
867
933
  throw TypeError("Cannot add the same private member more than once");
868
934
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
869
935
  };
870
- var __privateSet$3 = (obj, member, value, setter) => {
871
- __accessCheck$5(obj, member, "write to private field");
936
+ var __privateSet$5 = (obj, member, value, setter) => {
937
+ __accessCheck$6(obj, member, "write to private field");
872
938
  setter ? setter.call(obj, value) : member.set(obj, value);
873
939
  return value;
874
940
  };
875
941
  var _query;
876
942
  class Page {
877
943
  constructor(query, meta, records = []) {
878
- __privateAdd$5(this, _query, void 0);
879
- __privateSet$3(this, _query, query);
944
+ __privateAdd$6(this, _query, void 0);
945
+ __privateSet$5(this, _query, query);
880
946
  this.meta = meta;
881
947
  this.records = records;
882
948
  }
883
949
  async nextPage(size, offset) {
884
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
950
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
885
951
  }
886
952
  async previousPage(size, offset) {
887
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
953
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
888
954
  }
889
955
  async firstPage(size, offset) {
890
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
956
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
891
957
  }
892
958
  async lastPage(size, offset) {
893
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
959
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
894
960
  }
895
961
  hasNextPage() {
896
962
  return this.meta.page.more;
@@ -902,46 +968,47 @@ const PAGINATION_DEFAULT_SIZE = 200;
902
968
  const PAGINATION_MAX_OFFSET = 800;
903
969
  const PAGINATION_DEFAULT_OFFSET = 0;
904
970
 
905
- var __accessCheck$4 = (obj, member, msg) => {
971
+ var __accessCheck$5 = (obj, member, msg) => {
906
972
  if (!member.has(obj))
907
973
  throw TypeError("Cannot " + msg);
908
974
  };
909
- var __privateGet$3 = (obj, member, getter) => {
910
- __accessCheck$4(obj, member, "read from private field");
975
+ var __privateGet$5 = (obj, member, getter) => {
976
+ __accessCheck$5(obj, member, "read from private field");
911
977
  return getter ? getter.call(obj) : member.get(obj);
912
978
  };
913
- var __privateAdd$4 = (obj, member, value) => {
979
+ var __privateAdd$5 = (obj, member, value) => {
914
980
  if (member.has(obj))
915
981
  throw TypeError("Cannot add the same private member more than once");
916
982
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
917
983
  };
918
- var __privateSet$2 = (obj, member, value, setter) => {
919
- __accessCheck$4(obj, member, "write to private field");
984
+ var __privateSet$4 = (obj, member, value, setter) => {
985
+ __accessCheck$5(obj, member, "write to private field");
920
986
  setter ? setter.call(obj, value) : member.set(obj, value);
921
987
  return value;
922
988
  };
923
989
  var _table$1, _repository, _data;
924
990
  const _Query = class {
925
991
  constructor(repository, table, data, parent) {
926
- __privateAdd$4(this, _table$1, void 0);
927
- __privateAdd$4(this, _repository, void 0);
928
- __privateAdd$4(this, _data, { filter: {} });
992
+ __privateAdd$5(this, _table$1, void 0);
993
+ __privateAdd$5(this, _repository, void 0);
994
+ __privateAdd$5(this, _data, { filter: {} });
929
995
  this.meta = { page: { cursor: "start", more: true } };
930
996
  this.records = [];
931
- __privateSet$2(this, _table$1, table);
997
+ __privateSet$4(this, _table$1, table);
932
998
  if (repository) {
933
- __privateSet$2(this, _repository, repository);
999
+ __privateSet$4(this, _repository, repository);
934
1000
  } else {
935
- __privateSet$2(this, _repository, this);
1001
+ __privateSet$4(this, _repository, this);
936
1002
  }
937
- __privateGet$3(this, _data).filter = data.filter ?? parent?.filter ?? {};
938
- __privateGet$3(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
939
- __privateGet$3(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
940
- __privateGet$3(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
941
- __privateGet$3(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
942
- __privateGet$3(this, _data).sort = data.sort ?? parent?.sort;
943
- __privateGet$3(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
944
- __privateGet$3(this, _data).page = data.page ?? parent?.page;
1003
+ __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
1004
+ __privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
1005
+ __privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
1006
+ __privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
1007
+ __privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
1008
+ __privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
1009
+ __privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
1010
+ __privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
1011
+ __privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
945
1012
  this.any = this.any.bind(this);
946
1013
  this.all = this.all.bind(this);
947
1014
  this.not = this.not.bind(this);
@@ -952,58 +1019,64 @@ const _Query = class {
952
1019
  Object.defineProperty(this, "repository", { enumerable: false });
953
1020
  }
954
1021
  getQueryOptions() {
955
- return __privateGet$3(this, _data);
1022
+ return __privateGet$5(this, _data);
1023
+ }
1024
+ key() {
1025
+ const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
1026
+ const key = JSON.stringify({ columns, filter, sort, pagination });
1027
+ return toBase64(key);
956
1028
  }
957
1029
  any(...queries) {
958
1030
  const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
959
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $any } }, __privateGet$3(this, _data));
1031
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
960
1032
  }
961
1033
  all(...queries) {
962
1034
  const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
963
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
1035
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
964
1036
  }
965
1037
  not(...queries) {
966
1038
  const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
967
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $not } }, __privateGet$3(this, _data));
1039
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
968
1040
  }
969
1041
  none(...queries) {
970
1042
  const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
971
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $none } }, __privateGet$3(this, _data));
1043
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
972
1044
  }
973
1045
  filter(a, b) {
974
1046
  if (arguments.length === 1) {
975
1047
  const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
976
- const $all = compact([__privateGet$3(this, _data).filter?.$all].flat().concat(constraints));
977
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
1048
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
1049
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
978
1050
  } else {
979
- const $all = compact([__privateGet$3(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
980
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
1051
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1052
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
981
1053
  }
982
1054
  }
983
1055
  sort(column, direction) {
984
- const originalSort = [__privateGet$3(this, _data).sort ?? []].flat();
1056
+ const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
985
1057
  const sort = [...originalSort, { column, direction }];
986
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { sort }, __privateGet$3(this, _data));
1058
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
987
1059
  }
988
1060
  select(columns) {
989
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { columns }, __privateGet$3(this, _data));
1061
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
990
1062
  }
991
1063
  getPaginated(options = {}) {
992
- const query = new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), options, __privateGet$3(this, _data));
993
- return __privateGet$3(this, _repository).query(query);
1064
+ const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
1065
+ return __privateGet$5(this, _repository).query(query);
994
1066
  }
995
1067
  async *[Symbol.asyncIterator]() {
996
- for await (const [record] of this.getIterator(1)) {
1068
+ for await (const [record] of this.getIterator({ batchSize: 1 })) {
997
1069
  yield record;
998
1070
  }
999
1071
  }
1000
- async *getIterator(chunk, options = {}) {
1072
+ async *getIterator(options = {}) {
1073
+ const { batchSize = 1 } = options;
1001
1074
  let offset = 0;
1002
1075
  let end = false;
1003
1076
  while (!end) {
1004
- const { records, meta } = await this.getPaginated({ ...options, page: { size: chunk, offset } });
1077
+ const { records, meta } = await this.getPaginated({ ...options, pagination: { size: batchSize, offset } });
1005
1078
  yield records;
1006
- offset += chunk;
1079
+ offset += batchSize;
1007
1080
  end = !meta.page.more;
1008
1081
  }
1009
1082
  }
@@ -1011,16 +1084,20 @@ const _Query = class {
1011
1084
  const { records } = await this.getPaginated(options);
1012
1085
  return records;
1013
1086
  }
1014
- async getAll(chunk = PAGINATION_MAX_SIZE, options = {}) {
1087
+ async getAll(options = {}) {
1088
+ const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
1015
1089
  const results = [];
1016
- for await (const page of this.getIterator(chunk, options)) {
1090
+ for await (const page of this.getIterator({ ...rest, batchSize })) {
1017
1091
  results.push(...page);
1018
1092
  }
1019
1093
  return results;
1020
1094
  }
1021
- async getOne(options = {}) {
1022
- const records = await this.getMany({ ...options, page: { size: 1 } });
1023
- return records[0] || null;
1095
+ async getFirst(options = {}) {
1096
+ const records = await this.getMany({ ...options, pagination: { size: 1 } });
1097
+ return records[0] ?? null;
1098
+ }
1099
+ cache(ttl) {
1100
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
1024
1101
  }
1025
1102
  nextPage(size, offset) {
1026
1103
  return this.firstPage(size, offset);
@@ -1029,10 +1106,10 @@ const _Query = class {
1029
1106
  return this.firstPage(size, offset);
1030
1107
  }
1031
1108
  firstPage(size, offset) {
1032
- return this.getPaginated({ page: { size, offset } });
1109
+ return this.getPaginated({ pagination: { size, offset } });
1033
1110
  }
1034
1111
  lastPage(size, offset) {
1035
- return this.getPaginated({ page: { size, offset, before: "end" } });
1112
+ return this.getPaginated({ pagination: { size, offset, before: "end" } });
1036
1113
  }
1037
1114
  hasNextPage() {
1038
1115
  return this.meta.page.more;
@@ -1073,75 +1150,94 @@ function buildSortFilter(filter) {
1073
1150
  }
1074
1151
  }
1075
1152
 
1076
- var __accessCheck$3 = (obj, member, msg) => {
1153
+ var __accessCheck$4 = (obj, member, msg) => {
1077
1154
  if (!member.has(obj))
1078
1155
  throw TypeError("Cannot " + msg);
1079
1156
  };
1080
- var __privateGet$2 = (obj, member, getter) => {
1081
- __accessCheck$3(obj, member, "read from private field");
1157
+ var __privateGet$4 = (obj, member, getter) => {
1158
+ __accessCheck$4(obj, member, "read from private field");
1082
1159
  return getter ? getter.call(obj) : member.get(obj);
1083
1160
  };
1084
- var __privateAdd$3 = (obj, member, value) => {
1161
+ var __privateAdd$4 = (obj, member, value) => {
1085
1162
  if (member.has(obj))
1086
1163
  throw TypeError("Cannot add the same private member more than once");
1087
1164
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1088
1165
  };
1089
- var __privateSet$1 = (obj, member, value, setter) => {
1090
- __accessCheck$3(obj, member, "write to private field");
1166
+ var __privateSet$3 = (obj, member, value, setter) => {
1167
+ __accessCheck$4(obj, member, "write to private field");
1091
1168
  setter ? setter.call(obj, value) : member.set(obj, value);
1092
1169
  return value;
1093
1170
  };
1094
1171
  var __privateMethod$2 = (obj, member, method) => {
1095
- __accessCheck$3(obj, member, "access private method");
1172
+ __accessCheck$4(obj, member, "access private method");
1096
1173
  return method;
1097
1174
  };
1098
- var _table, _links, _getFetchProps, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn;
1175
+ 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;
1099
1176
  class Repository extends Query {
1100
1177
  }
1101
1178
  class RestRepository extends Query {
1102
1179
  constructor(options) {
1103
1180
  super(null, options.table, {});
1104
- __privateAdd$3(this, _insertRecordWithoutId);
1105
- __privateAdd$3(this, _insertRecordWithId);
1106
- __privateAdd$3(this, _bulkInsertTableRecords);
1107
- __privateAdd$3(this, _updateRecordWithID);
1108
- __privateAdd$3(this, _upsertRecordWithID);
1109
- __privateAdd$3(this, _deleteRecord);
1110
- __privateAdd$3(this, _table, void 0);
1111
- __privateAdd$3(this, _links, void 0);
1112
- __privateAdd$3(this, _getFetchProps, void 0);
1113
- __privateSet$1(this, _table, options.table);
1114
- __privateSet$1(this, _links, options.links ?? {});
1115
- __privateSet$1(this, _getFetchProps, options.getFetchProps);
1181
+ __privateAdd$4(this, _insertRecordWithoutId);
1182
+ __privateAdd$4(this, _insertRecordWithId);
1183
+ __privateAdd$4(this, _bulkInsertTableRecords);
1184
+ __privateAdd$4(this, _updateRecordWithID);
1185
+ __privateAdd$4(this, _upsertRecordWithID);
1186
+ __privateAdd$4(this, _deleteRecord);
1187
+ __privateAdd$4(this, _invalidateCache);
1188
+ __privateAdd$4(this, _setCacheRecord);
1189
+ __privateAdd$4(this, _getCacheRecord);
1190
+ __privateAdd$4(this, _setCacheQuery);
1191
+ __privateAdd$4(this, _getCacheQuery);
1192
+ __privateAdd$4(this, _getSchema$1);
1193
+ __privateAdd$4(this, _table, void 0);
1194
+ __privateAdd$4(this, _getFetchProps, void 0);
1195
+ __privateAdd$4(this, _cache, void 0);
1196
+ __privateAdd$4(this, _schema$1, void 0);
1197
+ __privateSet$3(this, _table, options.table);
1198
+ __privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
1116
1199
  this.db = options.db;
1200
+ __privateSet$3(this, _cache, options.pluginOptions.cache);
1117
1201
  }
1118
1202
  async create(a, b) {
1119
1203
  if (Array.isArray(a)) {
1120
- return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1204
+ const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1205
+ await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1206
+ return records;
1121
1207
  }
1122
1208
  if (isString(a) && isObject(b)) {
1123
1209
  if (a === "")
1124
1210
  throw new Error("The id can't be empty");
1125
- return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1211
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1212
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1213
+ return record;
1126
1214
  }
1127
1215
  if (isObject(a) && isString(a.id)) {
1128
1216
  if (a.id === "")
1129
1217
  throw new Error("The id can't be empty");
1130
- return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
1218
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
1219
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1220
+ return record;
1131
1221
  }
1132
1222
  if (isObject(a)) {
1133
- return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1223
+ const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1224
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1225
+ return record;
1134
1226
  }
1135
1227
  throw new Error("Invalid arguments for create method");
1136
1228
  }
1137
1229
  async read(recordId) {
1138
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1230
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1231
+ if (cacheRecord)
1232
+ return cacheRecord;
1233
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1139
1234
  try {
1140
1235
  const response = await getRecord({
1141
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1236
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1142
1237
  ...fetchProps
1143
1238
  });
1144
- return initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), response);
1239
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1240
+ return initObject(this.db, schema, __privateGet$4(this, _table), response);
1145
1241
  } catch (e) {
1146
1242
  if (isObject(e) && e.status === 404) {
1147
1243
  return null;
@@ -1157,10 +1253,16 @@ class RestRepository extends Query {
1157
1253
  return Promise.all(a.map((object) => this.update(object)));
1158
1254
  }
1159
1255
  if (isString(a) && isObject(b)) {
1160
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1256
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1257
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1258
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1259
+ return record;
1161
1260
  }
1162
1261
  if (isObject(a) && isString(a.id)) {
1163
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1262
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1263
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1264
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1265
+ return record;
1164
1266
  }
1165
1267
  throw new Error("Invalid arguments for update method");
1166
1268
  }
@@ -1172,70 +1274,89 @@ class RestRepository extends Query {
1172
1274
  return Promise.all(a.map((object) => this.createOrUpdate(object)));
1173
1275
  }
1174
1276
  if (isString(a) && isObject(b)) {
1175
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1277
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1278
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1279
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1280
+ return record;
1176
1281
  }
1177
1282
  if (isObject(a) && isString(a.id)) {
1178
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1283
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1284
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1285
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1286
+ return record;
1179
1287
  }
1180
1288
  throw new Error("Invalid arguments for createOrUpdate method");
1181
1289
  }
1182
- async delete(recordId) {
1183
- if (Array.isArray(recordId)) {
1184
- if (recordId.length > 100) {
1290
+ async delete(a) {
1291
+ if (Array.isArray(a)) {
1292
+ if (a.length > 100) {
1185
1293
  console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1186
1294
  }
1187
- await Promise.all(recordId.map((id) => this.delete(id)));
1295
+ await Promise.all(a.map((id) => this.delete(id)));
1188
1296
  return;
1189
1297
  }
1190
- if (isString(recordId)) {
1191
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId);
1298
+ if (isString(a)) {
1299
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
1300
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1192
1301
  return;
1193
1302
  }
1194
- if (isObject(recordId) && isString(recordId.id)) {
1195
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId.id);
1303
+ if (isObject(a) && isString(a.id)) {
1304
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
1305
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1196
1306
  return;
1197
1307
  }
1198
1308
  throw new Error("Invalid arguments for delete method");
1199
1309
  }
1200
1310
  async search(query, options = {}) {
1201
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1202
- const { records } = await searchBranch({
1203
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1204
- body: { tables: [__privateGet$2(this, _table)], query, fuzziness: options.fuzziness },
1311
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1312
+ const { records } = await searchTable({
1313
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1314
+ body: {
1315
+ query,
1316
+ fuzziness: options.fuzziness,
1317
+ filter: options.filter
1318
+ },
1205
1319
  ...fetchProps
1206
1320
  });
1207
- return records.map((item) => initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), item));
1321
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1322
+ return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
1208
1323
  }
1209
1324
  async query(query) {
1325
+ const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
1326
+ if (cacheQuery)
1327
+ return new Page(query, cacheQuery.meta, cacheQuery.records);
1210
1328
  const data = query.getQueryOptions();
1211
1329
  const body = {
1212
1330
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1213
- sort: data.sort ? buildSortFilter(data.sort) : void 0,
1214
- page: data.page,
1331
+ sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1332
+ page: data.pagination,
1215
1333
  columns: data.columns
1216
1334
  };
1217
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1335
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1218
1336
  const { meta, records: objects } = await queryTable({
1219
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1337
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1220
1338
  body,
1221
1339
  ...fetchProps
1222
1340
  });
1223
- const records = objects.map((record) => initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), record));
1341
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1342
+ const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
1343
+ await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1224
1344
  return new Page(query, meta, records);
1225
1345
  }
1226
1346
  }
1227
1347
  _table = new WeakMap();
1228
- _links = new WeakMap();
1229
1348
  _getFetchProps = new WeakMap();
1349
+ _cache = new WeakMap();
1350
+ _schema$1 = new WeakMap();
1230
1351
  _insertRecordWithoutId = new WeakSet();
1231
1352
  insertRecordWithoutId_fn = async function(object) {
1232
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1353
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1233
1354
  const record = transformObjectLinks(object);
1234
1355
  const response = await insertRecord({
1235
1356
  pathParams: {
1236
1357
  workspace: "{workspaceId}",
1237
1358
  dbBranchName: "{dbBranch}",
1238
- tableName: __privateGet$2(this, _table)
1359
+ tableName: __privateGet$4(this, _table)
1239
1360
  },
1240
1361
  body: record,
1241
1362
  ...fetchProps
@@ -1248,13 +1369,13 @@ insertRecordWithoutId_fn = async function(object) {
1248
1369
  };
1249
1370
  _insertRecordWithId = new WeakSet();
1250
1371
  insertRecordWithId_fn = async function(recordId, object) {
1251
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1372
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1252
1373
  const record = transformObjectLinks(object);
1253
1374
  const response = await insertRecordWithID({
1254
1375
  pathParams: {
1255
1376
  workspace: "{workspaceId}",
1256
1377
  dbBranchName: "{dbBranch}",
1257
- tableName: __privateGet$2(this, _table),
1378
+ tableName: __privateGet$4(this, _table),
1258
1379
  recordId
1259
1380
  },
1260
1381
  body: record,
@@ -1269,10 +1390,10 @@ insertRecordWithId_fn = async function(recordId, object) {
1269
1390
  };
1270
1391
  _bulkInsertTableRecords = new WeakSet();
1271
1392
  bulkInsertTableRecords_fn = async function(objects) {
1272
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1393
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1273
1394
  const records = objects.map((object) => transformObjectLinks(object));
1274
1395
  const response = await bulkInsertTableRecords({
1275
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1396
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1276
1397
  body: { records },
1277
1398
  ...fetchProps
1278
1399
  });
@@ -1284,10 +1405,10 @@ bulkInsertTableRecords_fn = async function(objects) {
1284
1405
  };
1285
1406
  _updateRecordWithID = new WeakSet();
1286
1407
  updateRecordWithID_fn = async function(recordId, object) {
1287
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1408
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1288
1409
  const record = transformObjectLinks(object);
1289
1410
  const response = await updateRecordWithID({
1290
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1411
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1291
1412
  body: record,
1292
1413
  ...fetchProps
1293
1414
  });
@@ -1298,9 +1419,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1298
1419
  };
1299
1420
  _upsertRecordWithID = new WeakSet();
1300
1421
  upsertRecordWithID_fn = async function(recordId, object) {
1301
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1422
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1302
1423
  const response = await upsertRecordWithID({
1303
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1424
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1304
1425
  body: object,
1305
1426
  ...fetchProps
1306
1427
  });
@@ -1311,12 +1432,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
1311
1432
  };
1312
1433
  _deleteRecord = new WeakSet();
1313
1434
  deleteRecord_fn = async function(recordId) {
1314
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1435
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1315
1436
  await deleteRecord({
1316
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1437
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1317
1438
  ...fetchProps
1318
1439
  });
1319
1440
  };
1441
+ _invalidateCache = new WeakSet();
1442
+ invalidateCache_fn = async function(recordId) {
1443
+ await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1444
+ const cacheItems = await __privateGet$4(this, _cache).getAll();
1445
+ const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1446
+ for (const [key, value] of queries) {
1447
+ const ids = getIds(value);
1448
+ if (ids.includes(recordId))
1449
+ await __privateGet$4(this, _cache).delete(key);
1450
+ }
1451
+ };
1452
+ _setCacheRecord = new WeakSet();
1453
+ setCacheRecord_fn = async function(record) {
1454
+ if (!__privateGet$4(this, _cache).cacheRecords)
1455
+ return;
1456
+ await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
1457
+ };
1458
+ _getCacheRecord = new WeakSet();
1459
+ getCacheRecord_fn = async function(recordId) {
1460
+ if (!__privateGet$4(this, _cache).cacheRecords)
1461
+ return null;
1462
+ return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1463
+ };
1464
+ _setCacheQuery = new WeakSet();
1465
+ setCacheQuery_fn = async function(query, meta, records) {
1466
+ await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1467
+ };
1468
+ _getCacheQuery = new WeakSet();
1469
+ getCacheQuery_fn = async function(query) {
1470
+ const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
1471
+ const result = await __privateGet$4(this, _cache).get(key);
1472
+ if (!result)
1473
+ return null;
1474
+ const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1475
+ if (ttl < 0)
1476
+ return null;
1477
+ const hasExpired = result.date.getTime() + ttl < Date.now();
1478
+ return hasExpired ? null : result;
1479
+ };
1480
+ _getSchema$1 = new WeakSet();
1481
+ getSchema_fn$1 = async function() {
1482
+ if (__privateGet$4(this, _schema$1))
1483
+ return __privateGet$4(this, _schema$1);
1484
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1485
+ const { schema } = await getBranchDetails({
1486
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1487
+ ...fetchProps
1488
+ });
1489
+ __privateSet$3(this, _schema$1, schema);
1490
+ return schema;
1491
+ };
1320
1492
  const transformObjectLinks = (object) => {
1321
1493
  return Object.entries(object).reduce((acc, [key, value]) => {
1322
1494
  if (key === "xata")
@@ -1324,15 +1496,33 @@ const transformObjectLinks = (object) => {
1324
1496
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1325
1497
  }, {});
1326
1498
  };
1327
- const initObject = (db, links, table, object) => {
1499
+ const initObject = (db, schema, table, object) => {
1328
1500
  const result = {};
1329
1501
  Object.assign(result, object);
1330
- const tableLinks = links[table] || [];
1331
- for (const link of tableLinks) {
1332
- const [field, linkTable] = link;
1333
- const value = result[field];
1334
- if (value && isObject(value)) {
1335
- result[field] = initObject(db, links, linkTable, value);
1502
+ const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
1503
+ if (!columns)
1504
+ console.error(`Table ${table} not found in schema`);
1505
+ for (const column of columns ?? []) {
1506
+ const value = result[column.name];
1507
+ switch (column.type) {
1508
+ case "datetime": {
1509
+ const date = new Date(value);
1510
+ if (isNaN(date.getTime())) {
1511
+ console.error(`Failed to parse date ${value} for field ${column.name}`);
1512
+ } else {
1513
+ result[column.name] = date;
1514
+ }
1515
+ break;
1516
+ }
1517
+ case "link": {
1518
+ const linkTable = column.link?.table;
1519
+ if (!linkTable) {
1520
+ console.error(`Failed to parse link for field ${column.name}`);
1521
+ } else if (isObject(value)) {
1522
+ result[column.name] = initObject(db, schema, linkTable, value);
1523
+ }
1524
+ break;
1525
+ }
1336
1526
  }
1337
1527
  }
1338
1528
  result.read = function() {
@@ -1350,6 +1540,65 @@ const initObject = (db, links, table, object) => {
1350
1540
  Object.freeze(result);
1351
1541
  return result;
1352
1542
  };
1543
+ function getIds(value) {
1544
+ if (Array.isArray(value)) {
1545
+ return value.map((item) => getIds(item)).flat();
1546
+ }
1547
+ if (!isObject(value))
1548
+ return [];
1549
+ const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
1550
+ return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
1551
+ }
1552
+
1553
+ var __accessCheck$3 = (obj, member, msg) => {
1554
+ if (!member.has(obj))
1555
+ throw TypeError("Cannot " + msg);
1556
+ };
1557
+ var __privateGet$3 = (obj, member, getter) => {
1558
+ __accessCheck$3(obj, member, "read from private field");
1559
+ return getter ? getter.call(obj) : member.get(obj);
1560
+ };
1561
+ var __privateAdd$3 = (obj, member, value) => {
1562
+ if (member.has(obj))
1563
+ throw TypeError("Cannot add the same private member more than once");
1564
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1565
+ };
1566
+ var __privateSet$2 = (obj, member, value, setter) => {
1567
+ __accessCheck$3(obj, member, "write to private field");
1568
+ setter ? setter.call(obj, value) : member.set(obj, value);
1569
+ return value;
1570
+ };
1571
+ var _map;
1572
+ class SimpleCache {
1573
+ constructor(options = {}) {
1574
+ __privateAdd$3(this, _map, void 0);
1575
+ __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1576
+ this.capacity = options.max ?? 500;
1577
+ this.cacheRecords = options.cacheRecords ?? true;
1578
+ this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1579
+ }
1580
+ async getAll() {
1581
+ return Object.fromEntries(__privateGet$3(this, _map));
1582
+ }
1583
+ async get(key) {
1584
+ return __privateGet$3(this, _map).get(key) ?? null;
1585
+ }
1586
+ async set(key, value) {
1587
+ await this.delete(key);
1588
+ __privateGet$3(this, _map).set(key, value);
1589
+ if (__privateGet$3(this, _map).size > this.capacity) {
1590
+ const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
1591
+ await this.delete(leastRecentlyUsed);
1592
+ }
1593
+ }
1594
+ async delete(key) {
1595
+ __privateGet$3(this, _map).delete(key);
1596
+ }
1597
+ async clear() {
1598
+ return __privateGet$3(this, _map).clear();
1599
+ }
1600
+ }
1601
+ _map = new WeakMap();
1353
1602
 
1354
1603
  const gt = (value) => ({ $gt: value });
1355
1604
  const ge = (value) => ({ $ge: value });
@@ -1374,7 +1623,7 @@ var __accessCheck$2 = (obj, member, msg) => {
1374
1623
  if (!member.has(obj))
1375
1624
  throw TypeError("Cannot " + msg);
1376
1625
  };
1377
- var __privateGet$1 = (obj, member, getter) => {
1626
+ var __privateGet$2 = (obj, member, getter) => {
1378
1627
  __accessCheck$2(obj, member, "read from private field");
1379
1628
  return getter ? getter.call(obj) : member.get(obj);
1380
1629
  };
@@ -1385,26 +1634,24 @@ var __privateAdd$2 = (obj, member, value) => {
1385
1634
  };
1386
1635
  var _tables;
1387
1636
  class SchemaPlugin extends XataPlugin {
1388
- constructor(links, tableNames) {
1637
+ constructor(tableNames) {
1389
1638
  super();
1390
- this.links = links;
1391
1639
  this.tableNames = tableNames;
1392
1640
  __privateAdd$2(this, _tables, {});
1393
1641
  }
1394
- build(options) {
1395
- const { getFetchProps } = options;
1396
- const links = this.links;
1642
+ build(pluginOptions) {
1397
1643
  const db = new Proxy({}, {
1398
1644
  get: (_target, table) => {
1399
1645
  if (!isString(table))
1400
1646
  throw new Error("Invalid table name");
1401
- if (!__privateGet$1(this, _tables)[table])
1402
- __privateGet$1(this, _tables)[table] = new RestRepository({ db, getFetchProps, table, links });
1403
- return __privateGet$1(this, _tables)[table];
1647
+ if (__privateGet$2(this, _tables)[table] === void 0) {
1648
+ __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1649
+ }
1650
+ return __privateGet$2(this, _tables)[table];
1404
1651
  }
1405
1652
  });
1406
1653
  for (const table of this.tableNames ?? []) {
1407
- db[table] = new RestRepository({ db, getFetchProps, table, links });
1654
+ db[table] = new RestRepository({ db, pluginOptions, table });
1408
1655
  }
1409
1656
  return db;
1410
1657
  }
@@ -1415,44 +1662,57 @@ var __accessCheck$1 = (obj, member, msg) => {
1415
1662
  if (!member.has(obj))
1416
1663
  throw TypeError("Cannot " + msg);
1417
1664
  };
1665
+ var __privateGet$1 = (obj, member, getter) => {
1666
+ __accessCheck$1(obj, member, "read from private field");
1667
+ return getter ? getter.call(obj) : member.get(obj);
1668
+ };
1418
1669
  var __privateAdd$1 = (obj, member, value) => {
1419
1670
  if (member.has(obj))
1420
1671
  throw TypeError("Cannot add the same private member more than once");
1421
1672
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1422
1673
  };
1674
+ var __privateSet$1 = (obj, member, value, setter) => {
1675
+ __accessCheck$1(obj, member, "write to private field");
1676
+ setter ? setter.call(obj, value) : member.set(obj, value);
1677
+ return value;
1678
+ };
1423
1679
  var __privateMethod$1 = (obj, member, method) => {
1424
1680
  __accessCheck$1(obj, member, "access private method");
1425
1681
  return method;
1426
1682
  };
1427
- var _search, search_fn;
1683
+ var _schema, _search, search_fn, _getSchema, getSchema_fn;
1428
1684
  class SearchPlugin extends XataPlugin {
1429
- constructor(db, links) {
1685
+ constructor(db) {
1430
1686
  super();
1431
1687
  this.db = db;
1432
- this.links = links;
1433
1688
  __privateAdd$1(this, _search);
1689
+ __privateAdd$1(this, _getSchema);
1690
+ __privateAdd$1(this, _schema, void 0);
1434
1691
  }
1435
1692
  build({ getFetchProps }) {
1436
1693
  return {
1437
1694
  all: async (query, options = {}) => {
1438
1695
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1696
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1439
1697
  return records.map((record) => {
1440
1698
  const { table = "orphan" } = record.xata;
1441
- return { table, record: initObject(this.db, this.links, table, record) };
1699
+ return { table, record: initObject(this.db, schema, table, record) };
1442
1700
  });
1443
1701
  },
1444
1702
  byTable: async (query, options = {}) => {
1445
1703
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1704
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1446
1705
  return records.reduce((acc, record) => {
1447
1706
  const { table = "orphan" } = record.xata;
1448
1707
  const items = acc[table] ?? [];
1449
- const item = initObject(this.db, this.links, table, record);
1708
+ const item = initObject(this.db, schema, table, record);
1450
1709
  return { ...acc, [table]: [...items, item] };
1451
1710
  }, {});
1452
1711
  }
1453
1712
  };
1454
1713
  }
1455
1714
  }
1715
+ _schema = new WeakMap();
1456
1716
  _search = new WeakSet();
1457
1717
  search_fn = async function(query, options, getFetchProps) {
1458
1718
  const fetchProps = await getFetchProps();
@@ -1464,6 +1724,18 @@ search_fn = async function(query, options, getFetchProps) {
1464
1724
  });
1465
1725
  return records;
1466
1726
  };
1727
+ _getSchema = new WeakSet();
1728
+ getSchema_fn = async function(getFetchProps) {
1729
+ if (__privateGet$1(this, _schema))
1730
+ return __privateGet$1(this, _schema);
1731
+ const fetchProps = await getFetchProps();
1732
+ const { schema } = await getBranchDetails({
1733
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1734
+ ...fetchProps
1735
+ });
1736
+ __privateSet$1(this, _schema, schema);
1737
+ return schema;
1738
+ };
1467
1739
 
1468
1740
  const isBranchStrategyBuilder = (strategy) => {
1469
1741
  return typeof strategy === "function";
@@ -1475,30 +1747,39 @@ const envBranchNames = [
1475
1747
  "CF_PAGES_BRANCH",
1476
1748
  "BRANCH"
1477
1749
  ];
1478
- const defaultBranch = "main";
1479
1750
  async function getCurrentBranchName(options) {
1480
- const env = await getBranchByEnvVariable();
1481
- if (env)
1482
- return env;
1483
- const branch = await getGitBranch();
1484
- if (!branch)
1485
- return defaultBranch;
1486
- const details = await getDatabaseBranch(branch, options);
1487
- if (details)
1488
- return branch;
1489
- return defaultBranch;
1751
+ const env = getBranchByEnvVariable();
1752
+ if (env) {
1753
+ const details = await getDatabaseBranch(env, options);
1754
+ if (details)
1755
+ return env;
1756
+ console.warn(`Branch ${env} not found in Xata. Ignoring...`);
1757
+ }
1758
+ const gitBranch = await getGitBranch();
1759
+ return resolveXataBranch(gitBranch, options);
1490
1760
  }
1491
1761
  async function getCurrentBranchDetails(options) {
1492
- const env = await getBranchByEnvVariable();
1493
- if (env)
1494
- return getDatabaseBranch(env, options);
1495
- const branch = await getGitBranch();
1496
- if (!branch)
1497
- return getDatabaseBranch(defaultBranch, options);
1498
- const details = await getDatabaseBranch(branch, options);
1499
- if (details)
1500
- return details;
1501
- return getDatabaseBranch(defaultBranch, options);
1762
+ const branch = await getCurrentBranchName(options);
1763
+ return getDatabaseBranch(branch, options);
1764
+ }
1765
+ async function resolveXataBranch(gitBranch, options) {
1766
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1767
+ const apiKey = options?.apiKey || getAPIKey();
1768
+ if (!databaseURL)
1769
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1770
+ if (!apiKey)
1771
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1772
+ const [protocol, , host, , dbName] = databaseURL.split("/");
1773
+ const [workspace] = host.split(".");
1774
+ const { branch } = await resolveBranch({
1775
+ apiKey,
1776
+ apiUrl: databaseURL,
1777
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1778
+ workspacesApiUrl: `${protocol}//${host}`,
1779
+ pathParams: { dbName, workspace },
1780
+ queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
1781
+ });
1782
+ return branch;
1502
1783
  }
1503
1784
  async function getDatabaseBranch(branch, options) {
1504
1785
  const databaseURL = options?.databaseURL || getDatabaseURL();
@@ -1572,22 +1853,24 @@ var __privateMethod = (obj, member, method) => {
1572
1853
  const buildClient = (plugins) => {
1573
1854
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1574
1855
  return _a = class {
1575
- constructor(options = {}, links, tables) {
1856
+ constructor(options = {}, tables) {
1576
1857
  __privateAdd(this, _parseOptions);
1577
1858
  __privateAdd(this, _getFetchProps);
1578
1859
  __privateAdd(this, _evaluateBranch);
1579
1860
  __privateAdd(this, _branch, void 0);
1580
1861
  const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
1581
- const db = new SchemaPlugin(links, tables).build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1582
- const search = new SearchPlugin(db, links ?? {}).build({
1583
- getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions)
1584
- });
1862
+ const pluginOptions = {
1863
+ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1864
+ cache: safeOptions.cache
1865
+ };
1866
+ const db = new SchemaPlugin(tables).build(pluginOptions);
1867
+ const search = new SearchPlugin(db).build(pluginOptions);
1585
1868
  this.db = db;
1586
1869
  this.search = search;
1587
1870
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1588
- if (!namespace)
1871
+ if (namespace === void 0)
1589
1872
  continue;
1590
- const result = namespace.build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1873
+ const result = namespace.build(pluginOptions);
1591
1874
  if (result instanceof Promise) {
1592
1875
  void result.then((namespace2) => {
1593
1876
  this[key] = namespace2;
@@ -1601,11 +1884,12 @@ const buildClient = (plugins) => {
1601
1884
  const fetch = getFetchImplementation(options?.fetch);
1602
1885
  const databaseURL = options?.databaseURL || getDatabaseURL();
1603
1886
  const apiKey = options?.apiKey || getAPIKey();
1604
- const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1887
+ const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1888
+ const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1605
1889
  if (!databaseURL || !apiKey) {
1606
1890
  throw new Error("Options databaseURL and apiKey are required");
1607
1891
  }
1608
- return { fetch, databaseURL, apiKey, branch };
1892
+ return { fetch, databaseURL, apiKey, branch, cache };
1609
1893
  }, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
1610
1894
  fetch,
1611
1895
  apiKey,
@@ -1628,7 +1912,7 @@ const buildClient = (plugins) => {
1628
1912
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
1629
1913
  if (__privateGet(this, _branch))
1630
1914
  return __privateGet(this, _branch);
1631
- if (!param)
1915
+ if (param === void 0)
1632
1916
  return void 0;
1633
1917
  const strategies = Array.isArray(param) ? [...param] : [param];
1634
1918
  const evaluateBranch = async (strategy) => {
@@ -1665,11 +1949,13 @@ exports.Repository = Repository;
1665
1949
  exports.RestRepository = RestRepository;
1666
1950
  exports.SchemaPlugin = SchemaPlugin;
1667
1951
  exports.SearchPlugin = SearchPlugin;
1952
+ exports.SimpleCache = SimpleCache;
1668
1953
  exports.XataApiClient = XataApiClient;
1669
1954
  exports.XataApiPlugin = XataApiPlugin;
1670
1955
  exports.XataError = XataError;
1671
1956
  exports.XataPlugin = XataPlugin;
1672
1957
  exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
1958
+ exports.addGitBranchesEntry = addGitBranchesEntry;
1673
1959
  exports.addTableColumn = addTableColumn;
1674
1960
  exports.buildClient = buildClient;
1675
1961
  exports.bulkInsertTableRecords = bulkInsertTableRecords;
@@ -1704,6 +1990,7 @@ exports.getCurrentBranchDetails = getCurrentBranchDetails;
1704
1990
  exports.getCurrentBranchName = getCurrentBranchName;
1705
1991
  exports.getDatabaseList = getDatabaseList;
1706
1992
  exports.getDatabaseURL = getDatabaseURL;
1993
+ exports.getGitBranchesMapping = getGitBranchesMapping;
1707
1994
  exports.getRecord = getRecord;
1708
1995
  exports.getTableColumns = getTableColumns;
1709
1996
  exports.getTableSchema = getTableSchema;
@@ -1732,9 +2019,12 @@ exports.notExists = notExists;
1732
2019
  exports.operationsByTag = operationsByTag;
1733
2020
  exports.pattern = pattern;
1734
2021
  exports.queryTable = queryTable;
2022
+ exports.removeGitBranchesEntry = removeGitBranchesEntry;
1735
2023
  exports.removeWorkspaceMember = removeWorkspaceMember;
1736
2024
  exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
2025
+ exports.resolveBranch = resolveBranch;
1737
2026
  exports.searchBranch = searchBranch;
2027
+ exports.searchTable = searchTable;
1738
2028
  exports.setTableSchema = setTableSchema;
1739
2029
  exports.startsWith = startsWith;
1740
2030
  exports.updateBranchMetadata = updateBranchMetadata;