@xata.io/client 0.0.0-alpha.vf3ed7b8 → 0.0.0-alpha.vf45a2df

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -11,8 +11,19 @@ function compact(arr) {
11
11
  function isObject(value) {
12
12
  return Boolean(value) && typeof value === "object" && !Array.isArray(value);
13
13
  }
14
+ function isDefined(value) {
15
+ return value !== null && value !== void 0;
16
+ }
14
17
  function isString(value) {
15
- return value !== void 0 && value !== null && typeof value === "string";
18
+ return isDefined(value) && typeof value === "string";
19
+ }
20
+ function toBase64(value) {
21
+ try {
22
+ return btoa(value);
23
+ } catch (err) {
24
+ const buf = Buffer;
25
+ return buf.from(value).toString("base64");
26
+ }
16
27
  }
17
28
 
18
29
  function getEnvVariable(name) {
@@ -31,7 +42,10 @@ function getEnvVariable(name) {
31
42
  }
32
43
  async function getGitBranch() {
33
44
  try {
34
- return require("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
45
+ if (typeof require === "function") {
46
+ const req = require;
47
+ return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
48
+ }
35
49
  } catch (err) {
36
50
  }
37
51
  try {
@@ -64,16 +78,28 @@ function getFetchImplementation(userFetch) {
64
78
  return fetchImpl;
65
79
  }
66
80
 
67
- class FetcherError extends Error {
68
- constructor(status, data) {
81
+ const VERSION = "0.0.0-alpha.vf45a2df";
82
+
83
+ class ErrorWithCause extends Error {
84
+ constructor(message, options) {
85
+ super(message, options);
86
+ }
87
+ }
88
+ class FetcherError extends ErrorWithCause {
89
+ constructor(status, data, requestId) {
69
90
  super(getMessage(data));
70
91
  this.status = status;
71
92
  this.errors = isBulkError(data) ? data.errors : void 0;
93
+ this.requestId = requestId;
72
94
  if (data instanceof Error) {
73
95
  this.stack = data.stack;
74
96
  this.cause = data.cause;
75
97
  }
76
98
  }
99
+ toString() {
100
+ const error = super.toString();
101
+ return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
102
+ }
77
103
  }
78
104
  function isBulkError(error) {
79
105
  return isObject(error) && Array.isArray(error.errors);
@@ -96,7 +122,12 @@ function getMessage(data) {
96
122
  }
97
123
 
98
124
  const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
99
- const query = new URLSearchParams(queryParams).toString();
125
+ const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
126
+ if (value === void 0 || value === null)
127
+ return acc;
128
+ return { ...acc, [key]: value };
129
+ }, {});
130
+ const query = new URLSearchParams(cleanQueryParams).toString();
100
131
  const queryString = query.length > 0 ? `?${query}` : "";
101
132
  return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
102
133
  };
@@ -136,6 +167,7 @@ async function fetch$1({
136
167
  body: body ? JSON.stringify(body) : void 0,
137
168
  headers: {
138
169
  "Content-Type": "application/json",
170
+ "User-Agent": `Xata client-ts/${VERSION}`,
139
171
  ...headers,
140
172
  ...hostHeader(fullUrl),
141
173
  Authorization: `Bearer ${apiKey}`
@@ -144,14 +176,15 @@ async function fetch$1({
144
176
  if (response.status === 204) {
145
177
  return {};
146
178
  }
179
+ const requestId = response.headers?.get("x-request-id") ?? void 0;
147
180
  try {
148
181
  const jsonResponse = await response.json();
149
182
  if (response.ok) {
150
183
  return jsonResponse;
151
184
  }
152
- throw new FetcherError(response.status, jsonResponse);
185
+ throw new FetcherError(response.status, jsonResponse, requestId);
153
186
  } catch (error) {
154
- throw new FetcherError(response.status, error);
187
+ throw new FetcherError(response.status, error, requestId);
155
188
  }
156
189
  }
157
190
 
@@ -245,6 +278,14 @@ const deleteDatabase = (variables) => fetch$1({
245
278
  method: "delete",
246
279
  ...variables
247
280
  });
281
+ const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
282
+ const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
283
+ const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
284
+ const resolveBranch = (variables) => fetch$1({
285
+ url: "/dbs/{dbName}/resolveBranch",
286
+ method: "get",
287
+ ...variables
288
+ });
248
289
  const getBranchDetails = (variables) => fetch$1({
249
290
  url: "/db/{dbBranchName}",
250
291
  method: "get",
@@ -352,6 +393,11 @@ const queryTable = (variables) => fetch$1({
352
393
  method: "post",
353
394
  ...variables
354
395
  });
396
+ const searchTable = (variables) => fetch$1({
397
+ url: "/db/{dbBranchName}/tables/{tableName}/search",
398
+ method: "post",
399
+ ...variables
400
+ });
355
401
  const searchBranch = (variables) => fetch$1({
356
402
  url: "/db/{dbBranchName}/search",
357
403
  method: "post",
@@ -373,7 +419,15 @@ const operationsByTag = {
373
419
  resendWorkspaceMemberInvite,
374
420
  acceptWorkspaceMemberInvite
375
421
  },
376
- database: { getDatabaseList, createDatabase, deleteDatabase },
422
+ database: {
423
+ getDatabaseList,
424
+ createDatabase,
425
+ deleteDatabase,
426
+ getGitBranchesMapping,
427
+ addGitBranchesEntry,
428
+ removeGitBranchesEntry,
429
+ resolveBranch
430
+ },
377
431
  branch: {
378
432
  getBranchList,
379
433
  getBranchDetails,
@@ -407,6 +461,7 @@ const operationsByTag = {
407
461
  getRecord,
408
462
  bulkInsertTableRecords,
409
463
  queryTable,
464
+ searchTable,
410
465
  searchBranch
411
466
  }
412
467
  };
@@ -436,35 +491,35 @@ function isValidBuilder(builder) {
436
491
  return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
437
492
  }
438
493
 
439
- var __accessCheck$6 = (obj, member, msg) => {
494
+ var __accessCheck$7 = (obj, member, msg) => {
440
495
  if (!member.has(obj))
441
496
  throw TypeError("Cannot " + msg);
442
497
  };
443
- var __privateGet$5 = (obj, member, getter) => {
444
- __accessCheck$6(obj, member, "read from private field");
498
+ var __privateGet$7 = (obj, member, getter) => {
499
+ __accessCheck$7(obj, member, "read from private field");
445
500
  return getter ? getter.call(obj) : member.get(obj);
446
501
  };
447
- var __privateAdd$6 = (obj, member, value) => {
502
+ var __privateAdd$7 = (obj, member, value) => {
448
503
  if (member.has(obj))
449
504
  throw TypeError("Cannot add the same private member more than once");
450
505
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
451
506
  };
452
- var __privateSet$4 = (obj, member, value, setter) => {
453
- __accessCheck$6(obj, member, "write to private field");
507
+ var __privateSet$6 = (obj, member, value, setter) => {
508
+ __accessCheck$7(obj, member, "write to private field");
454
509
  setter ? setter.call(obj, value) : member.set(obj, value);
455
510
  return value;
456
511
  };
457
512
  var _extraProps, _namespaces;
458
513
  class XataApiClient {
459
514
  constructor(options = {}) {
460
- __privateAdd$6(this, _extraProps, void 0);
461
- __privateAdd$6(this, _namespaces, {});
515
+ __privateAdd$7(this, _extraProps, void 0);
516
+ __privateAdd$7(this, _namespaces, {});
462
517
  const provider = options.host ?? "production";
463
518
  const apiKey = options?.apiKey ?? getAPIKey();
464
519
  if (!apiKey) {
465
520
  throw new Error("Could not resolve a valid apiKey");
466
521
  }
467
- __privateSet$4(this, _extraProps, {
522
+ __privateSet$6(this, _extraProps, {
468
523
  apiUrl: getHostUrl(provider, "main"),
469
524
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
470
525
  fetchImpl: getFetchImplementation(options.fetch),
@@ -472,34 +527,34 @@ class XataApiClient {
472
527
  });
473
528
  }
474
529
  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;
530
+ if (!__privateGet$7(this, _namespaces).user)
531
+ __privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
532
+ return __privateGet$7(this, _namespaces).user;
478
533
  }
479
534
  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;
535
+ if (!__privateGet$7(this, _namespaces).workspaces)
536
+ __privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
537
+ return __privateGet$7(this, _namespaces).workspaces;
483
538
  }
484
539
  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;
540
+ if (!__privateGet$7(this, _namespaces).databases)
541
+ __privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
542
+ return __privateGet$7(this, _namespaces).databases;
488
543
  }
489
544
  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;
545
+ if (!__privateGet$7(this, _namespaces).branches)
546
+ __privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
547
+ return __privateGet$7(this, _namespaces).branches;
493
548
  }
494
549
  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;
550
+ if (!__privateGet$7(this, _namespaces).tables)
551
+ __privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
552
+ return __privateGet$7(this, _namespaces).tables;
498
553
  }
499
554
  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;
555
+ if (!__privateGet$7(this, _namespaces).records)
556
+ __privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
557
+ return __privateGet$7(this, _namespaces).records;
503
558
  }
504
559
  }
505
560
  _extraProps = new WeakMap();
@@ -633,6 +688,33 @@ class DatabaseApi {
633
688
  ...this.extraProps
634
689
  });
635
690
  }
691
+ getGitBranchesMapping(workspace, dbName) {
692
+ return operationsByTag.database.getGitBranchesMapping({
693
+ pathParams: { workspace, dbName },
694
+ ...this.extraProps
695
+ });
696
+ }
697
+ addGitBranchesEntry(workspace, dbName, body) {
698
+ return operationsByTag.database.addGitBranchesEntry({
699
+ pathParams: { workspace, dbName },
700
+ body,
701
+ ...this.extraProps
702
+ });
703
+ }
704
+ removeGitBranchesEntry(workspace, dbName, gitBranch) {
705
+ return operationsByTag.database.removeGitBranchesEntry({
706
+ pathParams: { workspace, dbName },
707
+ queryParams: { gitBranch },
708
+ ...this.extraProps
709
+ });
710
+ }
711
+ resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
712
+ return operationsByTag.database.resolveBranch({
713
+ pathParams: { workspace, dbName },
714
+ queryParams: { gitBranch, fallbackBranch },
715
+ ...this.extraProps
716
+ });
717
+ }
636
718
  }
637
719
  class BranchApi {
638
720
  constructor(extraProps) {
@@ -650,10 +732,10 @@ class BranchApi {
650
732
  ...this.extraProps
651
733
  });
652
734
  }
653
- createBranch(workspace, database, branch, from = "", options = {}) {
735
+ createBranch(workspace, database, branch, from, options = {}) {
654
736
  return operationsByTag.branch.createBranch({
655
737
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
656
- queryParams: { from },
738
+ queryParams: isString(from) ? { from } : void 0,
657
739
  body: options,
658
740
  ...this.extraProps
659
741
  });
@@ -835,6 +917,13 @@ class RecordsApi {
835
917
  ...this.extraProps
836
918
  });
837
919
  }
920
+ searchTable(workspace, database, branch, tableName, query) {
921
+ return operationsByTag.records.searchTable({
922
+ pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
923
+ body: query,
924
+ ...this.extraProps
925
+ });
926
+ }
838
927
  searchBranch(workspace, database, branch, query) {
839
928
  return operationsByTag.records.searchBranch({
840
929
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
@@ -854,43 +943,43 @@ class XataApiPlugin {
854
943
  class XataPlugin {
855
944
  }
856
945
 
857
- var __accessCheck$5 = (obj, member, msg) => {
946
+ var __accessCheck$6 = (obj, member, msg) => {
858
947
  if (!member.has(obj))
859
948
  throw TypeError("Cannot " + msg);
860
949
  };
861
- var __privateGet$4 = (obj, member, getter) => {
862
- __accessCheck$5(obj, member, "read from private field");
950
+ var __privateGet$6 = (obj, member, getter) => {
951
+ __accessCheck$6(obj, member, "read from private field");
863
952
  return getter ? getter.call(obj) : member.get(obj);
864
953
  };
865
- var __privateAdd$5 = (obj, member, value) => {
954
+ var __privateAdd$6 = (obj, member, value) => {
866
955
  if (member.has(obj))
867
956
  throw TypeError("Cannot add the same private member more than once");
868
957
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
869
958
  };
870
- var __privateSet$3 = (obj, member, value, setter) => {
871
- __accessCheck$5(obj, member, "write to private field");
959
+ var __privateSet$5 = (obj, member, value, setter) => {
960
+ __accessCheck$6(obj, member, "write to private field");
872
961
  setter ? setter.call(obj, value) : member.set(obj, value);
873
962
  return value;
874
963
  };
875
- var _query;
964
+ var _query, _page;
876
965
  class Page {
877
966
  constructor(query, meta, records = []) {
878
- __privateAdd$5(this, _query, void 0);
879
- __privateSet$3(this, _query, query);
967
+ __privateAdd$6(this, _query, void 0);
968
+ __privateSet$5(this, _query, query);
880
969
  this.meta = meta;
881
- this.records = records;
970
+ this.records = new RecordArray(this, records);
882
971
  }
883
972
  async nextPage(size, offset) {
884
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
973
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
885
974
  }
886
975
  async previousPage(size, offset) {
887
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
976
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
888
977
  }
889
978
  async firstPage(size, offset) {
890
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
979
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
891
980
  }
892
981
  async lastPage(size, offset) {
893
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
982
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
894
983
  }
895
984
  hasNextPage() {
896
985
  return this.meta.page.more;
@@ -898,50 +987,93 @@ class Page {
898
987
  }
899
988
  _query = new WeakMap();
900
989
  const PAGINATION_MAX_SIZE = 200;
901
- const PAGINATION_DEFAULT_SIZE = 200;
990
+ const PAGINATION_DEFAULT_SIZE = 20;
902
991
  const PAGINATION_MAX_OFFSET = 800;
903
992
  const PAGINATION_DEFAULT_OFFSET = 0;
993
+ function isCursorPaginationOptions(options) {
994
+ return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
995
+ }
996
+ const _RecordArray = class extends Array {
997
+ constructor(page, overrideRecords) {
998
+ super(..._RecordArray.parseConstructorParams(page, overrideRecords));
999
+ __privateAdd$6(this, _page, void 0);
1000
+ __privateSet$5(this, _page, page);
1001
+ }
1002
+ static parseConstructorParams(...args) {
1003
+ if (args.length === 1 && typeof args[0] === "number") {
1004
+ return new Array(args[0]);
1005
+ }
1006
+ if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
1007
+ const result = args[1] ?? args[0].records ?? [];
1008
+ return new Array(...result);
1009
+ }
1010
+ return new Array(...args);
1011
+ }
1012
+ async nextPage(size, offset) {
1013
+ const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
1014
+ return new _RecordArray(newPage);
1015
+ }
1016
+ async previousPage(size, offset) {
1017
+ const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
1018
+ return new _RecordArray(newPage);
1019
+ }
1020
+ async firstPage(size, offset) {
1021
+ const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
1022
+ return new _RecordArray(newPage);
1023
+ }
1024
+ async lastPage(size, offset) {
1025
+ const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
1026
+ return new _RecordArray(newPage);
1027
+ }
1028
+ hasNextPage() {
1029
+ return __privateGet$6(this, _page).meta.page.more;
1030
+ }
1031
+ };
1032
+ let RecordArray = _RecordArray;
1033
+ _page = new WeakMap();
904
1034
 
905
- var __accessCheck$4 = (obj, member, msg) => {
1035
+ var __accessCheck$5 = (obj, member, msg) => {
906
1036
  if (!member.has(obj))
907
1037
  throw TypeError("Cannot " + msg);
908
1038
  };
909
- var __privateGet$3 = (obj, member, getter) => {
910
- __accessCheck$4(obj, member, "read from private field");
1039
+ var __privateGet$5 = (obj, member, getter) => {
1040
+ __accessCheck$5(obj, member, "read from private field");
911
1041
  return getter ? getter.call(obj) : member.get(obj);
912
1042
  };
913
- var __privateAdd$4 = (obj, member, value) => {
1043
+ var __privateAdd$5 = (obj, member, value) => {
914
1044
  if (member.has(obj))
915
1045
  throw TypeError("Cannot add the same private member more than once");
916
1046
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
917
1047
  };
918
- var __privateSet$2 = (obj, member, value, setter) => {
919
- __accessCheck$4(obj, member, "write to private field");
1048
+ var __privateSet$4 = (obj, member, value, setter) => {
1049
+ __accessCheck$5(obj, member, "write to private field");
920
1050
  setter ? setter.call(obj, value) : member.set(obj, value);
921
1051
  return value;
922
1052
  };
923
1053
  var _table$1, _repository, _data;
924
1054
  const _Query = class {
925
- 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: {} });
1055
+ constructor(repository, table, data, rawParent) {
1056
+ __privateAdd$5(this, _table$1, void 0);
1057
+ __privateAdd$5(this, _repository, void 0);
1058
+ __privateAdd$5(this, _data, { filter: {} });
929
1059
  this.meta = { page: { cursor: "start", more: true } };
930
- this.records = [];
931
- __privateSet$2(this, _table$1, table);
1060
+ this.records = new RecordArray(this, []);
1061
+ __privateSet$4(this, _table$1, table);
932
1062
  if (repository) {
933
- __privateSet$2(this, _repository, repository);
1063
+ __privateSet$4(this, _repository, repository);
934
1064
  } else {
935
- __privateSet$2(this, _repository, this);
1065
+ __privateSet$4(this, _repository, this);
936
1066
  }
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;
1067
+ const parent = cleanParent(data, rawParent);
1068
+ __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
1069
+ __privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
1070
+ __privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
1071
+ __privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
1072
+ __privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
1073
+ __privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
1074
+ __privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
1075
+ __privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
1076
+ __privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
945
1077
  this.any = this.any.bind(this);
946
1078
  this.all = this.all.bind(this);
947
1079
  this.not = this.not.bind(this);
@@ -952,75 +1084,88 @@ const _Query = class {
952
1084
  Object.defineProperty(this, "repository", { enumerable: false });
953
1085
  }
954
1086
  getQueryOptions() {
955
- return __privateGet$3(this, _data);
1087
+ return __privateGet$5(this, _data);
1088
+ }
1089
+ key() {
1090
+ const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
1091
+ const key = JSON.stringify({ columns, filter, sort, pagination });
1092
+ return toBase64(key);
956
1093
  }
957
1094
  any(...queries) {
958
1095
  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));
1096
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
960
1097
  }
961
1098
  all(...queries) {
962
1099
  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));
1100
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
964
1101
  }
965
1102
  not(...queries) {
966
1103
  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));
1104
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
968
1105
  }
969
1106
  none(...queries) {
970
1107
  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));
1108
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
972
1109
  }
973
1110
  filter(a, b) {
974
1111
  if (arguments.length === 1) {
975
1112
  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));
1113
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
1114
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
978
1115
  } 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));
1116
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1117
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
981
1118
  }
982
1119
  }
983
1120
  sort(column, direction) {
984
- const originalSort = [__privateGet$3(this, _data).sort ?? []].flat();
1121
+ const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
985
1122
  const sort = [...originalSort, { column, direction }];
986
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { sort }, __privateGet$3(this, _data));
1123
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
987
1124
  }
988
1125
  select(columns) {
989
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { columns }, __privateGet$3(this, _data));
1126
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
990
1127
  }
991
1128
  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);
1129
+ const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
1130
+ return __privateGet$5(this, _repository).query(query);
994
1131
  }
995
1132
  async *[Symbol.asyncIterator]() {
996
- for await (const [record] of this.getIterator(1)) {
1133
+ for await (const [record] of this.getIterator({ batchSize: 1 })) {
997
1134
  yield record;
998
1135
  }
999
1136
  }
1000
- async *getIterator(chunk, options = {}) {
1001
- let offset = 0;
1002
- let end = false;
1003
- while (!end) {
1004
- const { records, meta } = await this.getPaginated({ ...options, page: { size: chunk, offset } });
1005
- yield records;
1006
- offset += chunk;
1007
- end = !meta.page.more;
1137
+ async *getIterator(options = {}) {
1138
+ const { batchSize = 1 } = options;
1139
+ let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
1140
+ let more = page.hasNextPage();
1141
+ yield page.records;
1142
+ while (more) {
1143
+ page = await page.nextPage();
1144
+ more = page.hasNextPage();
1145
+ yield page.records;
1008
1146
  }
1009
1147
  }
1010
1148
  async getMany(options = {}) {
1011
- const { records } = await this.getPaginated(options);
1012
- return records;
1149
+ const page = await this.getPaginated(options);
1150
+ if (page.hasNextPage() && options.pagination?.size === void 0) {
1151
+ console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
1152
+ }
1153
+ return page.records;
1013
1154
  }
1014
- async getAll(chunk = PAGINATION_MAX_SIZE, options = {}) {
1155
+ async getAll(options = {}) {
1156
+ const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
1015
1157
  const results = [];
1016
- for await (const page of this.getIterator(chunk, options)) {
1158
+ for await (const page of this.getIterator({ ...rest, batchSize })) {
1017
1159
  results.push(...page);
1018
1160
  }
1019
1161
  return results;
1020
1162
  }
1021
- async getOne(options = {}) {
1022
- const records = await this.getMany({ ...options, page: { size: 1 } });
1023
- return records[0] || null;
1163
+ async getFirst(options = {}) {
1164
+ const records = await this.getMany({ ...options, pagination: { size: 1 } });
1165
+ return records[0] ?? null;
1166
+ }
1167
+ cache(ttl) {
1168
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
1024
1169
  }
1025
1170
  nextPage(size, offset) {
1026
1171
  return this.firstPage(size, offset);
@@ -1029,10 +1174,10 @@ const _Query = class {
1029
1174
  return this.firstPage(size, offset);
1030
1175
  }
1031
1176
  firstPage(size, offset) {
1032
- return this.getPaginated({ page: { size, offset } });
1177
+ return this.getPaginated({ pagination: { size, offset } });
1033
1178
  }
1034
1179
  lastPage(size, offset) {
1035
- return this.getPaginated({ page: { size, offset, before: "end" } });
1180
+ return this.getPaginated({ pagination: { size, offset, before: "end" } });
1036
1181
  }
1037
1182
  hasNextPage() {
1038
1183
  return this.meta.page.more;
@@ -1042,12 +1187,20 @@ let Query = _Query;
1042
1187
  _table$1 = new WeakMap();
1043
1188
  _repository = new WeakMap();
1044
1189
  _data = new WeakMap();
1190
+ function cleanParent(data, parent) {
1191
+ if (isCursorPaginationOptions(data.pagination)) {
1192
+ return { ...parent, sorting: void 0, filter: void 0 };
1193
+ }
1194
+ return parent;
1195
+ }
1045
1196
 
1046
1197
  function isIdentifiable(x) {
1047
1198
  return isObject(x) && isString(x?.id);
1048
1199
  }
1049
1200
  function isXataRecord(x) {
1050
- return isIdentifiable(x) && typeof x?.xata === "object" && typeof x?.xata?.version === "number";
1201
+ const record = x;
1202
+ const metadata = record?.getMetadata();
1203
+ return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
1051
1204
  }
1052
1205
 
1053
1206
  function isSortFilterString(value) {
@@ -1073,169 +1226,231 @@ function buildSortFilter(filter) {
1073
1226
  }
1074
1227
  }
1075
1228
 
1076
- var __accessCheck$3 = (obj, member, msg) => {
1229
+ var __accessCheck$4 = (obj, member, msg) => {
1077
1230
  if (!member.has(obj))
1078
1231
  throw TypeError("Cannot " + msg);
1079
1232
  };
1080
- var __privateGet$2 = (obj, member, getter) => {
1081
- __accessCheck$3(obj, member, "read from private field");
1233
+ var __privateGet$4 = (obj, member, getter) => {
1234
+ __accessCheck$4(obj, member, "read from private field");
1082
1235
  return getter ? getter.call(obj) : member.get(obj);
1083
1236
  };
1084
- var __privateAdd$3 = (obj, member, value) => {
1237
+ var __privateAdd$4 = (obj, member, value) => {
1085
1238
  if (member.has(obj))
1086
1239
  throw TypeError("Cannot add the same private member more than once");
1087
1240
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1088
1241
  };
1089
- var __privateSet$1 = (obj, member, value, setter) => {
1090
- __accessCheck$3(obj, member, "write to private field");
1242
+ var __privateSet$3 = (obj, member, value, setter) => {
1243
+ __accessCheck$4(obj, member, "write to private field");
1091
1244
  setter ? setter.call(obj, value) : member.set(obj, value);
1092
1245
  return value;
1093
1246
  };
1094
1247
  var __privateMethod$2 = (obj, member, method) => {
1095
- __accessCheck$3(obj, member, "access private method");
1248
+ __accessCheck$4(obj, member, "access private method");
1096
1249
  return method;
1097
1250
  };
1098
- var _table, _links, _getFetchProps, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn;
1251
+ 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
1252
  class Repository extends Query {
1100
1253
  }
1101
1254
  class RestRepository extends Query {
1102
1255
  constructor(options) {
1103
1256
  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);
1257
+ __privateAdd$4(this, _insertRecordWithoutId);
1258
+ __privateAdd$4(this, _insertRecordWithId);
1259
+ __privateAdd$4(this, _bulkInsertTableRecords);
1260
+ __privateAdd$4(this, _updateRecordWithID);
1261
+ __privateAdd$4(this, _upsertRecordWithID);
1262
+ __privateAdd$4(this, _deleteRecord);
1263
+ __privateAdd$4(this, _invalidateCache);
1264
+ __privateAdd$4(this, _setCacheRecord);
1265
+ __privateAdd$4(this, _getCacheRecord);
1266
+ __privateAdd$4(this, _setCacheQuery);
1267
+ __privateAdd$4(this, _getCacheQuery);
1268
+ __privateAdd$4(this, _getSchema$1);
1269
+ __privateAdd$4(this, _table, void 0);
1270
+ __privateAdd$4(this, _getFetchProps, void 0);
1271
+ __privateAdd$4(this, _cache, void 0);
1272
+ __privateAdd$4(this, _schema$1, void 0);
1273
+ __privateSet$3(this, _table, options.table);
1274
+ __privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
1116
1275
  this.db = options.db;
1276
+ __privateSet$3(this, _cache, options.pluginOptions.cache);
1117
1277
  }
1118
1278
  async create(a, b) {
1119
1279
  if (Array.isArray(a)) {
1120
- return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1280
+ if (a.length === 0)
1281
+ return [];
1282
+ const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1283
+ await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1284
+ return records;
1121
1285
  }
1122
1286
  if (isString(a) && isObject(b)) {
1123
1287
  if (a === "")
1124
1288
  throw new Error("The id can't be empty");
1125
- return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1289
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1290
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1291
+ return record;
1126
1292
  }
1127
1293
  if (isObject(a) && isString(a.id)) {
1128
1294
  if (a.id === "")
1129
1295
  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 });
1296
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
1297
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1298
+ return record;
1131
1299
  }
1132
1300
  if (isObject(a)) {
1133
- return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1301
+ const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1302
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1303
+ return record;
1134
1304
  }
1135
1305
  throw new Error("Invalid arguments for create method");
1136
1306
  }
1137
- async read(recordId) {
1138
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1139
- try {
1140
- const response = await getRecord({
1141
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1142
- ...fetchProps
1143
- });
1144
- return initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), response);
1145
- } catch (e) {
1146
- if (isObject(e) && e.status === 404) {
1147
- return null;
1307
+ async read(a) {
1308
+ if (Array.isArray(a)) {
1309
+ if (a.length === 0)
1310
+ return [];
1311
+ const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
1312
+ return this.getAll({ filter: { id: { $any: ids } } });
1313
+ }
1314
+ const id = isString(a) ? a : a.id;
1315
+ if (isString(id)) {
1316
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
1317
+ if (cacheRecord)
1318
+ return cacheRecord;
1319
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1320
+ try {
1321
+ const response = await getRecord({
1322
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
1323
+ ...fetchProps
1324
+ });
1325
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1326
+ return initObject(this.db, schema, __privateGet$4(this, _table), response);
1327
+ } catch (e) {
1328
+ if (isObject(e) && e.status === 404) {
1329
+ return null;
1330
+ }
1331
+ throw e;
1148
1332
  }
1149
- throw e;
1150
1333
  }
1151
1334
  }
1152
1335
  async update(a, b) {
1153
1336
  if (Array.isArray(a)) {
1337
+ if (a.length === 0)
1338
+ return [];
1154
1339
  if (a.length > 100) {
1155
1340
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1156
1341
  }
1157
1342
  return Promise.all(a.map((object) => this.update(object)));
1158
1343
  }
1159
1344
  if (isString(a) && isObject(b)) {
1160
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1345
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1346
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1347
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1348
+ return record;
1161
1349
  }
1162
1350
  if (isObject(a) && isString(a.id)) {
1163
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1351
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1352
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1353
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1354
+ return record;
1164
1355
  }
1165
1356
  throw new Error("Invalid arguments for update method");
1166
1357
  }
1167
1358
  async createOrUpdate(a, b) {
1168
1359
  if (Array.isArray(a)) {
1360
+ if (a.length === 0)
1361
+ return [];
1169
1362
  if (a.length > 100) {
1170
1363
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1171
1364
  }
1172
1365
  return Promise.all(a.map((object) => this.createOrUpdate(object)));
1173
1366
  }
1174
1367
  if (isString(a) && isObject(b)) {
1175
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1368
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1369
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1370
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1371
+ return record;
1176
1372
  }
1177
1373
  if (isObject(a) && isString(a.id)) {
1178
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1374
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1375
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1376
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1377
+ return record;
1179
1378
  }
1180
1379
  throw new Error("Invalid arguments for createOrUpdate method");
1181
1380
  }
1182
- async delete(recordId) {
1183
- if (Array.isArray(recordId)) {
1184
- if (recordId.length > 100) {
1381
+ async delete(a) {
1382
+ if (Array.isArray(a)) {
1383
+ if (a.length === 0)
1384
+ return;
1385
+ if (a.length > 100) {
1185
1386
  console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1186
1387
  }
1187
- await Promise.all(recordId.map((id) => this.delete(id)));
1388
+ await Promise.all(a.map((id) => this.delete(id)));
1188
1389
  return;
1189
1390
  }
1190
- if (isString(recordId)) {
1191
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId);
1391
+ if (isString(a)) {
1392
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
1393
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1192
1394
  return;
1193
1395
  }
1194
- if (isObject(recordId) && isString(recordId.id)) {
1195
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId.id);
1396
+ if (isObject(a) && isString(a.id)) {
1397
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
1398
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1196
1399
  return;
1197
1400
  }
1198
1401
  throw new Error("Invalid arguments for delete method");
1199
1402
  }
1200
1403
  async search(query, options = {}) {
1201
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1202
- const { records } = await searchBranch({
1203
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1204
- body: { tables: [__privateGet$2(this, _table)], query, fuzziness: options.fuzziness },
1404
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1405
+ const { records } = await searchTable({
1406
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1407
+ body: {
1408
+ query,
1409
+ fuzziness: options.fuzziness,
1410
+ highlight: options.highlight,
1411
+ filter: options.filter
1412
+ },
1205
1413
  ...fetchProps
1206
1414
  });
1207
- return records.map((item) => initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), item));
1415
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1416
+ return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
1208
1417
  }
1209
1418
  async query(query) {
1419
+ const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
1420
+ if (cacheQuery)
1421
+ return new Page(query, cacheQuery.meta, cacheQuery.records);
1210
1422
  const data = query.getQueryOptions();
1211
1423
  const body = {
1212
1424
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1213
- sort: data.sort ? buildSortFilter(data.sort) : void 0,
1214
- page: data.page,
1425
+ sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1426
+ page: data.pagination,
1215
1427
  columns: data.columns
1216
1428
  };
1217
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1429
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1218
1430
  const { meta, records: objects } = await queryTable({
1219
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1431
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1220
1432
  body,
1221
1433
  ...fetchProps
1222
1434
  });
1223
- const records = objects.map((record) => initObject(this.db, __privateGet$2(this, _links), __privateGet$2(this, _table), record));
1435
+ const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1436
+ const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
1437
+ await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1224
1438
  return new Page(query, meta, records);
1225
1439
  }
1226
1440
  }
1227
1441
  _table = new WeakMap();
1228
- _links = new WeakMap();
1229
1442
  _getFetchProps = new WeakMap();
1443
+ _cache = new WeakMap();
1444
+ _schema$1 = new WeakMap();
1230
1445
  _insertRecordWithoutId = new WeakSet();
1231
1446
  insertRecordWithoutId_fn = async function(object) {
1232
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1447
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1233
1448
  const record = transformObjectLinks(object);
1234
1449
  const response = await insertRecord({
1235
1450
  pathParams: {
1236
1451
  workspace: "{workspaceId}",
1237
1452
  dbBranchName: "{dbBranch}",
1238
- tableName: __privateGet$2(this, _table)
1453
+ tableName: __privateGet$4(this, _table)
1239
1454
  },
1240
1455
  body: record,
1241
1456
  ...fetchProps
@@ -1248,13 +1463,13 @@ insertRecordWithoutId_fn = async function(object) {
1248
1463
  };
1249
1464
  _insertRecordWithId = new WeakSet();
1250
1465
  insertRecordWithId_fn = async function(recordId, object) {
1251
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1466
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1252
1467
  const record = transformObjectLinks(object);
1253
1468
  const response = await insertRecordWithID({
1254
1469
  pathParams: {
1255
1470
  workspace: "{workspaceId}",
1256
1471
  dbBranchName: "{dbBranch}",
1257
- tableName: __privateGet$2(this, _table),
1472
+ tableName: __privateGet$4(this, _table),
1258
1473
  recordId
1259
1474
  },
1260
1475
  body: record,
@@ -1269,25 +1484,29 @@ insertRecordWithId_fn = async function(recordId, object) {
1269
1484
  };
1270
1485
  _bulkInsertTableRecords = new WeakSet();
1271
1486
  bulkInsertTableRecords_fn = async function(objects) {
1272
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1487
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1273
1488
  const records = objects.map((object) => transformObjectLinks(object));
1274
- const response = await bulkInsertTableRecords({
1275
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1489
+ const { recordIDs } = await bulkInsertTableRecords({
1490
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1276
1491
  body: { records },
1277
1492
  ...fetchProps
1278
1493
  });
1279
- const finalObjects = await this.any(...response.recordIDs.map((id) => this.filter("id", id))).getAll();
1494
+ const finalObjects = await this.read(recordIDs);
1280
1495
  if (finalObjects.length !== objects.length) {
1281
1496
  throw new Error("The server failed to save some records");
1282
1497
  }
1283
- return finalObjects;
1498
+ const dictionary = finalObjects.reduce((acc, object) => {
1499
+ acc[object.id] = object;
1500
+ return acc;
1501
+ }, {});
1502
+ return recordIDs.map((id) => dictionary[id]);
1284
1503
  };
1285
1504
  _updateRecordWithID = new WeakSet();
1286
1505
  updateRecordWithID_fn = async function(recordId, object) {
1287
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1506
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1288
1507
  const record = transformObjectLinks(object);
1289
1508
  const response = await updateRecordWithID({
1290
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1509
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1291
1510
  body: record,
1292
1511
  ...fetchProps
1293
1512
  });
@@ -1298,9 +1517,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1298
1517
  };
1299
1518
  _upsertRecordWithID = new WeakSet();
1300
1519
  upsertRecordWithID_fn = async function(recordId, object) {
1301
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1520
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1302
1521
  const response = await upsertRecordWithID({
1303
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1522
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1304
1523
  body: object,
1305
1524
  ...fetchProps
1306
1525
  });
@@ -1311,12 +1530,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
1311
1530
  };
1312
1531
  _deleteRecord = new WeakSet();
1313
1532
  deleteRecord_fn = async function(recordId) {
1314
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1533
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1315
1534
  await deleteRecord({
1316
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1535
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1317
1536
  ...fetchProps
1318
1537
  });
1319
1538
  };
1539
+ _invalidateCache = new WeakSet();
1540
+ invalidateCache_fn = async function(recordId) {
1541
+ await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1542
+ const cacheItems = await __privateGet$4(this, _cache).getAll();
1543
+ const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1544
+ for (const [key, value] of queries) {
1545
+ const ids = getIds(value);
1546
+ if (ids.includes(recordId))
1547
+ await __privateGet$4(this, _cache).delete(key);
1548
+ }
1549
+ };
1550
+ _setCacheRecord = new WeakSet();
1551
+ setCacheRecord_fn = async function(record) {
1552
+ if (!__privateGet$4(this, _cache).cacheRecords)
1553
+ return;
1554
+ await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
1555
+ };
1556
+ _getCacheRecord = new WeakSet();
1557
+ getCacheRecord_fn = async function(recordId) {
1558
+ if (!__privateGet$4(this, _cache).cacheRecords)
1559
+ return null;
1560
+ return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1561
+ };
1562
+ _setCacheQuery = new WeakSet();
1563
+ setCacheQuery_fn = async function(query, meta, records) {
1564
+ await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1565
+ };
1566
+ _getCacheQuery = new WeakSet();
1567
+ getCacheQuery_fn = async function(query) {
1568
+ const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
1569
+ const result = await __privateGet$4(this, _cache).get(key);
1570
+ if (!result)
1571
+ return null;
1572
+ const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1573
+ if (ttl < 0)
1574
+ return null;
1575
+ const hasExpired = result.date.getTime() + ttl < Date.now();
1576
+ return hasExpired ? null : result;
1577
+ };
1578
+ _getSchema$1 = new WeakSet();
1579
+ getSchema_fn$1 = async function() {
1580
+ if (__privateGet$4(this, _schema$1))
1581
+ return __privateGet$4(this, _schema$1);
1582
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1583
+ const { schema } = await getBranchDetails({
1584
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1585
+ ...fetchProps
1586
+ });
1587
+ __privateSet$3(this, _schema$1, schema);
1588
+ return schema;
1589
+ };
1320
1590
  const transformObjectLinks = (object) => {
1321
1591
  return Object.entries(object).reduce((acc, [key, value]) => {
1322
1592
  if (key === "xata")
@@ -1324,15 +1594,34 @@ const transformObjectLinks = (object) => {
1324
1594
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1325
1595
  }, {});
1326
1596
  };
1327
- const initObject = (db, links, table, object) => {
1597
+ const initObject = (db, schema, table, object) => {
1328
1598
  const result = {};
1329
- 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);
1599
+ const { xata, ...rest } = object ?? {};
1600
+ Object.assign(result, rest);
1601
+ const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
1602
+ if (!columns)
1603
+ console.error(`Table ${table} not found in schema`);
1604
+ for (const column of columns ?? []) {
1605
+ const value = result[column.name];
1606
+ switch (column.type) {
1607
+ case "datetime": {
1608
+ const date = value !== void 0 ? new Date(value) : void 0;
1609
+ if (date && isNaN(date.getTime())) {
1610
+ console.error(`Failed to parse date ${value} for field ${column.name}`);
1611
+ } else if (date) {
1612
+ result[column.name] = date;
1613
+ }
1614
+ break;
1615
+ }
1616
+ case "link": {
1617
+ const linkTable = column.link?.table;
1618
+ if (!linkTable) {
1619
+ console.error(`Failed to parse link for field ${column.name}`);
1620
+ } else if (isObject(value)) {
1621
+ result[column.name] = initObject(db, schema, linkTable, value);
1622
+ }
1623
+ break;
1624
+ }
1336
1625
  }
1337
1626
  }
1338
1627
  result.read = function() {
@@ -1344,12 +1633,74 @@ const initObject = (db, links, table, object) => {
1344
1633
  result.delete = function() {
1345
1634
  return db[table].delete(result["id"]);
1346
1635
  };
1347
- for (const prop of ["read", "update", "delete"]) {
1636
+ result.getMetadata = function() {
1637
+ return xata;
1638
+ };
1639
+ for (const prop of ["read", "update", "delete", "getMetadata"]) {
1348
1640
  Object.defineProperty(result, prop, { enumerable: false });
1349
1641
  }
1350
1642
  Object.freeze(result);
1351
1643
  return result;
1352
1644
  };
1645
+ function getIds(value) {
1646
+ if (Array.isArray(value)) {
1647
+ return value.map((item) => getIds(item)).flat();
1648
+ }
1649
+ if (!isObject(value))
1650
+ return [];
1651
+ const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
1652
+ return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
1653
+ }
1654
+
1655
+ var __accessCheck$3 = (obj, member, msg) => {
1656
+ if (!member.has(obj))
1657
+ throw TypeError("Cannot " + msg);
1658
+ };
1659
+ var __privateGet$3 = (obj, member, getter) => {
1660
+ __accessCheck$3(obj, member, "read from private field");
1661
+ return getter ? getter.call(obj) : member.get(obj);
1662
+ };
1663
+ var __privateAdd$3 = (obj, member, value) => {
1664
+ if (member.has(obj))
1665
+ throw TypeError("Cannot add the same private member more than once");
1666
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1667
+ };
1668
+ var __privateSet$2 = (obj, member, value, setter) => {
1669
+ __accessCheck$3(obj, member, "write to private field");
1670
+ setter ? setter.call(obj, value) : member.set(obj, value);
1671
+ return value;
1672
+ };
1673
+ var _map;
1674
+ class SimpleCache {
1675
+ constructor(options = {}) {
1676
+ __privateAdd$3(this, _map, void 0);
1677
+ __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1678
+ this.capacity = options.max ?? 500;
1679
+ this.cacheRecords = options.cacheRecords ?? true;
1680
+ this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1681
+ }
1682
+ async getAll() {
1683
+ return Object.fromEntries(__privateGet$3(this, _map));
1684
+ }
1685
+ async get(key) {
1686
+ return __privateGet$3(this, _map).get(key) ?? null;
1687
+ }
1688
+ async set(key, value) {
1689
+ await this.delete(key);
1690
+ __privateGet$3(this, _map).set(key, value);
1691
+ if (__privateGet$3(this, _map).size > this.capacity) {
1692
+ const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
1693
+ await this.delete(leastRecentlyUsed);
1694
+ }
1695
+ }
1696
+ async delete(key) {
1697
+ __privateGet$3(this, _map).delete(key);
1698
+ }
1699
+ async clear() {
1700
+ return __privateGet$3(this, _map).clear();
1701
+ }
1702
+ }
1703
+ _map = new WeakMap();
1353
1704
 
1354
1705
  const gt = (value) => ({ $gt: value });
1355
1706
  const ge = (value) => ({ $ge: value });
@@ -1374,7 +1725,7 @@ var __accessCheck$2 = (obj, member, msg) => {
1374
1725
  if (!member.has(obj))
1375
1726
  throw TypeError("Cannot " + msg);
1376
1727
  };
1377
- var __privateGet$1 = (obj, member, getter) => {
1728
+ var __privateGet$2 = (obj, member, getter) => {
1378
1729
  __accessCheck$2(obj, member, "read from private field");
1379
1730
  return getter ? getter.call(obj) : member.get(obj);
1380
1731
  };
@@ -1385,26 +1736,24 @@ var __privateAdd$2 = (obj, member, value) => {
1385
1736
  };
1386
1737
  var _tables;
1387
1738
  class SchemaPlugin extends XataPlugin {
1388
- constructor(links, tableNames) {
1739
+ constructor(tableNames) {
1389
1740
  super();
1390
- this.links = links;
1391
1741
  this.tableNames = tableNames;
1392
1742
  __privateAdd$2(this, _tables, {});
1393
1743
  }
1394
- build(options) {
1395
- const { getFetchProps } = options;
1396
- const links = this.links;
1744
+ build(pluginOptions) {
1397
1745
  const db = new Proxy({}, {
1398
1746
  get: (_target, table) => {
1399
1747
  if (!isString(table))
1400
1748
  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];
1749
+ if (__privateGet$2(this, _tables)[table] === void 0) {
1750
+ __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1751
+ }
1752
+ return __privateGet$2(this, _tables)[table];
1404
1753
  }
1405
1754
  });
1406
1755
  for (const table of this.tableNames ?? []) {
1407
- db[table] = new RestRepository({ db, getFetchProps, table, links });
1756
+ db[table] = new RestRepository({ db, pluginOptions, table });
1408
1757
  }
1409
1758
  return db;
1410
1759
  }
@@ -1415,55 +1764,80 @@ var __accessCheck$1 = (obj, member, msg) => {
1415
1764
  if (!member.has(obj))
1416
1765
  throw TypeError("Cannot " + msg);
1417
1766
  };
1767
+ var __privateGet$1 = (obj, member, getter) => {
1768
+ __accessCheck$1(obj, member, "read from private field");
1769
+ return getter ? getter.call(obj) : member.get(obj);
1770
+ };
1418
1771
  var __privateAdd$1 = (obj, member, value) => {
1419
1772
  if (member.has(obj))
1420
1773
  throw TypeError("Cannot add the same private member more than once");
1421
1774
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1422
1775
  };
1776
+ var __privateSet$1 = (obj, member, value, setter) => {
1777
+ __accessCheck$1(obj, member, "write to private field");
1778
+ setter ? setter.call(obj, value) : member.set(obj, value);
1779
+ return value;
1780
+ };
1423
1781
  var __privateMethod$1 = (obj, member, method) => {
1424
1782
  __accessCheck$1(obj, member, "access private method");
1425
1783
  return method;
1426
1784
  };
1427
- var _search, search_fn;
1785
+ var _schema, _search, search_fn, _getSchema, getSchema_fn;
1428
1786
  class SearchPlugin extends XataPlugin {
1429
- constructor(db, links) {
1787
+ constructor(db) {
1430
1788
  super();
1431
1789
  this.db = db;
1432
- this.links = links;
1433
1790
  __privateAdd$1(this, _search);
1791
+ __privateAdd$1(this, _getSchema);
1792
+ __privateAdd$1(this, _schema, void 0);
1434
1793
  }
1435
1794
  build({ getFetchProps }) {
1436
1795
  return {
1437
1796
  all: async (query, options = {}) => {
1438
1797
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1798
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1439
1799
  return records.map((record) => {
1440
1800
  const { table = "orphan" } = record.xata;
1441
- return { table, record: initObject(this.db, this.links, table, record) };
1801
+ return { table, record: initObject(this.db, schema, table, record) };
1442
1802
  });
1443
1803
  },
1444
1804
  byTable: async (query, options = {}) => {
1445
1805
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1806
+ const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1446
1807
  return records.reduce((acc, record) => {
1447
1808
  const { table = "orphan" } = record.xata;
1448
1809
  const items = acc[table] ?? [];
1449
- const item = initObject(this.db, this.links, table, record);
1810
+ const item = initObject(this.db, schema, table, record);
1450
1811
  return { ...acc, [table]: [...items, item] };
1451
1812
  }, {});
1452
1813
  }
1453
1814
  };
1454
1815
  }
1455
1816
  }
1817
+ _schema = new WeakMap();
1456
1818
  _search = new WeakSet();
1457
1819
  search_fn = async function(query, options, getFetchProps) {
1458
1820
  const fetchProps = await getFetchProps();
1459
- const { tables, fuzziness } = options ?? {};
1821
+ const { tables, fuzziness, highlight } = options ?? {};
1460
1822
  const { records } = await searchBranch({
1461
1823
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1462
- body: { tables, query, fuzziness },
1824
+ body: { tables, query, fuzziness, highlight },
1463
1825
  ...fetchProps
1464
1826
  });
1465
1827
  return records;
1466
1828
  };
1829
+ _getSchema = new WeakSet();
1830
+ getSchema_fn = async function(getFetchProps) {
1831
+ if (__privateGet$1(this, _schema))
1832
+ return __privateGet$1(this, _schema);
1833
+ const fetchProps = await getFetchProps();
1834
+ const { schema } = await getBranchDetails({
1835
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1836
+ ...fetchProps
1837
+ });
1838
+ __privateSet$1(this, _schema, schema);
1839
+ return schema;
1840
+ };
1467
1841
 
1468
1842
  const isBranchStrategyBuilder = (strategy) => {
1469
1843
  return typeof strategy === "function";
@@ -1475,30 +1849,39 @@ const envBranchNames = [
1475
1849
  "CF_PAGES_BRANCH",
1476
1850
  "BRANCH"
1477
1851
  ];
1478
- const defaultBranch = "main";
1479
1852
  async function getCurrentBranchName(options) {
1480
- const env = await getBranchByEnvVariable();
1481
- if (env)
1482
- return env;
1483
- const branch = await getGitBranch();
1484
- if (!branch)
1485
- return defaultBranch;
1486
- const details = await getDatabaseBranch(branch, options);
1487
- if (details)
1488
- return branch;
1489
- return defaultBranch;
1853
+ const env = getBranchByEnvVariable();
1854
+ if (env) {
1855
+ const details = await getDatabaseBranch(env, options);
1856
+ if (details)
1857
+ return env;
1858
+ console.warn(`Branch ${env} not found in Xata. Ignoring...`);
1859
+ }
1860
+ const gitBranch = await getGitBranch();
1861
+ return resolveXataBranch(gitBranch, options);
1490
1862
  }
1491
1863
  async function getCurrentBranchDetails(options) {
1492
- const env = await getBranchByEnvVariable();
1493
- if (env)
1494
- return getDatabaseBranch(env, options);
1495
- const branch = await getGitBranch();
1496
- if (!branch)
1497
- return getDatabaseBranch(defaultBranch, options);
1498
- const details = await getDatabaseBranch(branch, options);
1499
- if (details)
1500
- return details;
1501
- return getDatabaseBranch(defaultBranch, options);
1864
+ const branch = await getCurrentBranchName(options);
1865
+ return getDatabaseBranch(branch, options);
1866
+ }
1867
+ async function resolveXataBranch(gitBranch, options) {
1868
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1869
+ const apiKey = options?.apiKey || getAPIKey();
1870
+ if (!databaseURL)
1871
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1872
+ if (!apiKey)
1873
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1874
+ const [protocol, , host, , dbName] = databaseURL.split("/");
1875
+ const [workspace] = host.split(".");
1876
+ const { branch } = await resolveBranch({
1877
+ apiKey,
1878
+ apiUrl: databaseURL,
1879
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1880
+ workspacesApiUrl: `${protocol}//${host}`,
1881
+ pathParams: { dbName, workspace },
1882
+ queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
1883
+ });
1884
+ return branch;
1502
1885
  }
1503
1886
  async function getDatabaseBranch(branch, options) {
1504
1887
  const databaseURL = options?.databaseURL || getDatabaseURL();
@@ -1516,10 +1899,7 @@ async function getDatabaseBranch(branch, options) {
1516
1899
  apiUrl: databaseURL,
1517
1900
  fetchImpl: getFetchImplementation(options?.fetchImpl),
1518
1901
  workspacesApiUrl: `${protocol}//${host}`,
1519
- pathParams: {
1520
- dbBranchName,
1521
- workspace
1522
- }
1902
+ pathParams: { dbBranchName, workspace }
1523
1903
  });
1524
1904
  } catch (err) {
1525
1905
  if (isObject(err) && err.status === 404)
@@ -1572,22 +1952,24 @@ var __privateMethod = (obj, member, method) => {
1572
1952
  const buildClient = (plugins) => {
1573
1953
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1574
1954
  return _a = class {
1575
- constructor(options = {}, links, tables) {
1955
+ constructor(options = {}, tables) {
1576
1956
  __privateAdd(this, _parseOptions);
1577
1957
  __privateAdd(this, _getFetchProps);
1578
1958
  __privateAdd(this, _evaluateBranch);
1579
1959
  __privateAdd(this, _branch, void 0);
1580
1960
  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
- });
1961
+ const pluginOptions = {
1962
+ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1963
+ cache: safeOptions.cache
1964
+ };
1965
+ const db = new SchemaPlugin(tables).build(pluginOptions);
1966
+ const search = new SearchPlugin(db).build(pluginOptions);
1585
1967
  this.db = db;
1586
1968
  this.search = search;
1587
1969
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1588
- if (!namespace)
1970
+ if (namespace === void 0)
1589
1971
  continue;
1590
- const result = namespace.build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1972
+ const result = namespace.build(pluginOptions);
1591
1973
  if (result instanceof Promise) {
1592
1974
  void result.then((namespace2) => {
1593
1975
  this[key] = namespace2;
@@ -1601,11 +1983,12 @@ const buildClient = (plugins) => {
1601
1983
  const fetch = getFetchImplementation(options?.fetch);
1602
1984
  const databaseURL = options?.databaseURL || getDatabaseURL();
1603
1985
  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 });
1986
+ const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1987
+ const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1605
1988
  if (!databaseURL || !apiKey) {
1606
1989
  throw new Error("Options databaseURL and apiKey are required");
1607
1990
  }
1608
- return { fetch, databaseURL, apiKey, branch };
1991
+ return { fetch, databaseURL, apiKey, branch, cache };
1609
1992
  }, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
1610
1993
  fetch,
1611
1994
  apiKey,
@@ -1628,7 +2011,7 @@ const buildClient = (plugins) => {
1628
2011
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
1629
2012
  if (__privateGet(this, _branch))
1630
2013
  return __privateGet(this, _branch);
1631
- if (!param)
2014
+ if (param === void 0)
1632
2015
  return void 0;
1633
2016
  const strategies = Array.isArray(param) ? [...param] : [param];
1634
2017
  const evaluateBranch = async (strategy) => {
@@ -1661,15 +2044,18 @@ exports.PAGINATION_MAX_OFFSET = PAGINATION_MAX_OFFSET;
1661
2044
  exports.PAGINATION_MAX_SIZE = PAGINATION_MAX_SIZE;
1662
2045
  exports.Page = Page;
1663
2046
  exports.Query = Query;
2047
+ exports.RecordArray = RecordArray;
1664
2048
  exports.Repository = Repository;
1665
2049
  exports.RestRepository = RestRepository;
1666
2050
  exports.SchemaPlugin = SchemaPlugin;
1667
2051
  exports.SearchPlugin = SearchPlugin;
2052
+ exports.SimpleCache = SimpleCache;
1668
2053
  exports.XataApiClient = XataApiClient;
1669
2054
  exports.XataApiPlugin = XataApiPlugin;
1670
2055
  exports.XataError = XataError;
1671
2056
  exports.XataPlugin = XataPlugin;
1672
2057
  exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
2058
+ exports.addGitBranchesEntry = addGitBranchesEntry;
1673
2059
  exports.addTableColumn = addTableColumn;
1674
2060
  exports.buildClient = buildClient;
1675
2061
  exports.bulkInsertTableRecords = bulkInsertTableRecords;
@@ -1704,6 +2090,7 @@ exports.getCurrentBranchDetails = getCurrentBranchDetails;
1704
2090
  exports.getCurrentBranchName = getCurrentBranchName;
1705
2091
  exports.getDatabaseList = getDatabaseList;
1706
2092
  exports.getDatabaseURL = getDatabaseURL;
2093
+ exports.getGitBranchesMapping = getGitBranchesMapping;
1707
2094
  exports.getRecord = getRecord;
1708
2095
  exports.getTableColumns = getTableColumns;
1709
2096
  exports.getTableSchema = getTableSchema;
@@ -1722,6 +2109,7 @@ exports.insertRecord = insertRecord;
1722
2109
  exports.insertRecordWithID = insertRecordWithID;
1723
2110
  exports.inviteWorkspaceMember = inviteWorkspaceMember;
1724
2111
  exports.is = is;
2112
+ exports.isCursorPaginationOptions = isCursorPaginationOptions;
1725
2113
  exports.isIdentifiable = isIdentifiable;
1726
2114
  exports.isNot = isNot;
1727
2115
  exports.isXataRecord = isXataRecord;
@@ -1732,9 +2120,12 @@ exports.notExists = notExists;
1732
2120
  exports.operationsByTag = operationsByTag;
1733
2121
  exports.pattern = pattern;
1734
2122
  exports.queryTable = queryTable;
2123
+ exports.removeGitBranchesEntry = removeGitBranchesEntry;
1735
2124
  exports.removeWorkspaceMember = removeWorkspaceMember;
1736
2125
  exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
2126
+ exports.resolveBranch = resolveBranch;
1737
2127
  exports.searchBranch = searchBranch;
2128
+ exports.searchTable = searchTable;
1738
2129
  exports.setTableSchema = setTableSchema;
1739
2130
  exports.startsWith = startsWith;
1740
2131
  exports.updateBranchMetadata = updateBranchMetadata;