@xata.io/client 0.13.4 → 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/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @xata.io/client
2
2
 
3
+ ## 0.14.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#409](https://github.com/xataio/client-ts/pull/409) [`8812380`](https://github.com/xataio/client-ts/commit/881238062b5eeac2dc8b9ba156720e0acc22c5c5) Thanks [@SferaDev](https://github.com/SferaDev)! - Infer types from schema in codegen
8
+
9
+ * [#457](https://github.com/xataio/client-ts/pull/457) [`0584a5b`](https://github.com/xataio/client-ts/commit/0584a5b207a21dbc36ddc1d44b276f1d5bb60dc5) Thanks [@SferaDev](https://github.com/SferaDev)! - Load env variables so that code analysis detects them
10
+
11
+ - [#469](https://github.com/xataio/client-ts/pull/469) [`8d8a912`](https://github.com/xataio/client-ts/commit/8d8a9129e36452266c4c12fe35b421f66e572498) Thanks [@gimenete](https://github.com/gimenete)! - Treat branch name specified with third party env variables as git branches in the resolution algorithm
12
+
13
+ ### Patch Changes
14
+
15
+ - [#462](https://github.com/xataio/client-ts/pull/462) [`7547b7e`](https://github.com/xataio/client-ts/commit/7547b7edbc9a95c6620784cc5348316f27502c73) Thanks [@SferaDev](https://github.com/SferaDev)! - Fix bug with RecordArray.map
16
+
17
+ * [#472](https://github.com/xataio/client-ts/pull/472) [`e99010c`](https://github.com/xataio/client-ts/commit/e99010c9ab9d355abadcfbcf98b5a3fcc80c307a) Thanks [@SferaDev](https://github.com/SferaDev)! - Add id as entity property
18
+
19
+ - [#443](https://github.com/xataio/client-ts/pull/443) [`c4be404`](https://github.com/xataio/client-ts/commit/c4be404a3ecb34df9b1ef4501c92f5bdc221f19c) Thanks [@SferaDev](https://github.com/SferaDev)! - Improve performance with `create([])` operation
20
+
3
21
  ## 0.13.4
4
22
 
5
23
  ### Patch Changes
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.4";
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,10 @@ 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(..._RecordArray.parseConstructorParams(page, overrideRecords));
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: [] });
1001
1066
  }
1002
1067
  static parseConstructorParams(...args) {
1003
1068
  if (args.length === 1 && typeof args[0] === "number") {
@@ -1009,6 +1074,12 @@ const _RecordArray = class extends Array {
1009
1074
  }
1010
1075
  return new Array(...args);
1011
1076
  }
1077
+ toArray() {
1078
+ return new Array(...this);
1079
+ }
1080
+ map(callbackfn, thisArg) {
1081
+ return this.toArray().map(callbackfn, thisArg);
1082
+ }
1012
1083
  async nextPage(size, offset) {
1013
1084
  const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
1014
1085
  return new _RecordArray(newPage);
@@ -1045,7 +1116,7 @@ var __privateAdd$5 = (obj, member, value) => {
1045
1116
  throw TypeError("Cannot add the same private member more than once");
1046
1117
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1047
1118
  };
1048
- var __privateSet$4 = (obj, member, value, setter) => {
1119
+ var __privateSet$5 = (obj, member, value, setter) => {
1049
1120
  __accessCheck$5(obj, member, "write to private field");
1050
1121
  setter ? setter.call(obj, value) : member.set(obj, value);
1051
1122
  return value;
@@ -1058,11 +1129,11 @@ const _Query = class {
1058
1129
  __privateAdd$5(this, _data, { filter: {} });
1059
1130
  this.meta = { page: { cursor: "start", more: true } };
1060
1131
  this.records = new RecordArray(this, []);
1061
- __privateSet$4(this, _table$1, table);
1132
+ __privateSet$5(this, _table$1, table);
1062
1133
  if (repository) {
1063
- __privateSet$4(this, _repository, repository);
1134
+ __privateSet$5(this, _repository, repository);
1064
1135
  } else {
1065
- __privateSet$4(this, _repository, this);
1136
+ __privateSet$5(this, _repository, this);
1066
1137
  }
1067
1138
  const parent = cleanParent(data, rawParent);
1068
1139
  __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
@@ -1239,7 +1310,7 @@ var __privateAdd$4 = (obj, member, value) => {
1239
1310
  throw TypeError("Cannot add the same private member more than once");
1240
1311
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1241
1312
  };
1242
- var __privateSet$3 = (obj, member, value, setter) => {
1313
+ var __privateSet$4 = (obj, member, value, setter) => {
1243
1314
  __accessCheck$4(obj, member, "write to private field");
1244
1315
  setter ? setter.call(obj, value) : member.set(obj, value);
1245
1316
  return value;
@@ -1248,7 +1319,7 @@ var __privateMethod$2 = (obj, member, method) => {
1248
1319
  __accessCheck$4(obj, member, "access private method");
1249
1320
  return method;
1250
1321
  };
1251
- var _table, _getFetchProps, _cache, _schema$1, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _invalidateCache, invalidateCache_fn, _setCacheRecord, setCacheRecord_fn, _getCacheRecord, getCacheRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn, _getSchema$1, getSchema_fn$1;
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;
1252
1323
  class Repository extends Query {
1253
1324
  }
1254
1325
  class RestRepository extends Query {
@@ -1265,43 +1336,24 @@ class RestRepository extends Query {
1265
1336
  __privateAdd$4(this, _getCacheRecord);
1266
1337
  __privateAdd$4(this, _setCacheQuery);
1267
1338
  __privateAdd$4(this, _getCacheQuery);
1268
- __privateAdd$4(this, _getSchema$1);
1339
+ __privateAdd$4(this, _getSchemaTables$1);
1269
1340
  __privateAdd$4(this, _table, void 0);
1270
1341
  __privateAdd$4(this, _getFetchProps, void 0);
1271
1342
  __privateAdd$4(this, _cache, void 0);
1272
- __privateAdd$4(this, _schema$1, void 0);
1273
- __privateSet$3(this, _table, options.table);
1274
- __privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
1343
+ __privateAdd$4(this, _schemaTables$2, void 0);
1344
+ __privateSet$4(this, _table, options.table);
1345
+ __privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
1275
1346
  this.db = options.db;
1276
- __privateSet$3(this, _cache, options.pluginOptions.cache);
1347
+ __privateSet$4(this, _cache, options.pluginOptions.cache);
1348
+ __privateSet$4(this, _schemaTables$2, options.schemaTables);
1277
1349
  }
1278
1350
  async create(a, b) {
1279
1351
  if (Array.isArray(a)) {
1280
1352
  if (a.length === 0)
1281
1353
  return [];
1282
- const [itemsWithoutIds, itemsWithIds, order] = a.reduce(([accWithoutIds, accWithIds, accOrder], item) => {
1283
- const condition = isString(item.id);
1284
- accOrder.push(condition);
1285
- if (condition) {
1286
- accWithIds.push(item);
1287
- } else {
1288
- accWithoutIds.push(item);
1289
- }
1290
- return [accWithoutIds, accWithIds, accOrder];
1291
- }, [[], [], []]);
1292
- const recordsWithoutId = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, itemsWithoutIds);
1293
- await Promise.all(recordsWithoutId.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1294
- if (itemsWithIds.length > 100) {
1295
- console.warn("Bulk create operation with id is not optimized in the Xata API yet, this request might be slow");
1296
- }
1297
- const recordsWithId = await Promise.all(itemsWithIds.map((object) => this.create(object)));
1298
- return order.map((condition) => {
1299
- if (condition) {
1300
- return recordsWithId.shift();
1301
- } else {
1302
- return recordsWithoutId.shift();
1303
- }
1304
- }).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;
1305
1357
  }
1306
1358
  if (isString(a) && isObject(b)) {
1307
1359
  if (a === "")
@@ -1342,8 +1394,8 @@ class RestRepository extends Query {
1342
1394
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
1343
1395
  ...fetchProps
1344
1396
  });
1345
- const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1346
- 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);
1347
1399
  } catch (e) {
1348
1400
  if (isObject(e) && e.status === 404) {
1349
1401
  return null;
@@ -1432,8 +1484,8 @@ class RestRepository extends Query {
1432
1484
  },
1433
1485
  ...fetchProps
1434
1486
  });
1435
- const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1436
- 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));
1437
1489
  }
1438
1490
  async query(query) {
1439
1491
  const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
@@ -1452,8 +1504,8 @@ class RestRepository extends Query {
1452
1504
  body,
1453
1505
  ...fetchProps
1454
1506
  });
1455
- const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1456
- 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));
1457
1509
  await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1458
1510
  return new Page(query, meta, records);
1459
1511
  }
@@ -1461,7 +1513,7 @@ class RestRepository extends Query {
1461
1513
  _table = new WeakMap();
1462
1514
  _getFetchProps = new WeakMap();
1463
1515
  _cache = new WeakMap();
1464
- _schema$1 = new WeakMap();
1516
+ _schemaTables$2 = new WeakMap();
1465
1517
  _insertRecordWithoutId = new WeakSet();
1466
1518
  insertRecordWithoutId_fn = async function(object) {
1467
1519
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
@@ -1506,16 +1558,20 @@ _bulkInsertTableRecords = new WeakSet();
1506
1558
  bulkInsertTableRecords_fn = async function(objects) {
1507
1559
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1508
1560
  const records = objects.map((object) => transformObjectLinks(object));
1509
- const response = await bulkInsertTableRecords({
1561
+ const { recordIDs } = await bulkInsertTableRecords({
1510
1562
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1511
1563
  body: { records },
1512
1564
  ...fetchProps
1513
1565
  });
1514
- const finalObjects = await this.read(response.recordIDs);
1566
+ const finalObjects = await this.read(recordIDs);
1515
1567
  if (finalObjects.length !== objects.length) {
1516
1568
  throw new Error("The server failed to save some records");
1517
1569
  }
1518
- 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]);
1519
1575
  };
1520
1576
  _updateRecordWithID = new WeakSet();
1521
1577
  updateRecordWithID_fn = async function(recordId, object) {
@@ -1591,17 +1647,17 @@ getCacheQuery_fn = async function(query) {
1591
1647
  const hasExpired = result.date.getTime() + ttl < Date.now();
1592
1648
  return hasExpired ? null : result;
1593
1649
  };
1594
- _getSchema$1 = new WeakSet();
1595
- getSchema_fn$1 = async function() {
1596
- if (__privateGet$4(this, _schema$1))
1597
- 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);
1598
1654
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1599
1655
  const { schema } = await getBranchDetails({
1600
1656
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1601
1657
  ...fetchProps
1602
1658
  });
1603
- __privateSet$3(this, _schema$1, schema);
1604
- return schema;
1659
+ __privateSet$4(this, _schemaTables$2, schema.tables);
1660
+ return schema.tables;
1605
1661
  };
1606
1662
  const transformObjectLinks = (object) => {
1607
1663
  return Object.entries(object).reduce((acc, [key, value]) => {
@@ -1610,11 +1666,11 @@ const transformObjectLinks = (object) => {
1610
1666
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1611
1667
  }, {});
1612
1668
  };
1613
- const initObject = (db, schema, table, object) => {
1669
+ const initObject = (db, schemaTables, table, object) => {
1614
1670
  const result = {};
1615
1671
  const { xata, ...rest } = object ?? {};
1616
1672
  Object.assign(result, rest);
1617
- const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
1673
+ const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
1618
1674
  if (!columns)
1619
1675
  console.error(`Table ${table} not found in schema`);
1620
1676
  for (const column of columns ?? []) {
@@ -1634,7 +1690,7 @@ const initObject = (db, schema, table, object) => {
1634
1690
  if (!linkTable) {
1635
1691
  console.error(`Failed to parse link for field ${column.name}`);
1636
1692
  } else if (isObject(value)) {
1637
- result[column.name] = initObject(db, schema, linkTable, value);
1693
+ result[column.name] = initObject(db, schemaTables, linkTable, value);
1638
1694
  }
1639
1695
  break;
1640
1696
  }
@@ -1681,7 +1737,7 @@ var __privateAdd$3 = (obj, member, value) => {
1681
1737
  throw TypeError("Cannot add the same private member more than once");
1682
1738
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1683
1739
  };
1684
- var __privateSet$2 = (obj, member, value, setter) => {
1740
+ var __privateSet$3 = (obj, member, value, setter) => {
1685
1741
  __accessCheck$3(obj, member, "write to private field");
1686
1742
  setter ? setter.call(obj, value) : member.set(obj, value);
1687
1743
  return value;
@@ -1690,7 +1746,7 @@ var _map;
1690
1746
  class SimpleCache {
1691
1747
  constructor(options = {}) {
1692
1748
  __privateAdd$3(this, _map, void 0);
1693
- __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1749
+ __privateSet$3(this, _map, /* @__PURE__ */ new Map());
1694
1750
  this.capacity = options.max ?? 500;
1695
1751
  this.cacheRecords = options.cacheRecords ?? true;
1696
1752
  this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
@@ -1750,12 +1806,18 @@ var __privateAdd$2 = (obj, member, value) => {
1750
1806
  throw TypeError("Cannot add the same private member more than once");
1751
1807
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1752
1808
  };
1753
- 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;
1754
1815
  class SchemaPlugin extends XataPlugin {
1755
- constructor(tableNames) {
1816
+ constructor(schemaTables) {
1756
1817
  super();
1757
- this.tableNames = tableNames;
1758
1818
  __privateAdd$2(this, _tables, {});
1819
+ __privateAdd$2(this, _schemaTables$1, void 0);
1820
+ __privateSet$2(this, _schemaTables$1, schemaTables);
1759
1821
  }
1760
1822
  build(pluginOptions) {
1761
1823
  const db = new Proxy({}, {
@@ -1763,18 +1825,20 @@ class SchemaPlugin extends XataPlugin {
1763
1825
  if (!isString(table))
1764
1826
  throw new Error("Invalid table name");
1765
1827
  if (__privateGet$2(this, _tables)[table] === void 0) {
1766
- __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) });
1767
1829
  }
1768
1830
  return __privateGet$2(this, _tables)[table];
1769
1831
  }
1770
1832
  });
1771
- for (const table of this.tableNames ?? []) {
1772
- 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) });
1773
1836
  }
1774
1837
  return db;
1775
1838
  }
1776
1839
  }
1777
1840
  _tables = new WeakMap();
1841
+ _schemaTables$1 = new WeakMap();
1778
1842
 
1779
1843
  var __accessCheck$1 = (obj, member, msg) => {
1780
1844
  if (!member.has(obj))
@@ -1798,39 +1862,40 @@ var __privateMethod$1 = (obj, member, method) => {
1798
1862
  __accessCheck$1(obj, member, "access private method");
1799
1863
  return method;
1800
1864
  };
1801
- var _schema, _search, search_fn, _getSchema, getSchema_fn;
1865
+ var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
1802
1866
  class SearchPlugin extends XataPlugin {
1803
- constructor(db) {
1867
+ constructor(db, schemaTables) {
1804
1868
  super();
1805
1869
  this.db = db;
1806
1870
  __privateAdd$1(this, _search);
1807
- __privateAdd$1(this, _getSchema);
1808
- __privateAdd$1(this, _schema, void 0);
1871
+ __privateAdd$1(this, _getSchemaTables);
1872
+ __privateAdd$1(this, _schemaTables, void 0);
1873
+ __privateSet$1(this, _schemaTables, schemaTables);
1809
1874
  }
1810
1875
  build({ getFetchProps }) {
1811
1876
  return {
1812
1877
  all: async (query, options = {}) => {
1813
1878
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1814
- 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);
1815
1880
  return records.map((record) => {
1816
1881
  const { table = "orphan" } = record.xata;
1817
- return { table, record: initObject(this.db, schema, table, record) };
1882
+ return { table, record: initObject(this.db, schemaTables, table, record) };
1818
1883
  });
1819
1884
  },
1820
1885
  byTable: async (query, options = {}) => {
1821
1886
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1822
- 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);
1823
1888
  return records.reduce((acc, record) => {
1824
1889
  const { table = "orphan" } = record.xata;
1825
1890
  const items = acc[table] ?? [];
1826
- const item = initObject(this.db, schema, table, record);
1891
+ const item = initObject(this.db, schemaTables, table, record);
1827
1892
  return { ...acc, [table]: [...items, item] };
1828
1893
  }, {});
1829
1894
  }
1830
1895
  };
1831
1896
  }
1832
1897
  }
1833
- _schema = new WeakMap();
1898
+ _schemaTables = new WeakMap();
1834
1899
  _search = new WeakSet();
1835
1900
  search_fn = async function(query, options, getFetchProps) {
1836
1901
  const fetchProps = await getFetchProps();
@@ -1842,38 +1907,32 @@ search_fn = async function(query, options, getFetchProps) {
1842
1907
  });
1843
1908
  return records;
1844
1909
  };
1845
- _getSchema = new WeakSet();
1846
- getSchema_fn = async function(getFetchProps) {
1847
- if (__privateGet$1(this, _schema))
1848
- 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);
1849
1914
  const fetchProps = await getFetchProps();
1850
1915
  const { schema } = await getBranchDetails({
1851
1916
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1852
1917
  ...fetchProps
1853
1918
  });
1854
- __privateSet$1(this, _schema, schema);
1855
- return schema;
1919
+ __privateSet$1(this, _schemaTables, schema.tables);
1920
+ return schema.tables;
1856
1921
  };
1857
1922
 
1858
1923
  const isBranchStrategyBuilder = (strategy) => {
1859
1924
  return typeof strategy === "function";
1860
1925
  };
1861
1926
 
1862
- const envBranchNames = [
1863
- "XATA_BRANCH",
1864
- "VERCEL_GIT_COMMIT_REF",
1865
- "CF_PAGES_BRANCH",
1866
- "BRANCH"
1867
- ];
1868
1927
  async function getCurrentBranchName(options) {
1869
- const env = getBranchByEnvVariable();
1870
- if (env) {
1871
- const details = await getDatabaseBranch(env, options);
1928
+ const { branch, envBranch } = getEnvironment();
1929
+ if (branch) {
1930
+ const details = await getDatabaseBranch(branch, options);
1872
1931
  if (details)
1873
- return env;
1874
- console.warn(`Branch ${env} not found in Xata. Ignoring...`);
1932
+ return branch;
1933
+ console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
1875
1934
  }
1876
- const gitBranch = await getGitBranch();
1935
+ const gitBranch = envBranch || await getGitBranch();
1877
1936
  return resolveXataBranch(gitBranch, options);
1878
1937
  }
1879
1938
  async function getCurrentBranchDetails(options) {
@@ -1889,13 +1948,14 @@ async function resolveXataBranch(gitBranch, options) {
1889
1948
  throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
1890
1949
  const [protocol, , host, , dbName] = databaseURL.split("/");
1891
1950
  const [workspace] = host.split(".");
1951
+ const { fallbackBranch } = getEnvironment();
1892
1952
  const { branch } = await resolveBranch({
1893
1953
  apiKey,
1894
1954
  apiUrl: databaseURL,
1895
1955
  fetchImpl: getFetchImplementation(options?.fetchImpl),
1896
1956
  workspacesApiUrl: `${protocol}//${host}`,
1897
1957
  pathParams: { dbName, workspace },
1898
- queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
1958
+ queryParams: { gitBranch, fallbackBranch }
1899
1959
  });
1900
1960
  return branch;
1901
1961
  }
@@ -1923,21 +1983,10 @@ async function getDatabaseBranch(branch, options) {
1923
1983
  throw err;
1924
1984
  }
1925
1985
  }
1926
- function getBranchByEnvVariable() {
1927
- for (const name of envBranchNames) {
1928
- const value = getEnvVariable(name);
1929
- if (value) {
1930
- return value;
1931
- }
1932
- }
1933
- try {
1934
- return XATA_BRANCH;
1935
- } catch (err) {
1936
- }
1937
- }
1938
1986
  function getDatabaseURL() {
1939
1987
  try {
1940
- return getEnvVariable("XATA_DATABASE_URL") ?? XATA_DATABASE_URL;
1988
+ const { databaseURL } = getEnvironment();
1989
+ return databaseURL;
1941
1990
  } catch (err) {
1942
1991
  return void 0;
1943
1992
  }
@@ -1968,7 +2017,7 @@ var __privateMethod = (obj, member, method) => {
1968
2017
  const buildClient = (plugins) => {
1969
2018
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1970
2019
  return _a = class {
1971
- constructor(options = {}, tables) {
2020
+ constructor(options = {}, schemaTables) {
1972
2021
  __privateAdd(this, _parseOptions);
1973
2022
  __privateAdd(this, _getFetchProps);
1974
2023
  __privateAdd(this, _evaluateBranch);
@@ -1978,8 +2027,8 @@ const buildClient = (plugins) => {
1978
2027
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1979
2028
  cache: safeOptions.cache
1980
2029
  };
1981
- const db = new SchemaPlugin(tables).build(pluginOptions);
1982
- const search = new SearchPlugin(db).build(pluginOptions);
2030
+ const db = new SchemaPlugin(schemaTables).build(pluginOptions);
2031
+ const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
1983
2032
  this.db = db;
1984
2033
  this.search = search;
1985
2034
  for (const [key, namespace] of Object.entries(plugins ?? {})) {