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

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",
@@ -373,7 +391,15 @@ const operationsByTag = {
373
391
  resendWorkspaceMemberInvite,
374
392
  acceptWorkspaceMemberInvite
375
393
  },
376
- database: { getDatabaseList, createDatabase, deleteDatabase },
394
+ database: {
395
+ getDatabaseList,
396
+ createDatabase,
397
+ deleteDatabase,
398
+ getGitBranchesMapping,
399
+ addGitBranchesEntry,
400
+ removeGitBranchesEntry,
401
+ resolveBranch
402
+ },
377
403
  branch: {
378
404
  getBranchList,
379
405
  getBranchDetails,
@@ -436,35 +462,35 @@ function isValidBuilder(builder) {
436
462
  return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
437
463
  }
438
464
 
439
- var __accessCheck$6 = (obj, member, msg) => {
465
+ var __accessCheck$7 = (obj, member, msg) => {
440
466
  if (!member.has(obj))
441
467
  throw TypeError("Cannot " + msg);
442
468
  };
443
- var __privateGet$5 = (obj, member, getter) => {
444
- __accessCheck$6(obj, member, "read from private field");
469
+ var __privateGet$7 = (obj, member, getter) => {
470
+ __accessCheck$7(obj, member, "read from private field");
445
471
  return getter ? getter.call(obj) : member.get(obj);
446
472
  };
447
- var __privateAdd$6 = (obj, member, value) => {
473
+ var __privateAdd$7 = (obj, member, value) => {
448
474
  if (member.has(obj))
449
475
  throw TypeError("Cannot add the same private member more than once");
450
476
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
451
477
  };
452
- var __privateSet$4 = (obj, member, value, setter) => {
453
- __accessCheck$6(obj, member, "write to private field");
478
+ var __privateSet$6 = (obj, member, value, setter) => {
479
+ __accessCheck$7(obj, member, "write to private field");
454
480
  setter ? setter.call(obj, value) : member.set(obj, value);
455
481
  return value;
456
482
  };
457
483
  var _extraProps, _namespaces;
458
484
  class XataApiClient {
459
485
  constructor(options = {}) {
460
- __privateAdd$6(this, _extraProps, void 0);
461
- __privateAdd$6(this, _namespaces, {});
486
+ __privateAdd$7(this, _extraProps, void 0);
487
+ __privateAdd$7(this, _namespaces, {});
462
488
  const provider = options.host ?? "production";
463
489
  const apiKey = options?.apiKey ?? getAPIKey();
464
490
  if (!apiKey) {
465
491
  throw new Error("Could not resolve a valid apiKey");
466
492
  }
467
- __privateSet$4(this, _extraProps, {
493
+ __privateSet$6(this, _extraProps, {
468
494
  apiUrl: getHostUrl(provider, "main"),
469
495
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
470
496
  fetchImpl: getFetchImplementation(options.fetch),
@@ -472,34 +498,34 @@ class XataApiClient {
472
498
  });
473
499
  }
474
500
  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;
501
+ if (!__privateGet$7(this, _namespaces).user)
502
+ __privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
503
+ return __privateGet$7(this, _namespaces).user;
478
504
  }
479
505
  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;
506
+ if (!__privateGet$7(this, _namespaces).workspaces)
507
+ __privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
508
+ return __privateGet$7(this, _namespaces).workspaces;
483
509
  }
484
510
  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;
511
+ if (!__privateGet$7(this, _namespaces).databases)
512
+ __privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
513
+ return __privateGet$7(this, _namespaces).databases;
488
514
  }
489
515
  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;
516
+ if (!__privateGet$7(this, _namespaces).branches)
517
+ __privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
518
+ return __privateGet$7(this, _namespaces).branches;
493
519
  }
494
520
  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;
521
+ if (!__privateGet$7(this, _namespaces).tables)
522
+ __privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
523
+ return __privateGet$7(this, _namespaces).tables;
498
524
  }
499
525
  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;
526
+ if (!__privateGet$7(this, _namespaces).records)
527
+ __privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
528
+ return __privateGet$7(this, _namespaces).records;
503
529
  }
504
530
  }
505
531
  _extraProps = new WeakMap();
@@ -633,6 +659,33 @@ class DatabaseApi {
633
659
  ...this.extraProps
634
660
  });
635
661
  }
662
+ getGitBranchesMapping(workspace, dbName) {
663
+ return operationsByTag.database.getGitBranchesMapping({
664
+ pathParams: { workspace, dbName },
665
+ ...this.extraProps
666
+ });
667
+ }
668
+ addGitBranchesEntry(workspace, dbName, body) {
669
+ return operationsByTag.database.addGitBranchesEntry({
670
+ pathParams: { workspace, dbName },
671
+ body,
672
+ ...this.extraProps
673
+ });
674
+ }
675
+ removeGitBranchesEntry(workspace, dbName, gitBranch) {
676
+ return operationsByTag.database.removeGitBranchesEntry({
677
+ pathParams: { workspace, dbName },
678
+ queryParams: { gitBranch },
679
+ ...this.extraProps
680
+ });
681
+ }
682
+ resolveBranch(workspace, dbName, gitBranch) {
683
+ return operationsByTag.database.resolveBranch({
684
+ pathParams: { workspace, dbName },
685
+ queryParams: { gitBranch },
686
+ ...this.extraProps
687
+ });
688
+ }
636
689
  }
637
690
  class BranchApi {
638
691
  constructor(extraProps) {
@@ -650,10 +703,10 @@ class BranchApi {
650
703
  ...this.extraProps
651
704
  });
652
705
  }
653
- createBranch(workspace, database, branch, from = "", options = {}) {
706
+ createBranch(workspace, database, branch, from, options = {}) {
654
707
  return operationsByTag.branch.createBranch({
655
708
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
656
- queryParams: { from },
709
+ queryParams: isString(from) ? { from } : void 0,
657
710
  body: options,
658
711
  ...this.extraProps
659
712
  });
@@ -854,43 +907,43 @@ class XataApiPlugin {
854
907
  class XataPlugin {
855
908
  }
856
909
 
857
- var __accessCheck$5 = (obj, member, msg) => {
910
+ var __accessCheck$6 = (obj, member, msg) => {
858
911
  if (!member.has(obj))
859
912
  throw TypeError("Cannot " + msg);
860
913
  };
861
- var __privateGet$4 = (obj, member, getter) => {
862
- __accessCheck$5(obj, member, "read from private field");
914
+ var __privateGet$6 = (obj, member, getter) => {
915
+ __accessCheck$6(obj, member, "read from private field");
863
916
  return getter ? getter.call(obj) : member.get(obj);
864
917
  };
865
- var __privateAdd$5 = (obj, member, value) => {
918
+ var __privateAdd$6 = (obj, member, value) => {
866
919
  if (member.has(obj))
867
920
  throw TypeError("Cannot add the same private member more than once");
868
921
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
869
922
  };
870
- var __privateSet$3 = (obj, member, value, setter) => {
871
- __accessCheck$5(obj, member, "write to private field");
923
+ var __privateSet$5 = (obj, member, value, setter) => {
924
+ __accessCheck$6(obj, member, "write to private field");
872
925
  setter ? setter.call(obj, value) : member.set(obj, value);
873
926
  return value;
874
927
  };
875
928
  var _query;
876
929
  class Page {
877
930
  constructor(query, meta, records = []) {
878
- __privateAdd$5(this, _query, void 0);
879
- __privateSet$3(this, _query, query);
931
+ __privateAdd$6(this, _query, void 0);
932
+ __privateSet$5(this, _query, query);
880
933
  this.meta = meta;
881
934
  this.records = records;
882
935
  }
883
936
  async nextPage(size, offset) {
884
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
937
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
885
938
  }
886
939
  async previousPage(size, offset) {
887
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
940
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
888
941
  }
889
942
  async firstPage(size, offset) {
890
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
943
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
891
944
  }
892
945
  async lastPage(size, offset) {
893
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
946
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
894
947
  }
895
948
  hasNextPage() {
896
949
  return this.meta.page.more;
@@ -902,46 +955,47 @@ const PAGINATION_DEFAULT_SIZE = 200;
902
955
  const PAGINATION_MAX_OFFSET = 800;
903
956
  const PAGINATION_DEFAULT_OFFSET = 0;
904
957
 
905
- var __accessCheck$4 = (obj, member, msg) => {
958
+ var __accessCheck$5 = (obj, member, msg) => {
906
959
  if (!member.has(obj))
907
960
  throw TypeError("Cannot " + msg);
908
961
  };
909
- var __privateGet$3 = (obj, member, getter) => {
910
- __accessCheck$4(obj, member, "read from private field");
962
+ var __privateGet$5 = (obj, member, getter) => {
963
+ __accessCheck$5(obj, member, "read from private field");
911
964
  return getter ? getter.call(obj) : member.get(obj);
912
965
  };
913
- var __privateAdd$4 = (obj, member, value) => {
966
+ var __privateAdd$5 = (obj, member, value) => {
914
967
  if (member.has(obj))
915
968
  throw TypeError("Cannot add the same private member more than once");
916
969
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
917
970
  };
918
- var __privateSet$2 = (obj, member, value, setter) => {
919
- __accessCheck$4(obj, member, "write to private field");
971
+ var __privateSet$4 = (obj, member, value, setter) => {
972
+ __accessCheck$5(obj, member, "write to private field");
920
973
  setter ? setter.call(obj, value) : member.set(obj, value);
921
974
  return value;
922
975
  };
923
976
  var _table$1, _repository, _data;
924
977
  const _Query = class {
925
978
  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: {} });
979
+ __privateAdd$5(this, _table$1, void 0);
980
+ __privateAdd$5(this, _repository, void 0);
981
+ __privateAdd$5(this, _data, { filter: {} });
929
982
  this.meta = { page: { cursor: "start", more: true } };
930
983
  this.records = [];
931
- __privateSet$2(this, _table$1, table);
984
+ __privateSet$4(this, _table$1, table);
932
985
  if (repository) {
933
- __privateSet$2(this, _repository, repository);
986
+ __privateSet$4(this, _repository, repository);
934
987
  } else {
935
- __privateSet$2(this, _repository, this);
988
+ __privateSet$4(this, _repository, this);
936
989
  }
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;
990
+ __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
991
+ __privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
992
+ __privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
993
+ __privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
994
+ __privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
995
+ __privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
996
+ __privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
997
+ __privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
998
+ __privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
945
999
  this.any = this.any.bind(this);
946
1000
  this.all = this.all.bind(this);
947
1001
  this.not = this.not.bind(this);
@@ -952,58 +1006,64 @@ const _Query = class {
952
1006
  Object.defineProperty(this, "repository", { enumerable: false });
953
1007
  }
954
1008
  getQueryOptions() {
955
- return __privateGet$3(this, _data);
1009
+ return __privateGet$5(this, _data);
1010
+ }
1011
+ key() {
1012
+ const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
1013
+ const key = JSON.stringify({ columns, filter, sort, pagination });
1014
+ return toBase64(key);
956
1015
  }
957
1016
  any(...queries) {
958
1017
  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));
1018
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
960
1019
  }
961
1020
  all(...queries) {
962
1021
  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));
1022
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
964
1023
  }
965
1024
  not(...queries) {
966
1025
  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));
1026
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
968
1027
  }
969
1028
  none(...queries) {
970
1029
  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));
1030
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
972
1031
  }
973
1032
  filter(a, b) {
974
1033
  if (arguments.length === 1) {
975
1034
  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));
1035
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
1036
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
978
1037
  } 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));
1038
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1039
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
981
1040
  }
982
1041
  }
983
1042
  sort(column, direction) {
984
- const originalSort = [__privateGet$3(this, _data).sort ?? []].flat();
1043
+ const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
985
1044
  const sort = [...originalSort, { column, direction }];
986
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { sort }, __privateGet$3(this, _data));
1045
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
987
1046
  }
988
1047
  select(columns) {
989
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { columns }, __privateGet$3(this, _data));
1048
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
990
1049
  }
991
1050
  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);
1051
+ const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
1052
+ return __privateGet$5(this, _repository).query(query);
994
1053
  }
995
1054
  async *[Symbol.asyncIterator]() {
996
- for await (const [record] of this.getIterator(1)) {
1055
+ for await (const [record] of this.getIterator({ batchSize: 1 })) {
997
1056
  yield record;
998
1057
  }
999
1058
  }
1000
- async *getIterator(chunk, options = {}) {
1059
+ async *getIterator(options = {}) {
1060
+ const { batchSize = 1 } = options;
1001
1061
  let offset = 0;
1002
1062
  let end = false;
1003
1063
  while (!end) {
1004
- const { records, meta } = await this.getPaginated({ ...options, page: { size: chunk, offset } });
1064
+ const { records, meta } = await this.getPaginated({ ...options, pagination: { size: batchSize, offset } });
1005
1065
  yield records;
1006
- offset += chunk;
1066
+ offset += batchSize;
1007
1067
  end = !meta.page.more;
1008
1068
  }
1009
1069
  }
@@ -1011,17 +1071,21 @@ const _Query = class {
1011
1071
  const { records } = await this.getPaginated(options);
1012
1072
  return records;
1013
1073
  }
1014
- async getAll(chunk = PAGINATION_MAX_SIZE, options = {}) {
1074
+ async getAll(options = {}) {
1075
+ const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
1015
1076
  const results = [];
1016
- for await (const page of this.getIterator(chunk, options)) {
1077
+ for await (const page of this.getIterator({ ...rest, batchSize })) {
1017
1078
  results.push(...page);
1018
1079
  }
1019
1080
  return results;
1020
1081
  }
1021
- async getOne(options = {}) {
1022
- const records = await this.getMany({ ...options, page: { size: 1 } });
1082
+ async getFirst(options = {}) {
1083
+ const records = await this.getMany({ ...options, pagination: { size: 1 } });
1023
1084
  return records[0] || null;
1024
1085
  }
1086
+ cache(ttl) {
1087
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
1088
+ }
1025
1089
  nextPage(size, offset) {
1026
1090
  return this.firstPage(size, offset);
1027
1091
  }
@@ -1029,10 +1093,10 @@ const _Query = class {
1029
1093
  return this.firstPage(size, offset);
1030
1094
  }
1031
1095
  firstPage(size, offset) {
1032
- return this.getPaginated({ page: { size, offset } });
1096
+ return this.getPaginated({ pagination: { size, offset } });
1033
1097
  }
1034
1098
  lastPage(size, offset) {
1035
- return this.getPaginated({ page: { size, offset, before: "end" } });
1099
+ return this.getPaginated({ pagination: { size, offset, before: "end" } });
1036
1100
  }
1037
1101
  hasNextPage() {
1038
1102
  return this.meta.page.more;
@@ -1073,75 +1137,94 @@ function buildSortFilter(filter) {
1073
1137
  }
1074
1138
  }
1075
1139
 
1076
- var __accessCheck$3 = (obj, member, msg) => {
1140
+ var __accessCheck$4 = (obj, member, msg) => {
1077
1141
  if (!member.has(obj))
1078
1142
  throw TypeError("Cannot " + msg);
1079
1143
  };
1080
- var __privateGet$2 = (obj, member, getter) => {
1081
- __accessCheck$3(obj, member, "read from private field");
1144
+ var __privateGet$4 = (obj, member, getter) => {
1145
+ __accessCheck$4(obj, member, "read from private field");
1082
1146
  return getter ? getter.call(obj) : member.get(obj);
1083
1147
  };
1084
- var __privateAdd$3 = (obj, member, value) => {
1148
+ var __privateAdd$4 = (obj, member, value) => {
1085
1149
  if (member.has(obj))
1086
1150
  throw TypeError("Cannot add the same private member more than once");
1087
1151
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1088
1152
  };
1089
- var __privateSet$1 = (obj, member, value, setter) => {
1090
- __accessCheck$3(obj, member, "write to private field");
1153
+ var __privateSet$3 = (obj, member, value, setter) => {
1154
+ __accessCheck$4(obj, member, "write to private field");
1091
1155
  setter ? setter.call(obj, value) : member.set(obj, value);
1092
1156
  return value;
1093
1157
  };
1094
1158
  var __privateMethod$2 = (obj, member, method) => {
1095
- __accessCheck$3(obj, member, "access private method");
1159
+ __accessCheck$4(obj, member, "access private method");
1096
1160
  return method;
1097
1161
  };
1098
- var _table, _links, _getFetchProps, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn;
1162
+ 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
1163
  class Repository extends Query {
1100
1164
  }
1101
1165
  class RestRepository extends Query {
1102
1166
  constructor(options) {
1103
1167
  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);
1168
+ __privateAdd$4(this, _insertRecordWithoutId);
1169
+ __privateAdd$4(this, _insertRecordWithId);
1170
+ __privateAdd$4(this, _bulkInsertTableRecords);
1171
+ __privateAdd$4(this, _updateRecordWithID);
1172
+ __privateAdd$4(this, _upsertRecordWithID);
1173
+ __privateAdd$4(this, _deleteRecord);
1174
+ __privateAdd$4(this, _invalidateCache);
1175
+ __privateAdd$4(this, _setCacheRecord);
1176
+ __privateAdd$4(this, _getCacheRecord);
1177
+ __privateAdd$4(this, _setCacheQuery);
1178
+ __privateAdd$4(this, _getCacheQuery);
1179
+ __privateAdd$4(this, _getSchema$1);
1180
+ __privateAdd$4(this, _table, void 0);
1181
+ __privateAdd$4(this, _getFetchProps, void 0);
1182
+ __privateAdd$4(this, _cache, void 0);
1183
+ __privateAdd$4(this, _schema$1, void 0);
1184
+ __privateSet$3(this, _table, options.table);
1185
+ __privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
1116
1186
  this.db = options.db;
1187
+ __privateSet$3(this, _cache, options.pluginOptions.cache);
1117
1188
  }
1118
1189
  async create(a, b) {
1119
1190
  if (Array.isArray(a)) {
1120
- return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1191
+ const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1192
+ await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1193
+ return records;
1121
1194
  }
1122
1195
  if (isString(a) && isObject(b)) {
1123
1196
  if (a === "")
1124
1197
  throw new Error("The id can't be empty");
1125
- return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1198
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1199
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1200
+ return record;
1126
1201
  }
1127
1202
  if (isObject(a) && isString(a.id)) {
1128
1203
  if (a.id === "")
1129
1204
  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 });
1205
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
1206
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1207
+ return record;
1131
1208
  }
1132
1209
  if (isObject(a)) {
1133
- return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1210
+ const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1211
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1212
+ return record;
1134
1213
  }
1135
1214
  throw new Error("Invalid arguments for create method");
1136
1215
  }
1137
1216
  async read(recordId) {
1138
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1217
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1218
+ if (cacheRecord)
1219
+ return cacheRecord;
1220
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1139
1221
  try {
1140
1222
  const response = await getRecord({
1141
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1223
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1142
1224
  ...fetchProps
1143
1225
  });
1144
- return initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), response);
1226
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1227
+ return initObject(this.db, schema, __privateGet$4(this, _table), response);
1145
1228
  } catch (e) {
1146
1229
  if (isObject(e) && e.status === 404) {
1147
1230
  return null;
@@ -1157,10 +1240,16 @@ class RestRepository extends Query {
1157
1240
  return Promise.all(a.map((object) => this.update(object)));
1158
1241
  }
1159
1242
  if (isString(a) && isObject(b)) {
1160
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1243
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1244
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1245
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1246
+ return record;
1161
1247
  }
1162
1248
  if (isObject(a) && isString(a.id)) {
1163
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1249
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1250
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1251
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1252
+ return record;
1164
1253
  }
1165
1254
  throw new Error("Invalid arguments for update method");
1166
1255
  }
@@ -1172,70 +1261,85 @@ class RestRepository extends Query {
1172
1261
  return Promise.all(a.map((object) => this.createOrUpdate(object)));
1173
1262
  }
1174
1263
  if (isString(a) && isObject(b)) {
1175
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1264
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1265
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1266
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1267
+ return record;
1176
1268
  }
1177
1269
  if (isObject(a) && isString(a.id)) {
1178
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1270
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1271
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1272
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1273
+ return record;
1179
1274
  }
1180
1275
  throw new Error("Invalid arguments for createOrUpdate method");
1181
1276
  }
1182
- async delete(recordId) {
1183
- if (Array.isArray(recordId)) {
1184
- if (recordId.length > 100) {
1277
+ async delete(a) {
1278
+ if (Array.isArray(a)) {
1279
+ if (a.length > 100) {
1185
1280
  console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1186
1281
  }
1187
- await Promise.all(recordId.map((id) => this.delete(id)));
1282
+ await Promise.all(a.map((id) => this.delete(id)));
1188
1283
  return;
1189
1284
  }
1190
- if (isString(recordId)) {
1191
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId);
1285
+ if (isString(a)) {
1286
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
1287
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1192
1288
  return;
1193
1289
  }
1194
- if (isObject(recordId) && isString(recordId.id)) {
1195
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId.id);
1290
+ if (isObject(a) && isString(a.id)) {
1291
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
1292
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1196
1293
  return;
1197
1294
  }
1198
1295
  throw new Error("Invalid arguments for delete method");
1199
1296
  }
1200
1297
  async search(query, options = {}) {
1201
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1298
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1202
1299
  const { records } = await searchBranch({
1203
1300
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1204
- body: { tables: [__privateGet$2(this, _table)], query, fuzziness: options.fuzziness },
1301
+ body: { tables: [__privateGet$4(this, _table)], query, fuzziness: options.fuzziness },
1205
1302
  ...fetchProps
1206
1303
  });
1207
- return records.map((item) => initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), item));
1304
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1305
+ return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
1208
1306
  }
1209
1307
  async query(query) {
1308
+ const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
1309
+ if (cacheQuery)
1310
+ return new Page(query, cacheQuery.meta, cacheQuery.records);
1210
1311
  const data = query.getQueryOptions();
1211
1312
  const body = {
1212
1313
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1213
1314
  sort: data.sort ? buildSortFilter(data.sort) : void 0,
1214
- page: data.page,
1315
+ page: data.pagination,
1215
1316
  columns: data.columns
1216
1317
  };
1217
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1318
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1218
1319
  const { meta, records: objects } = await queryTable({
1219
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1320
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1220
1321
  body,
1221
1322
  ...fetchProps
1222
1323
  });
1223
- const records = objects.map((record) => initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), record));
1324
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1325
+ const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
1326
+ await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1224
1327
  return new Page(query, meta, records);
1225
1328
  }
1226
1329
  }
1227
1330
  _table = new WeakMap();
1228
- _links = new WeakMap();
1229
1331
  _getFetchProps = new WeakMap();
1332
+ _cache = new WeakMap();
1333
+ _schema$1 = new WeakMap();
1230
1334
  _insertRecordWithoutId = new WeakSet();
1231
1335
  insertRecordWithoutId_fn = async function(object) {
1232
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1336
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1233
1337
  const record = transformObjectLinks(object);
1234
1338
  const response = await insertRecord({
1235
1339
  pathParams: {
1236
1340
  workspace: "{workspaceId}",
1237
1341
  dbBranchName: "{dbBranch}",
1238
- tableName: __privateGet$2(this, _table)
1342
+ tableName: __privateGet$4(this, _table)
1239
1343
  },
1240
1344
  body: record,
1241
1345
  ...fetchProps
@@ -1248,13 +1352,13 @@ insertRecordWithoutId_fn = async function(object) {
1248
1352
  };
1249
1353
  _insertRecordWithId = new WeakSet();
1250
1354
  insertRecordWithId_fn = async function(recordId, object) {
1251
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1355
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1252
1356
  const record = transformObjectLinks(object);
1253
1357
  const response = await insertRecordWithID({
1254
1358
  pathParams: {
1255
1359
  workspace: "{workspaceId}",
1256
1360
  dbBranchName: "{dbBranch}",
1257
- tableName: __privateGet$2(this, _table),
1361
+ tableName: __privateGet$4(this, _table),
1258
1362
  recordId
1259
1363
  },
1260
1364
  body: record,
@@ -1269,10 +1373,10 @@ insertRecordWithId_fn = async function(recordId, object) {
1269
1373
  };
1270
1374
  _bulkInsertTableRecords = new WeakSet();
1271
1375
  bulkInsertTableRecords_fn = async function(objects) {
1272
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1376
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1273
1377
  const records = objects.map((object) => transformObjectLinks(object));
1274
1378
  const response = await bulkInsertTableRecords({
1275
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1379
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1276
1380
  body: { records },
1277
1381
  ...fetchProps
1278
1382
  });
@@ -1284,10 +1388,10 @@ bulkInsertTableRecords_fn = async function(objects) {
1284
1388
  };
1285
1389
  _updateRecordWithID = new WeakSet();
1286
1390
  updateRecordWithID_fn = async function(recordId, object) {
1287
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1391
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1288
1392
  const record = transformObjectLinks(object);
1289
1393
  const response = await updateRecordWithID({
1290
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1394
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1291
1395
  body: record,
1292
1396
  ...fetchProps
1293
1397
  });
@@ -1298,9 +1402,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1298
1402
  };
1299
1403
  _upsertRecordWithID = new WeakSet();
1300
1404
  upsertRecordWithID_fn = async function(recordId, object) {
1301
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1405
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1302
1406
  const response = await upsertRecordWithID({
1303
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1407
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1304
1408
  body: object,
1305
1409
  ...fetchProps
1306
1410
  });
@@ -1311,11 +1415,62 @@ upsertRecordWithID_fn = async function(recordId, object) {
1311
1415
  };
1312
1416
  _deleteRecord = new WeakSet();
1313
1417
  deleteRecord_fn = async function(recordId) {
1314
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1418
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1315
1419
  await deleteRecord({
1316
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1420
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1421
+ ...fetchProps
1422
+ });
1423
+ };
1424
+ _invalidateCache = new WeakSet();
1425
+ invalidateCache_fn = async function(recordId) {
1426
+ await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1427
+ const cacheItems = await __privateGet$4(this, _cache).getAll();
1428
+ const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1429
+ for (const [key, value] of queries) {
1430
+ const ids = getIds(value);
1431
+ if (ids.includes(recordId))
1432
+ await __privateGet$4(this, _cache).delete(key);
1433
+ }
1434
+ };
1435
+ _setCacheRecord = new WeakSet();
1436
+ setCacheRecord_fn = async function(record) {
1437
+ if (!__privateGet$4(this, _cache).cacheRecords)
1438
+ return;
1439
+ await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
1440
+ };
1441
+ _getCacheRecord = new WeakSet();
1442
+ getCacheRecord_fn = async function(recordId) {
1443
+ if (!__privateGet$4(this, _cache).cacheRecords)
1444
+ return null;
1445
+ return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1446
+ };
1447
+ _setCacheQuery = new WeakSet();
1448
+ setCacheQuery_fn = async function(query, meta, records) {
1449
+ await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1450
+ };
1451
+ _getCacheQuery = new WeakSet();
1452
+ getCacheQuery_fn = async function(query) {
1453
+ const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
1454
+ const result = await __privateGet$4(this, _cache).get(key);
1455
+ if (!result)
1456
+ return null;
1457
+ const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1458
+ if (ttl < 0)
1459
+ return null;
1460
+ const hasExpired = result.date.getTime() + ttl < Date.now();
1461
+ return hasExpired ? null : result;
1462
+ };
1463
+ _getSchema$1 = new WeakSet();
1464
+ getSchema_fn$1 = async function() {
1465
+ if (__privateGet$4(this, _schema$1))
1466
+ return __privateGet$4(this, _schema$1);
1467
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1468
+ const { schema } = await getBranchDetails({
1469
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1317
1470
  ...fetchProps
1318
1471
  });
1472
+ __privateSet$3(this, _schema$1, schema);
1473
+ return schema;
1319
1474
  };
1320
1475
  const transformObjectLinks = (object) => {
1321
1476
  return Object.entries(object).reduce((acc, [key, value]) => {
@@ -1324,15 +1479,33 @@ const transformObjectLinks = (object) => {
1324
1479
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1325
1480
  }, {});
1326
1481
  };
1327
- const initObject = (db, links, table, object) => {
1482
+ const initObject = (db, schema, table, object) => {
1328
1483
  const result = {};
1329
1484
  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);
1485
+ const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
1486
+ if (!columns)
1487
+ console.error(`Table ${table} not found in schema`);
1488
+ for (const column of columns ?? []) {
1489
+ const value = result[column.name];
1490
+ switch (column.type) {
1491
+ case "datetime": {
1492
+ const date = new Date(value);
1493
+ if (isNaN(date.getTime())) {
1494
+ console.error(`Failed to parse date ${value} for field ${column.name}`);
1495
+ } else {
1496
+ result[column.name] = date;
1497
+ }
1498
+ break;
1499
+ }
1500
+ case "link": {
1501
+ const linkTable = column.link?.table;
1502
+ if (!linkTable) {
1503
+ console.error(`Failed to parse link for field ${column.name}`);
1504
+ } else if (value && isObject(value)) {
1505
+ result[column.name] = initObject(db, schema, linkTable, value);
1506
+ }
1507
+ break;
1508
+ }
1336
1509
  }
1337
1510
  }
1338
1511
  result.read = function() {
@@ -1350,6 +1523,65 @@ const initObject = (db, links, table, object) => {
1350
1523
  Object.freeze(result);
1351
1524
  return result;
1352
1525
  };
1526
+ function getIds(value) {
1527
+ if (Array.isArray(value)) {
1528
+ return value.map((item) => getIds(item)).flat();
1529
+ }
1530
+ if (!isObject(value))
1531
+ return [];
1532
+ const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
1533
+ return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
1534
+ }
1535
+
1536
+ var __accessCheck$3 = (obj, member, msg) => {
1537
+ if (!member.has(obj))
1538
+ throw TypeError("Cannot " + msg);
1539
+ };
1540
+ var __privateGet$3 = (obj, member, getter) => {
1541
+ __accessCheck$3(obj, member, "read from private field");
1542
+ return getter ? getter.call(obj) : member.get(obj);
1543
+ };
1544
+ var __privateAdd$3 = (obj, member, value) => {
1545
+ if (member.has(obj))
1546
+ throw TypeError("Cannot add the same private member more than once");
1547
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1548
+ };
1549
+ var __privateSet$2 = (obj, member, value, setter) => {
1550
+ __accessCheck$3(obj, member, "write to private field");
1551
+ setter ? setter.call(obj, value) : member.set(obj, value);
1552
+ return value;
1553
+ };
1554
+ var _map;
1555
+ class SimpleCache {
1556
+ constructor(options = {}) {
1557
+ __privateAdd$3(this, _map, void 0);
1558
+ __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1559
+ this.capacity = options.max ?? 500;
1560
+ this.cacheRecords = options.cacheRecords ?? true;
1561
+ this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1562
+ }
1563
+ async getAll() {
1564
+ return Object.fromEntries(__privateGet$3(this, _map));
1565
+ }
1566
+ async get(key) {
1567
+ return __privateGet$3(this, _map).get(key) ?? null;
1568
+ }
1569
+ async set(key, value) {
1570
+ await this.delete(key);
1571
+ __privateGet$3(this, _map).set(key, value);
1572
+ if (__privateGet$3(this, _map).size > this.capacity) {
1573
+ const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
1574
+ await this.delete(leastRecentlyUsed);
1575
+ }
1576
+ }
1577
+ async delete(key) {
1578
+ __privateGet$3(this, _map).delete(key);
1579
+ }
1580
+ async clear() {
1581
+ return __privateGet$3(this, _map).clear();
1582
+ }
1583
+ }
1584
+ _map = new WeakMap();
1353
1585
 
1354
1586
  const gt = (value) => ({ $gt: value });
1355
1587
  const ge = (value) => ({ $ge: value });
@@ -1374,7 +1606,7 @@ var __accessCheck$2 = (obj, member, msg) => {
1374
1606
  if (!member.has(obj))
1375
1607
  throw TypeError("Cannot " + msg);
1376
1608
  };
1377
- var __privateGet$1 = (obj, member, getter) => {
1609
+ var __privateGet$2 = (obj, member, getter) => {
1378
1610
  __accessCheck$2(obj, member, "read from private field");
1379
1611
  return getter ? getter.call(obj) : member.get(obj);
1380
1612
  };
@@ -1385,26 +1617,24 @@ var __privateAdd$2 = (obj, member, value) => {
1385
1617
  };
1386
1618
  var _tables;
1387
1619
  class SchemaPlugin extends XataPlugin {
1388
- constructor(links, tableNames) {
1620
+ constructor(tableNames) {
1389
1621
  super();
1390
- this.links = links;
1391
1622
  this.tableNames = tableNames;
1392
1623
  __privateAdd$2(this, _tables, {});
1393
1624
  }
1394
- build(options) {
1395
- const { getFetchProps } = options;
1396
- const links = this.links;
1625
+ build(pluginOptions) {
1397
1626
  const db = new Proxy({}, {
1398
1627
  get: (_target, table) => {
1399
1628
  if (!isString(table))
1400
1629
  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];
1630
+ if (!__privateGet$2(this, _tables)[table]) {
1631
+ __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1632
+ }
1633
+ return __privateGet$2(this, _tables)[table];
1404
1634
  }
1405
1635
  });
1406
1636
  for (const table of this.tableNames ?? []) {
1407
- db[table] = new RestRepository({ db, getFetchProps, table, links });
1637
+ db[table] = new RestRepository({ db, pluginOptions, table });
1408
1638
  }
1409
1639
  return db;
1410
1640
  }
@@ -1415,44 +1645,57 @@ var __accessCheck$1 = (obj, member, msg) => {
1415
1645
  if (!member.has(obj))
1416
1646
  throw TypeError("Cannot " + msg);
1417
1647
  };
1648
+ var __privateGet$1 = (obj, member, getter) => {
1649
+ __accessCheck$1(obj, member, "read from private field");
1650
+ return getter ? getter.call(obj) : member.get(obj);
1651
+ };
1418
1652
  var __privateAdd$1 = (obj, member, value) => {
1419
1653
  if (member.has(obj))
1420
1654
  throw TypeError("Cannot add the same private member more than once");
1421
1655
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1422
1656
  };
1657
+ var __privateSet$1 = (obj, member, value, setter) => {
1658
+ __accessCheck$1(obj, member, "write to private field");
1659
+ setter ? setter.call(obj, value) : member.set(obj, value);
1660
+ return value;
1661
+ };
1423
1662
  var __privateMethod$1 = (obj, member, method) => {
1424
1663
  __accessCheck$1(obj, member, "access private method");
1425
1664
  return method;
1426
1665
  };
1427
- var _search, search_fn;
1666
+ var _schema, _search, search_fn, _getSchema, getSchema_fn;
1428
1667
  class SearchPlugin extends XataPlugin {
1429
- constructor(db, links) {
1668
+ constructor(db) {
1430
1669
  super();
1431
1670
  this.db = db;
1432
- this.links = links;
1433
1671
  __privateAdd$1(this, _search);
1672
+ __privateAdd$1(this, _getSchema);
1673
+ __privateAdd$1(this, _schema, void 0);
1434
1674
  }
1435
1675
  build({ getFetchProps }) {
1436
1676
  return {
1437
1677
  all: async (query, options = {}) => {
1438
1678
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1679
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1439
1680
  return records.map((record) => {
1440
1681
  const { table = "orphan" } = record.xata;
1441
- return { table, record: initObject(this.db, this.links, table, record) };
1682
+ return { table, record: initObject(this.db, schema, table, record) };
1442
1683
  });
1443
1684
  },
1444
1685
  byTable: async (query, options = {}) => {
1445
1686
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1687
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1446
1688
  return records.reduce((acc, record) => {
1447
1689
  const { table = "orphan" } = record.xata;
1448
1690
  const items = acc[table] ?? [];
1449
- const item = initObject(this.db, this.links, table, record);
1691
+ const item = initObject(this.db, schema, table, record);
1450
1692
  return { ...acc, [table]: [...items, item] };
1451
1693
  }, {});
1452
1694
  }
1453
1695
  };
1454
1696
  }
1455
1697
  }
1698
+ _schema = new WeakMap();
1456
1699
  _search = new WeakSet();
1457
1700
  search_fn = async function(query, options, getFetchProps) {
1458
1701
  const fetchProps = await getFetchProps();
@@ -1464,6 +1707,18 @@ search_fn = async function(query, options, getFetchProps) {
1464
1707
  });
1465
1708
  return records;
1466
1709
  };
1710
+ _getSchema = new WeakSet();
1711
+ getSchema_fn = async function(getFetchProps) {
1712
+ if (__privateGet$1(this, _schema))
1713
+ return __privateGet$1(this, _schema);
1714
+ const fetchProps = await getFetchProps();
1715
+ const { schema } = await getBranchDetails({
1716
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1717
+ ...fetchProps
1718
+ });
1719
+ __privateSet$1(this, _schema, schema);
1720
+ return schema;
1721
+ };
1467
1722
 
1468
1723
  const isBranchStrategyBuilder = (strategy) => {
1469
1724
  return typeof strategy === "function";
@@ -1477,19 +1732,19 @@ const envBranchNames = [
1477
1732
  ];
1478
1733
  const defaultBranch = "main";
1479
1734
  async function getCurrentBranchName(options) {
1480
- const env = await getBranchByEnvVariable();
1735
+ const env = getBranchByEnvVariable();
1481
1736
  if (env)
1482
1737
  return env;
1483
1738
  const branch = await getGitBranch();
1484
1739
  if (!branch)
1485
- return defaultBranch;
1740
+ return void 0;
1486
1741
  const details = await getDatabaseBranch(branch, options);
1487
1742
  if (details)
1488
1743
  return branch;
1489
- return defaultBranch;
1744
+ return void 0;
1490
1745
  }
1491
1746
  async function getCurrentBranchDetails(options) {
1492
- const env = await getBranchByEnvVariable();
1747
+ const env = getBranchByEnvVariable();
1493
1748
  if (env)
1494
1749
  return getDatabaseBranch(env, options);
1495
1750
  const branch = await getGitBranch();
@@ -1572,22 +1827,24 @@ var __privateMethod = (obj, member, method) => {
1572
1827
  const buildClient = (plugins) => {
1573
1828
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1574
1829
  return _a = class {
1575
- constructor(options = {}, links, tables) {
1830
+ constructor(options = {}, tables) {
1576
1831
  __privateAdd(this, _parseOptions);
1577
1832
  __privateAdd(this, _getFetchProps);
1578
1833
  __privateAdd(this, _evaluateBranch);
1579
1834
  __privateAdd(this, _branch, void 0);
1580
1835
  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
- });
1836
+ const pluginOptions = {
1837
+ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1838
+ cache: safeOptions.cache
1839
+ };
1840
+ const db = new SchemaPlugin(tables).build(pluginOptions);
1841
+ const search = new SearchPlugin(db).build(pluginOptions);
1585
1842
  this.db = db;
1586
1843
  this.search = search;
1587
1844
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1588
1845
  if (!namespace)
1589
1846
  continue;
1590
- const result = namespace.build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1847
+ const result = namespace.build(pluginOptions);
1591
1848
  if (result instanceof Promise) {
1592
1849
  void result.then((namespace2) => {
1593
1850
  this[key] = namespace2;
@@ -1601,11 +1858,12 @@ const buildClient = (plugins) => {
1601
1858
  const fetch = getFetchImplementation(options?.fetch);
1602
1859
  const databaseURL = options?.databaseURL || getDatabaseURL();
1603
1860
  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 });
1861
+ const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1862
+ const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch }) ?? defaultBranch;
1605
1863
  if (!databaseURL || !apiKey) {
1606
1864
  throw new Error("Options databaseURL and apiKey are required");
1607
1865
  }
1608
- return { fetch, databaseURL, apiKey, branch };
1866
+ return { fetch, databaseURL, apiKey, branch, cache };
1609
1867
  }, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
1610
1868
  fetch,
1611
1869
  apiKey,
@@ -1665,11 +1923,13 @@ exports.Repository = Repository;
1665
1923
  exports.RestRepository = RestRepository;
1666
1924
  exports.SchemaPlugin = SchemaPlugin;
1667
1925
  exports.SearchPlugin = SearchPlugin;
1926
+ exports.SimpleCache = SimpleCache;
1668
1927
  exports.XataApiClient = XataApiClient;
1669
1928
  exports.XataApiPlugin = XataApiPlugin;
1670
1929
  exports.XataError = XataError;
1671
1930
  exports.XataPlugin = XataPlugin;
1672
1931
  exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
1932
+ exports.addGitBranchesEntry = addGitBranchesEntry;
1673
1933
  exports.addTableColumn = addTableColumn;
1674
1934
  exports.buildClient = buildClient;
1675
1935
  exports.bulkInsertTableRecords = bulkInsertTableRecords;
@@ -1680,6 +1940,7 @@ exports.createDatabase = createDatabase;
1680
1940
  exports.createTable = createTable;
1681
1941
  exports.createUserAPIKey = createUserAPIKey;
1682
1942
  exports.createWorkspace = createWorkspace;
1943
+ exports.defaultBranch = defaultBranch;
1683
1944
  exports.deleteBranch = deleteBranch;
1684
1945
  exports.deleteColumn = deleteColumn;
1685
1946
  exports.deleteDatabase = deleteDatabase;
@@ -1704,6 +1965,7 @@ exports.getCurrentBranchDetails = getCurrentBranchDetails;
1704
1965
  exports.getCurrentBranchName = getCurrentBranchName;
1705
1966
  exports.getDatabaseList = getDatabaseList;
1706
1967
  exports.getDatabaseURL = getDatabaseURL;
1968
+ exports.getGitBranchesMapping = getGitBranchesMapping;
1707
1969
  exports.getRecord = getRecord;
1708
1970
  exports.getTableColumns = getTableColumns;
1709
1971
  exports.getTableSchema = getTableSchema;
@@ -1732,8 +1994,10 @@ exports.notExists = notExists;
1732
1994
  exports.operationsByTag = operationsByTag;
1733
1995
  exports.pattern = pattern;
1734
1996
  exports.queryTable = queryTable;
1997
+ exports.removeGitBranchesEntry = removeGitBranchesEntry;
1735
1998
  exports.removeWorkspaceMember = removeWorkspaceMember;
1736
1999
  exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
2000
+ exports.resolveBranch = resolveBranch;
1737
2001
  exports.searchBranch = searchBranch;
1738
2002
  exports.setTableSchema = setTableSchema;
1739
2003
  exports.startsWith = startsWith;