@xata.io/client 0.13.2 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2,6 +2,24 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ function _interopNamespace(e) {
6
+ if (e && e.__esModule) return e;
7
+ var n = Object.create(null);
8
+ if (e) {
9
+ Object.keys(e).forEach(function (k) {
10
+ if (k !== 'default') {
11
+ var d = Object.getOwnPropertyDescriptor(e, k);
12
+ Object.defineProperty(n, k, d.get ? d : {
13
+ enumerable: true,
14
+ get: function () { return e[k]; }
15
+ });
16
+ }
17
+ });
18
+ }
19
+ n["default"] = e;
20
+ return Object.freeze(n);
21
+ }
22
+
5
23
  function notEmpty(value) {
6
24
  return value !== null && value !== void 0;
7
25
  }
@@ -26,35 +44,81 @@ function toBase64(value) {
26
44
  }
27
45
  }
28
46
 
29
- function getEnvVariable(name) {
47
+ function getEnvironment() {
30
48
  try {
31
- if (isObject(process) && isString(process?.env?.[name])) {
32
- return process.env[name];
49
+ if (isObject(process) && isObject(process.env)) {
50
+ return {
51
+ apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
52
+ databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
53
+ branch: process.env.XATA_BRANCH ?? getGlobalBranch(),
54
+ envBranch: process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH,
55
+ fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
56
+ };
33
57
  }
34
58
  } catch (err) {
35
59
  }
36
60
  try {
37
- if (isObject(Deno) && isString(Deno?.env?.get(name))) {
38
- return Deno.env.get(name);
61
+ if (isObject(Deno) && isObject(Deno.env)) {
62
+ return {
63
+ apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
64
+ databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
65
+ branch: Deno.env.get("XATA_BRANCH") ?? getGlobalBranch(),
66
+ envBranch: Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH"),
67
+ fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
68
+ };
39
69
  }
40
70
  } catch (err) {
41
71
  }
72
+ return {
73
+ apiKey: getGlobalApiKey(),
74
+ databaseURL: getGlobalDatabaseURL(),
75
+ branch: getGlobalBranch(),
76
+ envBranch: void 0,
77
+ fallbackBranch: getGlobalFallbackBranch()
78
+ };
79
+ }
80
+ function getGlobalApiKey() {
81
+ try {
82
+ return XATA_API_KEY;
83
+ } catch (err) {
84
+ return void 0;
85
+ }
86
+ }
87
+ function getGlobalDatabaseURL() {
88
+ try {
89
+ return XATA_DATABASE_URL;
90
+ } catch (err) {
91
+ return void 0;
92
+ }
93
+ }
94
+ function getGlobalBranch() {
95
+ try {
96
+ return XATA_BRANCH;
97
+ } catch (err) {
98
+ return void 0;
99
+ }
100
+ }
101
+ function getGlobalFallbackBranch() {
102
+ try {
103
+ return XATA_FALLBACK_BRANCH;
104
+ } catch (err) {
105
+ return void 0;
106
+ }
42
107
  }
43
108
  async function getGitBranch() {
109
+ const cmd = ["git", "branch", "--show-current"];
110
+ const nodeModule = ["child", "process"].join("_");
44
111
  try {
45
112
  if (typeof require === "function") {
46
- const req = require;
47
- return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
113
+ return require(nodeModule).execSync(cmd.join(" "), { encoding: "utf-8" }).trim();
48
114
  }
115
+ const { execSync } = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(nodeModule);
116
+ return execSync(cmd.join(" "), { encoding: "utf-8" }).toString().trim();
49
117
  } catch (err) {
50
118
  }
51
119
  try {
52
120
  if (isObject(Deno)) {
53
- const process2 = Deno.run({
54
- cmd: ["git", "branch", "--show-current"],
55
- stdout: "piped",
56
- stderr: "piped"
57
- });
121
+ const process2 = Deno.run({ cmd, stdout: "piped", stderr: "piped" });
58
122
  return new TextDecoder().decode(await process2.output()).trim();
59
123
  }
60
124
  } catch (err) {
@@ -63,7 +127,8 @@ async function getGitBranch() {
63
127
 
64
128
  function getAPIKey() {
65
129
  try {
66
- return getEnvVariable("XATA_API_KEY") ?? XATA_API_KEY;
130
+ const { apiKey } = getEnvironment();
131
+ return apiKey;
67
132
  } catch (err) {
68
133
  return void 0;
69
134
  }
@@ -78,7 +143,7 @@ function getFetchImplementation(userFetch) {
78
143
  return fetchImpl;
79
144
  }
80
145
 
81
- const VERSION = "0.13.2";
146
+ const VERSION = "0.14.0";
82
147
 
83
148
  class ErrorWithCause extends Error {
84
149
  constructor(message, options) {
@@ -504,7 +569,7 @@ var __privateAdd$7 = (obj, member, value) => {
504
569
  throw TypeError("Cannot add the same private member more than once");
505
570
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
506
571
  };
507
- var __privateSet$6 = (obj, member, value, setter) => {
572
+ var __privateSet$7 = (obj, member, value, setter) => {
508
573
  __accessCheck$7(obj, member, "write to private field");
509
574
  setter ? setter.call(obj, value) : member.set(obj, value);
510
575
  return value;
@@ -519,7 +584,7 @@ class XataApiClient {
519
584
  if (!apiKey) {
520
585
  throw new Error("Could not resolve a valid apiKey");
521
586
  }
522
- __privateSet$6(this, _extraProps, {
587
+ __privateSet$7(this, _extraProps, {
523
588
  apiUrl: getHostUrl(provider, "main"),
524
589
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
525
590
  fetchImpl: getFetchImplementation(options.fetch),
@@ -956,7 +1021,7 @@ var __privateAdd$6 = (obj, member, value) => {
956
1021
  throw TypeError("Cannot add the same private member more than once");
957
1022
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
958
1023
  };
959
- var __privateSet$5 = (obj, member, value, setter) => {
1024
+ var __privateSet$6 = (obj, member, value, setter) => {
960
1025
  __accessCheck$6(obj, member, "write to private field");
961
1026
  setter ? setter.call(obj, value) : member.set(obj, value);
962
1027
  return value;
@@ -965,7 +1030,7 @@ var _query, _page;
965
1030
  class Page {
966
1031
  constructor(query, meta, records = []) {
967
1032
  __privateAdd$6(this, _query, void 0);
968
- __privateSet$5(this, _query, query);
1033
+ __privateSet$6(this, _query, query);
969
1034
  this.meta = meta;
970
1035
  this.records = new RecordArray(this, records);
971
1036
  }
@@ -994,10 +1059,26 @@ function isCursorPaginationOptions(options) {
994
1059
  return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
995
1060
  }
996
1061
  const _RecordArray = class extends Array {
997
- constructor(page, overrideRecords) {
998
- super(...overrideRecords ?? page.records);
1062
+ constructor(...args) {
1063
+ super(..._RecordArray.parseConstructorParams(...args));
999
1064
  __privateAdd$6(this, _page, void 0);
1000
- __privateSet$5(this, _page, page);
1065
+ __privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
1066
+ }
1067
+ static parseConstructorParams(...args) {
1068
+ if (args.length === 1 && typeof args[0] === "number") {
1069
+ return new Array(args[0]);
1070
+ }
1071
+ if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
1072
+ const result = args[1] ?? args[0].records ?? [];
1073
+ return new Array(...result);
1074
+ }
1075
+ return new Array(...args);
1076
+ }
1077
+ toArray() {
1078
+ return new Array(...this);
1079
+ }
1080
+ map(callbackfn, thisArg) {
1081
+ return this.toArray().map(callbackfn, thisArg);
1001
1082
  }
1002
1083
  async nextPage(size, offset) {
1003
1084
  const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
@@ -1035,7 +1116,7 @@ var __privateAdd$5 = (obj, member, value) => {
1035
1116
  throw TypeError("Cannot add the same private member more than once");
1036
1117
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1037
1118
  };
1038
- var __privateSet$4 = (obj, member, value, setter) => {
1119
+ var __privateSet$5 = (obj, member, value, setter) => {
1039
1120
  __accessCheck$5(obj, member, "write to private field");
1040
1121
  setter ? setter.call(obj, value) : member.set(obj, value);
1041
1122
  return value;
@@ -1048,11 +1129,11 @@ const _Query = class {
1048
1129
  __privateAdd$5(this, _data, { filter: {} });
1049
1130
  this.meta = { page: { cursor: "start", more: true } };
1050
1131
  this.records = new RecordArray(this, []);
1051
- __privateSet$4(this, _table$1, table);
1132
+ __privateSet$5(this, _table$1, table);
1052
1133
  if (repository) {
1053
- __privateSet$4(this, _repository, repository);
1134
+ __privateSet$5(this, _repository, repository);
1054
1135
  } else {
1055
- __privateSet$4(this, _repository, this);
1136
+ __privateSet$5(this, _repository, this);
1056
1137
  }
1057
1138
  const parent = cleanParent(data, rawParent);
1058
1139
  __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
@@ -1229,7 +1310,7 @@ var __privateAdd$4 = (obj, member, value) => {
1229
1310
  throw TypeError("Cannot add the same private member more than once");
1230
1311
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1231
1312
  };
1232
- var __privateSet$3 = (obj, member, value, setter) => {
1313
+ var __privateSet$4 = (obj, member, value, setter) => {
1233
1314
  __accessCheck$4(obj, member, "write to private field");
1234
1315
  setter ? setter.call(obj, value) : member.set(obj, value);
1235
1316
  return value;
@@ -1238,7 +1319,7 @@ var __privateMethod$2 = (obj, member, method) => {
1238
1319
  __accessCheck$4(obj, member, "access private method");
1239
1320
  return method;
1240
1321
  };
1241
- var _table, _getFetchProps, _cache, _schema$1, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _invalidateCache, invalidateCache_fn, _setCacheRecord, setCacheRecord_fn, _getCacheRecord, getCacheRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn, _getSchema$1, getSchema_fn$1;
1322
+ 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;
1242
1323
  class Repository extends Query {
1243
1324
  }
1244
1325
  class RestRepository extends Query {
@@ -1255,43 +1336,24 @@ class RestRepository extends Query {
1255
1336
  __privateAdd$4(this, _getCacheRecord);
1256
1337
  __privateAdd$4(this, _setCacheQuery);
1257
1338
  __privateAdd$4(this, _getCacheQuery);
1258
- __privateAdd$4(this, _getSchema$1);
1339
+ __privateAdd$4(this, _getSchemaTables$1);
1259
1340
  __privateAdd$4(this, _table, void 0);
1260
1341
  __privateAdd$4(this, _getFetchProps, void 0);
1261
1342
  __privateAdd$4(this, _cache, void 0);
1262
- __privateAdd$4(this, _schema$1, void 0);
1263
- __privateSet$3(this, _table, options.table);
1264
- __privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
1343
+ __privateAdd$4(this, _schemaTables$2, void 0);
1344
+ __privateSet$4(this, _table, options.table);
1345
+ __privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
1265
1346
  this.db = options.db;
1266
- __privateSet$3(this, _cache, options.pluginOptions.cache);
1347
+ __privateSet$4(this, _cache, options.pluginOptions.cache);
1348
+ __privateSet$4(this, _schemaTables$2, options.schemaTables);
1267
1349
  }
1268
1350
  async create(a, b) {
1269
1351
  if (Array.isArray(a)) {
1270
1352
  if (a.length === 0)
1271
1353
  return [];
1272
- const [itemsWithoutIds, itemsWithIds, order] = a.reduce(([accWithoutIds, accWithIds, accOrder], item) => {
1273
- const condition = isString(item.id);
1274
- accOrder.push(condition);
1275
- if (condition) {
1276
- accWithIds.push(item);
1277
- } else {
1278
- accWithoutIds.push(item);
1279
- }
1280
- return [accWithoutIds, accWithIds, accOrder];
1281
- }, [[], [], []]);
1282
- const recordsWithoutId = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, itemsWithoutIds);
1283
- await Promise.all(recordsWithoutId.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1284
- if (itemsWithIds.length > 100) {
1285
- console.warn("Bulk create operation with id is not optimized in the Xata API yet, this request might be slow");
1286
- }
1287
- const recordsWithId = await Promise.all(itemsWithIds.map((object) => this.create(object)));
1288
- return order.map((condition) => {
1289
- if (condition) {
1290
- return recordsWithId.shift();
1291
- } else {
1292
- return recordsWithoutId.shift();
1293
- }
1294
- }).filter((record) => !!record);
1354
+ const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1355
+ await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1356
+ return records;
1295
1357
  }
1296
1358
  if (isString(a) && isObject(b)) {
1297
1359
  if (a === "")
@@ -1332,8 +1394,8 @@ class RestRepository extends Query {
1332
1394
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
1333
1395
  ...fetchProps
1334
1396
  });
1335
- const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1336
- return initObject(this.db, schema, __privateGet$4(this, _table), response);
1397
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1398
+ return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
1337
1399
  } catch (e) {
1338
1400
  if (isObject(e) && e.status === 404) {
1339
1401
  return null;
@@ -1422,8 +1484,8 @@ class RestRepository extends Query {
1422
1484
  },
1423
1485
  ...fetchProps
1424
1486
  });
1425
- const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1426
- return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
1487
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1488
+ return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
1427
1489
  }
1428
1490
  async query(query) {
1429
1491
  const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
@@ -1442,8 +1504,8 @@ class RestRepository extends Query {
1442
1504
  body,
1443
1505
  ...fetchProps
1444
1506
  });
1445
- const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1446
- const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
1507
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1508
+ const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
1447
1509
  await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1448
1510
  return new Page(query, meta, records);
1449
1511
  }
@@ -1451,7 +1513,7 @@ class RestRepository extends Query {
1451
1513
  _table = new WeakMap();
1452
1514
  _getFetchProps = new WeakMap();
1453
1515
  _cache = new WeakMap();
1454
- _schema$1 = new WeakMap();
1516
+ _schemaTables$2 = new WeakMap();
1455
1517
  _insertRecordWithoutId = new WeakSet();
1456
1518
  insertRecordWithoutId_fn = async function(object) {
1457
1519
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
@@ -1496,16 +1558,20 @@ _bulkInsertTableRecords = new WeakSet();
1496
1558
  bulkInsertTableRecords_fn = async function(objects) {
1497
1559
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1498
1560
  const records = objects.map((object) => transformObjectLinks(object));
1499
- const response = await bulkInsertTableRecords({
1561
+ const { recordIDs } = await bulkInsertTableRecords({
1500
1562
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1501
1563
  body: { records },
1502
1564
  ...fetchProps
1503
1565
  });
1504
- const finalObjects = await this.read(response.recordIDs);
1566
+ const finalObjects = await this.read(recordIDs);
1505
1567
  if (finalObjects.length !== objects.length) {
1506
1568
  throw new Error("The server failed to save some records");
1507
1569
  }
1508
- return finalObjects;
1570
+ const dictionary = finalObjects.reduce((acc, object) => {
1571
+ acc[object.id] = object;
1572
+ return acc;
1573
+ }, {});
1574
+ return recordIDs.map((id) => dictionary[id]);
1509
1575
  };
1510
1576
  _updateRecordWithID = new WeakSet();
1511
1577
  updateRecordWithID_fn = async function(recordId, object) {
@@ -1581,17 +1647,17 @@ getCacheQuery_fn = async function(query) {
1581
1647
  const hasExpired = result.date.getTime() + ttl < Date.now();
1582
1648
  return hasExpired ? null : result;
1583
1649
  };
1584
- _getSchema$1 = new WeakSet();
1585
- getSchema_fn$1 = async function() {
1586
- if (__privateGet$4(this, _schema$1))
1587
- return __privateGet$4(this, _schema$1);
1650
+ _getSchemaTables$1 = new WeakSet();
1651
+ getSchemaTables_fn$1 = async function() {
1652
+ if (__privateGet$4(this, _schemaTables$2))
1653
+ return __privateGet$4(this, _schemaTables$2);
1588
1654
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1589
1655
  const { schema } = await getBranchDetails({
1590
1656
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1591
1657
  ...fetchProps
1592
1658
  });
1593
- __privateSet$3(this, _schema$1, schema);
1594
- return schema;
1659
+ __privateSet$4(this, _schemaTables$2, schema.tables);
1660
+ return schema.tables;
1595
1661
  };
1596
1662
  const transformObjectLinks = (object) => {
1597
1663
  return Object.entries(object).reduce((acc, [key, value]) => {
@@ -1600,11 +1666,11 @@ const transformObjectLinks = (object) => {
1600
1666
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1601
1667
  }, {});
1602
1668
  };
1603
- const initObject = (db, schema, table, object) => {
1669
+ const initObject = (db, schemaTables, table, object) => {
1604
1670
  const result = {};
1605
1671
  const { xata, ...rest } = object ?? {};
1606
1672
  Object.assign(result, rest);
1607
- const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
1673
+ const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
1608
1674
  if (!columns)
1609
1675
  console.error(`Table ${table} not found in schema`);
1610
1676
  for (const column of columns ?? []) {
@@ -1624,7 +1690,7 @@ const initObject = (db, schema, table, object) => {
1624
1690
  if (!linkTable) {
1625
1691
  console.error(`Failed to parse link for field ${column.name}`);
1626
1692
  } else if (isObject(value)) {
1627
- result[column.name] = initObject(db, schema, linkTable, value);
1693
+ result[column.name] = initObject(db, schemaTables, linkTable, value);
1628
1694
  }
1629
1695
  break;
1630
1696
  }
@@ -1671,7 +1737,7 @@ var __privateAdd$3 = (obj, member, value) => {
1671
1737
  throw TypeError("Cannot add the same private member more than once");
1672
1738
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1673
1739
  };
1674
- var __privateSet$2 = (obj, member, value, setter) => {
1740
+ var __privateSet$3 = (obj, member, value, setter) => {
1675
1741
  __accessCheck$3(obj, member, "write to private field");
1676
1742
  setter ? setter.call(obj, value) : member.set(obj, value);
1677
1743
  return value;
@@ -1680,7 +1746,7 @@ var _map;
1680
1746
  class SimpleCache {
1681
1747
  constructor(options = {}) {
1682
1748
  __privateAdd$3(this, _map, void 0);
1683
- __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1749
+ __privateSet$3(this, _map, /* @__PURE__ */ new Map());
1684
1750
  this.capacity = options.max ?? 500;
1685
1751
  this.cacheRecords = options.cacheRecords ?? true;
1686
1752
  this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
@@ -1740,12 +1806,18 @@ var __privateAdd$2 = (obj, member, value) => {
1740
1806
  throw TypeError("Cannot add the same private member more than once");
1741
1807
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1742
1808
  };
1743
- var _tables;
1809
+ var __privateSet$2 = (obj, member, value, setter) => {
1810
+ __accessCheck$2(obj, member, "write to private field");
1811
+ setter ? setter.call(obj, value) : member.set(obj, value);
1812
+ return value;
1813
+ };
1814
+ var _tables, _schemaTables$1;
1744
1815
  class SchemaPlugin extends XataPlugin {
1745
- constructor(tableNames) {
1816
+ constructor(schemaTables) {
1746
1817
  super();
1747
- this.tableNames = tableNames;
1748
1818
  __privateAdd$2(this, _tables, {});
1819
+ __privateAdd$2(this, _schemaTables$1, void 0);
1820
+ __privateSet$2(this, _schemaTables$1, schemaTables);
1749
1821
  }
1750
1822
  build(pluginOptions) {
1751
1823
  const db = new Proxy({}, {
@@ -1753,18 +1825,20 @@ class SchemaPlugin extends XataPlugin {
1753
1825
  if (!isString(table))
1754
1826
  throw new Error("Invalid table name");
1755
1827
  if (__privateGet$2(this, _tables)[table] === void 0) {
1756
- __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
1828
+ __privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
1757
1829
  }
1758
1830
  return __privateGet$2(this, _tables)[table];
1759
1831
  }
1760
1832
  });
1761
- for (const table of this.tableNames ?? []) {
1762
- db[table] = new RestRepository({ db, pluginOptions, table });
1833
+ const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
1834
+ for (const table of tableNames) {
1835
+ db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
1763
1836
  }
1764
1837
  return db;
1765
1838
  }
1766
1839
  }
1767
1840
  _tables = new WeakMap();
1841
+ _schemaTables$1 = new WeakMap();
1768
1842
 
1769
1843
  var __accessCheck$1 = (obj, member, msg) => {
1770
1844
  if (!member.has(obj))
@@ -1788,39 +1862,40 @@ var __privateMethod$1 = (obj, member, method) => {
1788
1862
  __accessCheck$1(obj, member, "access private method");
1789
1863
  return method;
1790
1864
  };
1791
- var _schema, _search, search_fn, _getSchema, getSchema_fn;
1865
+ var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
1792
1866
  class SearchPlugin extends XataPlugin {
1793
- constructor(db) {
1867
+ constructor(db, schemaTables) {
1794
1868
  super();
1795
1869
  this.db = db;
1796
1870
  __privateAdd$1(this, _search);
1797
- __privateAdd$1(this, _getSchema);
1798
- __privateAdd$1(this, _schema, void 0);
1871
+ __privateAdd$1(this, _getSchemaTables);
1872
+ __privateAdd$1(this, _schemaTables, void 0);
1873
+ __privateSet$1(this, _schemaTables, schemaTables);
1799
1874
  }
1800
1875
  build({ getFetchProps }) {
1801
1876
  return {
1802
1877
  all: async (query, options = {}) => {
1803
1878
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1804
- const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1879
+ const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
1805
1880
  return records.map((record) => {
1806
1881
  const { table = "orphan" } = record.xata;
1807
- return { table, record: initObject(this.db, schema, table, record) };
1882
+ return { table, record: initObject(this.db, schemaTables, table, record) };
1808
1883
  });
1809
1884
  },
1810
1885
  byTable: async (query, options = {}) => {
1811
1886
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1812
- const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1887
+ const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
1813
1888
  return records.reduce((acc, record) => {
1814
1889
  const { table = "orphan" } = record.xata;
1815
1890
  const items = acc[table] ?? [];
1816
- const item = initObject(this.db, schema, table, record);
1891
+ const item = initObject(this.db, schemaTables, table, record);
1817
1892
  return { ...acc, [table]: [...items, item] };
1818
1893
  }, {});
1819
1894
  }
1820
1895
  };
1821
1896
  }
1822
1897
  }
1823
- _schema = new WeakMap();
1898
+ _schemaTables = new WeakMap();
1824
1899
  _search = new WeakSet();
1825
1900
  search_fn = async function(query, options, getFetchProps) {
1826
1901
  const fetchProps = await getFetchProps();
@@ -1832,38 +1907,32 @@ search_fn = async function(query, options, getFetchProps) {
1832
1907
  });
1833
1908
  return records;
1834
1909
  };
1835
- _getSchema = new WeakSet();
1836
- getSchema_fn = async function(getFetchProps) {
1837
- if (__privateGet$1(this, _schema))
1838
- return __privateGet$1(this, _schema);
1910
+ _getSchemaTables = new WeakSet();
1911
+ getSchemaTables_fn = async function(getFetchProps) {
1912
+ if (__privateGet$1(this, _schemaTables))
1913
+ return __privateGet$1(this, _schemaTables);
1839
1914
  const fetchProps = await getFetchProps();
1840
1915
  const { schema } = await getBranchDetails({
1841
1916
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1842
1917
  ...fetchProps
1843
1918
  });
1844
- __privateSet$1(this, _schema, schema);
1845
- return schema;
1919
+ __privateSet$1(this, _schemaTables, schema.tables);
1920
+ return schema.tables;
1846
1921
  };
1847
1922
 
1848
1923
  const isBranchStrategyBuilder = (strategy) => {
1849
1924
  return typeof strategy === "function";
1850
1925
  };
1851
1926
 
1852
- const envBranchNames = [
1853
- "XATA_BRANCH",
1854
- "VERCEL_GIT_COMMIT_REF",
1855
- "CF_PAGES_BRANCH",
1856
- "BRANCH"
1857
- ];
1858
1927
  async function getCurrentBranchName(options) {
1859
- const env = getBranchByEnvVariable();
1860
- if (env) {
1861
- const details = await getDatabaseBranch(env, options);
1928
+ const { branch, envBranch } = getEnvironment();
1929
+ if (branch) {
1930
+ const details = await getDatabaseBranch(branch, options);
1862
1931
  if (details)
1863
- return env;
1864
- console.warn(`Branch ${env} not found in Xata. Ignoring...`);
1932
+ return branch;
1933
+ console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
1865
1934
  }
1866
- const gitBranch = await getGitBranch();
1935
+ const gitBranch = envBranch || await getGitBranch();
1867
1936
  return resolveXataBranch(gitBranch, options);
1868
1937
  }
1869
1938
  async function getCurrentBranchDetails(options) {
@@ -1879,13 +1948,14 @@ async function resolveXataBranch(gitBranch, options) {
1879
1948
  throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1880
1949
  const [protocol, , host, , dbName] = databaseURL.split("/");
1881
1950
  const [workspace] = host.split(".");
1951
+ const { fallbackBranch } = getEnvironment();
1882
1952
  const { branch } = await resolveBranch({
1883
1953
  apiKey,
1884
1954
  apiUrl: databaseURL,
1885
1955
  fetchImpl: getFetchImplementation(options?.fetchImpl),
1886
1956
  workspacesApiUrl: `${protocol}//${host}`,
1887
1957
  pathParams: { dbName, workspace },
1888
- queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
1958
+ queryParams: { gitBranch, fallbackBranch }
1889
1959
  });
1890
1960
  return branch;
1891
1961
  }
@@ -1913,21 +1983,10 @@ async function getDatabaseBranch(branch, options) {
1913
1983
  throw err;
1914
1984
  }
1915
1985
  }
1916
- function getBranchByEnvVariable() {
1917
- for (const name of envBranchNames) {
1918
- const value = getEnvVariable(name);
1919
- if (value) {
1920
- return value;
1921
- }
1922
- }
1923
- try {
1924
- return XATA_BRANCH;
1925
- } catch (err) {
1926
- }
1927
- }
1928
1986
  function getDatabaseURL() {
1929
1987
  try {
1930
- return getEnvVariable("XATA_DATABASE_URL") ?? XATA_DATABASE_URL;
1988
+ const { databaseURL } = getEnvironment();
1989
+ return databaseURL;
1931
1990
  } catch (err) {
1932
1991
  return void 0;
1933
1992
  }
@@ -1958,7 +2017,7 @@ var __privateMethod = (obj, member, method) => {
1958
2017
  const buildClient = (plugins) => {
1959
2018
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1960
2019
  return _a = class {
1961
- constructor(options = {}, tables) {
2020
+ constructor(options = {}, schemaTables) {
1962
2021
  __privateAdd(this, _parseOptions);
1963
2022
  __privateAdd(this, _getFetchProps);
1964
2023
  __privateAdd(this, _evaluateBranch);
@@ -1968,8 +2027,8 @@ const buildClient = (plugins) => {
1968
2027
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1969
2028
  cache: safeOptions.cache
1970
2029
  };
1971
- const db = new SchemaPlugin(tables).build(pluginOptions);
1972
- const search = new SearchPlugin(db).build(pluginOptions);
2030
+ const db = new SchemaPlugin(schemaTables).build(pluginOptions);
2031
+ const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
1973
2032
  this.db = db;
1974
2033
  this.search = search;
1975
2034
  for (const [key, namespace] of Object.entries(plugins ?? {})) {