@xata.io/client 0.0.0-alpha.vf45a2df → 0.0.0-alpha.vf481c73
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 +132 -90
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +69 -16
- package/dist/index.mjs +132 -90
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
@@ -26,19 +26,66 @@ function toBase64(value) {
|
|
26
26
|
}
|
27
27
|
}
|
28
28
|
|
29
|
-
function
|
29
|
+
function getEnvironment() {
|
30
30
|
try {
|
31
|
-
if (isObject(process) &&
|
32
|
-
return
|
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,
|
36
|
+
envBranch: process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH,
|
37
|
+
fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
|
38
|
+
};
|
33
39
|
}
|
34
40
|
} catch (err) {
|
35
41
|
}
|
36
42
|
try {
|
37
|
-
if (isObject(Deno) &&
|
38
|
-
return
|
43
|
+
if (isObject(Deno) && isObject(Deno.env)) {
|
44
|
+
return {
|
45
|
+
apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
|
46
|
+
databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
|
47
|
+
branch: Deno.env.get("XATA_BRANCH"),
|
48
|
+
envBranch: Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH"),
|
49
|
+
fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
|
50
|
+
};
|
39
51
|
}
|
40
52
|
} catch (err) {
|
41
53
|
}
|
54
|
+
return {
|
55
|
+
apiKey: getGlobalApiKey(),
|
56
|
+
databaseURL: getGlobalDatabaseURL(),
|
57
|
+
branch: getGlobalBranch(),
|
58
|
+
envBranch: void 0,
|
59
|
+
fallbackBranch: getGlobalFallbackBranch()
|
60
|
+
};
|
61
|
+
}
|
62
|
+
function getGlobalApiKey() {
|
63
|
+
try {
|
64
|
+
return XATA_API_KEY;
|
65
|
+
} catch (err) {
|
66
|
+
return void 0;
|
67
|
+
}
|
68
|
+
}
|
69
|
+
function getGlobalDatabaseURL() {
|
70
|
+
try {
|
71
|
+
return XATA_DATABASE_URL;
|
72
|
+
} catch (err) {
|
73
|
+
return void 0;
|
74
|
+
}
|
75
|
+
}
|
76
|
+
function getGlobalBranch() {
|
77
|
+
try {
|
78
|
+
return XATA_BRANCH;
|
79
|
+
} catch (err) {
|
80
|
+
return void 0;
|
81
|
+
}
|
82
|
+
}
|
83
|
+
function getGlobalFallbackBranch() {
|
84
|
+
try {
|
85
|
+
return XATA_FALLBACK_BRANCH;
|
86
|
+
} catch (err) {
|
87
|
+
return void 0;
|
88
|
+
}
|
42
89
|
}
|
43
90
|
async function getGitBranch() {
|
44
91
|
try {
|
@@ -63,7 +110,8 @@ async function getGitBranch() {
|
|
63
110
|
|
64
111
|
function getAPIKey() {
|
65
112
|
try {
|
66
|
-
|
113
|
+
const { apiKey } = getEnvironment();
|
114
|
+
return apiKey;
|
67
115
|
} catch (err) {
|
68
116
|
return void 0;
|
69
117
|
}
|
@@ -78,7 +126,7 @@ function getFetchImplementation(userFetch) {
|
|
78
126
|
return fetchImpl;
|
79
127
|
}
|
80
128
|
|
81
|
-
const VERSION = "0.0.0-alpha.
|
129
|
+
const VERSION = "0.0.0-alpha.vf481c73";
|
82
130
|
|
83
131
|
class ErrorWithCause extends Error {
|
84
132
|
constructor(message, options) {
|
@@ -504,7 +552,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
504
552
|
throw TypeError("Cannot add the same private member more than once");
|
505
553
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
506
554
|
};
|
507
|
-
var __privateSet$
|
555
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
508
556
|
__accessCheck$7(obj, member, "write to private field");
|
509
557
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
510
558
|
return value;
|
@@ -519,7 +567,7 @@ class XataApiClient {
|
|
519
567
|
if (!apiKey) {
|
520
568
|
throw new Error("Could not resolve a valid apiKey");
|
521
569
|
}
|
522
|
-
__privateSet$
|
570
|
+
__privateSet$7(this, _extraProps, {
|
523
571
|
apiUrl: getHostUrl(provider, "main"),
|
524
572
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
525
573
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -956,7 +1004,7 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
956
1004
|
throw TypeError("Cannot add the same private member more than once");
|
957
1005
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
958
1006
|
};
|
959
|
-
var __privateSet$
|
1007
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
960
1008
|
__accessCheck$6(obj, member, "write to private field");
|
961
1009
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
962
1010
|
return value;
|
@@ -965,7 +1013,7 @@ var _query, _page;
|
|
965
1013
|
class Page {
|
966
1014
|
constructor(query, meta, records = []) {
|
967
1015
|
__privateAdd$6(this, _query, void 0);
|
968
|
-
__privateSet$
|
1016
|
+
__privateSet$6(this, _query, query);
|
969
1017
|
this.meta = meta;
|
970
1018
|
this.records = new RecordArray(this, records);
|
971
1019
|
}
|
@@ -997,7 +1045,7 @@ const _RecordArray = class extends Array {
|
|
997
1045
|
constructor(page, overrideRecords) {
|
998
1046
|
super(..._RecordArray.parseConstructorParams(page, overrideRecords));
|
999
1047
|
__privateAdd$6(this, _page, void 0);
|
1000
|
-
__privateSet$
|
1048
|
+
__privateSet$6(this, _page, page);
|
1001
1049
|
}
|
1002
1050
|
static parseConstructorParams(...args) {
|
1003
1051
|
if (args.length === 1 && typeof args[0] === "number") {
|
@@ -1045,7 +1093,7 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
1045
1093
|
throw TypeError("Cannot add the same private member more than once");
|
1046
1094
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1047
1095
|
};
|
1048
|
-
var __privateSet$
|
1096
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
1049
1097
|
__accessCheck$5(obj, member, "write to private field");
|
1050
1098
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1051
1099
|
return value;
|
@@ -1058,11 +1106,11 @@ const _Query = class {
|
|
1058
1106
|
__privateAdd$5(this, _data, { filter: {} });
|
1059
1107
|
this.meta = { page: { cursor: "start", more: true } };
|
1060
1108
|
this.records = new RecordArray(this, []);
|
1061
|
-
__privateSet$
|
1109
|
+
__privateSet$5(this, _table$1, table);
|
1062
1110
|
if (repository) {
|
1063
|
-
__privateSet$
|
1111
|
+
__privateSet$5(this, _repository, repository);
|
1064
1112
|
} else {
|
1065
|
-
__privateSet$
|
1113
|
+
__privateSet$5(this, _repository, this);
|
1066
1114
|
}
|
1067
1115
|
const parent = cleanParent(data, rawParent);
|
1068
1116
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
@@ -1239,7 +1287,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1239
1287
|
throw TypeError("Cannot add the same private member more than once");
|
1240
1288
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1241
1289
|
};
|
1242
|
-
var __privateSet$
|
1290
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1243
1291
|
__accessCheck$4(obj, member, "write to private field");
|
1244
1292
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1245
1293
|
return value;
|
@@ -1248,7 +1296,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1248
1296
|
__accessCheck$4(obj, member, "access private method");
|
1249
1297
|
return method;
|
1250
1298
|
};
|
1251
|
-
var _table, _getFetchProps, _cache,
|
1299
|
+
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
1300
|
class Repository extends Query {
|
1253
1301
|
}
|
1254
1302
|
class RestRepository extends Query {
|
@@ -1265,15 +1313,16 @@ class RestRepository extends Query {
|
|
1265
1313
|
__privateAdd$4(this, _getCacheRecord);
|
1266
1314
|
__privateAdd$4(this, _setCacheQuery);
|
1267
1315
|
__privateAdd$4(this, _getCacheQuery);
|
1268
|
-
__privateAdd$4(this,
|
1316
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1269
1317
|
__privateAdd$4(this, _table, void 0);
|
1270
1318
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1271
1319
|
__privateAdd$4(this, _cache, void 0);
|
1272
|
-
__privateAdd$4(this,
|
1273
|
-
__privateSet$
|
1274
|
-
__privateSet$
|
1320
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1321
|
+
__privateSet$4(this, _table, options.table);
|
1322
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1275
1323
|
this.db = options.db;
|
1276
|
-
__privateSet$
|
1324
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1325
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1277
1326
|
}
|
1278
1327
|
async create(a, b) {
|
1279
1328
|
if (Array.isArray(a)) {
|
@@ -1322,8 +1371,8 @@ class RestRepository extends Query {
|
|
1322
1371
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1323
1372
|
...fetchProps
|
1324
1373
|
});
|
1325
|
-
const
|
1326
|
-
return initObject(this.db,
|
1374
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1375
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1327
1376
|
} catch (e) {
|
1328
1377
|
if (isObject(e) && e.status === 404) {
|
1329
1378
|
return null;
|
@@ -1412,8 +1461,8 @@ class RestRepository extends Query {
|
|
1412
1461
|
},
|
1413
1462
|
...fetchProps
|
1414
1463
|
});
|
1415
|
-
const
|
1416
|
-
return records.map((item) => initObject(this.db,
|
1464
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1465
|
+
return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
|
1417
1466
|
}
|
1418
1467
|
async query(query) {
|
1419
1468
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1432,8 +1481,8 @@ class RestRepository extends Query {
|
|
1432
1481
|
body,
|
1433
1482
|
...fetchProps
|
1434
1483
|
});
|
1435
|
-
const
|
1436
|
-
const records = objects.map((record) => initObject(this.db,
|
1484
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1485
|
+
const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
|
1437
1486
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1438
1487
|
return new Page(query, meta, records);
|
1439
1488
|
}
|
@@ -1441,7 +1490,7 @@ class RestRepository extends Query {
|
|
1441
1490
|
_table = new WeakMap();
|
1442
1491
|
_getFetchProps = new WeakMap();
|
1443
1492
|
_cache = new WeakMap();
|
1444
|
-
|
1493
|
+
_schemaTables$2 = new WeakMap();
|
1445
1494
|
_insertRecordWithoutId = new WeakSet();
|
1446
1495
|
insertRecordWithoutId_fn = async function(object) {
|
1447
1496
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
@@ -1575,17 +1624,17 @@ getCacheQuery_fn = async function(query) {
|
|
1575
1624
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1576
1625
|
return hasExpired ? null : result;
|
1577
1626
|
};
|
1578
|
-
|
1579
|
-
|
1580
|
-
if (__privateGet$4(this,
|
1581
|
-
return __privateGet$4(this,
|
1627
|
+
_getSchemaTables$1 = new WeakSet();
|
1628
|
+
getSchemaTables_fn$1 = async function() {
|
1629
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1630
|
+
return __privateGet$4(this, _schemaTables$2);
|
1582
1631
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1583
1632
|
const { schema } = await getBranchDetails({
|
1584
1633
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1585
1634
|
...fetchProps
|
1586
1635
|
});
|
1587
|
-
__privateSet$
|
1588
|
-
return schema;
|
1636
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1637
|
+
return schema.tables;
|
1589
1638
|
};
|
1590
1639
|
const transformObjectLinks = (object) => {
|
1591
1640
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
@@ -1594,11 +1643,11 @@ const transformObjectLinks = (object) => {
|
|
1594
1643
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1595
1644
|
}, {});
|
1596
1645
|
};
|
1597
|
-
const initObject = (db,
|
1646
|
+
const initObject = (db, schemaTables, table, object) => {
|
1598
1647
|
const result = {};
|
1599
1648
|
const { xata, ...rest } = object ?? {};
|
1600
1649
|
Object.assign(result, rest);
|
1601
|
-
const { columns } =
|
1650
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1602
1651
|
if (!columns)
|
1603
1652
|
console.error(`Table ${table} not found in schema`);
|
1604
1653
|
for (const column of columns ?? []) {
|
@@ -1618,7 +1667,7 @@ const initObject = (db, schema, table, object) => {
|
|
1618
1667
|
if (!linkTable) {
|
1619
1668
|
console.error(`Failed to parse link for field ${column.name}`);
|
1620
1669
|
} else if (isObject(value)) {
|
1621
|
-
result[column.name] = initObject(db,
|
1670
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1622
1671
|
}
|
1623
1672
|
break;
|
1624
1673
|
}
|
@@ -1665,7 +1714,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1665
1714
|
throw TypeError("Cannot add the same private member more than once");
|
1666
1715
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1667
1716
|
};
|
1668
|
-
var __privateSet$
|
1717
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1669
1718
|
__accessCheck$3(obj, member, "write to private field");
|
1670
1719
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1671
1720
|
return value;
|
@@ -1674,7 +1723,7 @@ var _map;
|
|
1674
1723
|
class SimpleCache {
|
1675
1724
|
constructor(options = {}) {
|
1676
1725
|
__privateAdd$3(this, _map, void 0);
|
1677
|
-
__privateSet$
|
1726
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1678
1727
|
this.capacity = options.max ?? 500;
|
1679
1728
|
this.cacheRecords = options.cacheRecords ?? true;
|
1680
1729
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
@@ -1734,12 +1783,18 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1734
1783
|
throw TypeError("Cannot add the same private member more than once");
|
1735
1784
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1736
1785
|
};
|
1737
|
-
var
|
1786
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1787
|
+
__accessCheck$2(obj, member, "write to private field");
|
1788
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1789
|
+
return value;
|
1790
|
+
};
|
1791
|
+
var _tables, _schemaTables$1;
|
1738
1792
|
class SchemaPlugin extends XataPlugin {
|
1739
|
-
constructor(
|
1793
|
+
constructor(schemaTables) {
|
1740
1794
|
super();
|
1741
|
-
this.tableNames = tableNames;
|
1742
1795
|
__privateAdd$2(this, _tables, {});
|
1796
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1797
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1743
1798
|
}
|
1744
1799
|
build(pluginOptions) {
|
1745
1800
|
const db = new Proxy({}, {
|
@@ -1752,13 +1807,15 @@ class SchemaPlugin extends XataPlugin {
|
|
1752
1807
|
return __privateGet$2(this, _tables)[table];
|
1753
1808
|
}
|
1754
1809
|
});
|
1755
|
-
|
1756
|
-
|
1810
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1811
|
+
for (const table of tableNames) {
|
1812
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1757
1813
|
}
|
1758
1814
|
return db;
|
1759
1815
|
}
|
1760
1816
|
}
|
1761
1817
|
_tables = new WeakMap();
|
1818
|
+
_schemaTables$1 = new WeakMap();
|
1762
1819
|
|
1763
1820
|
var __accessCheck$1 = (obj, member, msg) => {
|
1764
1821
|
if (!member.has(obj))
|
@@ -1782,39 +1839,40 @@ var __privateMethod$1 = (obj, member, method) => {
|
|
1782
1839
|
__accessCheck$1(obj, member, "access private method");
|
1783
1840
|
return method;
|
1784
1841
|
};
|
1785
|
-
var
|
1842
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1786
1843
|
class SearchPlugin extends XataPlugin {
|
1787
|
-
constructor(db) {
|
1844
|
+
constructor(db, schemaTables) {
|
1788
1845
|
super();
|
1789
1846
|
this.db = db;
|
1790
1847
|
__privateAdd$1(this, _search);
|
1791
|
-
__privateAdd$1(this,
|
1792
|
-
__privateAdd$1(this,
|
1848
|
+
__privateAdd$1(this, _getSchemaTables);
|
1849
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1850
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1793
1851
|
}
|
1794
1852
|
build({ getFetchProps }) {
|
1795
1853
|
return {
|
1796
1854
|
all: async (query, options = {}) => {
|
1797
1855
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1798
|
-
const
|
1856
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1799
1857
|
return records.map((record) => {
|
1800
1858
|
const { table = "orphan" } = record.xata;
|
1801
|
-
return { table, record: initObject(this.db,
|
1859
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1802
1860
|
});
|
1803
1861
|
},
|
1804
1862
|
byTable: async (query, options = {}) => {
|
1805
1863
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1806
|
-
const
|
1864
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1807
1865
|
return records.reduce((acc, record) => {
|
1808
1866
|
const { table = "orphan" } = record.xata;
|
1809
1867
|
const items = acc[table] ?? [];
|
1810
|
-
const item = initObject(this.db,
|
1868
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1811
1869
|
return { ...acc, [table]: [...items, item] };
|
1812
1870
|
}, {});
|
1813
1871
|
}
|
1814
1872
|
};
|
1815
1873
|
}
|
1816
1874
|
}
|
1817
|
-
|
1875
|
+
_schemaTables = new WeakMap();
|
1818
1876
|
_search = new WeakSet();
|
1819
1877
|
search_fn = async function(query, options, getFetchProps) {
|
1820
1878
|
const fetchProps = await getFetchProps();
|
@@ -1826,38 +1884,32 @@ search_fn = async function(query, options, getFetchProps) {
|
|
1826
1884
|
});
|
1827
1885
|
return records;
|
1828
1886
|
};
|
1829
|
-
|
1830
|
-
|
1831
|
-
if (__privateGet$1(this,
|
1832
|
-
return __privateGet$1(this,
|
1887
|
+
_getSchemaTables = new WeakSet();
|
1888
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1889
|
+
if (__privateGet$1(this, _schemaTables))
|
1890
|
+
return __privateGet$1(this, _schemaTables);
|
1833
1891
|
const fetchProps = await getFetchProps();
|
1834
1892
|
const { schema } = await getBranchDetails({
|
1835
1893
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1836
1894
|
...fetchProps
|
1837
1895
|
});
|
1838
|
-
__privateSet$1(this,
|
1839
|
-
return schema;
|
1896
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1897
|
+
return schema.tables;
|
1840
1898
|
};
|
1841
1899
|
|
1842
1900
|
const isBranchStrategyBuilder = (strategy) => {
|
1843
1901
|
return typeof strategy === "function";
|
1844
1902
|
};
|
1845
1903
|
|
1846
|
-
const envBranchNames = [
|
1847
|
-
"XATA_BRANCH",
|
1848
|
-
"VERCEL_GIT_COMMIT_REF",
|
1849
|
-
"CF_PAGES_BRANCH",
|
1850
|
-
"BRANCH"
|
1851
|
-
];
|
1852
1904
|
async function getCurrentBranchName(options) {
|
1853
|
-
const
|
1854
|
-
if (
|
1855
|
-
const details = await getDatabaseBranch(
|
1905
|
+
const { branch, envBranch } = getEnvironment();
|
1906
|
+
if (branch) {
|
1907
|
+
const details = await getDatabaseBranch(branch, options);
|
1856
1908
|
if (details)
|
1857
|
-
return
|
1858
|
-
console.warn(`Branch ${
|
1909
|
+
return branch;
|
1910
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1859
1911
|
}
|
1860
|
-
const gitBranch = await getGitBranch();
|
1912
|
+
const gitBranch = envBranch || await getGitBranch();
|
1861
1913
|
return resolveXataBranch(gitBranch, options);
|
1862
1914
|
}
|
1863
1915
|
async function getCurrentBranchDetails(options) {
|
@@ -1873,13 +1925,14 @@ async function resolveXataBranch(gitBranch, options) {
|
|
1873
1925
|
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1874
1926
|
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1875
1927
|
const [workspace] = host.split(".");
|
1928
|
+
const { fallbackBranch } = getEnvironment();
|
1876
1929
|
const { branch } = await resolveBranch({
|
1877
1930
|
apiKey,
|
1878
1931
|
apiUrl: databaseURL,
|
1879
1932
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1880
1933
|
workspacesApiUrl: `${protocol}//${host}`,
|
1881
1934
|
pathParams: { dbName, workspace },
|
1882
|
-
queryParams: { gitBranch, fallbackBranch
|
1935
|
+
queryParams: { gitBranch, fallbackBranch }
|
1883
1936
|
});
|
1884
1937
|
return branch;
|
1885
1938
|
}
|
@@ -1907,21 +1960,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1907
1960
|
throw err;
|
1908
1961
|
}
|
1909
1962
|
}
|
1910
|
-
function getBranchByEnvVariable() {
|
1911
|
-
for (const name of envBranchNames) {
|
1912
|
-
const value = getEnvVariable(name);
|
1913
|
-
if (value) {
|
1914
|
-
return value;
|
1915
|
-
}
|
1916
|
-
}
|
1917
|
-
try {
|
1918
|
-
return XATA_BRANCH;
|
1919
|
-
} catch (err) {
|
1920
|
-
}
|
1921
|
-
}
|
1922
1963
|
function getDatabaseURL() {
|
1923
1964
|
try {
|
1924
|
-
|
1965
|
+
const { databaseURL } = getEnvironment();
|
1966
|
+
return databaseURL;
|
1925
1967
|
} catch (err) {
|
1926
1968
|
return void 0;
|
1927
1969
|
}
|
@@ -1952,7 +1994,7 @@ var __privateMethod = (obj, member, method) => {
|
|
1952
1994
|
const buildClient = (plugins) => {
|
1953
1995
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1954
1996
|
return _a = class {
|
1955
|
-
constructor(options = {},
|
1997
|
+
constructor(options = {}, schemaTables) {
|
1956
1998
|
__privateAdd(this, _parseOptions);
|
1957
1999
|
__privateAdd(this, _getFetchProps);
|
1958
2000
|
__privateAdd(this, _evaluateBranch);
|
@@ -1962,8 +2004,8 @@ const buildClient = (plugins) => {
|
|
1962
2004
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1963
2005
|
cache: safeOptions.cache
|
1964
2006
|
};
|
1965
|
-
const db = new SchemaPlugin(
|
1966
|
-
const search = new SearchPlugin(db).build(pluginOptions);
|
2007
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2008
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1967
2009
|
this.db = db;
|
1968
2010
|
this.search = search;
|
1969
2011
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|