@xata.io/client 0.8.3 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -14,17 +14,23 @@ function isObject(value) {
14
14
  function isString(value) {
15
15
  return value !== void 0 && value !== null && typeof value === "string";
16
16
  }
17
+ function toBase64(value) {
18
+ try {
19
+ return btoa(value);
20
+ } catch (err) {
21
+ return Buffer.from(value).toString("base64");
22
+ }
23
+ }
17
24
 
18
25
  function getEnvVariable(name) {
19
- var _a, _b;
20
26
  try {
21
- if (isObject(process) && isString((_a = process == null ? void 0 : process.env) == null ? void 0 : _a[name])) {
27
+ if (isObject(process) && isString(process?.env?.[name])) {
22
28
  return process.env[name];
23
29
  }
24
30
  } catch (err) {
25
31
  }
26
32
  try {
27
- if (isObject(Deno) && isString((_b = Deno == null ? void 0 : Deno.env) == null ? void 0 : _b.get(name))) {
33
+ if (isObject(Deno) && isString(Deno?.env?.get(name))) {
28
34
  return Deno.env.get(name);
29
35
  }
30
36
  } catch (err) {
@@ -32,7 +38,10 @@ function getEnvVariable(name) {
32
38
  }
33
39
  async function getGitBranch() {
34
40
  try {
35
- return require("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
41
+ if (typeof require === "function") {
42
+ const req = require;
43
+ return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
44
+ }
36
45
  } catch (err) {
37
46
  }
38
47
  try {
@@ -48,99 +57,51 @@ async function getGitBranch() {
48
57
  }
49
58
  }
50
59
 
60
+ function getAPIKey() {
61
+ try {
62
+ return getEnvVariable("XATA_API_KEY") ?? XATA_API_KEY;
63
+ } catch (err) {
64
+ return void 0;
65
+ }
66
+ }
67
+
51
68
  function getFetchImplementation(userFetch) {
52
69
  const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
53
- const fetchImpl = userFetch != null ? userFetch : globalFetch;
70
+ const fetchImpl = userFetch ?? globalFetch;
54
71
  if (!fetchImpl) {
55
72
  throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
56
73
  }
57
74
  return fetchImpl;
58
75
  }
59
76
 
60
- const envBranchNames = [
61
- "XATA_BRANCH",
62
- "VERCEL_GIT_COMMIT_REF",
63
- "CF_PAGES_BRANCH",
64
- "BRANCH"
65
- ];
66
- const defaultBranch = "main";
67
- async function getCurrentBranchName(options) {
68
- const env = await getBranchByEnvVariable();
69
- if (env)
70
- return env;
71
- const branch = await getGitBranch();
72
- if (!branch)
73
- return defaultBranch;
74
- const details = await getDatabaseBranch(branch, options);
75
- if (details)
76
- return branch;
77
- return defaultBranch;
78
- }
79
- async function getCurrentBranchDetails(options) {
80
- const env = await getBranchByEnvVariable();
81
- if (env)
82
- return getDatabaseBranch(env, options);
83
- const branch = await getGitBranch();
84
- if (!branch)
85
- return getDatabaseBranch(defaultBranch, options);
86
- const details = await getDatabaseBranch(branch, options);
87
- if (details)
88
- return details;
89
- return getDatabaseBranch(defaultBranch, options);
90
- }
91
- async function getDatabaseBranch(branch, options) {
92
- const databaseURL = (options == null ? void 0 : options.databaseURL) || getDatabaseURL();
93
- const apiKey = (options == null ? void 0 : options.apiKey) || getAPIKey();
94
- if (!databaseURL)
95
- throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
96
- if (!apiKey)
97
- throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
98
- const [protocol, , host, , database] = databaseURL.split("/");
99
- const [workspace] = host.split(".");
100
- const dbBranchName = `${database}:${branch}`;
101
- try {
102
- return await getBranchDetails({
103
- apiKey,
104
- apiUrl: databaseURL,
105
- fetchImpl: getFetchImplementation(options == null ? void 0 : options.fetchImpl),
106
- workspacesApiUrl: `${protocol}//${host}`,
107
- pathParams: {
108
- dbBranchName,
109
- workspace
110
- }
111
- });
112
- } catch (err) {
113
- if (isObject(err) && err.status === 404)
114
- return null;
115
- throw err;
116
- }
117
- }
118
- function getBranchByEnvVariable() {
119
- for (const name of envBranchNames) {
120
- const value = getEnvVariable(name);
121
- if (value) {
122
- return value;
77
+ class FetcherError extends Error {
78
+ constructor(status, data) {
79
+ super(getMessage(data));
80
+ this.status = status;
81
+ this.errors = isBulkError(data) ? data.errors : void 0;
82
+ if (data instanceof Error) {
83
+ this.stack = data.stack;
84
+ this.cause = data.cause;
123
85
  }
124
86
  }
125
- try {
126
- return XATA_BRANCH;
127
- } catch (err) {
128
- }
129
87
  }
130
- function getDatabaseURL() {
131
- var _a;
132
- try {
133
- return (_a = getEnvVariable("XATA_DATABASE_URL")) != null ? _a : XATA_DATABASE_URL;
134
- } catch (err) {
135
- return void 0;
136
- }
88
+ function isBulkError(error) {
89
+ return isObject(error) && Array.isArray(error.errors);
137
90
  }
138
- function getAPIKey() {
139
- var _a;
140
- try {
141
- return (_a = getEnvVariable("XATA_API_KEY")) != null ? _a : XATA_API_KEY;
142
- } catch (err) {
143
- return void 0;
91
+ function isErrorWithMessage(error) {
92
+ return isObject(error) && isString(error.message);
93
+ }
94
+ function getMessage(data) {
95
+ if (data instanceof Error) {
96
+ return data.message;
97
+ } else if (isString(data)) {
98
+ return data;
99
+ } else if (isErrorWithMessage(data)) {
100
+ return data.message;
101
+ } else if (isBulkError(data)) {
102
+ return "Bulk operation failed";
103
+ } else {
104
+ return "Unexpected error";
144
105
  }
145
106
  }
146
107
 
@@ -155,16 +116,15 @@ function buildBaseUrl({
155
116
  apiUrl,
156
117
  pathParams
157
118
  }) {
158
- if (!(pathParams == null ? void 0 : pathParams.workspace))
119
+ if (!pathParams?.workspace)
159
120
  return `${apiUrl}${path}`;
160
121
  const url = typeof workspacesApiUrl === "string" ? `${workspacesApiUrl}${path}` : workspacesApiUrl(path, pathParams);
161
122
  return url.replace("{workspaceId}", pathParams.workspace);
162
123
  }
163
124
  function hostHeader(url) {
164
- var _a;
165
125
  const pattern = /.*:\/\/(?<host>[^/]+).*/;
166
- const { groups } = (_a = pattern.exec(url)) != null ? _a : {};
167
- return (groups == null ? void 0 : groups.host) ? { Host: groups.host } : {};
126
+ const { groups } = pattern.exec(url) ?? {};
127
+ return groups?.host ? { Host: groups.host } : {};
168
128
  }
169
129
  async function fetch$1({
170
130
  url: path,
@@ -199,33 +159,20 @@ async function fetch$1({
199
159
  if (response.ok) {
200
160
  return jsonResponse;
201
161
  }
202
- const { message = "Unknown error", errors } = jsonResponse;
203
- throw new FetcherError({ message, status: response.status, errors });
162
+ throw new FetcherError(response.status, jsonResponse);
204
163
  } catch (error) {
205
- const message = hasMessage(error) ? error.message : "Unknown network error";
206
- const parent = error instanceof Error ? error : void 0;
207
- throw new FetcherError({ message, status: response.status }, parent);
208
- }
209
- }
210
- const hasMessage = (error) => {
211
- return isObject(error) && isString(error.message);
212
- };
213
- class FetcherError extends Error {
214
- constructor(data, parent) {
215
- super(data.message);
216
- this.status = data.status;
217
- this.errors = data.errors;
218
- if (parent) {
219
- this.stack = parent.stack;
220
- this.cause = parent.cause;
221
- }
164
+ throw new FetcherError(response.status, error);
222
165
  }
223
166
  }
224
167
 
225
168
  const getUser = (variables) => fetch$1({ url: "/user", method: "get", ...variables });
226
169
  const updateUser = (variables) => fetch$1({ url: "/user", method: "put", ...variables });
227
170
  const deleteUser = (variables) => fetch$1({ url: "/user", method: "delete", ...variables });
228
- const getUserAPIKeys = (variables) => fetch$1({ url: "/user/keys", method: "get", ...variables });
171
+ const getUserAPIKeys = (variables) => fetch$1({
172
+ url: "/user/keys",
173
+ method: "get",
174
+ ...variables
175
+ });
229
176
  const createUserAPIKey = (variables) => fetch$1({
230
177
  url: "/user/keys/{keyName}",
231
178
  method: "post",
@@ -236,8 +183,16 @@ const deleteUserAPIKey = (variables) => fetch$1({
236
183
  method: "delete",
237
184
  ...variables
238
185
  });
239
- const createWorkspace = (variables) => fetch$1({ url: "/workspaces", method: "post", ...variables });
240
- const getWorkspacesList = (variables) => fetch$1({ url: "/workspaces", method: "get", ...variables });
186
+ const createWorkspace = (variables) => fetch$1({
187
+ url: "/workspaces",
188
+ method: "post",
189
+ ...variables
190
+ });
191
+ const getWorkspacesList = (variables) => fetch$1({
192
+ url: "/workspaces",
193
+ method: "get",
194
+ ...variables
195
+ });
241
196
  const getWorkspace = (variables) => fetch$1({
242
197
  url: "/workspaces/{workspaceId}",
243
198
  method: "get",
@@ -258,21 +213,13 @@ const getWorkspaceMembersList = (variables) => fetch$1({
258
213
  method: "get",
259
214
  ...variables
260
215
  });
261
- const updateWorkspaceMemberRole = (variables) => fetch$1({
262
- url: "/workspaces/{workspaceId}/members/{userId}",
263
- method: "put",
264
- ...variables
265
- });
216
+ const updateWorkspaceMemberRole = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/members/{userId}", method: "put", ...variables });
266
217
  const removeWorkspaceMember = (variables) => fetch$1({
267
218
  url: "/workspaces/{workspaceId}/members/{userId}",
268
219
  method: "delete",
269
220
  ...variables
270
221
  });
271
- const inviteWorkspaceMember = (variables) => fetch$1({
272
- url: "/workspaces/{workspaceId}/invites",
273
- method: "post",
274
- ...variables
275
- });
222
+ const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
276
223
  const cancelWorkspaceMemberInvite = (variables) => fetch$1({
277
224
  url: "/workspaces/{workspaceId}/invites/{inviteId}",
278
225
  method: "delete",
@@ -308,6 +255,14 @@ const deleteDatabase = (variables) => fetch$1({
308
255
  method: "delete",
309
256
  ...variables
310
257
  });
258
+ const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
259
+ const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
260
+ const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
261
+ const resolveBranch = (variables) => fetch$1({
262
+ url: "/dbs/{dbName}/resolveBranch",
263
+ method: "get",
264
+ ...variables
265
+ });
311
266
  const getBranchDetails = (variables) => fetch$1({
312
267
  url: "/db/{dbBranchName}",
313
268
  method: "get",
@@ -334,16 +289,8 @@ const getBranchMetadata = (variables) => fetch$1({
334
289
  ...variables
335
290
  });
336
291
  const getBranchMigrationHistory = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations", method: "get", ...variables });
337
- const executeBranchMigrationPlan = (variables) => fetch$1({
338
- url: "/db/{dbBranchName}/migrations/execute",
339
- method: "post",
340
- ...variables
341
- });
342
- const getBranchMigrationPlan = (variables) => fetch$1({
343
- url: "/db/{dbBranchName}/migrations/plan",
344
- method: "post",
345
- ...variables
346
- });
292
+ const executeBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/execute", method: "post", ...variables });
293
+ const getBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/plan", method: "post", ...variables });
347
294
  const getBranchStats = (variables) => fetch$1({
348
295
  url: "/db/{dbBranchName}/stats",
349
296
  method: "get",
@@ -417,11 +364,7 @@ const getRecord = (variables) => fetch$1({
417
364
  method: "get",
418
365
  ...variables
419
366
  });
420
- const bulkInsertTableRecords = (variables) => fetch$1({
421
- url: "/db/{dbBranchName}/tables/{tableName}/bulk",
422
- method: "post",
423
- ...variables
424
- });
367
+ const bulkInsertTableRecords = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/bulk", method: "post", ...variables });
425
368
  const queryTable = (variables) => fetch$1({
426
369
  url: "/db/{dbBranchName}/tables/{tableName}/query",
427
370
  method: "post",
@@ -448,7 +391,15 @@ const operationsByTag = {
448
391
  resendWorkspaceMemberInvite,
449
392
  acceptWorkspaceMemberInvite
450
393
  },
451
- database: { getDatabaseList, createDatabase, deleteDatabase },
394
+ database: {
395
+ getDatabaseList,
396
+ createDatabase,
397
+ deleteDatabase,
398
+ getGitBranchesMapping,
399
+ addGitBranchesEntry,
400
+ removeGitBranchesEntry,
401
+ resolveBranch
402
+ },
452
403
  branch: {
453
404
  getBranchList,
454
405
  getBranchDetails,
@@ -511,36 +462,35 @@ function isValidBuilder(builder) {
511
462
  return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
512
463
  }
513
464
 
514
- var __accessCheck$6 = (obj, member, msg) => {
465
+ var __accessCheck$7 = (obj, member, msg) => {
515
466
  if (!member.has(obj))
516
467
  throw TypeError("Cannot " + msg);
517
468
  };
518
- var __privateGet$5 = (obj, member, getter) => {
519
- __accessCheck$6(obj, member, "read from private field");
469
+ var __privateGet$6 = (obj, member, getter) => {
470
+ __accessCheck$7(obj, member, "read from private field");
520
471
  return getter ? getter.call(obj) : member.get(obj);
521
472
  };
522
- var __privateAdd$6 = (obj, member, value) => {
473
+ var __privateAdd$7 = (obj, member, value) => {
523
474
  if (member.has(obj))
524
475
  throw TypeError("Cannot add the same private member more than once");
525
476
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
526
477
  };
527
- var __privateSet$4 = (obj, member, value, setter) => {
528
- __accessCheck$6(obj, member, "write to private field");
478
+ var __privateSet$5 = (obj, member, value, setter) => {
479
+ __accessCheck$7(obj, member, "write to private field");
529
480
  setter ? setter.call(obj, value) : member.set(obj, value);
530
481
  return value;
531
482
  };
532
483
  var _extraProps, _namespaces;
533
484
  class XataApiClient {
534
- constructor(options) {
535
- __privateAdd$6(this, _extraProps, void 0);
536
- __privateAdd$6(this, _namespaces, {});
537
- var _a, _b;
538
- const provider = (_a = options.host) != null ? _a : "production";
539
- const apiKey = (_b = options == null ? void 0 : options.apiKey) != null ? _b : getAPIKey();
485
+ constructor(options = {}) {
486
+ __privateAdd$7(this, _extraProps, void 0);
487
+ __privateAdd$7(this, _namespaces, {});
488
+ const provider = options.host ?? "production";
489
+ const apiKey = options?.apiKey ?? getAPIKey();
540
490
  if (!apiKey) {
541
491
  throw new Error("Could not resolve a valid apiKey");
542
492
  }
543
- __privateSet$4(this, _extraProps, {
493
+ __privateSet$5(this, _extraProps, {
544
494
  apiUrl: getHostUrl(provider, "main"),
545
495
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
546
496
  fetchImpl: getFetchImplementation(options.fetch),
@@ -548,34 +498,34 @@ class XataApiClient {
548
498
  });
549
499
  }
550
500
  get user() {
551
- if (!__privateGet$5(this, _namespaces).user)
552
- __privateGet$5(this, _namespaces).user = new UserApi(__privateGet$5(this, _extraProps));
553
- return __privateGet$5(this, _namespaces).user;
501
+ if (!__privateGet$6(this, _namespaces).user)
502
+ __privateGet$6(this, _namespaces).user = new UserApi(__privateGet$6(this, _extraProps));
503
+ return __privateGet$6(this, _namespaces).user;
554
504
  }
555
505
  get workspaces() {
556
- if (!__privateGet$5(this, _namespaces).workspaces)
557
- __privateGet$5(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$5(this, _extraProps));
558
- return __privateGet$5(this, _namespaces).workspaces;
506
+ if (!__privateGet$6(this, _namespaces).workspaces)
507
+ __privateGet$6(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$6(this, _extraProps));
508
+ return __privateGet$6(this, _namespaces).workspaces;
559
509
  }
560
510
  get databases() {
561
- if (!__privateGet$5(this, _namespaces).databases)
562
- __privateGet$5(this, _namespaces).databases = new DatabaseApi(__privateGet$5(this, _extraProps));
563
- return __privateGet$5(this, _namespaces).databases;
511
+ if (!__privateGet$6(this, _namespaces).databases)
512
+ __privateGet$6(this, _namespaces).databases = new DatabaseApi(__privateGet$6(this, _extraProps));
513
+ return __privateGet$6(this, _namespaces).databases;
564
514
  }
565
515
  get branches() {
566
- if (!__privateGet$5(this, _namespaces).branches)
567
- __privateGet$5(this, _namespaces).branches = new BranchApi(__privateGet$5(this, _extraProps));
568
- return __privateGet$5(this, _namespaces).branches;
516
+ if (!__privateGet$6(this, _namespaces).branches)
517
+ __privateGet$6(this, _namespaces).branches = new BranchApi(__privateGet$6(this, _extraProps));
518
+ return __privateGet$6(this, _namespaces).branches;
569
519
  }
570
520
  get tables() {
571
- if (!__privateGet$5(this, _namespaces).tables)
572
- __privateGet$5(this, _namespaces).tables = new TableApi(__privateGet$5(this, _extraProps));
573
- return __privateGet$5(this, _namespaces).tables;
521
+ if (!__privateGet$6(this, _namespaces).tables)
522
+ __privateGet$6(this, _namespaces).tables = new TableApi(__privateGet$6(this, _extraProps));
523
+ return __privateGet$6(this, _namespaces).tables;
574
524
  }
575
525
  get records() {
576
- if (!__privateGet$5(this, _namespaces).records)
577
- __privateGet$5(this, _namespaces).records = new RecordsApi(__privateGet$5(this, _extraProps));
578
- return __privateGet$5(this, _namespaces).records;
526
+ if (!__privateGet$6(this, _namespaces).records)
527
+ __privateGet$6(this, _namespaces).records = new RecordsApi(__privateGet$6(this, _extraProps));
528
+ return __privateGet$6(this, _namespaces).records;
579
529
  }
580
530
  }
581
531
  _extraProps = new WeakMap();
@@ -709,6 +659,33 @@ class DatabaseApi {
709
659
  ...this.extraProps
710
660
  });
711
661
  }
662
+ getGitBranchesMapping(workspace, dbName) {
663
+ return operationsByTag.database.getGitBranchesMapping({
664
+ pathParams: { workspace, dbName },
665
+ ...this.extraProps
666
+ });
667
+ }
668
+ addGitBranchesEntry(workspace, dbName, body) {
669
+ return operationsByTag.database.addGitBranchesEntry({
670
+ pathParams: { workspace, dbName },
671
+ body,
672
+ ...this.extraProps
673
+ });
674
+ }
675
+ removeGitBranchesEntry(workspace, dbName, gitBranch) {
676
+ return operationsByTag.database.removeGitBranchesEntry({
677
+ pathParams: { workspace, dbName },
678
+ queryParams: { gitBranch },
679
+ ...this.extraProps
680
+ });
681
+ }
682
+ resolveBranch(workspace, dbName, gitBranch) {
683
+ return operationsByTag.database.resolveBranch({
684
+ pathParams: { workspace, dbName },
685
+ queryParams: { gitBranch },
686
+ ...this.extraProps
687
+ });
688
+ }
712
689
  }
713
690
  class BranchApi {
714
691
  constructor(extraProps) {
@@ -930,43 +907,43 @@ class XataApiPlugin {
930
907
  class XataPlugin {
931
908
  }
932
909
 
933
- var __accessCheck$5 = (obj, member, msg) => {
910
+ var __accessCheck$6 = (obj, member, msg) => {
934
911
  if (!member.has(obj))
935
912
  throw TypeError("Cannot " + msg);
936
913
  };
937
- var __privateGet$4 = (obj, member, getter) => {
938
- __accessCheck$5(obj, member, "read from private field");
914
+ var __privateGet$5 = (obj, member, getter) => {
915
+ __accessCheck$6(obj, member, "read from private field");
939
916
  return getter ? getter.call(obj) : member.get(obj);
940
917
  };
941
- var __privateAdd$5 = (obj, member, value) => {
918
+ var __privateAdd$6 = (obj, member, value) => {
942
919
  if (member.has(obj))
943
920
  throw TypeError("Cannot add the same private member more than once");
944
921
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
945
922
  };
946
- var __privateSet$3 = (obj, member, value, setter) => {
947
- __accessCheck$5(obj, member, "write to private field");
923
+ var __privateSet$4 = (obj, member, value, setter) => {
924
+ __accessCheck$6(obj, member, "write to private field");
948
925
  setter ? setter.call(obj, value) : member.set(obj, value);
949
926
  return value;
950
927
  };
951
928
  var _query;
952
929
  class Page {
953
930
  constructor(query, meta, records = []) {
954
- __privateAdd$5(this, _query, void 0);
955
- __privateSet$3(this, _query, query);
931
+ __privateAdd$6(this, _query, void 0);
932
+ __privateSet$4(this, _query, query);
956
933
  this.meta = meta;
957
934
  this.records = records;
958
935
  }
959
936
  async nextPage(size, offset) {
960
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
937
+ return __privateGet$5(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
961
938
  }
962
939
  async previousPage(size, offset) {
963
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
940
+ return __privateGet$5(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
964
941
  }
965
942
  async firstPage(size, offset) {
966
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
943
+ return __privateGet$5(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
967
944
  }
968
945
  async lastPage(size, offset) {
969
- return __privateGet$4(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
946
+ return __privateGet$5(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
970
947
  }
971
948
  hasNextPage() {
972
949
  return this.meta.page.more;
@@ -978,47 +955,47 @@ const PAGINATION_DEFAULT_SIZE = 200;
978
955
  const PAGINATION_MAX_OFFSET = 800;
979
956
  const PAGINATION_DEFAULT_OFFSET = 0;
980
957
 
981
- var __accessCheck$4 = (obj, member, msg) => {
958
+ var __accessCheck$5 = (obj, member, msg) => {
982
959
  if (!member.has(obj))
983
960
  throw TypeError("Cannot " + msg);
984
961
  };
985
- var __privateGet$3 = (obj, member, getter) => {
986
- __accessCheck$4(obj, member, "read from private field");
962
+ var __privateGet$4 = (obj, member, getter) => {
963
+ __accessCheck$5(obj, member, "read from private field");
987
964
  return getter ? getter.call(obj) : member.get(obj);
988
965
  };
989
- var __privateAdd$4 = (obj, member, value) => {
966
+ var __privateAdd$5 = (obj, member, value) => {
990
967
  if (member.has(obj))
991
968
  throw TypeError("Cannot add the same private member more than once");
992
969
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
993
970
  };
994
- var __privateSet$2 = (obj, member, value, setter) => {
995
- __accessCheck$4(obj, member, "write to private field");
971
+ var __privateSet$3 = (obj, member, value, setter) => {
972
+ __accessCheck$5(obj, member, "write to private field");
996
973
  setter ? setter.call(obj, value) : member.set(obj, value);
997
974
  return value;
998
975
  };
999
976
  var _table$1, _repository, _data;
1000
977
  const _Query = class {
1001
978
  constructor(repository, table, data, parent) {
1002
- __privateAdd$4(this, _table$1, void 0);
1003
- __privateAdd$4(this, _repository, void 0);
1004
- __privateAdd$4(this, _data, { filter: {} });
979
+ __privateAdd$5(this, _table$1, void 0);
980
+ __privateAdd$5(this, _repository, void 0);
981
+ __privateAdd$5(this, _data, { filter: {} });
1005
982
  this.meta = { page: { cursor: "start", more: true } };
1006
983
  this.records = [];
1007
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
1008
- __privateSet$2(this, _table$1, table);
984
+ __privateSet$3(this, _table$1, table);
1009
985
  if (repository) {
1010
- __privateSet$2(this, _repository, repository);
986
+ __privateSet$3(this, _repository, repository);
1011
987
  } else {
1012
- __privateSet$2(this, _repository, this);
988
+ __privateSet$3(this, _repository, this);
1013
989
  }
1014
- __privateGet$3(this, _data).filter = (_b = (_a = data.filter) != null ? _a : parent == null ? void 0 : parent.filter) != null ? _b : {};
1015
- __privateGet$3(this, _data).filter.$any = (_e = (_c = data.filter) == null ? void 0 : _c.$any) != null ? _e : (_d = parent == null ? void 0 : parent.filter) == null ? void 0 : _d.$any;
1016
- __privateGet$3(this, _data).filter.$all = (_h = (_f = data.filter) == null ? void 0 : _f.$all) != null ? _h : (_g = parent == null ? void 0 : parent.filter) == null ? void 0 : _g.$all;
1017
- __privateGet$3(this, _data).filter.$not = (_k = (_i = data.filter) == null ? void 0 : _i.$not) != null ? _k : (_j = parent == null ? void 0 : parent.filter) == null ? void 0 : _j.$not;
1018
- __privateGet$3(this, _data).filter.$none = (_n = (_l = data.filter) == null ? void 0 : _l.$none) != null ? _n : (_m = parent == null ? void 0 : parent.filter) == null ? void 0 : _m.$none;
1019
- __privateGet$3(this, _data).sort = (_o = data.sort) != null ? _o : parent == null ? void 0 : parent.sort;
1020
- __privateGet$3(this, _data).columns = (_q = (_p = data.columns) != null ? _p : parent == null ? void 0 : parent.columns) != null ? _q : ["*"];
1021
- __privateGet$3(this, _data).page = (_r = data.page) != null ? _r : parent == null ? void 0 : parent.page;
990
+ __privateGet$4(this, _data).filter = data.filter ?? parent?.filter ?? {};
991
+ __privateGet$4(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
992
+ __privateGet$4(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
993
+ __privateGet$4(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
994
+ __privateGet$4(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
995
+ __privateGet$4(this, _data).sort = data.sort ?? parent?.sort;
996
+ __privateGet$4(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
997
+ __privateGet$4(this, _data).page = data.page ?? parent?.page;
998
+ __privateGet$4(this, _data).cache = data.cache ?? parent?.cache;
1022
999
  this.any = this.any.bind(this);
1023
1000
  this.all = this.all.bind(this);
1024
1001
  this.not = this.not.bind(this);
@@ -1029,59 +1006,50 @@ const _Query = class {
1029
1006
  Object.defineProperty(this, "repository", { enumerable: false });
1030
1007
  }
1031
1008
  getQueryOptions() {
1032
- return __privateGet$3(this, _data);
1009
+ return __privateGet$4(this, _data);
1010
+ }
1011
+ key() {
1012
+ const { columns = [], filter = {}, sort = [], page = {} } = __privateGet$4(this, _data);
1013
+ const key = JSON.stringify({ columns, filter, sort, page });
1014
+ return toBase64(key);
1033
1015
  }
1034
1016
  any(...queries) {
1035
- const $any = queries.map((query) => {
1036
- var _a;
1037
- return (_a = query.getQueryOptions().filter) != null ? _a : {};
1038
- });
1039
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $any } }, __privateGet$3(this, _data));
1017
+ const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
1018
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $any } }, __privateGet$4(this, _data));
1040
1019
  }
1041
1020
  all(...queries) {
1042
- const $all = queries.map((query) => {
1043
- var _a;
1044
- return (_a = query.getQueryOptions().filter) != null ? _a : {};
1045
- });
1046
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
1021
+ const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
1022
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1047
1023
  }
1048
1024
  not(...queries) {
1049
- const $not = queries.map((query) => {
1050
- var _a;
1051
- return (_a = query.getQueryOptions().filter) != null ? _a : {};
1052
- });
1053
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $not } }, __privateGet$3(this, _data));
1025
+ const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
1026
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $not } }, __privateGet$4(this, _data));
1054
1027
  }
1055
1028
  none(...queries) {
1056
- const $none = queries.map((query) => {
1057
- var _a;
1058
- return (_a = query.getQueryOptions().filter) != null ? _a : {};
1059
- });
1060
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $none } }, __privateGet$3(this, _data));
1029
+ const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
1030
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $none } }, __privateGet$4(this, _data));
1061
1031
  }
1062
1032
  filter(a, b) {
1063
- var _a, _b;
1064
1033
  if (arguments.length === 1) {
1065
1034
  const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
1066
- const $all = compact([(_a = __privateGet$3(this, _data).filter) == null ? void 0 : _a.$all].flat().concat(constraints));
1067
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
1035
+ const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat(constraints));
1036
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1068
1037
  } else {
1069
- const $all = compact([(_b = __privateGet$3(this, _data).filter) == null ? void 0 : _b.$all].flat().concat([{ [a]: b }]));
1070
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { filter: { $all } }, __privateGet$3(this, _data));
1038
+ const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1039
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1071
1040
  }
1072
1041
  }
1073
1042
  sort(column, direction) {
1074
- var _a;
1075
- const originalSort = [(_a = __privateGet$3(this, _data).sort) != null ? _a : []].flat();
1043
+ const originalSort = [__privateGet$4(this, _data).sort ?? []].flat();
1076
1044
  const sort = [...originalSort, { column, direction }];
1077
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { sort }, __privateGet$3(this, _data));
1045
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { sort }, __privateGet$4(this, _data));
1078
1046
  }
1079
1047
  select(columns) {
1080
- return new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), { columns }, __privateGet$3(this, _data));
1048
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { columns }, __privateGet$4(this, _data));
1081
1049
  }
1082
1050
  getPaginated(options = {}) {
1083
- const query = new _Query(__privateGet$3(this, _repository), __privateGet$3(this, _table$1), options, __privateGet$3(this, _data));
1084
- return __privateGet$3(this, _repository).query(query);
1051
+ const query = new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), options, __privateGet$4(this, _data));
1052
+ return __privateGet$4(this, _repository).query(query);
1085
1053
  }
1086
1054
  async *[Symbol.asyncIterator]() {
1087
1055
  for await (const [record] of this.getIterator(1)) {
@@ -1109,10 +1077,13 @@ const _Query = class {
1109
1077
  }
1110
1078
  return results;
1111
1079
  }
1112
- async getOne(options = {}) {
1080
+ async getFirst(options = {}) {
1113
1081
  const records = await this.getMany({ ...options, page: { size: 1 } });
1114
1082
  return records[0] || null;
1115
1083
  }
1084
+ cache(ttl) {
1085
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { cache: ttl }, __privateGet$4(this, _data));
1086
+ }
1116
1087
  nextPage(size, offset) {
1117
1088
  return this.firstPage(size, offset);
1118
1089
  }
@@ -1135,11 +1106,10 @@ _repository = new WeakMap();
1135
1106
  _data = new WeakMap();
1136
1107
 
1137
1108
  function isIdentifiable(x) {
1138
- return isObject(x) && isString(x == null ? void 0 : x.id);
1109
+ return isObject(x) && isString(x?.id);
1139
1110
  }
1140
1111
  function isXataRecord(x) {
1141
- var _a;
1142
- return isIdentifiable(x) && typeof (x == null ? void 0 : x.xata) === "object" && typeof ((_a = x == null ? void 0 : x.xata) == null ? void 0 : _a.version) === "number";
1112
+ return isIdentifiable(x) && typeof x?.xata === "object" && typeof x?.xata?.version === "number";
1143
1113
  }
1144
1114
 
1145
1115
  function isSortFilterString(value) {
@@ -1152,7 +1122,6 @@ function isSortFilterObject(filter) {
1152
1122
  return isObject(filter) && !isSortFilterBase(filter) && filter.column !== void 0;
1153
1123
  }
1154
1124
  function buildSortFilter(filter) {
1155
- var _a;
1156
1125
  if (isSortFilterString(filter)) {
1157
1126
  return { [filter]: "asc" };
1158
1127
  } else if (Array.isArray(filter)) {
@@ -1160,83 +1129,99 @@ function buildSortFilter(filter) {
1160
1129
  } else if (isSortFilterBase(filter)) {
1161
1130
  return filter;
1162
1131
  } else if (isSortFilterObject(filter)) {
1163
- return { [filter.column]: (_a = filter.direction) != null ? _a : "asc" };
1132
+ return { [filter.column]: filter.direction ?? "asc" };
1164
1133
  } else {
1165
1134
  throw new Error(`Invalid sort filter: ${filter}`);
1166
1135
  }
1167
1136
  }
1168
1137
 
1169
- var __accessCheck$3 = (obj, member, msg) => {
1138
+ var __accessCheck$4 = (obj, member, msg) => {
1170
1139
  if (!member.has(obj))
1171
1140
  throw TypeError("Cannot " + msg);
1172
1141
  };
1173
- var __privateGet$2 = (obj, member, getter) => {
1174
- __accessCheck$3(obj, member, "read from private field");
1142
+ var __privateGet$3 = (obj, member, getter) => {
1143
+ __accessCheck$4(obj, member, "read from private field");
1175
1144
  return getter ? getter.call(obj) : member.get(obj);
1176
1145
  };
1177
- var __privateAdd$3 = (obj, member, value) => {
1146
+ var __privateAdd$4 = (obj, member, value) => {
1178
1147
  if (member.has(obj))
1179
1148
  throw TypeError("Cannot add the same private member more than once");
1180
1149
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1181
1150
  };
1182
- var __privateSet$1 = (obj, member, value, setter) => {
1183
- __accessCheck$3(obj, member, "write to private field");
1151
+ var __privateSet$2 = (obj, member, value, setter) => {
1152
+ __accessCheck$4(obj, member, "write to private field");
1184
1153
  setter ? setter.call(obj, value) : member.set(obj, value);
1185
1154
  return value;
1186
1155
  };
1187
1156
  var __privateMethod$2 = (obj, member, method) => {
1188
- __accessCheck$3(obj, member, "access private method");
1157
+ __accessCheck$4(obj, member, "access private method");
1189
1158
  return method;
1190
1159
  };
1191
- var _table, _links, _getFetchProps, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _initObject, initObject_fn;
1160
+ var _table, _links, _getFetchProps, _cache, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _invalidateCache, invalidateCache_fn, _setCacheRecord, setCacheRecord_fn, _getCacheRecord, getCacheRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn;
1192
1161
  class Repository extends Query {
1193
1162
  }
1194
1163
  class RestRepository extends Query {
1195
1164
  constructor(options) {
1196
1165
  super(null, options.table, {});
1197
- __privateAdd$3(this, _insertRecordWithoutId);
1198
- __privateAdd$3(this, _insertRecordWithId);
1199
- __privateAdd$3(this, _bulkInsertTableRecords);
1200
- __privateAdd$3(this, _updateRecordWithID);
1201
- __privateAdd$3(this, _upsertRecordWithID);
1202
- __privateAdd$3(this, _deleteRecord);
1203
- __privateAdd$3(this, _initObject);
1204
- __privateAdd$3(this, _table, void 0);
1205
- __privateAdd$3(this, _links, void 0);
1206
- __privateAdd$3(this, _getFetchProps, void 0);
1207
- var _a;
1208
- __privateSet$1(this, _table, options.table);
1209
- __privateSet$1(this, _links, (_a = options.links) != null ? _a : {});
1210
- __privateSet$1(this, _getFetchProps, options.getFetchProps);
1166
+ __privateAdd$4(this, _insertRecordWithoutId);
1167
+ __privateAdd$4(this, _insertRecordWithId);
1168
+ __privateAdd$4(this, _bulkInsertTableRecords);
1169
+ __privateAdd$4(this, _updateRecordWithID);
1170
+ __privateAdd$4(this, _upsertRecordWithID);
1171
+ __privateAdd$4(this, _deleteRecord);
1172
+ __privateAdd$4(this, _invalidateCache);
1173
+ __privateAdd$4(this, _setCacheRecord);
1174
+ __privateAdd$4(this, _getCacheRecord);
1175
+ __privateAdd$4(this, _setCacheQuery);
1176
+ __privateAdd$4(this, _getCacheQuery);
1177
+ __privateAdd$4(this, _table, void 0);
1178
+ __privateAdd$4(this, _links, void 0);
1179
+ __privateAdd$4(this, _getFetchProps, void 0);
1180
+ __privateAdd$4(this, _cache, void 0);
1181
+ __privateSet$2(this, _table, options.table);
1182
+ __privateSet$2(this, _links, options.links ?? {});
1183
+ __privateSet$2(this, _getFetchProps, options.pluginOptions.getFetchProps);
1211
1184
  this.db = options.db;
1185
+ __privateSet$2(this, _cache, options.pluginOptions.cache);
1212
1186
  }
1213
1187
  async create(a, b) {
1214
1188
  if (Array.isArray(a)) {
1215
- return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1189
+ const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1190
+ await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1191
+ return records;
1216
1192
  }
1217
1193
  if (isString(a) && isObject(b)) {
1218
1194
  if (a === "")
1219
1195
  throw new Error("The id can't be empty");
1220
- return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1196
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
1197
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1198
+ return record;
1221
1199
  }
1222
1200
  if (isObject(a) && isString(a.id)) {
1223
1201
  if (a.id === "")
1224
1202
  throw new Error("The id can't be empty");
1225
- return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
1203
+ const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
1204
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1205
+ return record;
1226
1206
  }
1227
1207
  if (isObject(a)) {
1228
- return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1208
+ const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
1209
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1210
+ return record;
1229
1211
  }
1230
1212
  throw new Error("Invalid arguments for create method");
1231
1213
  }
1232
1214
  async read(recordId) {
1233
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1215
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1216
+ if (cacheRecord)
1217
+ return cacheRecord;
1218
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1234
1219
  try {
1235
1220
  const response = await getRecord({
1236
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1221
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1237
1222
  ...fetchProps
1238
1223
  });
1239
- return __privateMethod$2(this, _initObject, initObject_fn).call(this, __privateGet$2(this, _table), response);
1224
+ return initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), response);
1240
1225
  } catch (e) {
1241
1226
  if (isObject(e) && e.status === 404) {
1242
1227
  return null;
@@ -1252,10 +1237,16 @@ class RestRepository extends Query {
1252
1237
  return Promise.all(a.map((object) => this.update(object)));
1253
1238
  }
1254
1239
  if (isString(a) && isObject(b)) {
1255
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1240
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1241
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
1242
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1243
+ return record;
1256
1244
  }
1257
1245
  if (isObject(a) && isString(a.id)) {
1258
- return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1246
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1247
+ const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1248
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1249
+ return record;
1259
1250
  }
1260
1251
  throw new Error("Invalid arguments for update method");
1261
1252
  }
@@ -1267,71 +1258,83 @@ class RestRepository extends Query {
1267
1258
  return Promise.all(a.map((object) => this.createOrUpdate(object)));
1268
1259
  }
1269
1260
  if (isString(a) && isObject(b)) {
1270
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1261
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1262
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
1263
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1264
+ return record;
1271
1265
  }
1272
1266
  if (isObject(a) && isString(a.id)) {
1273
- return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1267
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1268
+ const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
1269
+ await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
1270
+ return record;
1274
1271
  }
1275
1272
  throw new Error("Invalid arguments for createOrUpdate method");
1276
1273
  }
1277
- async delete(recordId) {
1278
- if (Array.isArray(recordId)) {
1279
- if (recordId.length > 100) {
1274
+ async delete(a) {
1275
+ if (Array.isArray(a)) {
1276
+ if (a.length > 100) {
1280
1277
  console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1281
1278
  }
1282
- await Promise.all(recordId.map((id) => this.delete(id)));
1279
+ await Promise.all(a.map((id) => this.delete(id)));
1283
1280
  return;
1284
1281
  }
1285
- if (isString(recordId)) {
1286
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId);
1282
+ if (isString(a)) {
1283
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
1284
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
1287
1285
  return;
1288
1286
  }
1289
- if (isObject(recordId) && isString(recordId.id)) {
1290
- await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, recordId.id);
1287
+ if (isObject(a) && isString(a.id)) {
1288
+ await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
1289
+ await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
1291
1290
  return;
1292
1291
  }
1293
1292
  throw new Error("Invalid arguments for delete method");
1294
1293
  }
1295
1294
  async search(query, options = {}) {
1296
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1295
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1297
1296
  const { records } = await searchBranch({
1298
1297
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1299
- body: { tables: [__privateGet$2(this, _table)], query, fuzziness: options.fuzziness },
1298
+ body: { tables: [__privateGet$3(this, _table)], query, fuzziness: options.fuzziness },
1300
1299
  ...fetchProps
1301
1300
  });
1302
- return records.map((item) => __privateMethod$2(this, _initObject, initObject_fn).call(this, __privateGet$2(this, _table), item));
1301
+ return records.map((item) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), item));
1303
1302
  }
1304
1303
  async query(query) {
1305
- var _a;
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);
1306
1307
  const data = query.getQueryOptions();
1307
1308
  const body = {
1308
- filter: Object.values((_a = data.filter) != null ? _a : {}).some(Boolean) ? data.filter : void 0,
1309
+ filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1309
1310
  sort: data.sort ? buildSortFilter(data.sort) : void 0,
1310
1311
  page: data.page,
1311
1312
  columns: data.columns
1312
1313
  };
1313
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1314
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1314
1315
  const { meta, records: objects } = await queryTable({
1315
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1316
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1316
1317
  body,
1317
1318
  ...fetchProps
1318
1319
  });
1319
- const records = objects.map((record) => __privateMethod$2(this, _initObject, initObject_fn).call(this, __privateGet$2(this, _table), record));
1320
+ const records = objects.map((record) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), record));
1321
+ await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1320
1322
  return new Page(query, meta, records);
1321
1323
  }
1322
1324
  }
1323
1325
  _table = new WeakMap();
1324
1326
  _links = new WeakMap();
1325
1327
  _getFetchProps = new WeakMap();
1328
+ _cache = new WeakMap();
1326
1329
  _insertRecordWithoutId = new WeakSet();
1327
1330
  insertRecordWithoutId_fn = async function(object) {
1328
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1331
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1329
1332
  const record = transformObjectLinks(object);
1330
1333
  const response = await insertRecord({
1331
1334
  pathParams: {
1332
1335
  workspace: "{workspaceId}",
1333
1336
  dbBranchName: "{dbBranch}",
1334
- tableName: __privateGet$2(this, _table)
1337
+ tableName: __privateGet$3(this, _table)
1335
1338
  },
1336
1339
  body: record,
1337
1340
  ...fetchProps
@@ -1344,13 +1347,13 @@ insertRecordWithoutId_fn = async function(object) {
1344
1347
  };
1345
1348
  _insertRecordWithId = new WeakSet();
1346
1349
  insertRecordWithId_fn = async function(recordId, object) {
1347
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1350
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1348
1351
  const record = transformObjectLinks(object);
1349
1352
  const response = await insertRecordWithID({
1350
1353
  pathParams: {
1351
1354
  workspace: "{workspaceId}",
1352
1355
  dbBranchName: "{dbBranch}",
1353
- tableName: __privateGet$2(this, _table),
1356
+ tableName: __privateGet$3(this, _table),
1354
1357
  recordId
1355
1358
  },
1356
1359
  body: record,
@@ -1365,10 +1368,10 @@ insertRecordWithId_fn = async function(recordId, object) {
1365
1368
  };
1366
1369
  _bulkInsertTableRecords = new WeakSet();
1367
1370
  bulkInsertTableRecords_fn = async function(objects) {
1368
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1371
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1369
1372
  const records = objects.map((object) => transformObjectLinks(object));
1370
1373
  const response = await bulkInsertTableRecords({
1371
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table) },
1374
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1372
1375
  body: { records },
1373
1376
  ...fetchProps
1374
1377
  });
@@ -1380,10 +1383,10 @@ bulkInsertTableRecords_fn = async function(objects) {
1380
1383
  };
1381
1384
  _updateRecordWithID = new WeakSet();
1382
1385
  updateRecordWithID_fn = async function(recordId, object) {
1383
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1386
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1384
1387
  const record = transformObjectLinks(object);
1385
1388
  const response = await updateRecordWithID({
1386
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1389
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1387
1390
  body: record,
1388
1391
  ...fetchProps
1389
1392
  });
@@ -1394,9 +1397,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1394
1397
  };
1395
1398
  _upsertRecordWithID = new WeakSet();
1396
1399
  upsertRecordWithID_fn = async function(recordId, object) {
1397
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1400
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1398
1401
  const response = await upsertRecordWithID({
1399
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1402
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1400
1403
  body: object,
1401
1404
  ...fetchProps
1402
1405
  });
@@ -1407,25 +1410,69 @@ upsertRecordWithID_fn = async function(recordId, object) {
1407
1410
  };
1408
1411
  _deleteRecord = new WeakSet();
1409
1412
  deleteRecord_fn = async function(recordId) {
1410
- const fetchProps = await __privateGet$2(this, _getFetchProps).call(this);
1413
+ const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1411
1414
  await deleteRecord({
1412
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$2(this, _table), recordId },
1415
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1413
1416
  ...fetchProps
1414
1417
  });
1415
1418
  };
1416
- _initObject = new WeakSet();
1417
- initObject_fn = function(table, object) {
1419
+ _invalidateCache = new WeakSet();
1420
+ invalidateCache_fn = async function(recordId) {
1421
+ await __privateGet$3(this, _cache).delete(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1422
+ const cacheItems = await __privateGet$3(this, _cache).getAll();
1423
+ const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1424
+ for (const [key, value] of queries) {
1425
+ const ids = getIds(value);
1426
+ if (ids.includes(recordId))
1427
+ await __privateGet$3(this, _cache).delete(key);
1428
+ }
1429
+ };
1430
+ _setCacheRecord = new WeakSet();
1431
+ setCacheRecord_fn = async function(record) {
1432
+ if (!__privateGet$3(this, _cache).cacheRecords)
1433
+ return;
1434
+ await __privateGet$3(this, _cache).set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
1435
+ };
1436
+ _getCacheRecord = new WeakSet();
1437
+ getCacheRecord_fn = async function(recordId) {
1438
+ if (!__privateGet$3(this, _cache).cacheRecords)
1439
+ return null;
1440
+ return __privateGet$3(this, _cache).get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1441
+ };
1442
+ _setCacheQuery = new WeakSet();
1443
+ setCacheQuery_fn = async function(query, meta, records) {
1444
+ await __privateGet$3(this, _cache).set(`query_${__privateGet$3(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1445
+ };
1446
+ _getCacheQuery = new WeakSet();
1447
+ getCacheQuery_fn = async function(query) {
1448
+ const key = `query_${__privateGet$3(this, _table)}:${query.key()}`;
1449
+ const result = await __privateGet$3(this, _cache).get(key);
1450
+ if (!result)
1451
+ return null;
1452
+ const { cache: ttl = __privateGet$3(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1453
+ if (!ttl || ttl < 0)
1454
+ return result;
1455
+ const hasExpired = result.date.getTime() + ttl < Date.now();
1456
+ return hasExpired ? null : result;
1457
+ };
1458
+ const transformObjectLinks = (object) => {
1459
+ return Object.entries(object).reduce((acc, [key, value]) => {
1460
+ if (key === "xata")
1461
+ return acc;
1462
+ return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1463
+ }, {});
1464
+ };
1465
+ const initObject = (db, links, table, object) => {
1418
1466
  const result = {};
1419
1467
  Object.assign(result, object);
1420
- const tableLinks = __privateGet$2(this, _links)[table] || [];
1468
+ const tableLinks = links[table] || [];
1421
1469
  for (const link of tableLinks) {
1422
1470
  const [field, linkTable] = link;
1423
1471
  const value = result[field];
1424
1472
  if (value && isObject(value)) {
1425
- result[field] = __privateMethod$2(this, _initObject, initObject_fn).call(this, linkTable, value);
1473
+ result[field] = initObject(db, links, linkTable, value);
1426
1474
  }
1427
1475
  }
1428
- const db = this.db;
1429
1476
  result.read = function() {
1430
1477
  return db[table].read(result["id"]);
1431
1478
  };
@@ -1441,13 +1488,65 @@ initObject_fn = function(table, object) {
1441
1488
  Object.freeze(result);
1442
1489
  return result;
1443
1490
  };
1444
- const transformObjectLinks = (object) => {
1445
- return Object.entries(object).reduce((acc, [key, value]) => {
1446
- if (key === "xata")
1447
- return acc;
1448
- return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1449
- }, {});
1491
+ function getIds(value) {
1492
+ if (Array.isArray(value)) {
1493
+ return value.map((item) => getIds(item)).flat();
1494
+ }
1495
+ if (!isObject(value))
1496
+ return [];
1497
+ const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
1498
+ return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
1499
+ }
1500
+
1501
+ var __accessCheck$3 = (obj, member, msg) => {
1502
+ if (!member.has(obj))
1503
+ throw TypeError("Cannot " + msg);
1450
1504
  };
1505
+ var __privateGet$2 = (obj, member, getter) => {
1506
+ __accessCheck$3(obj, member, "read from private field");
1507
+ return getter ? getter.call(obj) : member.get(obj);
1508
+ };
1509
+ var __privateAdd$3 = (obj, member, value) => {
1510
+ if (member.has(obj))
1511
+ throw TypeError("Cannot add the same private member more than once");
1512
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1513
+ };
1514
+ var __privateSet$1 = (obj, member, value, setter) => {
1515
+ __accessCheck$3(obj, member, "write to private field");
1516
+ setter ? setter.call(obj, value) : member.set(obj, value);
1517
+ return value;
1518
+ };
1519
+ var _map;
1520
+ class SimpleCache {
1521
+ constructor(options = {}) {
1522
+ __privateAdd$3(this, _map, void 0);
1523
+ __privateSet$1(this, _map, /* @__PURE__ */ new Map());
1524
+ this.capacity = options.max ?? 500;
1525
+ this.cacheRecords = options.cacheRecords ?? true;
1526
+ this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1527
+ }
1528
+ async getAll() {
1529
+ return Object.fromEntries(__privateGet$2(this, _map));
1530
+ }
1531
+ async get(key) {
1532
+ return __privateGet$2(this, _map).get(key) ?? null;
1533
+ }
1534
+ async set(key, value) {
1535
+ await this.delete(key);
1536
+ __privateGet$2(this, _map).set(key, value);
1537
+ if (__privateGet$2(this, _map).size > this.capacity) {
1538
+ const leastRecentlyUsed = __privateGet$2(this, _map).keys().next().value;
1539
+ await this.delete(leastRecentlyUsed);
1540
+ }
1541
+ }
1542
+ async delete(key) {
1543
+ __privateGet$2(this, _map).delete(key);
1544
+ }
1545
+ async clear() {
1546
+ return __privateGet$2(this, _map).clear();
1547
+ }
1548
+ }
1549
+ _map = new WeakMap();
1451
1550
 
1452
1551
  const gt = (value) => ({ $gt: value });
1453
1552
  const ge = (value) => ({ $ge: value });
@@ -1483,23 +1582,27 @@ var __privateAdd$2 = (obj, member, value) => {
1483
1582
  };
1484
1583
  var _tables;
1485
1584
  class SchemaPlugin extends XataPlugin {
1486
- constructor(links) {
1585
+ constructor(links, tableNames) {
1487
1586
  super();
1488
1587
  this.links = links;
1588
+ this.tableNames = tableNames;
1489
1589
  __privateAdd$2(this, _tables, {});
1490
1590
  }
1491
- build(options) {
1492
- const { getFetchProps } = options;
1591
+ build(pluginOptions) {
1493
1592
  const links = this.links;
1494
1593
  const db = new Proxy({}, {
1495
1594
  get: (_target, table) => {
1496
1595
  if (!isString(table))
1497
1596
  throw new Error("Invalid table name");
1498
- if (!__privateGet$1(this, _tables)[table])
1499
- __privateGet$1(this, _tables)[table] = new RestRepository({ db, getFetchProps, table, links });
1597
+ if (!__privateGet$1(this, _tables)[table]) {
1598
+ __privateGet$1(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, links });
1599
+ }
1500
1600
  return __privateGet$1(this, _tables)[table];
1501
1601
  }
1502
1602
  });
1603
+ for (const table of this.tableNames ?? []) {
1604
+ db[table] = new RestRepository({ db, pluginOptions, table, links });
1605
+ }
1503
1606
  return db;
1504
1607
  }
1505
1608
  }
@@ -1520,8 +1623,10 @@ var __privateMethod$1 = (obj, member, method) => {
1520
1623
  };
1521
1624
  var _search, search_fn;
1522
1625
  class SearchPlugin extends XataPlugin {
1523
- constructor() {
1524
- super(...arguments);
1626
+ constructor(db, links) {
1627
+ super();
1628
+ this.db = db;
1629
+ this.links = links;
1525
1630
  __privateAdd$1(this, _search);
1526
1631
  }
1527
1632
  build({ getFetchProps }) {
@@ -1530,16 +1635,16 @@ class SearchPlugin extends XataPlugin {
1530
1635
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1531
1636
  return records.map((record) => {
1532
1637
  const { table = "orphan" } = record.xata;
1533
- return { table, record };
1638
+ return { table, record: initObject(this.db, this.links, table, record) };
1534
1639
  });
1535
1640
  },
1536
1641
  byTable: async (query, options = {}) => {
1537
1642
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1538
1643
  return records.reduce((acc, record) => {
1539
- var _a;
1540
1644
  const { table = "orphan" } = record.xata;
1541
- const items = (_a = acc[table]) != null ? _a : [];
1542
- return { ...acc, [table]: [...items, record] };
1645
+ const items = acc[table] ?? [];
1646
+ const item = initObject(this.db, this.links, table, record);
1647
+ return { ...acc, [table]: [...items, item] };
1543
1648
  }, {});
1544
1649
  }
1545
1650
  };
@@ -1548,7 +1653,7 @@ class SearchPlugin extends XataPlugin {
1548
1653
  _search = new WeakSet();
1549
1654
  search_fn = async function(query, options, getFetchProps) {
1550
1655
  const fetchProps = await getFetchProps();
1551
- const { tables, fuzziness } = options != null ? options : {};
1656
+ const { tables, fuzziness } = options ?? {};
1552
1657
  const { records } = await searchBranch({
1553
1658
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1554
1659
  body: { tables, query, fuzziness },
@@ -1561,6 +1666,84 @@ const isBranchStrategyBuilder = (strategy) => {
1561
1666
  return typeof strategy === "function";
1562
1667
  };
1563
1668
 
1669
+ const envBranchNames = [
1670
+ "XATA_BRANCH",
1671
+ "VERCEL_GIT_COMMIT_REF",
1672
+ "CF_PAGES_BRANCH",
1673
+ "BRANCH"
1674
+ ];
1675
+ const defaultBranch = "main";
1676
+ async function getCurrentBranchName(options) {
1677
+ const env = await getBranchByEnvVariable();
1678
+ if (env)
1679
+ return env;
1680
+ const branch = await getGitBranch();
1681
+ if (!branch)
1682
+ return defaultBranch;
1683
+ const details = await getDatabaseBranch(branch, options);
1684
+ if (details)
1685
+ return branch;
1686
+ return defaultBranch;
1687
+ }
1688
+ async function getCurrentBranchDetails(options) {
1689
+ const env = await getBranchByEnvVariable();
1690
+ if (env)
1691
+ return getDatabaseBranch(env, options);
1692
+ const branch = await getGitBranch();
1693
+ if (!branch)
1694
+ return getDatabaseBranch(defaultBranch, options);
1695
+ const details = await getDatabaseBranch(branch, options);
1696
+ if (details)
1697
+ return details;
1698
+ return getDatabaseBranch(defaultBranch, options);
1699
+ }
1700
+ async function getDatabaseBranch(branch, options) {
1701
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1702
+ const apiKey = options?.apiKey || getAPIKey();
1703
+ if (!databaseURL)
1704
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1705
+ if (!apiKey)
1706
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1707
+ const [protocol, , host, , database] = databaseURL.split("/");
1708
+ const [workspace] = host.split(".");
1709
+ const dbBranchName = `${database}:${branch}`;
1710
+ try {
1711
+ return await getBranchDetails({
1712
+ apiKey,
1713
+ apiUrl: databaseURL,
1714
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1715
+ workspacesApiUrl: `${protocol}//${host}`,
1716
+ pathParams: {
1717
+ dbBranchName,
1718
+ workspace
1719
+ }
1720
+ });
1721
+ } catch (err) {
1722
+ if (isObject(err) && err.status === 404)
1723
+ return null;
1724
+ throw err;
1725
+ }
1726
+ }
1727
+ function getBranchByEnvVariable() {
1728
+ for (const name of envBranchNames) {
1729
+ const value = getEnvVariable(name);
1730
+ if (value) {
1731
+ return value;
1732
+ }
1733
+ }
1734
+ try {
1735
+ return XATA_BRANCH;
1736
+ } catch (err) {
1737
+ }
1738
+ }
1739
+ function getDatabaseURL() {
1740
+ try {
1741
+ return getEnvVariable("XATA_DATABASE_URL") ?? XATA_DATABASE_URL;
1742
+ } catch (err) {
1743
+ return void 0;
1744
+ }
1745
+ }
1746
+
1564
1747
  var __accessCheck = (obj, member, msg) => {
1565
1748
  if (!member.has(obj))
1566
1749
  throw TypeError("Cannot " + msg);
@@ -1586,21 +1769,24 @@ var __privateMethod = (obj, member, method) => {
1586
1769
  const buildClient = (plugins) => {
1587
1770
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1588
1771
  return _a = class {
1589
- constructor(options = {}, links) {
1772
+ constructor(options = {}, links, tables) {
1590
1773
  __privateAdd(this, _parseOptions);
1591
1774
  __privateAdd(this, _getFetchProps);
1592
1775
  __privateAdd(this, _evaluateBranch);
1593
1776
  __privateAdd(this, _branch, void 0);
1594
1777
  const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
1595
- const namespaces = {
1596
- db: new SchemaPlugin(links),
1597
- search: new SearchPlugin(),
1598
- ...plugins
1778
+ const pluginOptions = {
1779
+ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1780
+ cache: safeOptions.cache
1599
1781
  };
1600
- for (const [key, namespace] of Object.entries(namespaces)) {
1782
+ const db = new SchemaPlugin(links, tables).build(pluginOptions);
1783
+ const search = new SearchPlugin(db, links ?? {}).build(pluginOptions);
1784
+ this.db = db;
1785
+ this.search = search;
1786
+ for (const [key, namespace] of Object.entries(plugins ?? {})) {
1601
1787
  if (!namespace)
1602
1788
  continue;
1603
- const result = namespace.build({ getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions) });
1789
+ const result = namespace.build(pluginOptions);
1604
1790
  if (result instanceof Promise) {
1605
1791
  void result.then((namespace2) => {
1606
1792
  this[key] = namespace2;
@@ -1611,14 +1797,15 @@ const buildClient = (plugins) => {
1611
1797
  }
1612
1798
  }
1613
1799
  }, _branch = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
1614
- const fetch = getFetchImplementation(options == null ? void 0 : options.fetch);
1615
- const databaseURL = (options == null ? void 0 : options.databaseURL) || getDatabaseURL();
1616
- const apiKey = (options == null ? void 0 : options.apiKey) || getAPIKey();
1617
- const branch = async () => (options == null ? void 0 : options.branch) ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options == null ? void 0 : options.fetch });
1800
+ const fetch = getFetchImplementation(options?.fetch);
1801
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1802
+ const apiKey = options?.apiKey || getAPIKey();
1803
+ const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1804
+ const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1618
1805
  if (!databaseURL || !apiKey) {
1619
1806
  throw new Error("Options databaseURL and apiKey are required");
1620
1807
  }
1621
- return { fetch, databaseURL, apiKey, branch };
1808
+ return { fetch, databaseURL, apiKey, branch, cache };
1622
1809
  }, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
1623
1810
  fetch,
1624
1811
  apiKey,
@@ -1633,8 +1820,7 @@ const buildClient = (plugins) => {
1633
1820
  apiKey,
1634
1821
  apiUrl: "",
1635
1822
  workspacesApiUrl: (path, params) => {
1636
- var _a2;
1637
- const hasBranch = (_a2 = params.dbBranchName) != null ? _a2 : params.branch;
1823
+ const hasBranch = params.dbBranchName ?? params.branch;
1638
1824
  const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : "");
1639
1825
  return databaseURL + newPath;
1640
1826
  }
@@ -1679,11 +1865,13 @@ exports.Repository = Repository;
1679
1865
  exports.RestRepository = RestRepository;
1680
1866
  exports.SchemaPlugin = SchemaPlugin;
1681
1867
  exports.SearchPlugin = SearchPlugin;
1868
+ exports.SimpleCache = SimpleCache;
1682
1869
  exports.XataApiClient = XataApiClient;
1683
1870
  exports.XataApiPlugin = XataApiPlugin;
1684
1871
  exports.XataError = XataError;
1685
1872
  exports.XataPlugin = XataPlugin;
1686
1873
  exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
1874
+ exports.addGitBranchesEntry = addGitBranchesEntry;
1687
1875
  exports.addTableColumn = addTableColumn;
1688
1876
  exports.buildClient = buildClient;
1689
1877
  exports.bulkInsertTableRecords = bulkInsertTableRecords;
@@ -1718,6 +1906,7 @@ exports.getCurrentBranchDetails = getCurrentBranchDetails;
1718
1906
  exports.getCurrentBranchName = getCurrentBranchName;
1719
1907
  exports.getDatabaseList = getDatabaseList;
1720
1908
  exports.getDatabaseURL = getDatabaseURL;
1909
+ exports.getGitBranchesMapping = getGitBranchesMapping;
1721
1910
  exports.getRecord = getRecord;
1722
1911
  exports.getTableColumns = getTableColumns;
1723
1912
  exports.getTableSchema = getTableSchema;
@@ -1746,8 +1935,10 @@ exports.notExists = notExists;
1746
1935
  exports.operationsByTag = operationsByTag;
1747
1936
  exports.pattern = pattern;
1748
1937
  exports.queryTable = queryTable;
1938
+ exports.removeGitBranchesEntry = removeGitBranchesEntry;
1749
1939
  exports.removeWorkspaceMember = removeWorkspaceMember;
1750
1940
  exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
1941
+ exports.resolveBranch = resolveBranch;
1751
1942
  exports.searchBranch = searchBranch;
1752
1943
  exports.setTableSchema = setTableSchema;
1753
1944
  exports.startsWith = startsWith;