@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/CHANGELOG.md +30 -0
- package/README.md +41 -35
- package/Usage.md +129 -114
- package/dist/index.cjs +185 -126
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +93 -23
- package/dist/index.mjs +167 -126
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -4
- package/LICENSE +0 -201
package/dist/index.mjs
CHANGED
@@ -22,35 +22,81 @@ function toBase64(value) {
|
|
22
22
|
}
|
23
23
|
}
|
24
24
|
|
25
|
-
function
|
25
|
+
function getEnvironment() {
|
26
26
|
try {
|
27
|
-
if (isObject(process) &&
|
28
|
-
return
|
27
|
+
if (isObject(process) && isObject(process.env)) {
|
28
|
+
return {
|
29
|
+
apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
|
30
|
+
databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
|
31
|
+
branch: process.env.XATA_BRANCH ?? getGlobalBranch(),
|
32
|
+
envBranch: process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH,
|
33
|
+
fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
|
34
|
+
};
|
29
35
|
}
|
30
36
|
} catch (err) {
|
31
37
|
}
|
32
38
|
try {
|
33
|
-
if (isObject(Deno) &&
|
34
|
-
return
|
39
|
+
if (isObject(Deno) && isObject(Deno.env)) {
|
40
|
+
return {
|
41
|
+
apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
|
42
|
+
databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
|
43
|
+
branch: Deno.env.get("XATA_BRANCH") ?? getGlobalBranch(),
|
44
|
+
envBranch: Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH"),
|
45
|
+
fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
|
46
|
+
};
|
35
47
|
}
|
36
48
|
} catch (err) {
|
37
49
|
}
|
50
|
+
return {
|
51
|
+
apiKey: getGlobalApiKey(),
|
52
|
+
databaseURL: getGlobalDatabaseURL(),
|
53
|
+
branch: getGlobalBranch(),
|
54
|
+
envBranch: void 0,
|
55
|
+
fallbackBranch: getGlobalFallbackBranch()
|
56
|
+
};
|
57
|
+
}
|
58
|
+
function getGlobalApiKey() {
|
59
|
+
try {
|
60
|
+
return XATA_API_KEY;
|
61
|
+
} catch (err) {
|
62
|
+
return void 0;
|
63
|
+
}
|
64
|
+
}
|
65
|
+
function getGlobalDatabaseURL() {
|
66
|
+
try {
|
67
|
+
return XATA_DATABASE_URL;
|
68
|
+
} catch (err) {
|
69
|
+
return void 0;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
function getGlobalBranch() {
|
73
|
+
try {
|
74
|
+
return XATA_BRANCH;
|
75
|
+
} catch (err) {
|
76
|
+
return void 0;
|
77
|
+
}
|
78
|
+
}
|
79
|
+
function getGlobalFallbackBranch() {
|
80
|
+
try {
|
81
|
+
return XATA_FALLBACK_BRANCH;
|
82
|
+
} catch (err) {
|
83
|
+
return void 0;
|
84
|
+
}
|
38
85
|
}
|
39
86
|
async function getGitBranch() {
|
87
|
+
const cmd = ["git", "branch", "--show-current"];
|
88
|
+
const nodeModule = ["child", "process"].join("_");
|
40
89
|
try {
|
41
90
|
if (typeof require === "function") {
|
42
|
-
|
43
|
-
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
91
|
+
return require(nodeModule).execSync(cmd.join(" "), { encoding: "utf-8" }).trim();
|
44
92
|
}
|
93
|
+
const { execSync } = await import(nodeModule);
|
94
|
+
return execSync(cmd.join(" "), { encoding: "utf-8" }).toString().trim();
|
45
95
|
} catch (err) {
|
46
96
|
}
|
47
97
|
try {
|
48
98
|
if (isObject(Deno)) {
|
49
|
-
const process2 = Deno.run({
|
50
|
-
cmd: ["git", "branch", "--show-current"],
|
51
|
-
stdout: "piped",
|
52
|
-
stderr: "piped"
|
53
|
-
});
|
99
|
+
const process2 = Deno.run({ cmd, stdout: "piped", stderr: "piped" });
|
54
100
|
return new TextDecoder().decode(await process2.output()).trim();
|
55
101
|
}
|
56
102
|
} catch (err) {
|
@@ -59,7 +105,8 @@ async function getGitBranch() {
|
|
59
105
|
|
60
106
|
function getAPIKey() {
|
61
107
|
try {
|
62
|
-
|
108
|
+
const { apiKey } = getEnvironment();
|
109
|
+
return apiKey;
|
63
110
|
} catch (err) {
|
64
111
|
return void 0;
|
65
112
|
}
|
@@ -74,7 +121,7 @@ function getFetchImplementation(userFetch) {
|
|
74
121
|
return fetchImpl;
|
75
122
|
}
|
76
123
|
|
77
|
-
const VERSION = "0.
|
124
|
+
const VERSION = "0.14.0";
|
78
125
|
|
79
126
|
class ErrorWithCause extends Error {
|
80
127
|
constructor(message, options) {
|
@@ -500,7 +547,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
500
547
|
throw TypeError("Cannot add the same private member more than once");
|
501
548
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
502
549
|
};
|
503
|
-
var __privateSet$
|
550
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
504
551
|
__accessCheck$7(obj, member, "write to private field");
|
505
552
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
506
553
|
return value;
|
@@ -515,7 +562,7 @@ class XataApiClient {
|
|
515
562
|
if (!apiKey) {
|
516
563
|
throw new Error("Could not resolve a valid apiKey");
|
517
564
|
}
|
518
|
-
__privateSet$
|
565
|
+
__privateSet$7(this, _extraProps, {
|
519
566
|
apiUrl: getHostUrl(provider, "main"),
|
520
567
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
521
568
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -952,7 +999,7 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
952
999
|
throw TypeError("Cannot add the same private member more than once");
|
953
1000
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
954
1001
|
};
|
955
|
-
var __privateSet$
|
1002
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
956
1003
|
__accessCheck$6(obj, member, "write to private field");
|
957
1004
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
958
1005
|
return value;
|
@@ -961,7 +1008,7 @@ var _query, _page;
|
|
961
1008
|
class Page {
|
962
1009
|
constructor(query, meta, records = []) {
|
963
1010
|
__privateAdd$6(this, _query, void 0);
|
964
|
-
__privateSet$
|
1011
|
+
__privateSet$6(this, _query, query);
|
965
1012
|
this.meta = meta;
|
966
1013
|
this.records = new RecordArray(this, records);
|
967
1014
|
}
|
@@ -990,10 +1037,26 @@ function isCursorPaginationOptions(options) {
|
|
990
1037
|
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
991
1038
|
}
|
992
1039
|
const _RecordArray = class extends Array {
|
993
|
-
constructor(
|
994
|
-
super(...
|
1040
|
+
constructor(...args) {
|
1041
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
995
1042
|
__privateAdd$6(this, _page, void 0);
|
996
|
-
__privateSet$
|
1043
|
+
__privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
|
1044
|
+
}
|
1045
|
+
static parseConstructorParams(...args) {
|
1046
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1047
|
+
return new Array(args[0]);
|
1048
|
+
}
|
1049
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1050
|
+
const result = args[1] ?? args[0].records ?? [];
|
1051
|
+
return new Array(...result);
|
1052
|
+
}
|
1053
|
+
return new Array(...args);
|
1054
|
+
}
|
1055
|
+
toArray() {
|
1056
|
+
return new Array(...this);
|
1057
|
+
}
|
1058
|
+
map(callbackfn, thisArg) {
|
1059
|
+
return this.toArray().map(callbackfn, thisArg);
|
997
1060
|
}
|
998
1061
|
async nextPage(size, offset) {
|
999
1062
|
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
@@ -1031,7 +1094,7 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
1031
1094
|
throw TypeError("Cannot add the same private member more than once");
|
1032
1095
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1033
1096
|
};
|
1034
|
-
var __privateSet$
|
1097
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
1035
1098
|
__accessCheck$5(obj, member, "write to private field");
|
1036
1099
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1037
1100
|
return value;
|
@@ -1044,11 +1107,11 @@ const _Query = class {
|
|
1044
1107
|
__privateAdd$5(this, _data, { filter: {} });
|
1045
1108
|
this.meta = { page: { cursor: "start", more: true } };
|
1046
1109
|
this.records = new RecordArray(this, []);
|
1047
|
-
__privateSet$
|
1110
|
+
__privateSet$5(this, _table$1, table);
|
1048
1111
|
if (repository) {
|
1049
|
-
__privateSet$
|
1112
|
+
__privateSet$5(this, _repository, repository);
|
1050
1113
|
} else {
|
1051
|
-
__privateSet$
|
1114
|
+
__privateSet$5(this, _repository, this);
|
1052
1115
|
}
|
1053
1116
|
const parent = cleanParent(data, rawParent);
|
1054
1117
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
@@ -1225,7 +1288,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1225
1288
|
throw TypeError("Cannot add the same private member more than once");
|
1226
1289
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1227
1290
|
};
|
1228
|
-
var __privateSet$
|
1291
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1229
1292
|
__accessCheck$4(obj, member, "write to private field");
|
1230
1293
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1231
1294
|
return value;
|
@@ -1234,7 +1297,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1234
1297
|
__accessCheck$4(obj, member, "access private method");
|
1235
1298
|
return method;
|
1236
1299
|
};
|
1237
|
-
var _table, _getFetchProps, _cache,
|
1300
|
+
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;
|
1238
1301
|
class Repository extends Query {
|
1239
1302
|
}
|
1240
1303
|
class RestRepository extends Query {
|
@@ -1251,43 +1314,24 @@ class RestRepository extends Query {
|
|
1251
1314
|
__privateAdd$4(this, _getCacheRecord);
|
1252
1315
|
__privateAdd$4(this, _setCacheQuery);
|
1253
1316
|
__privateAdd$4(this, _getCacheQuery);
|
1254
|
-
__privateAdd$4(this,
|
1317
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1255
1318
|
__privateAdd$4(this, _table, void 0);
|
1256
1319
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1257
1320
|
__privateAdd$4(this, _cache, void 0);
|
1258
|
-
__privateAdd$4(this,
|
1259
|
-
__privateSet$
|
1260
|
-
__privateSet$
|
1321
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1322
|
+
__privateSet$4(this, _table, options.table);
|
1323
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1261
1324
|
this.db = options.db;
|
1262
|
-
__privateSet$
|
1325
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1326
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1263
1327
|
}
|
1264
1328
|
async create(a, b) {
|
1265
1329
|
if (Array.isArray(a)) {
|
1266
1330
|
if (a.length === 0)
|
1267
1331
|
return [];
|
1268
|
-
const
|
1269
|
-
|
1270
|
-
|
1271
|
-
if (condition) {
|
1272
|
-
accWithIds.push(item);
|
1273
|
-
} else {
|
1274
|
-
accWithoutIds.push(item);
|
1275
|
-
}
|
1276
|
-
return [accWithoutIds, accWithIds, accOrder];
|
1277
|
-
}, [[], [], []]);
|
1278
|
-
const recordsWithoutId = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, itemsWithoutIds);
|
1279
|
-
await Promise.all(recordsWithoutId.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1280
|
-
if (itemsWithIds.length > 100) {
|
1281
|
-
console.warn("Bulk create operation with id is not optimized in the Xata API yet, this request might be slow");
|
1282
|
-
}
|
1283
|
-
const recordsWithId = await Promise.all(itemsWithIds.map((object) => this.create(object)));
|
1284
|
-
return order.map((condition) => {
|
1285
|
-
if (condition) {
|
1286
|
-
return recordsWithId.shift();
|
1287
|
-
} else {
|
1288
|
-
return recordsWithoutId.shift();
|
1289
|
-
}
|
1290
|
-
}).filter((record) => !!record);
|
1332
|
+
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1333
|
+
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1334
|
+
return records;
|
1291
1335
|
}
|
1292
1336
|
if (isString(a) && isObject(b)) {
|
1293
1337
|
if (a === "")
|
@@ -1328,8 +1372,8 @@ class RestRepository extends Query {
|
|
1328
1372
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1329
1373
|
...fetchProps
|
1330
1374
|
});
|
1331
|
-
const
|
1332
|
-
return initObject(this.db,
|
1375
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1376
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1333
1377
|
} catch (e) {
|
1334
1378
|
if (isObject(e) && e.status === 404) {
|
1335
1379
|
return null;
|
@@ -1418,8 +1462,8 @@ class RestRepository extends Query {
|
|
1418
1462
|
},
|
1419
1463
|
...fetchProps
|
1420
1464
|
});
|
1421
|
-
const
|
1422
|
-
return records.map((item) => initObject(this.db,
|
1465
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1466
|
+
return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
|
1423
1467
|
}
|
1424
1468
|
async query(query) {
|
1425
1469
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1438,8 +1482,8 @@ class RestRepository extends Query {
|
|
1438
1482
|
body,
|
1439
1483
|
...fetchProps
|
1440
1484
|
});
|
1441
|
-
const
|
1442
|
-
const records = objects.map((record) => initObject(this.db,
|
1485
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1486
|
+
const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
|
1443
1487
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1444
1488
|
return new Page(query, meta, records);
|
1445
1489
|
}
|
@@ -1447,7 +1491,7 @@ class RestRepository extends Query {
|
|
1447
1491
|
_table = new WeakMap();
|
1448
1492
|
_getFetchProps = new WeakMap();
|
1449
1493
|
_cache = new WeakMap();
|
1450
|
-
|
1494
|
+
_schemaTables$2 = new WeakMap();
|
1451
1495
|
_insertRecordWithoutId = new WeakSet();
|
1452
1496
|
insertRecordWithoutId_fn = async function(object) {
|
1453
1497
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
@@ -1492,16 +1536,20 @@ _bulkInsertTableRecords = new WeakSet();
|
|
1492
1536
|
bulkInsertTableRecords_fn = async function(objects) {
|
1493
1537
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1494
1538
|
const records = objects.map((object) => transformObjectLinks(object));
|
1495
|
-
const
|
1539
|
+
const { recordIDs } = await bulkInsertTableRecords({
|
1496
1540
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1497
1541
|
body: { records },
|
1498
1542
|
...fetchProps
|
1499
1543
|
});
|
1500
|
-
const finalObjects = await this.read(
|
1544
|
+
const finalObjects = await this.read(recordIDs);
|
1501
1545
|
if (finalObjects.length !== objects.length) {
|
1502
1546
|
throw new Error("The server failed to save some records");
|
1503
1547
|
}
|
1504
|
-
|
1548
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1549
|
+
acc[object.id] = object;
|
1550
|
+
return acc;
|
1551
|
+
}, {});
|
1552
|
+
return recordIDs.map((id) => dictionary[id]);
|
1505
1553
|
};
|
1506
1554
|
_updateRecordWithID = new WeakSet();
|
1507
1555
|
updateRecordWithID_fn = async function(recordId, object) {
|
@@ -1577,17 +1625,17 @@ getCacheQuery_fn = async function(query) {
|
|
1577
1625
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1578
1626
|
return hasExpired ? null : result;
|
1579
1627
|
};
|
1580
|
-
|
1581
|
-
|
1582
|
-
if (__privateGet$4(this,
|
1583
|
-
return __privateGet$4(this,
|
1628
|
+
_getSchemaTables$1 = new WeakSet();
|
1629
|
+
getSchemaTables_fn$1 = async function() {
|
1630
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1631
|
+
return __privateGet$4(this, _schemaTables$2);
|
1584
1632
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1585
1633
|
const { schema } = await getBranchDetails({
|
1586
1634
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1587
1635
|
...fetchProps
|
1588
1636
|
});
|
1589
|
-
__privateSet$
|
1590
|
-
return schema;
|
1637
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1638
|
+
return schema.tables;
|
1591
1639
|
};
|
1592
1640
|
const transformObjectLinks = (object) => {
|
1593
1641
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
@@ -1596,11 +1644,11 @@ const transformObjectLinks = (object) => {
|
|
1596
1644
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1597
1645
|
}, {});
|
1598
1646
|
};
|
1599
|
-
const initObject = (db,
|
1647
|
+
const initObject = (db, schemaTables, table, object) => {
|
1600
1648
|
const result = {};
|
1601
1649
|
const { xata, ...rest } = object ?? {};
|
1602
1650
|
Object.assign(result, rest);
|
1603
|
-
const { columns } =
|
1651
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1604
1652
|
if (!columns)
|
1605
1653
|
console.error(`Table ${table} not found in schema`);
|
1606
1654
|
for (const column of columns ?? []) {
|
@@ -1620,7 +1668,7 @@ const initObject = (db, schema, table, object) => {
|
|
1620
1668
|
if (!linkTable) {
|
1621
1669
|
console.error(`Failed to parse link for field ${column.name}`);
|
1622
1670
|
} else if (isObject(value)) {
|
1623
|
-
result[column.name] = initObject(db,
|
1671
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1624
1672
|
}
|
1625
1673
|
break;
|
1626
1674
|
}
|
@@ -1667,7 +1715,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1667
1715
|
throw TypeError("Cannot add the same private member more than once");
|
1668
1716
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1669
1717
|
};
|
1670
|
-
var __privateSet$
|
1718
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1671
1719
|
__accessCheck$3(obj, member, "write to private field");
|
1672
1720
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1673
1721
|
return value;
|
@@ -1676,7 +1724,7 @@ var _map;
|
|
1676
1724
|
class SimpleCache {
|
1677
1725
|
constructor(options = {}) {
|
1678
1726
|
__privateAdd$3(this, _map, void 0);
|
1679
|
-
__privateSet$
|
1727
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1680
1728
|
this.capacity = options.max ?? 500;
|
1681
1729
|
this.cacheRecords = options.cacheRecords ?? true;
|
1682
1730
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
@@ -1736,12 +1784,18 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1736
1784
|
throw TypeError("Cannot add the same private member more than once");
|
1737
1785
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1738
1786
|
};
|
1739
|
-
var
|
1787
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1788
|
+
__accessCheck$2(obj, member, "write to private field");
|
1789
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1790
|
+
return value;
|
1791
|
+
};
|
1792
|
+
var _tables, _schemaTables$1;
|
1740
1793
|
class SchemaPlugin extends XataPlugin {
|
1741
|
-
constructor(
|
1794
|
+
constructor(schemaTables) {
|
1742
1795
|
super();
|
1743
|
-
this.tableNames = tableNames;
|
1744
1796
|
__privateAdd$2(this, _tables, {});
|
1797
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1798
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1745
1799
|
}
|
1746
1800
|
build(pluginOptions) {
|
1747
1801
|
const db = new Proxy({}, {
|
@@ -1749,18 +1803,20 @@ class SchemaPlugin extends XataPlugin {
|
|
1749
1803
|
if (!isString(table))
|
1750
1804
|
throw new Error("Invalid table name");
|
1751
1805
|
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1752
|
-
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1806
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1753
1807
|
}
|
1754
1808
|
return __privateGet$2(this, _tables)[table];
|
1755
1809
|
}
|
1756
1810
|
});
|
1757
|
-
|
1758
|
-
|
1811
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1812
|
+
for (const table of tableNames) {
|
1813
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1759
1814
|
}
|
1760
1815
|
return db;
|
1761
1816
|
}
|
1762
1817
|
}
|
1763
1818
|
_tables = new WeakMap();
|
1819
|
+
_schemaTables$1 = new WeakMap();
|
1764
1820
|
|
1765
1821
|
var __accessCheck$1 = (obj, member, msg) => {
|
1766
1822
|
if (!member.has(obj))
|
@@ -1784,39 +1840,40 @@ var __privateMethod$1 = (obj, member, method) => {
|
|
1784
1840
|
__accessCheck$1(obj, member, "access private method");
|
1785
1841
|
return method;
|
1786
1842
|
};
|
1787
|
-
var
|
1843
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1788
1844
|
class SearchPlugin extends XataPlugin {
|
1789
|
-
constructor(db) {
|
1845
|
+
constructor(db, schemaTables) {
|
1790
1846
|
super();
|
1791
1847
|
this.db = db;
|
1792
1848
|
__privateAdd$1(this, _search);
|
1793
|
-
__privateAdd$1(this,
|
1794
|
-
__privateAdd$1(this,
|
1849
|
+
__privateAdd$1(this, _getSchemaTables);
|
1850
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1851
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1795
1852
|
}
|
1796
1853
|
build({ getFetchProps }) {
|
1797
1854
|
return {
|
1798
1855
|
all: async (query, options = {}) => {
|
1799
1856
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1800
|
-
const
|
1857
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1801
1858
|
return records.map((record) => {
|
1802
1859
|
const { table = "orphan" } = record.xata;
|
1803
|
-
return { table, record: initObject(this.db,
|
1860
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1804
1861
|
});
|
1805
1862
|
},
|
1806
1863
|
byTable: async (query, options = {}) => {
|
1807
1864
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1808
|
-
const
|
1865
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1809
1866
|
return records.reduce((acc, record) => {
|
1810
1867
|
const { table = "orphan" } = record.xata;
|
1811
1868
|
const items = acc[table] ?? [];
|
1812
|
-
const item = initObject(this.db,
|
1869
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1813
1870
|
return { ...acc, [table]: [...items, item] };
|
1814
1871
|
}, {});
|
1815
1872
|
}
|
1816
1873
|
};
|
1817
1874
|
}
|
1818
1875
|
}
|
1819
|
-
|
1876
|
+
_schemaTables = new WeakMap();
|
1820
1877
|
_search = new WeakSet();
|
1821
1878
|
search_fn = async function(query, options, getFetchProps) {
|
1822
1879
|
const fetchProps = await getFetchProps();
|
@@ -1828,38 +1885,32 @@ search_fn = async function(query, options, getFetchProps) {
|
|
1828
1885
|
});
|
1829
1886
|
return records;
|
1830
1887
|
};
|
1831
|
-
|
1832
|
-
|
1833
|
-
if (__privateGet$1(this,
|
1834
|
-
return __privateGet$1(this,
|
1888
|
+
_getSchemaTables = new WeakSet();
|
1889
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1890
|
+
if (__privateGet$1(this, _schemaTables))
|
1891
|
+
return __privateGet$1(this, _schemaTables);
|
1835
1892
|
const fetchProps = await getFetchProps();
|
1836
1893
|
const { schema } = await getBranchDetails({
|
1837
1894
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1838
1895
|
...fetchProps
|
1839
1896
|
});
|
1840
|
-
__privateSet$1(this,
|
1841
|
-
return schema;
|
1897
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1898
|
+
return schema.tables;
|
1842
1899
|
};
|
1843
1900
|
|
1844
1901
|
const isBranchStrategyBuilder = (strategy) => {
|
1845
1902
|
return typeof strategy === "function";
|
1846
1903
|
};
|
1847
1904
|
|
1848
|
-
const envBranchNames = [
|
1849
|
-
"XATA_BRANCH",
|
1850
|
-
"VERCEL_GIT_COMMIT_REF",
|
1851
|
-
"CF_PAGES_BRANCH",
|
1852
|
-
"BRANCH"
|
1853
|
-
];
|
1854
1905
|
async function getCurrentBranchName(options) {
|
1855
|
-
const
|
1856
|
-
if (
|
1857
|
-
const details = await getDatabaseBranch(
|
1906
|
+
const { branch, envBranch } = getEnvironment();
|
1907
|
+
if (branch) {
|
1908
|
+
const details = await getDatabaseBranch(branch, options);
|
1858
1909
|
if (details)
|
1859
|
-
return
|
1860
|
-
console.warn(`Branch ${
|
1910
|
+
return branch;
|
1911
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1861
1912
|
}
|
1862
|
-
const gitBranch = await getGitBranch();
|
1913
|
+
const gitBranch = envBranch || await getGitBranch();
|
1863
1914
|
return resolveXataBranch(gitBranch, options);
|
1864
1915
|
}
|
1865
1916
|
async function getCurrentBranchDetails(options) {
|
@@ -1875,13 +1926,14 @@ async function resolveXataBranch(gitBranch, options) {
|
|
1875
1926
|
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1876
1927
|
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1877
1928
|
const [workspace] = host.split(".");
|
1929
|
+
const { fallbackBranch } = getEnvironment();
|
1878
1930
|
const { branch } = await resolveBranch({
|
1879
1931
|
apiKey,
|
1880
1932
|
apiUrl: databaseURL,
|
1881
1933
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1882
1934
|
workspacesApiUrl: `${protocol}//${host}`,
|
1883
1935
|
pathParams: { dbName, workspace },
|
1884
|
-
queryParams: { gitBranch, fallbackBranch
|
1936
|
+
queryParams: { gitBranch, fallbackBranch }
|
1885
1937
|
});
|
1886
1938
|
return branch;
|
1887
1939
|
}
|
@@ -1909,21 +1961,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1909
1961
|
throw err;
|
1910
1962
|
}
|
1911
1963
|
}
|
1912
|
-
function getBranchByEnvVariable() {
|
1913
|
-
for (const name of envBranchNames) {
|
1914
|
-
const value = getEnvVariable(name);
|
1915
|
-
if (value) {
|
1916
|
-
return value;
|
1917
|
-
}
|
1918
|
-
}
|
1919
|
-
try {
|
1920
|
-
return XATA_BRANCH;
|
1921
|
-
} catch (err) {
|
1922
|
-
}
|
1923
|
-
}
|
1924
1964
|
function getDatabaseURL() {
|
1925
1965
|
try {
|
1926
|
-
|
1966
|
+
const { databaseURL } = getEnvironment();
|
1967
|
+
return databaseURL;
|
1927
1968
|
} catch (err) {
|
1928
1969
|
return void 0;
|
1929
1970
|
}
|
@@ -1954,7 +1995,7 @@ var __privateMethod = (obj, member, method) => {
|
|
1954
1995
|
const buildClient = (plugins) => {
|
1955
1996
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1956
1997
|
return _a = class {
|
1957
|
-
constructor(options = {},
|
1998
|
+
constructor(options = {}, schemaTables) {
|
1958
1999
|
__privateAdd(this, _parseOptions);
|
1959
2000
|
__privateAdd(this, _getFetchProps);
|
1960
2001
|
__privateAdd(this, _evaluateBranch);
|
@@ -1964,8 +2005,8 @@ const buildClient = (plugins) => {
|
|
1964
2005
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1965
2006
|
cache: safeOptions.cache
|
1966
2007
|
};
|
1967
|
-
const db = new SchemaPlugin(
|
1968
|
-
const search = new SearchPlugin(db).build(pluginOptions);
|
2008
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2009
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1969
2010
|
this.db = db;
|
1970
2011
|
this.search = search;
|
1971
2012
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|