@xata.io/client 0.0.0-alpha.ved00a04 → 0.0.0-alpha.ved669cc

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,34 +11,85 @@ 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";
16
19
  }
17
20
  function toBase64(value) {
18
21
  try {
19
22
  return btoa(value);
20
23
  } catch (err) {
21
- return Buffer.from(value).toString("base64");
24
+ const buf = Buffer;
25
+ return buf.from(value).toString("base64");
22
26
  }
23
27
  }
24
28
 
25
- function getEnvVariable(name) {
29
+ function getEnvironment() {
26
30
  try {
27
- if (isObject(process) && isString(process?.env?.[name])) {
28
- return process.env[name];
31
+ if (isObject(process) && isObject(process.env)) {
32
+ return {
33
+ apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
34
+ databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
35
+ branch: process.env.XATA_BRANCH ?? process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH ?? getGlobalBranch(),
36
+ fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
37
+ };
29
38
  }
30
39
  } catch (err) {
31
40
  }
32
41
  try {
33
- if (isObject(Deno) && isString(Deno?.env?.get(name))) {
34
- return Deno.env.get(name);
42
+ if (isObject(Deno) && isObject(Deno.env)) {
43
+ return {
44
+ apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
45
+ databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
46
+ branch: Deno.env.get("XATA_BRANCH") ?? Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH") ?? getGlobalBranch(),
47
+ fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
48
+ };
35
49
  }
36
50
  } catch (err) {
37
51
  }
52
+ return {
53
+ apiKey: getGlobalApiKey(),
54
+ databaseURL: getGlobalDatabaseURL(),
55
+ branch: getGlobalBranch(),
56
+ fallbackBranch: getGlobalFallbackBranch()
57
+ };
58
+ }
59
+ function getGlobalApiKey() {
60
+ try {
61
+ return XATA_API_KEY;
62
+ } catch (err) {
63
+ return void 0;
64
+ }
65
+ }
66
+ function getGlobalDatabaseURL() {
67
+ try {
68
+ return XATA_DATABASE_URL;
69
+ } catch (err) {
70
+ return void 0;
71
+ }
72
+ }
73
+ function getGlobalBranch() {
74
+ try {
75
+ return XATA_BRANCH;
76
+ } catch (err) {
77
+ return void 0;
78
+ }
79
+ }
80
+ function getGlobalFallbackBranch() {
81
+ try {
82
+ return XATA_FALLBACK_BRANCH;
83
+ } catch (err) {
84
+ return void 0;
85
+ }
38
86
  }
39
87
  async function getGitBranch() {
40
88
  try {
41
- return require("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
89
+ if (typeof require === "function") {
90
+ const req = require;
91
+ return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
92
+ }
42
93
  } catch (err) {
43
94
  }
44
95
  try {
@@ -56,7 +107,8 @@ async function getGitBranch() {
56
107
 
57
108
  function getAPIKey() {
58
109
  try {
59
- return getEnvVariable("XATA_API_KEY") ?? XATA_API_KEY;
110
+ const { apiKey } = getEnvironment();
111
+ return apiKey;
60
112
  } catch (err) {
61
113
  return void 0;
62
114
  }
@@ -71,16 +123,28 @@ function getFetchImplementation(userFetch) {
71
123
  return fetchImpl;
72
124
  }
73
125
 
74
- class FetcherError extends Error {
75
- constructor(status, data) {
126
+ const VERSION = "0.0.0-alpha.ved669cc";
127
+
128
+ class ErrorWithCause extends Error {
129
+ constructor(message, options) {
130
+ super(message, options);
131
+ }
132
+ }
133
+ class FetcherError extends ErrorWithCause {
134
+ constructor(status, data, requestId) {
76
135
  super(getMessage(data));
77
136
  this.status = status;
78
137
  this.errors = isBulkError(data) ? data.errors : void 0;
138
+ this.requestId = requestId;
79
139
  if (data instanceof Error) {
80
140
  this.stack = data.stack;
81
141
  this.cause = data.cause;
82
142
  }
83
143
  }
144
+ toString() {
145
+ const error = super.toString();
146
+ return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
147
+ }
84
148
  }
85
149
  function isBulkError(error) {
86
150
  return isObject(error) && Array.isArray(error.errors);
@@ -103,7 +167,12 @@ function getMessage(data) {
103
167
  }
104
168
 
105
169
  const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
106
- const query = new URLSearchParams(queryParams).toString();
170
+ const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
171
+ if (value === void 0 || value === null)
172
+ return acc;
173
+ return { ...acc, [key]: value };
174
+ }, {});
175
+ const query = new URLSearchParams(cleanQueryParams).toString();
107
176
  const queryString = query.length > 0 ? `?${query}` : "";
108
177
  return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
109
178
  };
@@ -143,6 +212,7 @@ async function fetch$1({
143
212
  body: body ? JSON.stringify(body) : void 0,
144
213
  headers: {
145
214
  "Content-Type": "application/json",
215
+ "User-Agent": `Xata client-ts/${VERSION}`,
146
216
  ...headers,
147
217
  ...hostHeader(fullUrl),
148
218
  Authorization: `Bearer ${apiKey}`
@@ -151,14 +221,15 @@ async function fetch$1({
151
221
  if (response.status === 204) {
152
222
  return {};
153
223
  }
224
+ const requestId = response.headers?.get("x-request-id") ?? void 0;
154
225
  try {
155
226
  const jsonResponse = await response.json();
156
227
  if (response.ok) {
157
228
  return jsonResponse;
158
229
  }
159
- throw new FetcherError(response.status, jsonResponse);
230
+ throw new FetcherError(response.status, jsonResponse, requestId);
160
231
  } catch (error) {
161
- throw new FetcherError(response.status, error);
232
+ throw new FetcherError(response.status, error, requestId);
162
233
  }
163
234
  }
164
235
 
@@ -252,6 +323,14 @@ const deleteDatabase = (variables) => fetch$1({
252
323
  method: "delete",
253
324
  ...variables
254
325
  });
326
+ const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
327
+ const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
328
+ const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
329
+ const resolveBranch = (variables) => fetch$1({
330
+ url: "/dbs/{dbName}/resolveBranch",
331
+ method: "get",
332
+ ...variables
333
+ });
255
334
  const getBranchDetails = (variables) => fetch$1({
256
335
  url: "/db/{dbBranchName}",
257
336
  method: "get",
@@ -280,17 +359,6 @@ const getBranchMetadata = (variables) => fetch$1({
280
359
  const getBranchMigrationHistory = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations", method: "get", ...variables });
281
360
  const executeBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/execute", method: "post", ...variables });
282
361
  const getBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/plan", method: "post", ...variables });
283
- const compareBranchSchema = (variables) => fetch$1({
284
- url: "/db/{dbBranchName}/schema/compare/{branchName}",
285
- method: "post",
286
- ...variables
287
- });
288
- const updateBranchSchema = (variables) => fetch$1({
289
- url: "/db/{dbBranchName}/schema/update",
290
- method: "post",
291
- ...variables
292
- });
293
- const getBranchSchemaHistory = (variables) => fetch$1({ url: "/db/{dbBranchName}/schema/history", method: "post", ...variables });
294
362
  const getBranchStats = (variables) => fetch$1({
295
363
  url: "/db/{dbBranchName}/stats",
296
364
  method: "get",
@@ -370,6 +438,11 @@ const queryTable = (variables) => fetch$1({
370
438
  method: "post",
371
439
  ...variables
372
440
  });
441
+ const searchTable = (variables) => fetch$1({
442
+ url: "/db/{dbBranchName}/tables/{tableName}/search",
443
+ method: "post",
444
+ ...variables
445
+ });
373
446
  const searchBranch = (variables) => fetch$1({
374
447
  url: "/db/{dbBranchName}/search",
375
448
  method: "post",
@@ -391,7 +464,15 @@ const operationsByTag = {
391
464
  resendWorkspaceMemberInvite,
392
465
  acceptWorkspaceMemberInvite
393
466
  },
394
- database: { getDatabaseList, createDatabase, deleteDatabase },
467
+ database: {
468
+ getDatabaseList,
469
+ createDatabase,
470
+ deleteDatabase,
471
+ getGitBranchesMapping,
472
+ addGitBranchesEntry,
473
+ removeGitBranchesEntry,
474
+ resolveBranch
475
+ },
395
476
  branch: {
396
477
  getBranchList,
397
478
  getBranchDetails,
@@ -402,9 +483,6 @@ const operationsByTag = {
402
483
  getBranchMigrationHistory,
403
484
  executeBranchMigrationPlan,
404
485
  getBranchMigrationPlan,
405
- compareBranchSchema,
406
- updateBranchSchema,
407
- getBranchSchemaHistory,
408
486
  getBranchStats
409
487
  },
410
488
  table: {
@@ -428,6 +506,7 @@ const operationsByTag = {
428
506
  getRecord,
429
507
  bulkInsertTableRecords,
430
508
  queryTable,
509
+ searchTable,
431
510
  searchBranch
432
511
  }
433
512
  };
@@ -461,7 +540,7 @@ var __accessCheck$7 = (obj, member, msg) => {
461
540
  if (!member.has(obj))
462
541
  throw TypeError("Cannot " + msg);
463
542
  };
464
- var __privateGet$6 = (obj, member, getter) => {
543
+ var __privateGet$7 = (obj, member, getter) => {
465
544
  __accessCheck$7(obj, member, "read from private field");
466
545
  return getter ? getter.call(obj) : member.get(obj);
467
546
  };
@@ -470,7 +549,7 @@ var __privateAdd$7 = (obj, member, value) => {
470
549
  throw TypeError("Cannot add the same private member more than once");
471
550
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
472
551
  };
473
- var __privateSet$5 = (obj, member, value, setter) => {
552
+ var __privateSet$7 = (obj, member, value, setter) => {
474
553
  __accessCheck$7(obj, member, "write to private field");
475
554
  setter ? setter.call(obj, value) : member.set(obj, value);
476
555
  return value;
@@ -485,7 +564,7 @@ class XataApiClient {
485
564
  if (!apiKey) {
486
565
  throw new Error("Could not resolve a valid apiKey");
487
566
  }
488
- __privateSet$5(this, _extraProps, {
567
+ __privateSet$7(this, _extraProps, {
489
568
  apiUrl: getHostUrl(provider, "main"),
490
569
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
491
570
  fetchImpl: getFetchImplementation(options.fetch),
@@ -493,34 +572,34 @@ class XataApiClient {
493
572
  });
494
573
  }
495
574
  get user() {
496
- if (!__privateGet$6(this, _namespaces).user)
497
- __privateGet$6(this, _namespaces).user = new UserApi(__privateGet$6(this, _extraProps));
498
- return __privateGet$6(this, _namespaces).user;
575
+ if (!__privateGet$7(this, _namespaces).user)
576
+ __privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
577
+ return __privateGet$7(this, _namespaces).user;
499
578
  }
500
579
  get workspaces() {
501
- if (!__privateGet$6(this, _namespaces).workspaces)
502
- __privateGet$6(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$6(this, _extraProps));
503
- return __privateGet$6(this, _namespaces).workspaces;
580
+ if (!__privateGet$7(this, _namespaces).workspaces)
581
+ __privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
582
+ return __privateGet$7(this, _namespaces).workspaces;
504
583
  }
505
584
  get databases() {
506
- if (!__privateGet$6(this, _namespaces).databases)
507
- __privateGet$6(this, _namespaces).databases = new DatabaseApi(__privateGet$6(this, _extraProps));
508
- return __privateGet$6(this, _namespaces).databases;
585
+ if (!__privateGet$7(this, _namespaces).databases)
586
+ __privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
587
+ return __privateGet$7(this, _namespaces).databases;
509
588
  }
510
589
  get branches() {
511
- if (!__privateGet$6(this, _namespaces).branches)
512
- __privateGet$6(this, _namespaces).branches = new BranchApi(__privateGet$6(this, _extraProps));
513
- return __privateGet$6(this, _namespaces).branches;
590
+ if (!__privateGet$7(this, _namespaces).branches)
591
+ __privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
592
+ return __privateGet$7(this, _namespaces).branches;
514
593
  }
515
594
  get tables() {
516
- if (!__privateGet$6(this, _namespaces).tables)
517
- __privateGet$6(this, _namespaces).tables = new TableApi(__privateGet$6(this, _extraProps));
518
- return __privateGet$6(this, _namespaces).tables;
595
+ if (!__privateGet$7(this, _namespaces).tables)
596
+ __privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
597
+ return __privateGet$7(this, _namespaces).tables;
519
598
  }
520
599
  get records() {
521
- if (!__privateGet$6(this, _namespaces).records)
522
- __privateGet$6(this, _namespaces).records = new RecordsApi(__privateGet$6(this, _extraProps));
523
- return __privateGet$6(this, _namespaces).records;
600
+ if (!__privateGet$7(this, _namespaces).records)
601
+ __privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
602
+ return __privateGet$7(this, _namespaces).records;
524
603
  }
525
604
  }
526
605
  _extraProps = new WeakMap();
@@ -654,6 +733,33 @@ class DatabaseApi {
654
733
  ...this.extraProps
655
734
  });
656
735
  }
736
+ getGitBranchesMapping(workspace, dbName) {
737
+ return operationsByTag.database.getGitBranchesMapping({
738
+ pathParams: { workspace, dbName },
739
+ ...this.extraProps
740
+ });
741
+ }
742
+ addGitBranchesEntry(workspace, dbName, body) {
743
+ return operationsByTag.database.addGitBranchesEntry({
744
+ pathParams: { workspace, dbName },
745
+ body,
746
+ ...this.extraProps
747
+ });
748
+ }
749
+ removeGitBranchesEntry(workspace, dbName, gitBranch) {
750
+ return operationsByTag.database.removeGitBranchesEntry({
751
+ pathParams: { workspace, dbName },
752
+ queryParams: { gitBranch },
753
+ ...this.extraProps
754
+ });
755
+ }
756
+ resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
757
+ return operationsByTag.database.resolveBranch({
758
+ pathParams: { workspace, dbName },
759
+ queryParams: { gitBranch, fallbackBranch },
760
+ ...this.extraProps
761
+ });
762
+ }
657
763
  }
658
764
  class BranchApi {
659
765
  constructor(extraProps) {
@@ -671,10 +777,10 @@ class BranchApi {
671
777
  ...this.extraProps
672
778
  });
673
779
  }
674
- createBranch(workspace, database, branch, from = "", options = {}) {
780
+ createBranch(workspace, database, branch, from, options = {}) {
675
781
  return operationsByTag.branch.createBranch({
676
782
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
677
- queryParams: { from },
783
+ queryParams: isString(from) ? { from } : void 0,
678
784
  body: options,
679
785
  ...this.extraProps
680
786
  });
@@ -856,6 +962,13 @@ class RecordsApi {
856
962
  ...this.extraProps
857
963
  });
858
964
  }
965
+ searchTable(workspace, database, branch, tableName, query) {
966
+ return operationsByTag.records.searchTable({
967
+ pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
968
+ body: query,
969
+ ...this.extraProps
970
+ });
971
+ }
859
972
  searchBranch(workspace, database, branch, query) {
860
973
  return operationsByTag.records.searchBranch({
861
974
  pathParams: { workspace, dbBranchName: `${database}:${branch}` },
@@ -879,7 +992,7 @@ var __accessCheck$6 = (obj, member, msg) => {
879
992
  if (!member.has(obj))
880
993
  throw TypeError("Cannot " + msg);
881
994
  };
882
- var __privateGet$5 = (obj, member, getter) => {
995
+ var __privateGet$6 = (obj, member, getter) => {
883
996
  __accessCheck$6(obj, member, "read from private field");
884
997
  return getter ? getter.call(obj) : member.get(obj);
885
998
  };
@@ -888,30 +1001,30 @@ var __privateAdd$6 = (obj, member, value) => {
888
1001
  throw TypeError("Cannot add the same private member more than once");
889
1002
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
890
1003
  };
891
- var __privateSet$4 = (obj, member, value, setter) => {
1004
+ var __privateSet$6 = (obj, member, value, setter) => {
892
1005
  __accessCheck$6(obj, member, "write to private field");
893
1006
  setter ? setter.call(obj, value) : member.set(obj, value);
894
1007
  return value;
895
1008
  };
896
- var _query;
1009
+ var _query, _page;
897
1010
  class Page {
898
1011
  constructor(query, meta, records = []) {
899
1012
  __privateAdd$6(this, _query, void 0);
900
- __privateSet$4(this, _query, query);
1013
+ __privateSet$6(this, _query, query);
901
1014
  this.meta = meta;
902
- this.records = records;
1015
+ this.records = new RecordArray(this, records);
903
1016
  }
904
1017
  async nextPage(size, offset) {
905
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
1018
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
906
1019
  }
907
1020
  async previousPage(size, offset) {
908
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
1021
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
909
1022
  }
910
1023
  async firstPage(size, offset) {
911
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
1024
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
912
1025
  }
913
1026
  async lastPage(size, offset) {
914
- return __privateGet$5(this, _query).getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
1027
+ return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
915
1028
  }
916
1029
  hasNextPage() {
917
1030
  return this.meta.page.more;
@@ -919,15 +1032,56 @@ class Page {
919
1032
  }
920
1033
  _query = new WeakMap();
921
1034
  const PAGINATION_MAX_SIZE = 200;
922
- const PAGINATION_DEFAULT_SIZE = 200;
1035
+ const PAGINATION_DEFAULT_SIZE = 20;
923
1036
  const PAGINATION_MAX_OFFSET = 800;
924
1037
  const PAGINATION_DEFAULT_OFFSET = 0;
1038
+ function isCursorPaginationOptions(options) {
1039
+ return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
1040
+ }
1041
+ const _RecordArray = class extends Array {
1042
+ constructor(page, overrideRecords) {
1043
+ super(..._RecordArray.parseConstructorParams(page, overrideRecords));
1044
+ __privateAdd$6(this, _page, void 0);
1045
+ __privateSet$6(this, _page, page);
1046
+ }
1047
+ static parseConstructorParams(...args) {
1048
+ if (args.length === 1 && typeof args[0] === "number") {
1049
+ return new Array(args[0]);
1050
+ }
1051
+ if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
1052
+ const result = args[1] ?? args[0].records ?? [];
1053
+ return new Array(...result);
1054
+ }
1055
+ return new Array(...args);
1056
+ }
1057
+ async nextPage(size, offset) {
1058
+ const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
1059
+ return new _RecordArray(newPage);
1060
+ }
1061
+ async previousPage(size, offset) {
1062
+ const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
1063
+ return new _RecordArray(newPage);
1064
+ }
1065
+ async firstPage(size, offset) {
1066
+ const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
1067
+ return new _RecordArray(newPage);
1068
+ }
1069
+ async lastPage(size, offset) {
1070
+ const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
1071
+ return new _RecordArray(newPage);
1072
+ }
1073
+ hasNextPage() {
1074
+ return __privateGet$6(this, _page).meta.page.more;
1075
+ }
1076
+ };
1077
+ let RecordArray = _RecordArray;
1078
+ _page = new WeakMap();
925
1079
 
926
1080
  var __accessCheck$5 = (obj, member, msg) => {
927
1081
  if (!member.has(obj))
928
1082
  throw TypeError("Cannot " + msg);
929
1083
  };
930
- var __privateGet$4 = (obj, member, getter) => {
1084
+ var __privateGet$5 = (obj, member, getter) => {
931
1085
  __accessCheck$5(obj, member, "read from private field");
932
1086
  return getter ? getter.call(obj) : member.get(obj);
933
1087
  };
@@ -936,34 +1090,35 @@ var __privateAdd$5 = (obj, member, value) => {
936
1090
  throw TypeError("Cannot add the same private member more than once");
937
1091
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
938
1092
  };
939
- var __privateSet$3 = (obj, member, value, setter) => {
1093
+ var __privateSet$5 = (obj, member, value, setter) => {
940
1094
  __accessCheck$5(obj, member, "write to private field");
941
1095
  setter ? setter.call(obj, value) : member.set(obj, value);
942
1096
  return value;
943
1097
  };
944
1098
  var _table$1, _repository, _data;
945
1099
  const _Query = class {
946
- constructor(repository, table, data, parent) {
1100
+ constructor(repository, table, data, rawParent) {
947
1101
  __privateAdd$5(this, _table$1, void 0);
948
1102
  __privateAdd$5(this, _repository, void 0);
949
1103
  __privateAdd$5(this, _data, { filter: {} });
950
1104
  this.meta = { page: { cursor: "start", more: true } };
951
- this.records = [];
952
- __privateSet$3(this, _table$1, table);
1105
+ this.records = new RecordArray(this, []);
1106
+ __privateSet$5(this, _table$1, table);
953
1107
  if (repository) {
954
- __privateSet$3(this, _repository, repository);
1108
+ __privateSet$5(this, _repository, repository);
955
1109
  } else {
956
- __privateSet$3(this, _repository, this);
1110
+ __privateSet$5(this, _repository, this);
957
1111
  }
958
- __privateGet$4(this, _data).filter = data.filter ?? parent?.filter ?? {};
959
- __privateGet$4(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
960
- __privateGet$4(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
961
- __privateGet$4(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
962
- __privateGet$4(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
963
- __privateGet$4(this, _data).sort = data.sort ?? parent?.sort;
964
- __privateGet$4(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
965
- __privateGet$4(this, _data).page = data.page ?? parent?.page;
966
- __privateGet$4(this, _data).cache = data.cache ?? parent?.cache;
1112
+ const parent = cleanParent(data, rawParent);
1113
+ __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
1114
+ __privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
1115
+ __privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
1116
+ __privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
1117
+ __privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
1118
+ __privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
1119
+ __privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
1120
+ __privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
1121
+ __privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
967
1122
  this.any = this.any.bind(this);
968
1123
  this.all = this.all.bind(this);
969
1124
  this.not = this.not.bind(this);
@@ -974,83 +1129,88 @@ const _Query = class {
974
1129
  Object.defineProperty(this, "repository", { enumerable: false });
975
1130
  }
976
1131
  getQueryOptions() {
977
- return __privateGet$4(this, _data);
1132
+ return __privateGet$5(this, _data);
978
1133
  }
979
1134
  key() {
980
- const { columns = [], filter = {}, sort = [], page = {} } = __privateGet$4(this, _data);
981
- const key = JSON.stringify({ columns, filter, sort, page });
1135
+ const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
1136
+ const key = JSON.stringify({ columns, filter, sort, pagination });
982
1137
  return toBase64(key);
983
1138
  }
984
1139
  any(...queries) {
985
1140
  const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
986
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $any } }, __privateGet$4(this, _data));
1141
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
987
1142
  }
988
1143
  all(...queries) {
989
1144
  const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
990
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1145
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
991
1146
  }
992
1147
  not(...queries) {
993
1148
  const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
994
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $not } }, __privateGet$4(this, _data));
1149
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
995
1150
  }
996
1151
  none(...queries) {
997
1152
  const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
998
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $none } }, __privateGet$4(this, _data));
1153
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
999
1154
  }
1000
1155
  filter(a, b) {
1001
1156
  if (arguments.length === 1) {
1002
1157
  const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
1003
- const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat(constraints));
1004
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1158
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
1159
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1005
1160
  } else {
1006
- const $all = compact([__privateGet$4(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1007
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { filter: { $all } }, __privateGet$4(this, _data));
1161
+ const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
1162
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
1008
1163
  }
1009
1164
  }
1010
1165
  sort(column, direction) {
1011
- const originalSort = [__privateGet$4(this, _data).sort ?? []].flat();
1166
+ const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
1012
1167
  const sort = [...originalSort, { column, direction }];
1013
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { sort }, __privateGet$4(this, _data));
1168
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
1014
1169
  }
1015
1170
  select(columns) {
1016
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { columns }, __privateGet$4(this, _data));
1171
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
1017
1172
  }
1018
1173
  getPaginated(options = {}) {
1019
- const query = new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), options, __privateGet$4(this, _data));
1020
- return __privateGet$4(this, _repository).query(query);
1174
+ const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
1175
+ return __privateGet$5(this, _repository).query(query);
1021
1176
  }
1022
1177
  async *[Symbol.asyncIterator]() {
1023
- for await (const [record] of this.getIterator(1)) {
1178
+ for await (const [record] of this.getIterator({ batchSize: 1 })) {
1024
1179
  yield record;
1025
1180
  }
1026
1181
  }
1027
- async *getIterator(chunk, options = {}) {
1028
- let offset = 0;
1029
- let end = false;
1030
- while (!end) {
1031
- const { records, meta } = await this.getPaginated({ ...options, page: { size: chunk, offset } });
1032
- yield records;
1033
- offset += chunk;
1034
- end = !meta.page.more;
1182
+ async *getIterator(options = {}) {
1183
+ const { batchSize = 1 } = options;
1184
+ let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
1185
+ let more = page.hasNextPage();
1186
+ yield page.records;
1187
+ while (more) {
1188
+ page = await page.nextPage();
1189
+ more = page.hasNextPage();
1190
+ yield page.records;
1035
1191
  }
1036
1192
  }
1037
1193
  async getMany(options = {}) {
1038
- const { records } = await this.getPaginated(options);
1039
- return records;
1194
+ const page = await this.getPaginated(options);
1195
+ if (page.hasNextPage() && options.pagination?.size === void 0) {
1196
+ console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
1197
+ }
1198
+ return page.records;
1040
1199
  }
1041
- async getAll(chunk = PAGINATION_MAX_SIZE, options = {}) {
1200
+ async getAll(options = {}) {
1201
+ const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
1042
1202
  const results = [];
1043
- for await (const page of this.getIterator(chunk, options)) {
1203
+ for await (const page of this.getIterator({ ...rest, batchSize })) {
1044
1204
  results.push(...page);
1045
1205
  }
1046
1206
  return results;
1047
1207
  }
1048
1208
  async getFirst(options = {}) {
1049
- const records = await this.getMany({ ...options, page: { size: 1 } });
1050
- return records[0] || null;
1209
+ const records = await this.getMany({ ...options, pagination: { size: 1 } });
1210
+ return records[0] ?? null;
1051
1211
  }
1052
1212
  cache(ttl) {
1053
- return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { cache: ttl }, __privateGet$4(this, _data));
1213
+ return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
1054
1214
  }
1055
1215
  nextPage(size, offset) {
1056
1216
  return this.firstPage(size, offset);
@@ -1059,10 +1219,10 @@ const _Query = class {
1059
1219
  return this.firstPage(size, offset);
1060
1220
  }
1061
1221
  firstPage(size, offset) {
1062
- return this.getPaginated({ page: { size, offset } });
1222
+ return this.getPaginated({ pagination: { size, offset } });
1063
1223
  }
1064
1224
  lastPage(size, offset) {
1065
- return this.getPaginated({ page: { size, offset, before: "end" } });
1225
+ return this.getPaginated({ pagination: { size, offset, before: "end" } });
1066
1226
  }
1067
1227
  hasNextPage() {
1068
1228
  return this.meta.page.more;
@@ -1072,12 +1232,20 @@ let Query = _Query;
1072
1232
  _table$1 = new WeakMap();
1073
1233
  _repository = new WeakMap();
1074
1234
  _data = new WeakMap();
1235
+ function cleanParent(data, parent) {
1236
+ if (isCursorPaginationOptions(data.pagination)) {
1237
+ return { ...parent, sorting: void 0, filter: void 0 };
1238
+ }
1239
+ return parent;
1240
+ }
1075
1241
 
1076
1242
  function isIdentifiable(x) {
1077
1243
  return isObject(x) && isString(x?.id);
1078
1244
  }
1079
1245
  function isXataRecord(x) {
1080
- return isIdentifiable(x) && typeof x?.xata === "object" && typeof x?.xata?.version === "number";
1246
+ const record = x;
1247
+ const metadata = record?.getMetadata();
1248
+ return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
1081
1249
  }
1082
1250
 
1083
1251
  function isSortFilterString(value) {
@@ -1107,7 +1275,7 @@ var __accessCheck$4 = (obj, member, msg) => {
1107
1275
  if (!member.has(obj))
1108
1276
  throw TypeError("Cannot " + msg);
1109
1277
  };
1110
- var __privateGet$3 = (obj, member, getter) => {
1278
+ var __privateGet$4 = (obj, member, getter) => {
1111
1279
  __accessCheck$4(obj, member, "read from private field");
1112
1280
  return getter ? getter.call(obj) : member.get(obj);
1113
1281
  };
@@ -1116,7 +1284,7 @@ var __privateAdd$4 = (obj, member, value) => {
1116
1284
  throw TypeError("Cannot add the same private member more than once");
1117
1285
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1118
1286
  };
1119
- var __privateSet$2 = (obj, member, value, setter) => {
1287
+ var __privateSet$4 = (obj, member, value, setter) => {
1120
1288
  __accessCheck$4(obj, member, "write to private field");
1121
1289
  setter ? setter.call(obj, value) : member.set(obj, value);
1122
1290
  return value;
@@ -1125,7 +1293,7 @@ var __privateMethod$2 = (obj, member, method) => {
1125
1293
  __accessCheck$4(obj, member, "access private method");
1126
1294
  return method;
1127
1295
  };
1128
- 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;
1296
+ var _table, _getFetchProps, _cache, _schemaTables$2, _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, _getSchemaTables$1, getSchemaTables_fn$1;
1129
1297
  class Repository extends Query {
1130
1298
  }
1131
1299
  class RestRepository extends Query {
@@ -1142,18 +1310,21 @@ class RestRepository extends Query {
1142
1310
  __privateAdd$4(this, _getCacheRecord);
1143
1311
  __privateAdd$4(this, _setCacheQuery);
1144
1312
  __privateAdd$4(this, _getCacheQuery);
1313
+ __privateAdd$4(this, _getSchemaTables$1);
1145
1314
  __privateAdd$4(this, _table, void 0);
1146
- __privateAdd$4(this, _links, void 0);
1147
1315
  __privateAdd$4(this, _getFetchProps, void 0);
1148
1316
  __privateAdd$4(this, _cache, void 0);
1149
- __privateSet$2(this, _table, options.table);
1150
- __privateSet$2(this, _links, options.links ?? {});
1151
- __privateSet$2(this, _getFetchProps, options.pluginOptions.getFetchProps);
1317
+ __privateAdd$4(this, _schemaTables$2, void 0);
1318
+ __privateSet$4(this, _table, options.table);
1319
+ __privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
1152
1320
  this.db = options.db;
1153
- __privateSet$2(this, _cache, options.pluginOptions.cache);
1321
+ __privateSet$4(this, _cache, options.pluginOptions.cache);
1322
+ __privateSet$4(this, _schemaTables$2, options.schemaTables);
1154
1323
  }
1155
1324
  async create(a, b) {
1156
1325
  if (Array.isArray(a)) {
1326
+ if (a.length === 0)
1327
+ return [];
1157
1328
  const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1158
1329
  await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1159
1330
  return records;
@@ -1179,26 +1350,38 @@ class RestRepository extends Query {
1179
1350
  }
1180
1351
  throw new Error("Invalid arguments for create method");
1181
1352
  }
1182
- async read(recordId) {
1183
- const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1184
- if (cacheRecord)
1185
- return cacheRecord;
1186
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1187
- try {
1188
- const response = await getRecord({
1189
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1190
- ...fetchProps
1191
- });
1192
- return initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), response);
1193
- } catch (e) {
1194
- if (isObject(e) && e.status === 404) {
1195
- return null;
1353
+ async read(a) {
1354
+ if (Array.isArray(a)) {
1355
+ if (a.length === 0)
1356
+ return [];
1357
+ const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
1358
+ return this.getAll({ filter: { id: { $any: ids } } });
1359
+ }
1360
+ const id = isString(a) ? a : a.id;
1361
+ if (isString(id)) {
1362
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
1363
+ if (cacheRecord)
1364
+ return cacheRecord;
1365
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1366
+ try {
1367
+ const response = await getRecord({
1368
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
1369
+ ...fetchProps
1370
+ });
1371
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1372
+ return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
1373
+ } catch (e) {
1374
+ if (isObject(e) && e.status === 404) {
1375
+ return null;
1376
+ }
1377
+ throw e;
1196
1378
  }
1197
- throw e;
1198
1379
  }
1199
1380
  }
1200
1381
  async update(a, b) {
1201
1382
  if (Array.isArray(a)) {
1383
+ if (a.length === 0)
1384
+ return [];
1202
1385
  if (a.length > 100) {
1203
1386
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1204
1387
  }
@@ -1220,6 +1403,8 @@ class RestRepository extends Query {
1220
1403
  }
1221
1404
  async createOrUpdate(a, b) {
1222
1405
  if (Array.isArray(a)) {
1406
+ if (a.length === 0)
1407
+ return [];
1223
1408
  if (a.length > 100) {
1224
1409
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1225
1410
  }
@@ -1241,6 +1426,8 @@ class RestRepository extends Query {
1241
1426
  }
1242
1427
  async delete(a) {
1243
1428
  if (Array.isArray(a)) {
1429
+ if (a.length === 0)
1430
+ return;
1244
1431
  if (a.length > 100) {
1245
1432
  console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1246
1433
  }
@@ -1260,13 +1447,19 @@ class RestRepository extends Query {
1260
1447
  throw new Error("Invalid arguments for delete method");
1261
1448
  }
1262
1449
  async search(query, options = {}) {
1263
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1264
- const { records } = await searchBranch({
1265
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1266
- body: { tables: [__privateGet$3(this, _table)], query, fuzziness: options.fuzziness },
1450
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1451
+ const { records } = await searchTable({
1452
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1453
+ body: {
1454
+ query,
1455
+ fuzziness: options.fuzziness,
1456
+ highlight: options.highlight,
1457
+ filter: options.filter
1458
+ },
1267
1459
  ...fetchProps
1268
1460
  });
1269
- return records.map((item) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), item));
1461
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1462
+ return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
1270
1463
  }
1271
1464
  async query(query) {
1272
1465
  const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
@@ -1275,34 +1468,35 @@ class RestRepository extends Query {
1275
1468
  const data = query.getQueryOptions();
1276
1469
  const body = {
1277
1470
  filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1278
- sort: data.sort ? buildSortFilter(data.sort) : void 0,
1279
- page: data.page,
1471
+ sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1472
+ page: data.pagination,
1280
1473
  columns: data.columns
1281
1474
  };
1282
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1475
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1283
1476
  const { meta, records: objects } = await queryTable({
1284
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1477
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1285
1478
  body,
1286
1479
  ...fetchProps
1287
1480
  });
1288
- const records = objects.map((record) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), record));
1481
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1482
+ const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
1289
1483
  await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1290
1484
  return new Page(query, meta, records);
1291
1485
  }
1292
1486
  }
1293
1487
  _table = new WeakMap();
1294
- _links = new WeakMap();
1295
1488
  _getFetchProps = new WeakMap();
1296
1489
  _cache = new WeakMap();
1490
+ _schemaTables$2 = new WeakMap();
1297
1491
  _insertRecordWithoutId = new WeakSet();
1298
1492
  insertRecordWithoutId_fn = async function(object) {
1299
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1493
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1300
1494
  const record = transformObjectLinks(object);
1301
1495
  const response = await insertRecord({
1302
1496
  pathParams: {
1303
1497
  workspace: "{workspaceId}",
1304
1498
  dbBranchName: "{dbBranch}",
1305
- tableName: __privateGet$3(this, _table)
1499
+ tableName: __privateGet$4(this, _table)
1306
1500
  },
1307
1501
  body: record,
1308
1502
  ...fetchProps
@@ -1315,13 +1509,13 @@ insertRecordWithoutId_fn = async function(object) {
1315
1509
  };
1316
1510
  _insertRecordWithId = new WeakSet();
1317
1511
  insertRecordWithId_fn = async function(recordId, object) {
1318
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1512
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1319
1513
  const record = transformObjectLinks(object);
1320
1514
  const response = await insertRecordWithID({
1321
1515
  pathParams: {
1322
1516
  workspace: "{workspaceId}",
1323
1517
  dbBranchName: "{dbBranch}",
1324
- tableName: __privateGet$3(this, _table),
1518
+ tableName: __privateGet$4(this, _table),
1325
1519
  recordId
1326
1520
  },
1327
1521
  body: record,
@@ -1336,25 +1530,29 @@ insertRecordWithId_fn = async function(recordId, object) {
1336
1530
  };
1337
1531
  _bulkInsertTableRecords = new WeakSet();
1338
1532
  bulkInsertTableRecords_fn = async function(objects) {
1339
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1533
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1340
1534
  const records = objects.map((object) => transformObjectLinks(object));
1341
- const response = await bulkInsertTableRecords({
1342
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table) },
1535
+ const { recordIDs } = await bulkInsertTableRecords({
1536
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1343
1537
  body: { records },
1344
1538
  ...fetchProps
1345
1539
  });
1346
- const finalObjects = await this.any(...response.recordIDs.map((id) => this.filter("id", id))).getAll();
1540
+ const finalObjects = await this.read(recordIDs);
1347
1541
  if (finalObjects.length !== objects.length) {
1348
1542
  throw new Error("The server failed to save some records");
1349
1543
  }
1350
- return finalObjects;
1544
+ const dictionary = finalObjects.reduce((acc, object) => {
1545
+ acc[object.id] = object;
1546
+ return acc;
1547
+ }, {});
1548
+ return recordIDs.map((id) => dictionary[id]);
1351
1549
  };
1352
1550
  _updateRecordWithID = new WeakSet();
1353
1551
  updateRecordWithID_fn = async function(recordId, object) {
1354
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1552
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1355
1553
  const record = transformObjectLinks(object);
1356
1554
  const response = await updateRecordWithID({
1357
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1555
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1358
1556
  body: record,
1359
1557
  ...fetchProps
1360
1558
  });
@@ -1365,9 +1563,9 @@ updateRecordWithID_fn = async function(recordId, object) {
1365
1563
  };
1366
1564
  _upsertRecordWithID = new WeakSet();
1367
1565
  upsertRecordWithID_fn = async function(recordId, object) {
1368
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1566
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1369
1567
  const response = await upsertRecordWithID({
1370
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1568
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1371
1569
  body: object,
1372
1570
  ...fetchProps
1373
1571
  });
@@ -1378,51 +1576,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
1378
1576
  };
1379
1577
  _deleteRecord = new WeakSet();
1380
1578
  deleteRecord_fn = async function(recordId) {
1381
- const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
1579
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1382
1580
  await deleteRecord({
1383
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
1581
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1384
1582
  ...fetchProps
1385
1583
  });
1386
1584
  };
1387
1585
  _invalidateCache = new WeakSet();
1388
1586
  invalidateCache_fn = async function(recordId) {
1389
- await __privateGet$3(this, _cache).delete(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1390
- const cacheItems = await __privateGet$3(this, _cache).getAll();
1587
+ await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1588
+ const cacheItems = await __privateGet$4(this, _cache).getAll();
1391
1589
  const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1392
1590
  for (const [key, value] of queries) {
1393
1591
  const ids = getIds(value);
1394
1592
  if (ids.includes(recordId))
1395
- await __privateGet$3(this, _cache).delete(key);
1593
+ await __privateGet$4(this, _cache).delete(key);
1396
1594
  }
1397
1595
  };
1398
1596
  _setCacheRecord = new WeakSet();
1399
1597
  setCacheRecord_fn = async function(record) {
1400
- if (!__privateGet$3(this, _cache).cacheRecords)
1598
+ if (!__privateGet$4(this, _cache).cacheRecords)
1401
1599
  return;
1402
- await __privateGet$3(this, _cache).set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
1600
+ await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
1403
1601
  };
1404
1602
  _getCacheRecord = new WeakSet();
1405
1603
  getCacheRecord_fn = async function(recordId) {
1406
- if (!__privateGet$3(this, _cache).cacheRecords)
1604
+ if (!__privateGet$4(this, _cache).cacheRecords)
1407
1605
  return null;
1408
- return __privateGet$3(this, _cache).get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1606
+ return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
1409
1607
  };
1410
1608
  _setCacheQuery = new WeakSet();
1411
1609
  setCacheQuery_fn = async function(query, meta, records) {
1412
- await __privateGet$3(this, _cache).set(`query_${__privateGet$3(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1610
+ await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1413
1611
  };
1414
1612
  _getCacheQuery = new WeakSet();
1415
1613
  getCacheQuery_fn = async function(query) {
1416
- const key = `query_${__privateGet$3(this, _table)}:${query.key()}`;
1417
- const result = await __privateGet$3(this, _cache).get(key);
1614
+ const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
1615
+ const result = await __privateGet$4(this, _cache).get(key);
1418
1616
  if (!result)
1419
1617
  return null;
1420
- const { cache: ttl = __privateGet$3(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1421
- if (!ttl || ttl < 0)
1422
- return result;
1618
+ const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1619
+ if (ttl < 0)
1620
+ return null;
1423
1621
  const hasExpired = result.date.getTime() + ttl < Date.now();
1424
1622
  return hasExpired ? null : result;
1425
1623
  };
1624
+ _getSchemaTables$1 = new WeakSet();
1625
+ getSchemaTables_fn$1 = async function() {
1626
+ if (__privateGet$4(this, _schemaTables$2))
1627
+ return __privateGet$4(this, _schemaTables$2);
1628
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1629
+ const { schema } = await getBranchDetails({
1630
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1631
+ ...fetchProps
1632
+ });
1633
+ __privateSet$4(this, _schemaTables$2, schema.tables);
1634
+ return schema.tables;
1635
+ };
1426
1636
  const transformObjectLinks = (object) => {
1427
1637
  return Object.entries(object).reduce((acc, [key, value]) => {
1428
1638
  if (key === "xata")
@@ -1430,15 +1640,34 @@ const transformObjectLinks = (object) => {
1430
1640
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1431
1641
  }, {});
1432
1642
  };
1433
- const initObject = (db, links, table, object) => {
1643
+ const initObject = (db, schemaTables, table, object) => {
1434
1644
  const result = {};
1435
- Object.assign(result, object);
1436
- const tableLinks = links[table] || [];
1437
- for (const link of tableLinks) {
1438
- const [field, linkTable] = link;
1439
- const value = result[field];
1440
- if (value && isObject(value)) {
1441
- result[field] = initObject(db, links, linkTable, value);
1645
+ const { xata, ...rest } = object ?? {};
1646
+ Object.assign(result, rest);
1647
+ const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
1648
+ if (!columns)
1649
+ console.error(`Table ${table} not found in schema`);
1650
+ for (const column of columns ?? []) {
1651
+ const value = result[column.name];
1652
+ switch (column.type) {
1653
+ case "datetime": {
1654
+ const date = value !== void 0 ? new Date(value) : void 0;
1655
+ if (date && isNaN(date.getTime())) {
1656
+ console.error(`Failed to parse date ${value} for field ${column.name}`);
1657
+ } else if (date) {
1658
+ result[column.name] = date;
1659
+ }
1660
+ break;
1661
+ }
1662
+ case "link": {
1663
+ const linkTable = column.link?.table;
1664
+ if (!linkTable) {
1665
+ console.error(`Failed to parse link for field ${column.name}`);
1666
+ } else if (isObject(value)) {
1667
+ result[column.name] = initObject(db, schemaTables, linkTable, value);
1668
+ }
1669
+ break;
1670
+ }
1442
1671
  }
1443
1672
  }
1444
1673
  result.read = function() {
@@ -1450,7 +1679,10 @@ const initObject = (db, links, table, object) => {
1450
1679
  result.delete = function() {
1451
1680
  return db[table].delete(result["id"]);
1452
1681
  };
1453
- for (const prop of ["read", "update", "delete"]) {
1682
+ result.getMetadata = function() {
1683
+ return xata;
1684
+ };
1685
+ for (const prop of ["read", "update", "delete", "getMetadata"]) {
1454
1686
  Object.defineProperty(result, prop, { enumerable: false });
1455
1687
  }
1456
1688
  Object.freeze(result);
@@ -1470,7 +1702,7 @@ var __accessCheck$3 = (obj, member, msg) => {
1470
1702
  if (!member.has(obj))
1471
1703
  throw TypeError("Cannot " + msg);
1472
1704
  };
1473
- var __privateGet$2 = (obj, member, getter) => {
1705
+ var __privateGet$3 = (obj, member, getter) => {
1474
1706
  __accessCheck$3(obj, member, "read from private field");
1475
1707
  return getter ? getter.call(obj) : member.get(obj);
1476
1708
  };
@@ -1479,7 +1711,7 @@ var __privateAdd$3 = (obj, member, value) => {
1479
1711
  throw TypeError("Cannot add the same private member more than once");
1480
1712
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1481
1713
  };
1482
- var __privateSet$1 = (obj, member, value, setter) => {
1714
+ var __privateSet$3 = (obj, member, value, setter) => {
1483
1715
  __accessCheck$3(obj, member, "write to private field");
1484
1716
  setter ? setter.call(obj, value) : member.set(obj, value);
1485
1717
  return value;
@@ -1488,30 +1720,30 @@ var _map;
1488
1720
  class SimpleCache {
1489
1721
  constructor(options = {}) {
1490
1722
  __privateAdd$3(this, _map, void 0);
1491
- __privateSet$1(this, _map, /* @__PURE__ */ new Map());
1723
+ __privateSet$3(this, _map, /* @__PURE__ */ new Map());
1492
1724
  this.capacity = options.max ?? 500;
1493
1725
  this.cacheRecords = options.cacheRecords ?? true;
1494
1726
  this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1495
1727
  }
1496
1728
  async getAll() {
1497
- return Object.fromEntries(__privateGet$2(this, _map));
1729
+ return Object.fromEntries(__privateGet$3(this, _map));
1498
1730
  }
1499
1731
  async get(key) {
1500
- return __privateGet$2(this, _map).get(key) ?? null;
1732
+ return __privateGet$3(this, _map).get(key) ?? null;
1501
1733
  }
1502
1734
  async set(key, value) {
1503
1735
  await this.delete(key);
1504
- __privateGet$2(this, _map).set(key, value);
1505
- if (__privateGet$2(this, _map).size > this.capacity) {
1506
- const leastRecentlyUsed = __privateGet$2(this, _map).keys().next().value;
1736
+ __privateGet$3(this, _map).set(key, value);
1737
+ if (__privateGet$3(this, _map).size > this.capacity) {
1738
+ const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
1507
1739
  await this.delete(leastRecentlyUsed);
1508
1740
  }
1509
1741
  }
1510
1742
  async delete(key) {
1511
- __privateGet$2(this, _map).delete(key);
1743
+ __privateGet$3(this, _map).delete(key);
1512
1744
  }
1513
1745
  async clear() {
1514
- return __privateGet$2(this, _map).clear();
1746
+ return __privateGet$3(this, _map).clear();
1515
1747
  }
1516
1748
  }
1517
1749
  _map = new WeakMap();
@@ -1539,7 +1771,7 @@ var __accessCheck$2 = (obj, member, msg) => {
1539
1771
  if (!member.has(obj))
1540
1772
  throw TypeError("Cannot " + msg);
1541
1773
  };
1542
- var __privateGet$1 = (obj, member, getter) => {
1774
+ var __privateGet$2 = (obj, member, getter) => {
1543
1775
  __accessCheck$2(obj, member, "read from private field");
1544
1776
  return getter ? getter.call(obj) : member.get(obj);
1545
1777
  };
@@ -1548,122 +1780,158 @@ var __privateAdd$2 = (obj, member, value) => {
1548
1780
  throw TypeError("Cannot add the same private member more than once");
1549
1781
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1550
1782
  };
1551
- var _tables;
1783
+ var __privateSet$2 = (obj, member, value, setter) => {
1784
+ __accessCheck$2(obj, member, "write to private field");
1785
+ setter ? setter.call(obj, value) : member.set(obj, value);
1786
+ return value;
1787
+ };
1788
+ var _tables, _schemaTables$1;
1552
1789
  class SchemaPlugin extends XataPlugin {
1553
- constructor(links, tableNames) {
1790
+ constructor(schemaTables) {
1554
1791
  super();
1555
- this.links = links;
1556
- this.tableNames = tableNames;
1557
1792
  __privateAdd$2(this, _tables, {});
1793
+ __privateAdd$2(this, _schemaTables$1, void 0);
1794
+ __privateSet$2(this, _schemaTables$1, schemaTables);
1558
1795
  }
1559
1796
  build(pluginOptions) {
1560
- const links = this.links;
1561
1797
  const db = new Proxy({}, {
1562
1798
  get: (_target, table) => {
1563
1799
  if (!isString(table))
1564
1800
  throw new Error("Invalid table name");
1565
- if (!__privateGet$1(this, _tables)[table]) {
1566
- __privateGet$1(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, links });
1801
+ if (__privateGet$2(this, _tables)[table] === void 0) {
1802
+ __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1567
1803
  }
1568
- return __privateGet$1(this, _tables)[table];
1804
+ return __privateGet$2(this, _tables)[table];
1569
1805
  }
1570
1806
  });
1571
- for (const table of this.tableNames ?? []) {
1572
- db[table] = new RestRepository({ db, pluginOptions, table, links });
1807
+ const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
1808
+ for (const table of tableNames) {
1809
+ db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
1573
1810
  }
1574
1811
  return db;
1575
1812
  }
1576
1813
  }
1577
1814
  _tables = new WeakMap();
1815
+ _schemaTables$1 = new WeakMap();
1578
1816
 
1579
1817
  var __accessCheck$1 = (obj, member, msg) => {
1580
1818
  if (!member.has(obj))
1581
1819
  throw TypeError("Cannot " + msg);
1582
1820
  };
1821
+ var __privateGet$1 = (obj, member, getter) => {
1822
+ __accessCheck$1(obj, member, "read from private field");
1823
+ return getter ? getter.call(obj) : member.get(obj);
1824
+ };
1583
1825
  var __privateAdd$1 = (obj, member, value) => {
1584
1826
  if (member.has(obj))
1585
1827
  throw TypeError("Cannot add the same private member more than once");
1586
1828
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1587
1829
  };
1830
+ var __privateSet$1 = (obj, member, value, setter) => {
1831
+ __accessCheck$1(obj, member, "write to private field");
1832
+ setter ? setter.call(obj, value) : member.set(obj, value);
1833
+ return value;
1834
+ };
1588
1835
  var __privateMethod$1 = (obj, member, method) => {
1589
1836
  __accessCheck$1(obj, member, "access private method");
1590
1837
  return method;
1591
1838
  };
1592
- var _search, search_fn;
1839
+ var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
1593
1840
  class SearchPlugin extends XataPlugin {
1594
- constructor(db, links) {
1841
+ constructor(db, schemaTables) {
1595
1842
  super();
1596
1843
  this.db = db;
1597
- this.links = links;
1598
1844
  __privateAdd$1(this, _search);
1845
+ __privateAdd$1(this, _getSchemaTables);
1846
+ __privateAdd$1(this, _schemaTables, void 0);
1847
+ __privateSet$1(this, _schemaTables, schemaTables);
1599
1848
  }
1600
1849
  build({ getFetchProps }) {
1601
1850
  return {
1602
1851
  all: async (query, options = {}) => {
1603
1852
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1853
+ const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
1604
1854
  return records.map((record) => {
1605
1855
  const { table = "orphan" } = record.xata;
1606
- return { table, record: initObject(this.db, this.links, table, record) };
1856
+ return { table, record: initObject(this.db, schemaTables, table, record) };
1607
1857
  });
1608
1858
  },
1609
1859
  byTable: async (query, options = {}) => {
1610
1860
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1861
+ const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
1611
1862
  return records.reduce((acc, record) => {
1612
1863
  const { table = "orphan" } = record.xata;
1613
1864
  const items = acc[table] ?? [];
1614
- const item = initObject(this.db, this.links, table, record);
1865
+ const item = initObject(this.db, schemaTables, table, record);
1615
1866
  return { ...acc, [table]: [...items, item] };
1616
1867
  }, {});
1617
1868
  }
1618
1869
  };
1619
1870
  }
1620
1871
  }
1872
+ _schemaTables = new WeakMap();
1621
1873
  _search = new WeakSet();
1622
1874
  search_fn = async function(query, options, getFetchProps) {
1623
1875
  const fetchProps = await getFetchProps();
1624
- const { tables, fuzziness } = options ?? {};
1876
+ const { tables, fuzziness, highlight } = options ?? {};
1625
1877
  const { records } = await searchBranch({
1626
1878
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1627
- body: { tables, query, fuzziness },
1879
+ body: { tables, query, fuzziness, highlight },
1628
1880
  ...fetchProps
1629
1881
  });
1630
1882
  return records;
1631
1883
  };
1884
+ _getSchemaTables = new WeakSet();
1885
+ getSchemaTables_fn = async function(getFetchProps) {
1886
+ if (__privateGet$1(this, _schemaTables))
1887
+ return __privateGet$1(this, _schemaTables);
1888
+ const fetchProps = await getFetchProps();
1889
+ const { schema } = await getBranchDetails({
1890
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1891
+ ...fetchProps
1892
+ });
1893
+ __privateSet$1(this, _schemaTables, schema.tables);
1894
+ return schema.tables;
1895
+ };
1632
1896
 
1633
1897
  const isBranchStrategyBuilder = (strategy) => {
1634
1898
  return typeof strategy === "function";
1635
1899
  };
1636
1900
 
1637
- const envBranchNames = [
1638
- "XATA_BRANCH",
1639
- "VERCEL_GIT_COMMIT_REF",
1640
- "CF_PAGES_BRANCH",
1641
- "BRANCH"
1642
- ];
1643
- const defaultBranch = "main";
1644
1901
  async function getCurrentBranchName(options) {
1645
- const env = await getBranchByEnvVariable();
1646
- if (env)
1647
- return env;
1648
- const branch = await getGitBranch();
1649
- if (!branch)
1650
- return defaultBranch;
1651
- const details = await getDatabaseBranch(branch, options);
1652
- if (details)
1653
- return branch;
1654
- return defaultBranch;
1902
+ const { branch } = getEnvironment();
1903
+ if (branch) {
1904
+ const details = await getDatabaseBranch(branch, options);
1905
+ if (details)
1906
+ return branch;
1907
+ console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
1908
+ }
1909
+ const gitBranch = await getGitBranch();
1910
+ return resolveXataBranch(gitBranch, options);
1655
1911
  }
1656
1912
  async function getCurrentBranchDetails(options) {
1657
- const env = await getBranchByEnvVariable();
1658
- if (env)
1659
- return getDatabaseBranch(env, options);
1660
- const branch = await getGitBranch();
1661
- if (!branch)
1662
- return getDatabaseBranch(defaultBranch, options);
1663
- const details = await getDatabaseBranch(branch, options);
1664
- if (details)
1665
- return details;
1666
- return getDatabaseBranch(defaultBranch, options);
1913
+ const branch = await getCurrentBranchName(options);
1914
+ return getDatabaseBranch(branch, options);
1915
+ }
1916
+ async function resolveXataBranch(gitBranch, options) {
1917
+ const databaseURL = options?.databaseURL || getDatabaseURL();
1918
+ const apiKey = options?.apiKey || getAPIKey();
1919
+ if (!databaseURL)
1920
+ throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
1921
+ if (!apiKey)
1922
+ throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1923
+ const [protocol, , host, , dbName] = databaseURL.split("/");
1924
+ const [workspace] = host.split(".");
1925
+ const { fallbackBranch } = getEnvironment();
1926
+ const { branch } = await resolveBranch({
1927
+ apiKey,
1928
+ apiUrl: databaseURL,
1929
+ fetchImpl: getFetchImplementation(options?.fetchImpl),
1930
+ workspacesApiUrl: `${protocol}//${host}`,
1931
+ pathParams: { dbName, workspace },
1932
+ queryParams: { gitBranch, fallbackBranch }
1933
+ });
1934
+ return branch;
1667
1935
  }
1668
1936
  async function getDatabaseBranch(branch, options) {
1669
1937
  const databaseURL = options?.databaseURL || getDatabaseURL();
@@ -1681,10 +1949,7 @@ async function getDatabaseBranch(branch, options) {
1681
1949
  apiUrl: databaseURL,
1682
1950
  fetchImpl: getFetchImplementation(options?.fetchImpl),
1683
1951
  workspacesApiUrl: `${protocol}//${host}`,
1684
- pathParams: {
1685
- dbBranchName,
1686
- workspace
1687
- }
1952
+ pathParams: { dbBranchName, workspace }
1688
1953
  });
1689
1954
  } catch (err) {
1690
1955
  if (isObject(err) && err.status === 404)
@@ -1692,21 +1957,10 @@ async function getDatabaseBranch(branch, options) {
1692
1957
  throw err;
1693
1958
  }
1694
1959
  }
1695
- function getBranchByEnvVariable() {
1696
- for (const name of envBranchNames) {
1697
- const value = getEnvVariable(name);
1698
- if (value) {
1699
- return value;
1700
- }
1701
- }
1702
- try {
1703
- return XATA_BRANCH;
1704
- } catch (err) {
1705
- }
1706
- }
1707
1960
  function getDatabaseURL() {
1708
1961
  try {
1709
- return getEnvVariable("XATA_DATABASE_URL") ?? XATA_DATABASE_URL;
1962
+ const { databaseURL } = getEnvironment();
1963
+ return databaseURL;
1710
1964
  } catch (err) {
1711
1965
  return void 0;
1712
1966
  }
@@ -1737,7 +1991,7 @@ var __privateMethod = (obj, member, method) => {
1737
1991
  const buildClient = (plugins) => {
1738
1992
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1739
1993
  return _a = class {
1740
- constructor(options = {}, links, tables) {
1994
+ constructor(options = {}, schemaTables) {
1741
1995
  __privateAdd(this, _parseOptions);
1742
1996
  __privateAdd(this, _getFetchProps);
1743
1997
  __privateAdd(this, _evaluateBranch);
@@ -1747,12 +2001,12 @@ const buildClient = (plugins) => {
1747
2001
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1748
2002
  cache: safeOptions.cache
1749
2003
  };
1750
- const db = new SchemaPlugin(links, tables).build(pluginOptions);
1751
- const search = new SearchPlugin(db, links ?? {}).build(pluginOptions);
2004
+ const db = new SchemaPlugin(schemaTables).build(pluginOptions);
2005
+ const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
1752
2006
  this.db = db;
1753
2007
  this.search = search;
1754
2008
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
1755
- if (!namespace)
2009
+ if (namespace === void 0)
1756
2010
  continue;
1757
2011
  const result = namespace.build(pluginOptions);
1758
2012
  if (result instanceof Promise) {
@@ -1769,7 +2023,7 @@ const buildClient = (plugins) => {
1769
2023
  const databaseURL = options?.databaseURL || getDatabaseURL();
1770
2024
  const apiKey = options?.apiKey || getAPIKey();
1771
2025
  const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1772
- const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
2026
+ const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1773
2027
  if (!databaseURL || !apiKey) {
1774
2028
  throw new Error("Options databaseURL and apiKey are required");
1775
2029
  }
@@ -1796,7 +2050,7 @@ const buildClient = (plugins) => {
1796
2050
  }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
1797
2051
  if (__privateGet(this, _branch))
1798
2052
  return __privateGet(this, _branch);
1799
- if (!param)
2053
+ if (param === void 0)
1800
2054
  return void 0;
1801
2055
  const strategies = Array.isArray(param) ? [...param] : [param];
1802
2056
  const evaluateBranch = async (strategy) => {
@@ -1829,6 +2083,7 @@ exports.PAGINATION_MAX_OFFSET = PAGINATION_MAX_OFFSET;
1829
2083
  exports.PAGINATION_MAX_SIZE = PAGINATION_MAX_SIZE;
1830
2084
  exports.Page = Page;
1831
2085
  exports.Query = Query;
2086
+ exports.RecordArray = RecordArray;
1832
2087
  exports.Repository = Repository;
1833
2088
  exports.RestRepository = RestRepository;
1834
2089
  exports.SchemaPlugin = SchemaPlugin;
@@ -1839,11 +2094,11 @@ exports.XataApiPlugin = XataApiPlugin;
1839
2094
  exports.XataError = XataError;
1840
2095
  exports.XataPlugin = XataPlugin;
1841
2096
  exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
2097
+ exports.addGitBranchesEntry = addGitBranchesEntry;
1842
2098
  exports.addTableColumn = addTableColumn;
1843
2099
  exports.buildClient = buildClient;
1844
2100
  exports.bulkInsertTableRecords = bulkInsertTableRecords;
1845
2101
  exports.cancelWorkspaceMemberInvite = cancelWorkspaceMemberInvite;
1846
- exports.compareBranchSchema = compareBranchSchema;
1847
2102
  exports.contains = contains;
1848
2103
  exports.createBranch = createBranch;
1849
2104
  exports.createDatabase = createDatabase;
@@ -1868,13 +2123,13 @@ exports.getBranchList = getBranchList;
1868
2123
  exports.getBranchMetadata = getBranchMetadata;
1869
2124
  exports.getBranchMigrationHistory = getBranchMigrationHistory;
1870
2125
  exports.getBranchMigrationPlan = getBranchMigrationPlan;
1871
- exports.getBranchSchemaHistory = getBranchSchemaHistory;
1872
2126
  exports.getBranchStats = getBranchStats;
1873
2127
  exports.getColumn = getColumn;
1874
2128
  exports.getCurrentBranchDetails = getCurrentBranchDetails;
1875
2129
  exports.getCurrentBranchName = getCurrentBranchName;
1876
2130
  exports.getDatabaseList = getDatabaseList;
1877
2131
  exports.getDatabaseURL = getDatabaseURL;
2132
+ exports.getGitBranchesMapping = getGitBranchesMapping;
1878
2133
  exports.getRecord = getRecord;
1879
2134
  exports.getTableColumns = getTableColumns;
1880
2135
  exports.getTableSchema = getTableSchema;
@@ -1893,6 +2148,7 @@ exports.insertRecord = insertRecord;
1893
2148
  exports.insertRecordWithID = insertRecordWithID;
1894
2149
  exports.inviteWorkspaceMember = inviteWorkspaceMember;
1895
2150
  exports.is = is;
2151
+ exports.isCursorPaginationOptions = isCursorPaginationOptions;
1896
2152
  exports.isIdentifiable = isIdentifiable;
1897
2153
  exports.isNot = isNot;
1898
2154
  exports.isXataRecord = isXataRecord;
@@ -1903,13 +2159,15 @@ exports.notExists = notExists;
1903
2159
  exports.operationsByTag = operationsByTag;
1904
2160
  exports.pattern = pattern;
1905
2161
  exports.queryTable = queryTable;
2162
+ exports.removeGitBranchesEntry = removeGitBranchesEntry;
1906
2163
  exports.removeWorkspaceMember = removeWorkspaceMember;
1907
2164
  exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
2165
+ exports.resolveBranch = resolveBranch;
1908
2166
  exports.searchBranch = searchBranch;
2167
+ exports.searchTable = searchTable;
1909
2168
  exports.setTableSchema = setTableSchema;
1910
2169
  exports.startsWith = startsWith;
1911
2170
  exports.updateBranchMetadata = updateBranchMetadata;
1912
- exports.updateBranchSchema = updateBranchSchema;
1913
2171
  exports.updateColumn = updateColumn;
1914
2172
  exports.updateRecordWithID = updateRecordWithID;
1915
2173
  exports.updateTable = updateTable;