@xata.io/client 0.13.3 → 0.15.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 +32 -0
- package/README.md +41 -35
- package/Usage.md +129 -114
- package/dist/index.cjs +187 -126
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +122 -23
- package/dist/index.mjs +169 -127
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -4
- package/LICENSE +0 -201
package/dist/index.mjs
CHANGED
@@ -22,35 +22,83 @@ 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 fullCmd = cmd.join(" ");
|
89
|
+
const nodeModule = ["child", "process"].join("_");
|
90
|
+
const execOptions = { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] };
|
40
91
|
try {
|
41
92
|
if (typeof require === "function") {
|
42
|
-
|
43
|
-
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
93
|
+
return require(nodeModule).execSync(fullCmd, execOptions).trim();
|
44
94
|
}
|
95
|
+
const { execSync } = await import(nodeModule);
|
96
|
+
return execSync(fullCmd, execOptions).toString().trim();
|
45
97
|
} catch (err) {
|
46
98
|
}
|
47
99
|
try {
|
48
100
|
if (isObject(Deno)) {
|
49
|
-
const process2 = Deno.run({
|
50
|
-
cmd: ["git", "branch", "--show-current"],
|
51
|
-
stdout: "piped",
|
52
|
-
stderr: "piped"
|
53
|
-
});
|
101
|
+
const process2 = Deno.run({ cmd, stdout: "piped", stderr: "null" });
|
54
102
|
return new TextDecoder().decode(await process2.output()).trim();
|
55
103
|
}
|
56
104
|
} catch (err) {
|
@@ -59,7 +107,8 @@ async function getGitBranch() {
|
|
59
107
|
|
60
108
|
function getAPIKey() {
|
61
109
|
try {
|
62
|
-
|
110
|
+
const { apiKey } = getEnvironment();
|
111
|
+
return apiKey;
|
63
112
|
} catch (err) {
|
64
113
|
return void 0;
|
65
114
|
}
|
@@ -74,7 +123,7 @@ function getFetchImplementation(userFetch) {
|
|
74
123
|
return fetchImpl;
|
75
124
|
}
|
76
125
|
|
77
|
-
const VERSION = "0.
|
126
|
+
const VERSION = "0.15.0";
|
78
127
|
|
79
128
|
class ErrorWithCause extends Error {
|
80
129
|
constructor(message, options) {
|
@@ -239,6 +288,7 @@ const removeWorkspaceMember = (variables) => fetch$1({
|
|
239
288
|
...variables
|
240
289
|
});
|
241
290
|
const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
|
291
|
+
const updateWorkspaceMemberInvite = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites/{inviteId}", method: "patch", ...variables });
|
242
292
|
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
243
293
|
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
244
294
|
method: "delete",
|
@@ -411,6 +461,7 @@ const operationsByTag = {
|
|
411
461
|
updateWorkspaceMemberRole,
|
412
462
|
removeWorkspaceMember,
|
413
463
|
inviteWorkspaceMember,
|
464
|
+
updateWorkspaceMemberInvite,
|
414
465
|
cancelWorkspaceMemberInvite,
|
415
466
|
resendWorkspaceMemberInvite,
|
416
467
|
acceptWorkspaceMemberInvite
|
@@ -500,7 +551,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
500
551
|
throw TypeError("Cannot add the same private member more than once");
|
501
552
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
502
553
|
};
|
503
|
-
var __privateSet$
|
554
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
504
555
|
__accessCheck$7(obj, member, "write to private field");
|
505
556
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
506
557
|
return value;
|
@@ -515,7 +566,7 @@ class XataApiClient {
|
|
515
566
|
if (!apiKey) {
|
516
567
|
throw new Error("Could not resolve a valid apiKey");
|
517
568
|
}
|
518
|
-
__privateSet$
|
569
|
+
__privateSet$7(this, _extraProps, {
|
519
570
|
apiUrl: getHostUrl(provider, "main"),
|
520
571
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
521
572
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -642,6 +693,13 @@ class WorkspaceApi {
|
|
642
693
|
...this.extraProps
|
643
694
|
});
|
644
695
|
}
|
696
|
+
updateWorkspaceMemberInvite(workspaceId, inviteId, role) {
|
697
|
+
return operationsByTag.workspaces.updateWorkspaceMemberInvite({
|
698
|
+
pathParams: { workspaceId, inviteId },
|
699
|
+
body: { role },
|
700
|
+
...this.extraProps
|
701
|
+
});
|
702
|
+
}
|
645
703
|
cancelWorkspaceMemberInvite(workspaceId, inviteId) {
|
646
704
|
return operationsByTag.workspaces.cancelWorkspaceMemberInvite({
|
647
705
|
pathParams: { workspaceId, inviteId },
|
@@ -952,7 +1010,7 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
952
1010
|
throw TypeError("Cannot add the same private member more than once");
|
953
1011
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
954
1012
|
};
|
955
|
-
var __privateSet$
|
1013
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
956
1014
|
__accessCheck$6(obj, member, "write to private field");
|
957
1015
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
958
1016
|
return value;
|
@@ -961,7 +1019,7 @@ var _query, _page;
|
|
961
1019
|
class Page {
|
962
1020
|
constructor(query, meta, records = []) {
|
963
1021
|
__privateAdd$6(this, _query, void 0);
|
964
|
-
__privateSet$
|
1022
|
+
__privateSet$6(this, _query, query);
|
965
1023
|
this.meta = meta;
|
966
1024
|
this.records = new RecordArray(this, records);
|
967
1025
|
}
|
@@ -990,10 +1048,10 @@ function isCursorPaginationOptions(options) {
|
|
990
1048
|
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
991
1049
|
}
|
992
1050
|
const _RecordArray = class extends Array {
|
993
|
-
constructor(
|
994
|
-
super(..._RecordArray.parseConstructorParams(
|
1051
|
+
constructor(...args) {
|
1052
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
995
1053
|
__privateAdd$6(this, _page, void 0);
|
996
|
-
__privateSet$
|
1054
|
+
__privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
|
997
1055
|
}
|
998
1056
|
static parseConstructorParams(...args) {
|
999
1057
|
if (args.length === 1 && typeof args[0] === "number") {
|
@@ -1005,6 +1063,12 @@ const _RecordArray = class extends Array {
|
|
1005
1063
|
}
|
1006
1064
|
return new Array(...args);
|
1007
1065
|
}
|
1066
|
+
toArray() {
|
1067
|
+
return new Array(...this);
|
1068
|
+
}
|
1069
|
+
map(callbackfn, thisArg) {
|
1070
|
+
return this.toArray().map(callbackfn, thisArg);
|
1071
|
+
}
|
1008
1072
|
async nextPage(size, offset) {
|
1009
1073
|
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1010
1074
|
return new _RecordArray(newPage);
|
@@ -1041,7 +1105,7 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
1041
1105
|
throw TypeError("Cannot add the same private member more than once");
|
1042
1106
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1043
1107
|
};
|
1044
|
-
var __privateSet$
|
1108
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
1045
1109
|
__accessCheck$5(obj, member, "write to private field");
|
1046
1110
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1047
1111
|
return value;
|
@@ -1054,11 +1118,11 @@ const _Query = class {
|
|
1054
1118
|
__privateAdd$5(this, _data, { filter: {} });
|
1055
1119
|
this.meta = { page: { cursor: "start", more: true } };
|
1056
1120
|
this.records = new RecordArray(this, []);
|
1057
|
-
__privateSet$
|
1121
|
+
__privateSet$5(this, _table$1, table);
|
1058
1122
|
if (repository) {
|
1059
|
-
__privateSet$
|
1123
|
+
__privateSet$5(this, _repository, repository);
|
1060
1124
|
} else {
|
1061
|
-
__privateSet$
|
1125
|
+
__privateSet$5(this, _repository, this);
|
1062
1126
|
}
|
1063
1127
|
const parent = cleanParent(data, rawParent);
|
1064
1128
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
@@ -1235,7 +1299,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1235
1299
|
throw TypeError("Cannot add the same private member more than once");
|
1236
1300
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1237
1301
|
};
|
1238
|
-
var __privateSet$
|
1302
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1239
1303
|
__accessCheck$4(obj, member, "write to private field");
|
1240
1304
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1241
1305
|
return value;
|
@@ -1244,7 +1308,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1244
1308
|
__accessCheck$4(obj, member, "access private method");
|
1245
1309
|
return method;
|
1246
1310
|
};
|
1247
|
-
var _table, _getFetchProps, _cache,
|
1311
|
+
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;
|
1248
1312
|
class Repository extends Query {
|
1249
1313
|
}
|
1250
1314
|
class RestRepository extends Query {
|
@@ -1261,43 +1325,24 @@ class RestRepository extends Query {
|
|
1261
1325
|
__privateAdd$4(this, _getCacheRecord);
|
1262
1326
|
__privateAdd$4(this, _setCacheQuery);
|
1263
1327
|
__privateAdd$4(this, _getCacheQuery);
|
1264
|
-
__privateAdd$4(this,
|
1328
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1265
1329
|
__privateAdd$4(this, _table, void 0);
|
1266
1330
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1267
1331
|
__privateAdd$4(this, _cache, void 0);
|
1268
|
-
__privateAdd$4(this,
|
1269
|
-
__privateSet$
|
1270
|
-
__privateSet$
|
1332
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1333
|
+
__privateSet$4(this, _table, options.table);
|
1334
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1271
1335
|
this.db = options.db;
|
1272
|
-
__privateSet$
|
1336
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1337
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1273
1338
|
}
|
1274
1339
|
async create(a, b) {
|
1275
1340
|
if (Array.isArray(a)) {
|
1276
1341
|
if (a.length === 0)
|
1277
1342
|
return [];
|
1278
|
-
const
|
1279
|
-
|
1280
|
-
|
1281
|
-
if (condition) {
|
1282
|
-
accWithIds.push(item);
|
1283
|
-
} else {
|
1284
|
-
accWithoutIds.push(item);
|
1285
|
-
}
|
1286
|
-
return [accWithoutIds, accWithIds, accOrder];
|
1287
|
-
}, [[], [], []]);
|
1288
|
-
const recordsWithoutId = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, itemsWithoutIds);
|
1289
|
-
await Promise.all(recordsWithoutId.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1290
|
-
if (itemsWithIds.length > 100) {
|
1291
|
-
console.warn("Bulk create operation with id is not optimized in the Xata API yet, this request might be slow");
|
1292
|
-
}
|
1293
|
-
const recordsWithId = await Promise.all(itemsWithIds.map((object) => this.create(object)));
|
1294
|
-
return order.map((condition) => {
|
1295
|
-
if (condition) {
|
1296
|
-
return recordsWithId.shift();
|
1297
|
-
} else {
|
1298
|
-
return recordsWithoutId.shift();
|
1299
|
-
}
|
1300
|
-
}).filter((record) => !!record);
|
1343
|
+
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1344
|
+
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1345
|
+
return records;
|
1301
1346
|
}
|
1302
1347
|
if (isString(a) && isObject(b)) {
|
1303
1348
|
if (a === "")
|
@@ -1338,8 +1383,8 @@ class RestRepository extends Query {
|
|
1338
1383
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1339
1384
|
...fetchProps
|
1340
1385
|
});
|
1341
|
-
const
|
1342
|
-
return initObject(this.db,
|
1386
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1387
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1343
1388
|
} catch (e) {
|
1344
1389
|
if (isObject(e) && e.status === 404) {
|
1345
1390
|
return null;
|
@@ -1428,8 +1473,8 @@ class RestRepository extends Query {
|
|
1428
1473
|
},
|
1429
1474
|
...fetchProps
|
1430
1475
|
});
|
1431
|
-
const
|
1432
|
-
return records.map((item) => initObject(this.db,
|
1476
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1477
|
+
return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
|
1433
1478
|
}
|
1434
1479
|
async query(query) {
|
1435
1480
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1448,8 +1493,8 @@ class RestRepository extends Query {
|
|
1448
1493
|
body,
|
1449
1494
|
...fetchProps
|
1450
1495
|
});
|
1451
|
-
const
|
1452
|
-
const records = objects.map((record) => initObject(this.db,
|
1496
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1497
|
+
const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
|
1453
1498
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1454
1499
|
return new Page(query, meta, records);
|
1455
1500
|
}
|
@@ -1457,7 +1502,7 @@ class RestRepository extends Query {
|
|
1457
1502
|
_table = new WeakMap();
|
1458
1503
|
_getFetchProps = new WeakMap();
|
1459
1504
|
_cache = new WeakMap();
|
1460
|
-
|
1505
|
+
_schemaTables$2 = new WeakMap();
|
1461
1506
|
_insertRecordWithoutId = new WeakSet();
|
1462
1507
|
insertRecordWithoutId_fn = async function(object) {
|
1463
1508
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
@@ -1502,16 +1547,20 @@ _bulkInsertTableRecords = new WeakSet();
|
|
1502
1547
|
bulkInsertTableRecords_fn = async function(objects) {
|
1503
1548
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1504
1549
|
const records = objects.map((object) => transformObjectLinks(object));
|
1505
|
-
const
|
1550
|
+
const { recordIDs } = await bulkInsertTableRecords({
|
1506
1551
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1507
1552
|
body: { records },
|
1508
1553
|
...fetchProps
|
1509
1554
|
});
|
1510
|
-
const finalObjects = await this.read(
|
1555
|
+
const finalObjects = await this.read(recordIDs);
|
1511
1556
|
if (finalObjects.length !== objects.length) {
|
1512
1557
|
throw new Error("The server failed to save some records");
|
1513
1558
|
}
|
1514
|
-
|
1559
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1560
|
+
acc[object.id] = object;
|
1561
|
+
return acc;
|
1562
|
+
}, {});
|
1563
|
+
return recordIDs.map((id) => dictionary[id]);
|
1515
1564
|
};
|
1516
1565
|
_updateRecordWithID = new WeakSet();
|
1517
1566
|
updateRecordWithID_fn = async function(recordId, object) {
|
@@ -1587,17 +1636,17 @@ getCacheQuery_fn = async function(query) {
|
|
1587
1636
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1588
1637
|
return hasExpired ? null : result;
|
1589
1638
|
};
|
1590
|
-
|
1591
|
-
|
1592
|
-
if (__privateGet$4(this,
|
1593
|
-
return __privateGet$4(this,
|
1639
|
+
_getSchemaTables$1 = new WeakSet();
|
1640
|
+
getSchemaTables_fn$1 = async function() {
|
1641
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1642
|
+
return __privateGet$4(this, _schemaTables$2);
|
1594
1643
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1595
1644
|
const { schema } = await getBranchDetails({
|
1596
1645
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1597
1646
|
...fetchProps
|
1598
1647
|
});
|
1599
|
-
__privateSet$
|
1600
|
-
return schema;
|
1648
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1649
|
+
return schema.tables;
|
1601
1650
|
};
|
1602
1651
|
const transformObjectLinks = (object) => {
|
1603
1652
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
@@ -1606,11 +1655,11 @@ const transformObjectLinks = (object) => {
|
|
1606
1655
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1607
1656
|
}, {});
|
1608
1657
|
};
|
1609
|
-
const initObject = (db,
|
1658
|
+
const initObject = (db, schemaTables, table, object) => {
|
1610
1659
|
const result = {};
|
1611
1660
|
const { xata, ...rest } = object ?? {};
|
1612
1661
|
Object.assign(result, rest);
|
1613
|
-
const { columns } =
|
1662
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1614
1663
|
if (!columns)
|
1615
1664
|
console.error(`Table ${table} not found in schema`);
|
1616
1665
|
for (const column of columns ?? []) {
|
@@ -1630,7 +1679,7 @@ const initObject = (db, schema, table, object) => {
|
|
1630
1679
|
if (!linkTable) {
|
1631
1680
|
console.error(`Failed to parse link for field ${column.name}`);
|
1632
1681
|
} else if (isObject(value)) {
|
1633
|
-
result[column.name] = initObject(db,
|
1682
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1634
1683
|
}
|
1635
1684
|
break;
|
1636
1685
|
}
|
@@ -1677,7 +1726,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1677
1726
|
throw TypeError("Cannot add the same private member more than once");
|
1678
1727
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1679
1728
|
};
|
1680
|
-
var __privateSet$
|
1729
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1681
1730
|
__accessCheck$3(obj, member, "write to private field");
|
1682
1731
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1683
1732
|
return value;
|
@@ -1686,7 +1735,7 @@ var _map;
|
|
1686
1735
|
class SimpleCache {
|
1687
1736
|
constructor(options = {}) {
|
1688
1737
|
__privateAdd$3(this, _map, void 0);
|
1689
|
-
__privateSet$
|
1738
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1690
1739
|
this.capacity = options.max ?? 500;
|
1691
1740
|
this.cacheRecords = options.cacheRecords ?? true;
|
1692
1741
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
@@ -1746,12 +1795,18 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1746
1795
|
throw TypeError("Cannot add the same private member more than once");
|
1747
1796
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1748
1797
|
};
|
1749
|
-
var
|
1798
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1799
|
+
__accessCheck$2(obj, member, "write to private field");
|
1800
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1801
|
+
return value;
|
1802
|
+
};
|
1803
|
+
var _tables, _schemaTables$1;
|
1750
1804
|
class SchemaPlugin extends XataPlugin {
|
1751
|
-
constructor(
|
1805
|
+
constructor(schemaTables) {
|
1752
1806
|
super();
|
1753
|
-
this.tableNames = tableNames;
|
1754
1807
|
__privateAdd$2(this, _tables, {});
|
1808
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1809
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1755
1810
|
}
|
1756
1811
|
build(pluginOptions) {
|
1757
1812
|
const db = new Proxy({}, {
|
@@ -1759,18 +1814,20 @@ class SchemaPlugin extends XataPlugin {
|
|
1759
1814
|
if (!isString(table))
|
1760
1815
|
throw new Error("Invalid table name");
|
1761
1816
|
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1762
|
-
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1817
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1763
1818
|
}
|
1764
1819
|
return __privateGet$2(this, _tables)[table];
|
1765
1820
|
}
|
1766
1821
|
});
|
1767
|
-
|
1768
|
-
|
1822
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1823
|
+
for (const table of tableNames) {
|
1824
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1769
1825
|
}
|
1770
1826
|
return db;
|
1771
1827
|
}
|
1772
1828
|
}
|
1773
1829
|
_tables = new WeakMap();
|
1830
|
+
_schemaTables$1 = new WeakMap();
|
1774
1831
|
|
1775
1832
|
var __accessCheck$1 = (obj, member, msg) => {
|
1776
1833
|
if (!member.has(obj))
|
@@ -1794,39 +1851,40 @@ var __privateMethod$1 = (obj, member, method) => {
|
|
1794
1851
|
__accessCheck$1(obj, member, "access private method");
|
1795
1852
|
return method;
|
1796
1853
|
};
|
1797
|
-
var
|
1854
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1798
1855
|
class SearchPlugin extends XataPlugin {
|
1799
|
-
constructor(db) {
|
1856
|
+
constructor(db, schemaTables) {
|
1800
1857
|
super();
|
1801
1858
|
this.db = db;
|
1802
1859
|
__privateAdd$1(this, _search);
|
1803
|
-
__privateAdd$1(this,
|
1804
|
-
__privateAdd$1(this,
|
1860
|
+
__privateAdd$1(this, _getSchemaTables);
|
1861
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1862
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1805
1863
|
}
|
1806
1864
|
build({ getFetchProps }) {
|
1807
1865
|
return {
|
1808
1866
|
all: async (query, options = {}) => {
|
1809
1867
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1810
|
-
const
|
1868
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1811
1869
|
return records.map((record) => {
|
1812
1870
|
const { table = "orphan" } = record.xata;
|
1813
|
-
return { table, record: initObject(this.db,
|
1871
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1814
1872
|
});
|
1815
1873
|
},
|
1816
1874
|
byTable: async (query, options = {}) => {
|
1817
1875
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1818
|
-
const
|
1876
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1819
1877
|
return records.reduce((acc, record) => {
|
1820
1878
|
const { table = "orphan" } = record.xata;
|
1821
1879
|
const items = acc[table] ?? [];
|
1822
|
-
const item = initObject(this.db,
|
1880
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1823
1881
|
return { ...acc, [table]: [...items, item] };
|
1824
1882
|
}, {});
|
1825
1883
|
}
|
1826
1884
|
};
|
1827
1885
|
}
|
1828
1886
|
}
|
1829
|
-
|
1887
|
+
_schemaTables = new WeakMap();
|
1830
1888
|
_search = new WeakSet();
|
1831
1889
|
search_fn = async function(query, options, getFetchProps) {
|
1832
1890
|
const fetchProps = await getFetchProps();
|
@@ -1838,38 +1896,32 @@ search_fn = async function(query, options, getFetchProps) {
|
|
1838
1896
|
});
|
1839
1897
|
return records;
|
1840
1898
|
};
|
1841
|
-
|
1842
|
-
|
1843
|
-
if (__privateGet$1(this,
|
1844
|
-
return __privateGet$1(this,
|
1899
|
+
_getSchemaTables = new WeakSet();
|
1900
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1901
|
+
if (__privateGet$1(this, _schemaTables))
|
1902
|
+
return __privateGet$1(this, _schemaTables);
|
1845
1903
|
const fetchProps = await getFetchProps();
|
1846
1904
|
const { schema } = await getBranchDetails({
|
1847
1905
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1848
1906
|
...fetchProps
|
1849
1907
|
});
|
1850
|
-
__privateSet$1(this,
|
1851
|
-
return schema;
|
1908
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1909
|
+
return schema.tables;
|
1852
1910
|
};
|
1853
1911
|
|
1854
1912
|
const isBranchStrategyBuilder = (strategy) => {
|
1855
1913
|
return typeof strategy === "function";
|
1856
1914
|
};
|
1857
1915
|
|
1858
|
-
const envBranchNames = [
|
1859
|
-
"XATA_BRANCH",
|
1860
|
-
"VERCEL_GIT_COMMIT_REF",
|
1861
|
-
"CF_PAGES_BRANCH",
|
1862
|
-
"BRANCH"
|
1863
|
-
];
|
1864
1916
|
async function getCurrentBranchName(options) {
|
1865
|
-
const
|
1866
|
-
if (
|
1867
|
-
const details = await getDatabaseBranch(
|
1917
|
+
const { branch, envBranch } = getEnvironment();
|
1918
|
+
if (branch) {
|
1919
|
+
const details = await getDatabaseBranch(branch, options);
|
1868
1920
|
if (details)
|
1869
|
-
return
|
1870
|
-
console.warn(`Branch ${
|
1921
|
+
return branch;
|
1922
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1871
1923
|
}
|
1872
|
-
const gitBranch = await getGitBranch();
|
1924
|
+
const gitBranch = envBranch || await getGitBranch();
|
1873
1925
|
return resolveXataBranch(gitBranch, options);
|
1874
1926
|
}
|
1875
1927
|
async function getCurrentBranchDetails(options) {
|
@@ -1885,13 +1937,14 @@ async function resolveXataBranch(gitBranch, options) {
|
|
1885
1937
|
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1886
1938
|
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1887
1939
|
const [workspace] = host.split(".");
|
1940
|
+
const { fallbackBranch } = getEnvironment();
|
1888
1941
|
const { branch } = await resolveBranch({
|
1889
1942
|
apiKey,
|
1890
1943
|
apiUrl: databaseURL,
|
1891
1944
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1892
1945
|
workspacesApiUrl: `${protocol}//${host}`,
|
1893
1946
|
pathParams: { dbName, workspace },
|
1894
|
-
queryParams: { gitBranch, fallbackBranch
|
1947
|
+
queryParams: { gitBranch, fallbackBranch }
|
1895
1948
|
});
|
1896
1949
|
return branch;
|
1897
1950
|
}
|
@@ -1919,21 +1972,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1919
1972
|
throw err;
|
1920
1973
|
}
|
1921
1974
|
}
|
1922
|
-
function getBranchByEnvVariable() {
|
1923
|
-
for (const name of envBranchNames) {
|
1924
|
-
const value = getEnvVariable(name);
|
1925
|
-
if (value) {
|
1926
|
-
return value;
|
1927
|
-
}
|
1928
|
-
}
|
1929
|
-
try {
|
1930
|
-
return XATA_BRANCH;
|
1931
|
-
} catch (err) {
|
1932
|
-
}
|
1933
|
-
}
|
1934
1975
|
function getDatabaseURL() {
|
1935
1976
|
try {
|
1936
|
-
|
1977
|
+
const { databaseURL } = getEnvironment();
|
1978
|
+
return databaseURL;
|
1937
1979
|
} catch (err) {
|
1938
1980
|
return void 0;
|
1939
1981
|
}
|
@@ -1964,7 +2006,7 @@ var __privateMethod = (obj, member, method) => {
|
|
1964
2006
|
const buildClient = (plugins) => {
|
1965
2007
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1966
2008
|
return _a = class {
|
1967
|
-
constructor(options = {},
|
2009
|
+
constructor(options = {}, schemaTables) {
|
1968
2010
|
__privateAdd(this, _parseOptions);
|
1969
2011
|
__privateAdd(this, _getFetchProps);
|
1970
2012
|
__privateAdd(this, _evaluateBranch);
|
@@ -1974,8 +2016,8 @@ const buildClient = (plugins) => {
|
|
1974
2016
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1975
2017
|
cache: safeOptions.cache
|
1976
2018
|
};
|
1977
|
-
const db = new SchemaPlugin(
|
1978
|
-
const search = new SearchPlugin(db).build(pluginOptions);
|
2019
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2020
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1979
2021
|
this.db = db;
|
1980
2022
|
this.search = search;
|
1981
2023
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
@@ -2048,5 +2090,5 @@ class XataError extends Error {
|
|
2048
2090
|
}
|
2049
2091
|
}
|
2050
2092
|
|
2051
|
-
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, RecordArray, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
2093
|
+
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, RecordArray, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberInvite, updateWorkspaceMemberRole, upsertRecordWithID };
|
2052
2094
|
//# sourceMappingURL=index.mjs.map
|