@xata.io/client 0.8.4 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -10,6 +10,13 @@ function isObject(value) {
10
10
  function isString(value) {
11
11
  return value !== void 0 && value !== null && typeof value === "string";
12
12
  }
13
+ function toBase64(value) {
14
+ try {
15
+ return btoa(value);
16
+ } catch (err) {
17
+ return Buffer.from(value).toString("base64");
18
+ }
19
+ }
13
20
 
14
21
  function getEnvVariable(name) {
15
22
  try {
@@ -27,7 +34,10 @@ function getEnvVariable(name) {
27
34
  }
28
35
  async function getGitBranch() {
29
36
  try {
30
- return require("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
37
+ if (typeof require === "function") {
38
+ const req = require;
39
+ return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
40
+ }
31
41
  } catch (err) {
32
42
  }
33
43
  try {
@@ -241,6 +251,14 @@ const deleteDatabase = (variables) => fetch$1({
241
251
  method: "delete",
242
252
  ...variables
243
253
  });
254
+ const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
255
+ const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
256
+ const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
257
+ const resolveBranch = (variables) => fetch$1({
258
+ url: "/dbs/{dbName}/resolveBranch",
259
+ method: "get",
260
+ ...variables
261
+ });
244
262
  const getBranchDetails = (variables) => fetch$1({
245
263
  url: "/db/{dbBranchName}",
246
264
  method: "get",
@@ -369,7 +387,15 @@ const operationsByTag = {
369
387
  resendWorkspaceMemberInvite,
370
388
  acceptWorkspaceMemberInvite
371
389
  },
372
- database: { getDatabaseList, createDatabase, deleteDatabase },
390
+ database: {
391
+ getDatabaseList,
392
+ createDatabase,
393
+ deleteDatabase,
394
+ getGitBranchesMapping,
395
+ addGitBranchesEntry,
396
+ removeGitBranchesEntry,
397
+ resolveBranch
398
+ },
373
399
  branch: {
374
400
  getBranchList,
375
401
  getBranchDetails,
@@ -432,35 +458,35 @@ function isValidBuilder(builder) {
432
458
  return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
433
459
  }
434
460
 
435
- var __accessCheck$6 = (obj, member, msg) => {
461
+ var __accessCheck$7 = (obj, member, msg) => {
436
462
  if (!member.has(obj))
437
463
  throw TypeError("Cannot " + msg);
438
464
  };
439
- var __privateGet$5 = (obj, member, getter) => {
440
- __accessCheck$6(obj, member, "read from private field");
465
+ var __privateGet$7 = (obj, member, getter) => {
466
+ __accessCheck$7(obj, member, "read from private field");
441
467
  return getter ? getter.call(obj) : member.get(obj);
442
468
  };
443
- var __privateAdd$6 = (obj, member, value) => {
469
+ var __privateAdd$7 = (obj, member, value) => {
444
470
  if (member.has(obj))
445
471
  throw TypeError("Cannot add the same private member more than once");
446
472
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
447
473
  };
448
- var __privateSet$4 = (obj, member, value, setter) => {
449
- __accessCheck$6(obj, member, "write to private field");
474
+ var __privateSet$6 = (obj, member, value, setter) => {
475
+ __accessCheck$7(obj, member, "write to private field");
450
476
  setter ? setter.call(obj, value) : member.set(obj, value);
451
477
  return value;
452
478
  };
453
479
  var _extraProps, _namespaces;
454
480
  class XataApiClient {
455
481
  constructor(options = {}) {
456
- __privateAdd$6(this, _extraProps, void 0);
457
- __privateAdd$6(this, _namespaces, {});
482
+ __privateAdd$7(this, _extraProps, void 0);
483
+ __privateAdd$7(this, _namespaces, {});
458
484
  const provider = options.host ?? "production";
459
485
  const apiKey = options?.apiKey ?? getAPIKey();
460
486
  if (!apiKey) {
461
487
  throw new Error("Could not resolve a valid apiKey");
462
488
  }
463
- __privateSet$4(this, _extraProps, {
489
+ __privateSet$6(this, _extraProps, {
464
490
  apiUrl: getHostUrl(provider, "main"),
465
491
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
466
492
  fetchImpl: getFetchImplementation(options.fetch),
@@ -468,34 +494,34 @@ class XataApiClient {
468
494
  });
469
495
  }
470
496
  get user() {
471
- if (!__privateGet$5(this, _namespaces).user)
472
- __privateGet$5(this, _namespaces).user = new UserApi(__privateGet$5(this, _extraProps));
473
- return __privateGet$5(this, _namespaces).user;
497
+ if (!__privateGet$7(this, _namespaces).user)
498
+ __privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
499
+ return __privateGet$7(this, _namespaces).user;
474
500
  }
475
501
  get workspaces() {
476
- if (!__privateGet$5(this, _namespaces).workspaces)
477
- __privateGet$5(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$5(this, _extraProps));
478
- return __privateGet$5(this, _namespaces).workspaces;
502
+ if (!__privateGet$7(this, _namespaces).workspaces)
503
+ __privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
504
+ return __privateGet$7(this, _namespaces).workspaces;
479
505
  }
480
506
  get databases() {
481
- if (!__privateGet$5(this, _namespaces).databases)
482
- __privateGet$5(this, _namespaces).databases = new DatabaseApi(__privateGet$5(this, _extraProps));
483
- return __privateGet$5(this, _namespaces).databases;
507
+ if (!__privateGet$7(this, _namespaces).databases)
508
+ __privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
509
+ return __privateGet$7(this, _namespaces).databases;
484
510
  }
485
511
  get branches() {
486
- if (!__privateGet$5(this, _namespaces).branches)
487
- __privateGet$5(this, _namespaces).branches = new BranchApi(__privateGet$5(this, _extraProps));
488
- return __privateGet$5(this, _namespaces).branches;
512
+ if (!__privateGet$7(this, _namespaces).branches)
513
+ __privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
514
+ return __privateGet$7(this, _namespaces).branches;
489
515
  }
490
516
  get tables() {
491
- if (!__privateGet$5(this, _namespaces).tables)
492
- __privateGet$5(this, _namespaces).tables = new TableApi(__privateGet$5(this, _extraProps));
493
- return __privateGet$5(this, _namespaces).tables;
517
+ if (!__privateGet$7(this, _namespaces).tables)
518
+ __privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
519
+ return __privateGet$7(this, _namespaces).tables;
494
520
  }
495
521
  get records() {
496
- if (!__privateGet$5(this, _namespaces).records)
497
- __privateGet$5(this, _namespaces).records = new RecordsApi(__privateGet$5(this, _extraProps));
498
- return __privateGet$5(this, _namespaces).records;
522
+ if (!__privateGet$7(this, _namespaces).records)
523
+ __privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
524
+ return __privateGet$7(this, _namespaces).records;
499
525
  }
500
526
  }
501
527
  _extraProps = new WeakMap();
@@ -629,6 +655,33 @@ class DatabaseApi {
629
655
  ...this.extraProps
630
656
  });
631
657
  }
658
+ getGitBranchesMapping(workspace, dbName) {
659
+ return operationsByTag.database.getGitBranchesMapping({
660
+ pathParams: { workspace, dbName },
661
+ ...this.extraProps
662
+ });
663
+ }
664
+ addGitBranchesEntry(workspace, dbName, body) {
665
+ return operationsByTag.database.addGitBranchesEntry({
666
+ pathParams: { workspace, dbName },
667
+ body,
668
+ ...this.extraProps
669
+ });
670
+ }
671
+ removeGitBranchesEntry(workspace, dbName, gitBranch) {
672
+ return operationsByTag.database.removeGitBranchesEntry({
673
+ pathParams: { workspace, dbName },
674
+ queryParams: { gitBranch },
675
+ ...this.extraProps
676
+ });
677
+ }
678
+ resolveBranch(workspace, dbName, gitBranch) {
679
+ return operationsByTag.database.resolveBranch({
680
+ pathParams: { workspace, dbName },
681
+ queryParams: { gitBranch },
682
+ ...this.extraProps
683
+ });
684
+ }
632
685
  }
633
686
  class BranchApi {
634
687
  constructor(extraProps) {
@@ -646,10 +699,10 @@ class BranchApi {
646
699
  ...this.extraProps
647
700
  });
648
701
  }
649
- createBranch(workspace, database, branch, from = "", options = {}) {
702
+ createBranch(workspace, database, branch, from, options = {}) {
650
703
  return operationsByTag.branch.createBranch({
651
704
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
652
- queryParams: { from },
705
+ queryParams: isString(from) ? { from } : void 0,
653
706
  body: options,
654
707
  ...this.extraProps
655
708
  });
@@ -850,43 +903,43 @@ class XataApiPlugin {
850
903
  class XataPlugin {
851
904
  }
852
905
 
853
- var __accessCheck$5 = (obj, member, msg) => {
906
+ var __accessCheck$6 = (obj, member, msg) => {
854
907
  if (!member.has(obj))
855
908
  throw TypeError("Cannot " + msg);
856
909
  };
857
- var __privateGet$4 = (obj, member, getter) => {
858
- __accessCheck$5(obj, member, "read from private field");
910
+ var __privateGet$6 = (obj, member, getter) => {
911
+ __accessCheck$6(obj, member, "read from private field");
859
912
  return getter ? getter.call(obj) : member.get(obj);
860
913
  };
861
- var __privateAdd$5 = (obj, member, value) => {
914
+ var __privateAdd$6 = (obj, member, value) => {
862
915
  if (member.has(obj))
863
916
  throw TypeError("Cannot add the same private member more than once");
864
917
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
865
918
  };
866
- var __privateSet$3 = (obj, member, value, setter) => {
867
- __accessCheck$5(obj, member, "write to private field");
919
+ var __privateSet$5 = (obj, member, value, setter) => {
920
+ __accessCheck$6(obj, member, "write to private field");
868
921
  setter ? setter.call(obj, value) : member.set(obj, value);
869
922
  return value;
870
923
  };
871
924
  var _query;
872
925
  class Page {
873
926
  constructor(query, meta, records = []) {
874
- __privateAdd$5(this, _query, void 0);
875
- __privateSet$3(this, _query, query);
927
+ __privateAdd$6(this, _query, void 0);
928
+ __privateSet$5(this, _query, query);
876
929
  this.meta = meta;
877
930
  this.records = records;
878
931
  }
879
932
  async nextPage(size, offset) {
880
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
933
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
881
934
  }
882
935
  async previousPage(size, offset) {
883
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
936
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
884
937
  }
885
938
  async firstPage(size, offset) {
886
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
939
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
887
940
  }
888
941
  async lastPage(size, offset) {
889
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
942
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
890
943
  }
891
944
  hasNextPage() {
892
945
  return this.meta.page.more;
@@ -898,46 +951,47 @@ const PAGINATION_DEFAULT_SIZE = 200;
898
951
  const PAGINATION_MAX_OFFSET = 800;
899
952
  const PAGINATION_DEFAULT_OFFSET = 0;
900
953
 
901
- var __accessCheck$4 = (obj, member, msg) => {
954
+ var __accessCheck$5 = (obj, member, msg) => {
902
955
  if (!member.has(obj))
903
956
  throw TypeError("Cannot " + msg);
904
957
  };
905
- var __privateGet$3 = (obj, member, getter) => {
906
- __accessCheck$4(obj, member, "read from private field");
958
+ var __privateGet$5 = (obj, member, getter) => {
959
+ __accessCheck$5(obj, member, "read from private field");
907
960
  return getter ? getter.call(obj) : member.get(obj);
908
961
  };
909
- var __privateAdd$4 = (obj, member, value) => {
962
+ var __privateAdd$5 = (obj, member, value) => {
910
963
  if (member.has(obj))
911
964
  throw TypeError("Cannot add the same private member more than once");
912
965
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
913
966
  };
914
- var __privateSet$2 = (obj, member, value, setter) => {
915
- __accessCheck$4(obj, member, "write to private field");
967
+ var __privateSet$4 = (obj, member, value, setter) => {
968
+ __accessCheck$5(obj, member, "write to private field");
916
969
  setter ? setter.call(obj, value) : member.set(obj, value);
917
970
  return value;
918
971
  };
919
972
  var _table$1, _repository, _data;
920
973
  const _Query = class {
921
974
  constructor(repository, table, data, parent) {
922
- __privateAdd$4(this, _table$1, void 0);
923
- __privateAdd$4(this, _repository, void 0);
924
- __privateAdd$4(this, _data, { filter: {} });
975
+ __privateAdd$5(this, _table$1, void 0);
976
+ __privateAdd$5(this, _repository, void 0);
977
+ __privateAdd$5(this, _data, { filter: {} });
925
978
  this.meta = { page: { cursor: "start", more: true } };
926
979
  this.records = [];
927
- __privateSet$2(this, _table$1, table);
980
+ __privateSet$4(this, _table$1, table);
928
981
  if (repository) {
929
- __privateSet$2(this, _repository, repository);
982
+ __privateSet$4(this, _repository, repository);
930
983
  } else {
931
- __privateSet$2(this, _repository, this);
984
+ __privateSet$4(this, _repository, this);
932
985
  }
933
- __privateGet$3(this, _data).filter = data.filter ?? parent?.filter ?? {};
934
- __privateGet$3(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
935
- __privateGet$3(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
936
- __privateGet$3(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
937
- __privateGet$3(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
938
- __privateGet$3(this, _data).sort = data.sort ?? parent?.sort;
939
- __privateGet$3(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
940
- __privateGet$3(this, _data).page = data.page ?? parent?.page;
986
+ __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
987
+ __privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
988
+ __privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
989
+ __privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
990
+ __privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
991
+ __privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
992
+ __privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
993
+ __privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
994
+ __privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
941
995
  this.any = this.any.bind(this);
942
996
  this.all = this.all.bind(this);
943
997
  this.not = this.not.bind(this);
@@ -948,58 +1002,64 @@ const _Query = class {
948
1002
  Object.defineProperty(this, "repository", { enumerable: false });
949
1003
  }
950
1004
  getQueryOptions() {
951
- return __privateGet$3(this, _data);
1005
+ return __privateGet$5(this, _data);
1006
+ }
1007
+ key() {
1008
+ const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
1009
+ const key = JSON.stringify({ columns, filter, sort, pagination });
1010
+ return toBase64(key);
952
1011
  }
953
1012
  any(...queries) {
954
1013
  const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
955
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $any } }, __privateGet$3(this, _data));
1014
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
956
1015
  }
957
1016
  all(...queries) {
958
1017
  const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
959
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
1018
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
960
1019
  }
961
1020
  not(...queries) {
962
1021
  const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
963
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $not } }, __privateGet$3(this, _data));
1022
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
964
1023
  }
965
1024
  none(...queries) {
966
1025
  const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
967
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $none } }, __privateGet$3(this, _data));
1026
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
968
1027
  }
969
1028
  filter(a, b) {
970
1029
  if (arguments.length === 1) {
971
1030
  const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
972
- const $all = compact([__privateGet$3(this, _data).filter?.$all].flat().concat(constraints));
973
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
1031
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
1032
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
974
1033
  } else {
975
- const $all = compact([__privateGet$3(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
976
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
1034
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1035
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
977
1036
  }
978
1037
  }
979
1038
  sort(column, direction) {
980
- const originalSort = [__privateGet$3(this, _data).sort ?? []].flat();
1039
+ const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
981
1040
  const sort = [...originalSort, { column, direction }];
982
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { sort }, __privateGet$3(this, _data));
1041
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
983
1042
  }
984
1043
  select(columns) {
985
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { columns }, __privateGet$3(this, _data));
1044
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
986
1045
  }
987
1046
  getPaginated(options = {}) {
988
- const query = new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), options, __privateGet$3(this, _data));
989
- return __privateGet$3(this, _repository).query(query);
1047
+ const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
1048
+ return __privateGet$5(this, _repository).query(query);
990
1049
  }
991
1050
  async *[Symbol.asyncIterator]() {
992
- for await (const [record] of this.getIterator(1)) {
1051
+ for await (const [record] of this.getIterator({ batchSize: 1 })) {
993
1052
  yield record;
994
1053
  }
995
1054
  }
996
- async *getIterator(chunk, options = {}) {
1055
+ async *getIterator(options = {}) {
1056
+ const { batchSize = 1 } = options;
997
1057
  let offset = 0;
998
1058
  let end = false;
999
1059
  while (!end) {
1000
- const { records, meta } = await this.getPaginated({ ...options, page: { size: chunk, offset } });
1060
+ const { records, meta } = await this.getPaginated({ ...options, pagination: { size: batchSize, offset } });
1001
1061
  yield records;
1002
- offset += chunk;
1062
+ offset += batchSize;
1003
1063
  end = !meta.page.more;
1004
1064
  }
1005
1065
  }
@@ -1007,17 +1067,21 @@ const _Query = class {
1007
1067
  const { records } = await this.getPaginated(options);
1008
1068
  return records;
1009
1069
  }
1010
- async getAll(chunk = PAGINATION_MAX_SIZE, options = {}) {
1070
+ async getAll(options = {}) {
1071
+ const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
1011
1072
  const results = [];
1012
- for await (const page of this.getIterator(chunk, options)) {
1073
+ for await (const page of this.getIterator({ ...rest, batchSize })) {
1013
1074
  results.push(...page);
1014
1075
  }
1015
1076
  return results;
1016
1077
  }
1017
- async getOne(options = {}) {
1018
- const records = await this.getMany({ ...options, page: { size: 1 } });
1078
+ async getFirst(options = {}) {
1079
+ const records = await this.getMany({ ...options, pagination: { size: 1 } });
1019
1080
  return records[0] || null;
1020
1081
  }
1082
+ cache(ttl) {
1083
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
1084
+ }
1021
1085
  nextPage(size, offset) {
1022
1086
  return this.firstPage(size, offset);
1023
1087
  }
@@ -1025,10 +1089,10 @@ const _Query = class {
1025
1089
  return this.firstPage(size, offset);
1026
1090
  }
1027
1091
  firstPage(size, offset) {
1028
- return this.getPaginated({ page: { size, offset } });
1092
+ return this.getPaginated({ pagination: { size, offset } });
1029
1093
  }
1030
1094
  lastPage(size, offset) {
1031
- return this.getPaginated({ page: { size, offset, before: "end" } });
1095
+ return this.getPaginated({ pagination: { size, offset, before: "end" } });
1032
1096
  }
1033
1097
  hasNextPage() {
1034
1098
  return this.meta.page.more;
@@ -1069,75 +1133,94 @@ function buildSortFilter(filter) {
1069
1133
  }
1070
1134
  }
1071
1135
 
1072
- var __accessCheck$3 = (obj, member, msg) => {
1136
+ var __accessCheck$4 = (obj, member, msg) => {
1073
1137
  if (!member.has(obj))
1074
1138
  throw TypeError("Cannot " + msg);
1075
1139
  };
1076
- var __privateGet$2 = (obj, member, getter) => {
1077
- __accessCheck$3(obj, member, "read from private field");
1140
+ var __privateGet$4 = (obj, member, getter) => {
1141
+ __accessCheck$4(obj, member, "read from private field");
1078
1142
  return getter ? getter.call(obj) : member.get(obj);
1079
1143
  };
1080
- var __privateAdd$3 = (obj, member, value) => {
1144
+ var __privateAdd$4 = (obj, member, value) => {
1081
1145
  if (member.has(obj))
1082
1146
  throw TypeError("Cannot add the same private member more than once");
1083
1147
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1084
1148
  };
1085
- var __privateSet$1 = (obj, member, value, setter) => {
1086
- __accessCheck$3(obj, member, "write to private field");
1149
+ var __privateSet$3 = (obj, member, value, setter) => {
1150
+ __accessCheck$4(obj, member, "write to private field");
1087
1151
  setter ? setter.call(obj, value) : member.set(obj, value);
1088
1152
  return value;
1089
1153
  };
1090
1154
  var __privateMethod$2 = (obj, member, method) => {
1091
- __accessCheck$3(obj, member, "access private method");
1155
+ __accessCheck$4(obj, member, "access private method");
1092
1156
  return method;
1093
1157
  };
1094
- var _table, _links, _getFetchProps, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn;
1158
+ 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;
1095
1159
  class Repository extends Query {
1096
1160
  }
1097
1161
  class RestRepository extends Query {
1098
1162
  constructor(options) {
1099
1163
  super(null, options.table, {});
1100
- __privateAdd$3(this, _insertRecordWithoutId);
1101
- __privateAdd$3(this, _insertRecordWithId);
1102
- __privateAdd$3(this, _bulkInsertTableRecords);
1103
- __privateAdd$3(this, _updateRecordWithID);
1104
- __privateAdd$3(this, _upsertRecordWithID);
1105
- __privateAdd$3(this, _deleteRecord);
1106
- __privateAdd$3(this, _table, void 0);
1107
- __privateAdd$3(this, _links, void 0);
1108
- __privateAdd$3(this, _getFetchProps, void 0);
1109
- __privateSet$1(this, _table, options.table);
1110
- __privateSet$1(this, _links, options.links ?? {});
1111
- __privateSet$1(this, _getFetchProps, options.getFetchProps);
1164
+ __privateAdd$4(this, _insertRecordWithoutId);
1165
+ __privateAdd$4(this, _insertRecordWithId);
1166
+ __privateAdd$4(this, _bulkInsertTableRecords);
1167
+ __privateAdd$4(this, _updateRecordWithID);
1168
+ __privateAdd$4(this, _upsertRecordWithID);
1169
+ __privateAdd$4(this, _deleteRecord);
1170
+ __privateAdd$4(this, _invalidateCache);
1171
+ __privateAdd$4(this, _setCacheRecord);
1172
+ __privateAdd$4(this, _getCacheRecord);
1173
+ __privateAdd$4(this, _setCacheQuery);
1174
+ __privateAdd$4(this, _getCacheQuery);
1175
+ __privateAdd$4(this, _getSchema$1);
1176
+ __privateAdd$4(this, _table, void 0);
1177
+ __privateAdd$4(this, _getFetchProps, void 0);
1178
+ __privateAdd$4(this, _cache, void 0);
1179
+ __privateAdd$4(this, _schema$1, void 0);
1180
+ __privateSet$3(this, _table, options.table);
1181
+ __privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
1112
1182
  this.db = options.db;
1183
+ __privateSet$3(this, _cache, options.pluginOptions.cache);
1113
1184
  }
1114
1185
  async create(a, b) {
1115
1186
  if (Array.isArray(a)) {
1116
- return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1187
+ const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1188
+ await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1189
+ return records;
1117
1190
  }
1118
1191
  if (isString(a) && isObject(b)) {
1119
1192
  if (a === "")
1120
1193
  throw new Error("The id can't be empty");
1121
- return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1194
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1195
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1196
+ return record;
1122
1197
  }
1123
1198
  if (isObject(a) && isString(a.id)) {
1124
1199
  if (a.id === "")
1125
1200
  throw new Error("The id can't be empty");
1126
- return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
1201
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
1202
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1203
+ return record;
1127
1204
  }
1128
1205
  if (isObject(a)) {
1129
- return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1206
+ const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1207
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1208
+ return record;
1130
1209
  }
1131
1210
  throw new Error("Invalid arguments for create method");
1132
1211
  }
1133
1212
  async read(recordId) {
1134
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1213
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1214
+ if (cacheRecord)
1215
+ return cacheRecord;
1216
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1135
1217
  try {
1136
1218
  const response = await getRecord({
1137
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1219
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1138
1220
  ...fetchProps
1139
1221
  });
1140
- return initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), response);
1222
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1223
+ return initObject(this.db, schema, __privateGet$4(this, _table), response);
1141
1224
  } catch (e) {
1142
1225
  if (isObject(e) && e.status === 404) {
1143
1226
  return null;
@@ -1153,10 +1236,16 @@ class RestRepository extends Query {
1153
1236
  return Promise.all(a.map((object) => this.update(object)));
1154
1237
  }
1155
1238
  if (isString(a) && isObject(b)) {
1156
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1239
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1240
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1241
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1242
+ return record;
1157
1243
  }
1158
1244
  if (isObject(a) && isString(a.id)) {
1159
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1245
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1246
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1247
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1248
+ return record;
1160
1249
  }
1161
1250
  throw new Error("Invalid arguments for update method");
1162
1251
  }
@@ -1168,70 +1257,85 @@ class RestRepository extends Query {
1168
1257
  return Promise.all(a.map((object) => this.createOrUpdate(object)));
1169
1258
  }
1170
1259
  if (isString(a) && isObject(b)) {
1171
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1260
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1261
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1262
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1263
+ return record;
1172
1264
  }
1173
1265
  if (isObject(a) && isString(a.id)) {
1174
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1266
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1267
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1268
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1269
+ return record;
1175
1270
  }
1176
1271
  throw new Error("Invalid arguments for createOrUpdate method");
1177
1272
  }
1178
- async delete(recordId) {
1179
- if (Array.isArray(recordId)) {
1180
- if (recordId.length > 100) {
1273
+ async delete(a) {
1274
+ if (Array.isArray(a)) {
1275
+ if (a.length > 100) {
1181
1276
  console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1182
1277
  }
1183
- await Promise.all(recordId.map((id) => this.delete(id)));
1278
+ await Promise.all(a.map((id) => this.delete(id)));
1184
1279
  return;
1185
1280
  }
1186
- if (isString(recordId)) {
1187
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId);
1281
+ if (isString(a)) {
1282
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
1283
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1188
1284
  return;
1189
1285
  }
1190
- if (isObject(recordId) && isString(recordId.id)) {
1191
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId.id);
1286
+ if (isObject(a) && isString(a.id)) {
1287
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
1288
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1192
1289
  return;
1193
1290
  }
1194
1291
  throw new Error("Invalid arguments for delete method");
1195
1292
  }
1196
1293
  async search(query, options = {}) {
1197
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1294
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1198
1295
  const { records } = await searchBranch({
1199
1296
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1200
- body: { tables: [__privateGet$2(this, _table)], query, fuzziness: options.fuzziness },
1297
+ body: { tables: [__privateGet$4(this, _table)], query, fuzziness: options.fuzziness },
1201
1298
  ...fetchProps
1202
1299
  });
1203
- return records.map((item) => initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), item));
1300
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1301
+ return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
1204
1302
  }
1205
1303
  async query(query) {
1304
+ const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
1305
+ if (cacheQuery)
1306
+ return new Page(query, cacheQuery.meta, cacheQuery.records);
1206
1307
  const data = query.getQueryOptions();
1207
1308
  const body = {
1208
1309
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1209
1310
  sort: data.sort ? buildSortFilter(data.sort) : void 0,
1210
- page: data.page,
1311
+ page: data.pagination,
1211
1312
  columns: data.columns
1212
1313
  };
1213
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1314
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1214
1315
  const { meta, records: objects } = await queryTable({
1215
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1316
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1216
1317
  body,
1217
1318
  ...fetchProps
1218
1319
  });
1219
- const records = objects.map((record) => initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), record));
1320
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1321
+ const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
1322
+ await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1220
1323
  return new Page(query, meta, records);
1221
1324
  }
1222
1325
  }
1223
1326
  _table = new WeakMap();
1224
- _links = new WeakMap();
1225
1327
  _getFetchProps = new WeakMap();
1328
+ _cache = new WeakMap();
1329
+ _schema$1 = new WeakMap();
1226
1330
  _insertRecordWithoutId = new WeakSet();
1227
1331
  insertRecordWithoutId_fn = async function(object) {
1228
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1332
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1229
1333
  const record = transformObjectLinks(object);
1230
1334
  const response = await insertRecord({
1231
1335
  pathParams: {
1232
1336
  workspace: "{workspaceId}",
1233
1337
  dbBranchName: "{dbBranch}",
1234
- tableName: __privateGet$2(this, _table)
1338
+ tableName: __privateGet$4(this, _table)
1235
1339
  },
1236
1340
  body: record,
1237
1341
  ...fetchProps
@@ -1244,13 +1348,13 @@ insertRecordWithoutId_fn = async function(object) {
1244
1348
  };
1245
1349
  _insertRecordWithId = new WeakSet();
1246
1350
  insertRecordWithId_fn = async function(recordId, object) {
1247
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1351
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1248
1352
  const record = transformObjectLinks(object);
1249
1353
  const response = await insertRecordWithID({
1250
1354
  pathParams: {
1251
1355
  workspace: "{workspaceId}",
1252
1356
  dbBranchName: "{dbBranch}",
1253
- tableName: __privateGet$2(this, _table),
1357
+ tableName: __privateGet$4(this, _table),
1254
1358
  recordId
1255
1359
  },
1256
1360
  body: record,
@@ -1265,10 +1369,10 @@ insertRecordWithId_fn = async function(recordId, object) {
1265
1369
  };
1266
1370
  _bulkInsertTableRecords = new WeakSet();
1267
1371
  bulkInsertTableRecords_fn = async function(objects) {
1268
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1372
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1269
1373
  const records = objects.map((object) => transformObjectLinks(object));
1270
1374
  const response = await bulkInsertTableRecords({
1271
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1375
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1272
1376
  body: { records },
1273
1377
  ...fetchProps
1274
1378
  });
@@ -1280,10 +1384,10 @@ bulkInsertTableRecords_fn = async function(objects) {
1280
1384
  };
1281
1385
  _updateRecordWithID = new WeakSet();
1282
1386
  updateRecordWithID_fn = async function(recordId, object) {
1283
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1387
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1284
1388
  const record = transformObjectLinks(object);
1285
1389
  const response = await updateRecordWithID({
1286
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1390
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1287
1391
  body: record,
1288
1392
  ...fetchProps
1289
1393
  });
@@ -1294,9 +1398,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1294
1398
  };
1295
1399
  _upsertRecordWithID = new WeakSet();
1296
1400
  upsertRecordWithID_fn = async function(recordId, object) {
1297
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1401
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1298
1402
  const response = await upsertRecordWithID({
1299
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1403
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1300
1404
  body: object,
1301
1405
  ...fetchProps
1302
1406
  });
@@ -1307,11 +1411,62 @@ upsertRecordWithID_fn = async function(recordId, object) {
1307
1411
  };
1308
1412
  _deleteRecord = new WeakSet();
1309
1413
  deleteRecord_fn = async function(recordId) {
1310
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1414
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1311
1415
  await deleteRecord({
1312
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1416
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1417
+ ...fetchProps
1418
+ });
1419
+ };
1420
+ _invalidateCache = new WeakSet();
1421
+ invalidateCache_fn = async function(recordId) {
1422
+ await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1423
+ const cacheItems = await __privateGet$4(this, _cache).getAll();
1424
+ const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1425
+ for (const [key, value] of queries) {
1426
+ const ids = getIds(value);
1427
+ if (ids.includes(recordId))
1428
+ await __privateGet$4(this, _cache).delete(key);
1429
+ }
1430
+ };
1431
+ _setCacheRecord = new WeakSet();
1432
+ setCacheRecord_fn = async function(record) {
1433
+ if (!__privateGet$4(this, _cache).cacheRecords)
1434
+ return;
1435
+ await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
1436
+ };
1437
+ _getCacheRecord = new WeakSet();
1438
+ getCacheRecord_fn = async function(recordId) {
1439
+ if (!__privateGet$4(this, _cache).cacheRecords)
1440
+ return null;
1441
+ return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1442
+ };
1443
+ _setCacheQuery = new WeakSet();
1444
+ setCacheQuery_fn = async function(query, meta, records) {
1445
+ await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1446
+ };
1447
+ _getCacheQuery = new WeakSet();
1448
+ getCacheQuery_fn = async function(query) {
1449
+ const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
1450
+ const result = await __privateGet$4(this, _cache).get(key);
1451
+ if (!result)
1452
+ return null;
1453
+ const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1454
+ if (ttl < 0)
1455
+ return null;
1456
+ const hasExpired = result.date.getTime() + ttl < Date.now();
1457
+ return hasExpired ? null : result;
1458
+ };
1459
+ _getSchema$1 = new WeakSet();
1460
+ getSchema_fn$1 = async function() {
1461
+ if (__privateGet$4(this, _schema$1))
1462
+ return __privateGet$4(this, _schema$1);
1463
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1464
+ const { schema } = await getBranchDetails({
1465
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1313
1466
  ...fetchProps
1314
1467
  });
1468
+ __privateSet$3(this, _schema$1, schema);
1469
+ return schema;
1315
1470
  };
1316
1471
  const transformObjectLinks = (object) => {
1317
1472
  return Object.entries(object).reduce((acc, [key, value]) => {
@@ -1320,15 +1475,33 @@ const transformObjectLinks = (object) => {
1320
1475
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1321
1476
  }, {});
1322
1477
  };
1323
- const initObject = (db, links, table, object) => {
1478
+ const initObject = (db, schema, table, object) => {
1324
1479
  const result = {};
1325
1480
  Object.assign(result, object);
1326
- const tableLinks = links[table] || [];
1327
- for (const link of tableLinks) {
1328
- const [field, linkTable] = link;
1329
- const value = result[field];
1330
- if (value && isObject(value)) {
1331
- result[field] = initObject(db, links, linkTable, value);
1481
+ const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
1482
+ if (!columns)
1483
+ console.error(`Table ${table} not found in schema`);
1484
+ for (const column of columns ?? []) {
1485
+ const value = result[column.name];
1486
+ switch (column.type) {
1487
+ case "datetime": {
1488
+ const date = new Date(value);
1489
+ if (isNaN(date.getTime())) {
1490
+ console.error(`Failed to parse date ${value} for field ${column.name}`);
1491
+ } else {
1492
+ result[column.name] = date;
1493
+ }
1494
+ break;
1495
+ }
1496
+ case "link": {
1497
+ const linkTable = column.link?.table;
1498
+ if (!linkTable) {
1499
+ console.error(`Failed to parse link for field ${column.name}`);
1500
+ } else if (value && isObject(value)) {
1501
+ result[column.name] = initObject(db, schema, linkTable, value);
1502
+ }
1503
+ break;
1504
+ }
1332
1505
  }
1333
1506
  }
1334
1507
  result.read = function() {
@@ -1346,6 +1519,65 @@ const initObject = (db, links, table, object) => {
1346
1519
  Object.freeze(result);
1347
1520
  return result;
1348
1521
  };
1522
+ function getIds(value) {
1523
+ if (Array.isArray(value)) {
1524
+ return value.map((item) => getIds(item)).flat();
1525
+ }
1526
+ if (!isObject(value))
1527
+ return [];
1528
+ const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
1529
+ return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
1530
+ }
1531
+
1532
+ var __accessCheck$3 = (obj, member, msg) => {
1533
+ if (!member.has(obj))
1534
+ throw TypeError("Cannot " + msg);
1535
+ };
1536
+ var __privateGet$3 = (obj, member, getter) => {
1537
+ __accessCheck$3(obj, member, "read from private field");
1538
+ return getter ? getter.call(obj) : member.get(obj);
1539
+ };
1540
+ var __privateAdd$3 = (obj, member, value) => {
1541
+ if (member.has(obj))
1542
+ throw TypeError("Cannot add the same private member more than once");
1543
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1544
+ };
1545
+ var __privateSet$2 = (obj, member, value, setter) => {
1546
+ __accessCheck$3(obj, member, "write to private field");
1547
+ setter ? setter.call(obj, value) : member.set(obj, value);
1548
+ return value;
1549
+ };
1550
+ var _map;
1551
+ class SimpleCache {
1552
+ constructor(options = {}) {
1553
+ __privateAdd$3(this, _map, void 0);
1554
+ __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1555
+ this.capacity = options.max ?? 500;
1556
+ this.cacheRecords = options.cacheRecords ?? true;
1557
+ this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1558
+ }
1559
+ async getAll() {
1560
+ return Object.fromEntries(__privateGet$3(this, _map));
1561
+ }
1562
+ async get(key) {
1563
+ return __privateGet$3(this, _map).get(key) ?? null;
1564
+ }
1565
+ async set(key, value) {
1566
+ await this.delete(key);
1567
+ __privateGet$3(this, _map).set(key, value);
1568
+ if (__privateGet$3(this, _map).size > this.capacity) {
1569
+ const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
1570
+ await this.delete(leastRecentlyUsed);
1571
+ }
1572
+ }
1573
+ async delete(key) {
1574
+ __privateGet$3(this, _map).delete(key);
1575
+ }
1576
+ async clear() {
1577
+ return __privateGet$3(this, _map).clear();
1578
+ }
1579
+ }
1580
+ _map = new WeakMap();
1349
1581
 
1350
1582
  const gt = (value) => ({ $gt: value });
1351
1583
  const ge = (value) => ({ $ge: value });
@@ -1370,7 +1602,7 @@ var __accessCheck$2 = (obj, member, msg) => {
1370
1602
  if (!member.has(obj))
1371
1603
  throw TypeError("Cannot " + msg);
1372
1604
  };
1373
- var __privateGet$1 = (obj, member, getter) => {
1605
+ var __privateGet$2 = (obj, member, getter) => {
1374
1606
  __accessCheck$2(obj, member, "read from private field");
1375
1607
  return getter ? getter.call(obj) : member.get(obj);
1376
1608
  };
@@ -1381,23 +1613,25 @@ var __privateAdd$2 = (obj, member, value) => {
1381
1613
  };
1382
1614
  var _tables;
1383
1615
  class SchemaPlugin extends XataPlugin {
1384
- constructor(links) {
1616
+ constructor(tableNames) {
1385
1617
  super();
1386
- this.links = links;
1618
+ this.tableNames = tableNames;
1387
1619
  __privateAdd$2(this, _tables, {});
1388
1620
  }
1389
- build(options) {
1390
- const { getFetchProps } = options;
1391
- const links = this.links;
1621
+ build(pluginOptions) {
1392
1622
  const db = new Proxy({}, {
1393
1623
  get: (_target, table) => {
1394
1624
  if (!isString(table))
1395
1625
  throw new Error("Invalid table name");
1396
- if (!__privateGet$1(this, _tables)[table])
1397
- __privateGet$1(this, _tables)[table] = new RestRepository({ db, getFetchProps, table, links });
1398
- return __privateGet$1(this, _tables)[table];
1626
+ if (!__privateGet$2(this, _tables)[table]) {
1627
+ __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1628
+ }
1629
+ return __privateGet$2(this, _tables)[table];
1399
1630
  }
1400
1631
  });
1632
+ for (const table of this.tableNames ?? []) {
1633
+ db[table] = new RestRepository({ db, pluginOptions, table });
1634
+ }
1401
1635
  return db;
1402
1636
  }
1403
1637
  }
@@ -1407,44 +1641,57 @@ var __accessCheck$1 = (obj, member, msg) => {
1407
1641
  if (!member.has(obj))
1408
1642
  throw TypeError("Cannot " + msg);
1409
1643
  };
1644
+ var __privateGet$1 = (obj, member, getter) => {
1645
+ __accessCheck$1(obj, member, "read from private field");
1646
+ return getter ? getter.call(obj) : member.get(obj);
1647
+ };
1410
1648
  var __privateAdd$1 = (obj, member, value) => {
1411
1649
  if (member.has(obj))
1412
1650
  throw TypeError("Cannot add the same private member more than once");
1413
1651
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1414
1652
  };
1653
+ var __privateSet$1 = (obj, member, value, setter) => {
1654
+ __accessCheck$1(obj, member, "write to private field");
1655
+ setter ? setter.call(obj, value) : member.set(obj, value);
1656
+ return value;
1657
+ };
1415
1658
  var __privateMethod$1 = (obj, member, method) => {
1416
1659
  __accessCheck$1(obj, member, "access private method");
1417
1660
  return method;
1418
1661
  };
1419
- var _search, search_fn;
1662
+ var _schema, _search, search_fn, _getSchema, getSchema_fn;
1420
1663
  class SearchPlugin extends XataPlugin {
1421
- constructor(db, links) {
1664
+ constructor(db) {
1422
1665
  super();
1423
1666
  this.db = db;
1424
- this.links = links;
1425
1667
  __privateAdd$1(this, _search);
1668
+ __privateAdd$1(this, _getSchema);
1669
+ __privateAdd$1(this, _schema, void 0);
1426
1670
  }
1427
1671
  build({ getFetchProps }) {
1428
1672
  return {
1429
1673
  all: async (query, options = {}) => {
1430
1674
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1675
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1431
1676
  return records.map((record) => {
1432
1677
  const { table = "orphan" } = record.xata;
1433
- return { table, record: initObject(this.db, this.links, table, record) };
1678
+ return { table, record: initObject(this.db, schema, table, record) };
1434
1679
  });
1435
1680
  },
1436
1681
  byTable: async (query, options = {}) => {
1437
1682
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1683
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1438
1684
  return records.reduce((acc, record) => {
1439
1685
  const { table = "orphan" } = record.xata;
1440
1686
  const items = acc[table] ?? [];
1441
- const item = initObject(this.db, this.links, table, record);
1687
+ const item = initObject(this.db, schema, table, record);
1442
1688
  return { ...acc, [table]: [...items, item] };
1443
1689
  }, {});
1444
1690
  }
1445
1691
  };
1446
1692
  }
1447
1693
  }
1694
+ _schema = new WeakMap();
1448
1695
  _search = new WeakSet();
1449
1696
  search_fn = async function(query, options, getFetchProps) {
1450
1697
  const fetchProps = await getFetchProps();
@@ -1456,6 +1703,18 @@ search_fn = async function(query, options, getFetchProps) {
1456
1703
  });
1457
1704
  return records;
1458
1705
  };
1706
+ _getSchema = new WeakSet();
1707
+ getSchema_fn = async function(getFetchProps) {
1708
+ if (__privateGet$1(this, _schema))
1709
+ return __privateGet$1(this, _schema);
1710
+ const fetchProps = await getFetchProps();
1711
+ const { schema } = await getBranchDetails({
1712
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1713
+ ...fetchProps
1714
+ });
1715
+ __privateSet$1(this, _schema, schema);
1716
+ return schema;
1717
+ };
1459
1718
 
1460
1719
  const isBranchStrategyBuilder = (strategy) => {
1461
1720
  return typeof strategy === "function";
@@ -1564,22 +1823,24 @@ var __privateMethod = (obj, member, method) => {
1564
1823
  const buildClient = (plugins) => {
1565
1824
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1566
1825
  return _a = class {
1567
- constructor(options = {}, links) {
1826
+ constructor(options = {}, tables) {
1568
1827
  __privateAdd(this, _parseOptions);
1569
1828
  __privateAdd(this, _getFetchProps);
1570
1829
  __privateAdd(this, _evaluateBranch);
1571
1830
  __privateAdd(this, _branch, void 0);
1572
1831
  const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
1573
- const db = new SchemaPlugin(links).build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1574
- const search = new SearchPlugin(db, links ?? {}).build({
1575
- getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions)
1576
- });
1832
+ const pluginOptions = {
1833
+ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1834
+ cache: safeOptions.cache
1835
+ };
1836
+ const db = new SchemaPlugin(tables).build(pluginOptions);
1837
+ const search = new SearchPlugin(db).build(pluginOptions);
1577
1838
  this.db = db;
1578
1839
  this.search = search;
1579
1840
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1580
1841
  if (!namespace)
1581
1842
  continue;
1582
- const result = namespace.build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1843
+ const result = namespace.build(pluginOptions);
1583
1844
  if (result instanceof Promise) {
1584
1845
  void result.then((namespace2) => {
1585
1846
  this[key] = namespace2;
@@ -1593,11 +1854,12 @@ const buildClient = (plugins) => {
1593
1854
  const fetch = getFetchImplementation(options?.fetch);
1594
1855
  const databaseURL = options?.databaseURL || getDatabaseURL();
1595
1856
  const apiKey = options?.apiKey || getAPIKey();
1857
+ const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1596
1858
  const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1597
1859
  if (!databaseURL || !apiKey) {
1598
1860
  throw new Error("Options databaseURL and apiKey are required");
1599
1861
  }
1600
- return { fetch, databaseURL, apiKey, branch };
1862
+ return { fetch, databaseURL, apiKey, branch, cache };
1601
1863
  }, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
1602
1864
  fetch,
1603
1865
  apiKey,
@@ -1645,5 +1907,5 @@ class XataError extends Error {
1645
1907
  }
1646
1908
  }
1647
1909
 
1648
- export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeWorkspaceMember, resendWorkspaceMemberInvite, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
1910
+ export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
1649
1911
  //# sourceMappingURL=index.mjs.map