@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.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,83 @@ function toBase64(value) {
|
|
26
44
|
}
|
27
45
|
}
|
28
46
|
|
29
|
-
function
|
47
|
+
function getEnvironment() {
|
30
48
|
try {
|
31
|
-
if (isObject(process) &&
|
32
|
-
return
|
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) &&
|
38
|
-
return
|
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 fullCmd = cmd.join(" ");
|
111
|
+
const nodeModule = ["child", "process"].join("_");
|
112
|
+
const execOptions = { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] };
|
44
113
|
try {
|
45
114
|
if (typeof require === "function") {
|
46
|
-
|
47
|
-
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
115
|
+
return require(nodeModule).execSync(fullCmd, execOptions).trim();
|
48
116
|
}
|
117
|
+
const { execSync } = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(nodeModule);
|
118
|
+
return execSync(fullCmd, execOptions).toString().trim();
|
49
119
|
} catch (err) {
|
50
120
|
}
|
51
121
|
try {
|
52
122
|
if (isObject(Deno)) {
|
53
|
-
const process2 = Deno.run({
|
54
|
-
cmd: ["git", "branch", "--show-current"],
|
55
|
-
stdout: "piped",
|
56
|
-
stderr: "piped"
|
57
|
-
});
|
123
|
+
const process2 = Deno.run({ cmd, stdout: "piped", stderr: "null" });
|
58
124
|
return new TextDecoder().decode(await process2.output()).trim();
|
59
125
|
}
|
60
126
|
} catch (err) {
|
@@ -63,7 +129,8 @@ async function getGitBranch() {
|
|
63
129
|
|
64
130
|
function getAPIKey() {
|
65
131
|
try {
|
66
|
-
|
132
|
+
const { apiKey } = getEnvironment();
|
133
|
+
return apiKey;
|
67
134
|
} catch (err) {
|
68
135
|
return void 0;
|
69
136
|
}
|
@@ -78,7 +145,7 @@ function getFetchImplementation(userFetch) {
|
|
78
145
|
return fetchImpl;
|
79
146
|
}
|
80
147
|
|
81
|
-
const VERSION = "0.
|
148
|
+
const VERSION = "0.15.0";
|
82
149
|
|
83
150
|
class ErrorWithCause extends Error {
|
84
151
|
constructor(message, options) {
|
@@ -243,6 +310,7 @@ const removeWorkspaceMember = (variables) => fetch$1({
|
|
243
310
|
...variables
|
244
311
|
});
|
245
312
|
const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
|
313
|
+
const updateWorkspaceMemberInvite = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites/{inviteId}", method: "patch", ...variables });
|
246
314
|
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
247
315
|
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
248
316
|
method: "delete",
|
@@ -415,6 +483,7 @@ const operationsByTag = {
|
|
415
483
|
updateWorkspaceMemberRole,
|
416
484
|
removeWorkspaceMember,
|
417
485
|
inviteWorkspaceMember,
|
486
|
+
updateWorkspaceMemberInvite,
|
418
487
|
cancelWorkspaceMemberInvite,
|
419
488
|
resendWorkspaceMemberInvite,
|
420
489
|
acceptWorkspaceMemberInvite
|
@@ -504,7 +573,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
504
573
|
throw TypeError("Cannot add the same private member more than once");
|
505
574
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
506
575
|
};
|
507
|
-
var __privateSet$
|
576
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
508
577
|
__accessCheck$7(obj, member, "write to private field");
|
509
578
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
510
579
|
return value;
|
@@ -519,7 +588,7 @@ class XataApiClient {
|
|
519
588
|
if (!apiKey) {
|
520
589
|
throw new Error("Could not resolve a valid apiKey");
|
521
590
|
}
|
522
|
-
__privateSet$
|
591
|
+
__privateSet$7(this, _extraProps, {
|
523
592
|
apiUrl: getHostUrl(provider, "main"),
|
524
593
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
525
594
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -646,6 +715,13 @@ class WorkspaceApi {
|
|
646
715
|
...this.extraProps
|
647
716
|
});
|
648
717
|
}
|
718
|
+
updateWorkspaceMemberInvite(workspaceId, inviteId, role) {
|
719
|
+
return operationsByTag.workspaces.updateWorkspaceMemberInvite({
|
720
|
+
pathParams: { workspaceId, inviteId },
|
721
|
+
body: { role },
|
722
|
+
...this.extraProps
|
723
|
+
});
|
724
|
+
}
|
649
725
|
cancelWorkspaceMemberInvite(workspaceId, inviteId) {
|
650
726
|
return operationsByTag.workspaces.cancelWorkspaceMemberInvite({
|
651
727
|
pathParams: { workspaceId, inviteId },
|
@@ -956,7 +1032,7 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
956
1032
|
throw TypeError("Cannot add the same private member more than once");
|
957
1033
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
958
1034
|
};
|
959
|
-
var __privateSet$
|
1035
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
960
1036
|
__accessCheck$6(obj, member, "write to private field");
|
961
1037
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
962
1038
|
return value;
|
@@ -965,7 +1041,7 @@ var _query, _page;
|
|
965
1041
|
class Page {
|
966
1042
|
constructor(query, meta, records = []) {
|
967
1043
|
__privateAdd$6(this, _query, void 0);
|
968
|
-
__privateSet$
|
1044
|
+
__privateSet$6(this, _query, query);
|
969
1045
|
this.meta = meta;
|
970
1046
|
this.records = new RecordArray(this, records);
|
971
1047
|
}
|
@@ -994,10 +1070,10 @@ function isCursorPaginationOptions(options) {
|
|
994
1070
|
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
995
1071
|
}
|
996
1072
|
const _RecordArray = class extends Array {
|
997
|
-
constructor(
|
998
|
-
super(..._RecordArray.parseConstructorParams(
|
1073
|
+
constructor(...args) {
|
1074
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
999
1075
|
__privateAdd$6(this, _page, void 0);
|
1000
|
-
__privateSet$
|
1076
|
+
__privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
|
1001
1077
|
}
|
1002
1078
|
static parseConstructorParams(...args) {
|
1003
1079
|
if (args.length === 1 && typeof args[0] === "number") {
|
@@ -1009,6 +1085,12 @@ const _RecordArray = class extends Array {
|
|
1009
1085
|
}
|
1010
1086
|
return new Array(...args);
|
1011
1087
|
}
|
1088
|
+
toArray() {
|
1089
|
+
return new Array(...this);
|
1090
|
+
}
|
1091
|
+
map(callbackfn, thisArg) {
|
1092
|
+
return this.toArray().map(callbackfn, thisArg);
|
1093
|
+
}
|
1012
1094
|
async nextPage(size, offset) {
|
1013
1095
|
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1014
1096
|
return new _RecordArray(newPage);
|
@@ -1045,7 +1127,7 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
1045
1127
|
throw TypeError("Cannot add the same private member more than once");
|
1046
1128
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1047
1129
|
};
|
1048
|
-
var __privateSet$
|
1130
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
1049
1131
|
__accessCheck$5(obj, member, "write to private field");
|
1050
1132
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1051
1133
|
return value;
|
@@ -1058,11 +1140,11 @@ const _Query = class {
|
|
1058
1140
|
__privateAdd$5(this, _data, { filter: {} });
|
1059
1141
|
this.meta = { page: { cursor: "start", more: true } };
|
1060
1142
|
this.records = new RecordArray(this, []);
|
1061
|
-
__privateSet$
|
1143
|
+
__privateSet$5(this, _table$1, table);
|
1062
1144
|
if (repository) {
|
1063
|
-
__privateSet$
|
1145
|
+
__privateSet$5(this, _repository, repository);
|
1064
1146
|
} else {
|
1065
|
-
__privateSet$
|
1147
|
+
__privateSet$5(this, _repository, this);
|
1066
1148
|
}
|
1067
1149
|
const parent = cleanParent(data, rawParent);
|
1068
1150
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
@@ -1239,7 +1321,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1239
1321
|
throw TypeError("Cannot add the same private member more than once");
|
1240
1322
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1241
1323
|
};
|
1242
|
-
var __privateSet$
|
1324
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1243
1325
|
__accessCheck$4(obj, member, "write to private field");
|
1244
1326
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1245
1327
|
return value;
|
@@ -1248,7 +1330,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1248
1330
|
__accessCheck$4(obj, member, "access private method");
|
1249
1331
|
return method;
|
1250
1332
|
};
|
1251
|
-
var _table, _getFetchProps, _cache,
|
1333
|
+
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
1334
|
class Repository extends Query {
|
1253
1335
|
}
|
1254
1336
|
class RestRepository extends Query {
|
@@ -1265,43 +1347,24 @@ class RestRepository extends Query {
|
|
1265
1347
|
__privateAdd$4(this, _getCacheRecord);
|
1266
1348
|
__privateAdd$4(this, _setCacheQuery);
|
1267
1349
|
__privateAdd$4(this, _getCacheQuery);
|
1268
|
-
__privateAdd$4(this,
|
1350
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1269
1351
|
__privateAdd$4(this, _table, void 0);
|
1270
1352
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1271
1353
|
__privateAdd$4(this, _cache, void 0);
|
1272
|
-
__privateAdd$4(this,
|
1273
|
-
__privateSet$
|
1274
|
-
__privateSet$
|
1354
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1355
|
+
__privateSet$4(this, _table, options.table);
|
1356
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1275
1357
|
this.db = options.db;
|
1276
|
-
__privateSet$
|
1358
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1359
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1277
1360
|
}
|
1278
1361
|
async create(a, b) {
|
1279
1362
|
if (Array.isArray(a)) {
|
1280
1363
|
if (a.length === 0)
|
1281
1364
|
return [];
|
1282
|
-
const
|
1283
|
-
|
1284
|
-
|
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);
|
1365
|
+
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1366
|
+
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1367
|
+
return records;
|
1305
1368
|
}
|
1306
1369
|
if (isString(a) && isObject(b)) {
|
1307
1370
|
if (a === "")
|
@@ -1342,8 +1405,8 @@ class RestRepository extends Query {
|
|
1342
1405
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1343
1406
|
...fetchProps
|
1344
1407
|
});
|
1345
|
-
const
|
1346
|
-
return initObject(this.db,
|
1408
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1409
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1347
1410
|
} catch (e) {
|
1348
1411
|
if (isObject(e) && e.status === 404) {
|
1349
1412
|
return null;
|
@@ -1432,8 +1495,8 @@ class RestRepository extends Query {
|
|
1432
1495
|
},
|
1433
1496
|
...fetchProps
|
1434
1497
|
});
|
1435
|
-
const
|
1436
|
-
return records.map((item) => initObject(this.db,
|
1498
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1499
|
+
return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
|
1437
1500
|
}
|
1438
1501
|
async query(query) {
|
1439
1502
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1452,8 +1515,8 @@ class RestRepository extends Query {
|
|
1452
1515
|
body,
|
1453
1516
|
...fetchProps
|
1454
1517
|
});
|
1455
|
-
const
|
1456
|
-
const records = objects.map((record) => initObject(this.db,
|
1518
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1519
|
+
const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
|
1457
1520
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1458
1521
|
return new Page(query, meta, records);
|
1459
1522
|
}
|
@@ -1461,7 +1524,7 @@ class RestRepository extends Query {
|
|
1461
1524
|
_table = new WeakMap();
|
1462
1525
|
_getFetchProps = new WeakMap();
|
1463
1526
|
_cache = new WeakMap();
|
1464
|
-
|
1527
|
+
_schemaTables$2 = new WeakMap();
|
1465
1528
|
_insertRecordWithoutId = new WeakSet();
|
1466
1529
|
insertRecordWithoutId_fn = async function(object) {
|
1467
1530
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
@@ -1506,16 +1569,20 @@ _bulkInsertTableRecords = new WeakSet();
|
|
1506
1569
|
bulkInsertTableRecords_fn = async function(objects) {
|
1507
1570
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1508
1571
|
const records = objects.map((object) => transformObjectLinks(object));
|
1509
|
-
const
|
1572
|
+
const { recordIDs } = await bulkInsertTableRecords({
|
1510
1573
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1511
1574
|
body: { records },
|
1512
1575
|
...fetchProps
|
1513
1576
|
});
|
1514
|
-
const finalObjects = await this.read(
|
1577
|
+
const finalObjects = await this.read(recordIDs);
|
1515
1578
|
if (finalObjects.length !== objects.length) {
|
1516
1579
|
throw new Error("The server failed to save some records");
|
1517
1580
|
}
|
1518
|
-
|
1581
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1582
|
+
acc[object.id] = object;
|
1583
|
+
return acc;
|
1584
|
+
}, {});
|
1585
|
+
return recordIDs.map((id) => dictionary[id]);
|
1519
1586
|
};
|
1520
1587
|
_updateRecordWithID = new WeakSet();
|
1521
1588
|
updateRecordWithID_fn = async function(recordId, object) {
|
@@ -1591,17 +1658,17 @@ getCacheQuery_fn = async function(query) {
|
|
1591
1658
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1592
1659
|
return hasExpired ? null : result;
|
1593
1660
|
};
|
1594
|
-
|
1595
|
-
|
1596
|
-
if (__privateGet$4(this,
|
1597
|
-
return __privateGet$4(this,
|
1661
|
+
_getSchemaTables$1 = new WeakSet();
|
1662
|
+
getSchemaTables_fn$1 = async function() {
|
1663
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1664
|
+
return __privateGet$4(this, _schemaTables$2);
|
1598
1665
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1599
1666
|
const { schema } = await getBranchDetails({
|
1600
1667
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1601
1668
|
...fetchProps
|
1602
1669
|
});
|
1603
|
-
__privateSet$
|
1604
|
-
return schema;
|
1670
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1671
|
+
return schema.tables;
|
1605
1672
|
};
|
1606
1673
|
const transformObjectLinks = (object) => {
|
1607
1674
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
@@ -1610,11 +1677,11 @@ const transformObjectLinks = (object) => {
|
|
1610
1677
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1611
1678
|
}, {});
|
1612
1679
|
};
|
1613
|
-
const initObject = (db,
|
1680
|
+
const initObject = (db, schemaTables, table, object) => {
|
1614
1681
|
const result = {};
|
1615
1682
|
const { xata, ...rest } = object ?? {};
|
1616
1683
|
Object.assign(result, rest);
|
1617
|
-
const { columns } =
|
1684
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1618
1685
|
if (!columns)
|
1619
1686
|
console.error(`Table ${table} not found in schema`);
|
1620
1687
|
for (const column of columns ?? []) {
|
@@ -1634,7 +1701,7 @@ const initObject = (db, schema, table, object) => {
|
|
1634
1701
|
if (!linkTable) {
|
1635
1702
|
console.error(`Failed to parse link for field ${column.name}`);
|
1636
1703
|
} else if (isObject(value)) {
|
1637
|
-
result[column.name] = initObject(db,
|
1704
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1638
1705
|
}
|
1639
1706
|
break;
|
1640
1707
|
}
|
@@ -1681,7 +1748,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1681
1748
|
throw TypeError("Cannot add the same private member more than once");
|
1682
1749
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1683
1750
|
};
|
1684
|
-
var __privateSet$
|
1751
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1685
1752
|
__accessCheck$3(obj, member, "write to private field");
|
1686
1753
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1687
1754
|
return value;
|
@@ -1690,7 +1757,7 @@ var _map;
|
|
1690
1757
|
class SimpleCache {
|
1691
1758
|
constructor(options = {}) {
|
1692
1759
|
__privateAdd$3(this, _map, void 0);
|
1693
|
-
__privateSet$
|
1760
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1694
1761
|
this.capacity = options.max ?? 500;
|
1695
1762
|
this.cacheRecords = options.cacheRecords ?? true;
|
1696
1763
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
@@ -1750,12 +1817,18 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1750
1817
|
throw TypeError("Cannot add the same private member more than once");
|
1751
1818
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1752
1819
|
};
|
1753
|
-
var
|
1820
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1821
|
+
__accessCheck$2(obj, member, "write to private field");
|
1822
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1823
|
+
return value;
|
1824
|
+
};
|
1825
|
+
var _tables, _schemaTables$1;
|
1754
1826
|
class SchemaPlugin extends XataPlugin {
|
1755
|
-
constructor(
|
1827
|
+
constructor(schemaTables) {
|
1756
1828
|
super();
|
1757
|
-
this.tableNames = tableNames;
|
1758
1829
|
__privateAdd$2(this, _tables, {});
|
1830
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1831
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1759
1832
|
}
|
1760
1833
|
build(pluginOptions) {
|
1761
1834
|
const db = new Proxy({}, {
|
@@ -1763,18 +1836,20 @@ class SchemaPlugin extends XataPlugin {
|
|
1763
1836
|
if (!isString(table))
|
1764
1837
|
throw new Error("Invalid table name");
|
1765
1838
|
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1766
|
-
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1839
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1767
1840
|
}
|
1768
1841
|
return __privateGet$2(this, _tables)[table];
|
1769
1842
|
}
|
1770
1843
|
});
|
1771
|
-
|
1772
|
-
|
1844
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1845
|
+
for (const table of tableNames) {
|
1846
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1773
1847
|
}
|
1774
1848
|
return db;
|
1775
1849
|
}
|
1776
1850
|
}
|
1777
1851
|
_tables = new WeakMap();
|
1852
|
+
_schemaTables$1 = new WeakMap();
|
1778
1853
|
|
1779
1854
|
var __accessCheck$1 = (obj, member, msg) => {
|
1780
1855
|
if (!member.has(obj))
|
@@ -1798,39 +1873,40 @@ var __privateMethod$1 = (obj, member, method) => {
|
|
1798
1873
|
__accessCheck$1(obj, member, "access private method");
|
1799
1874
|
return method;
|
1800
1875
|
};
|
1801
|
-
var
|
1876
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1802
1877
|
class SearchPlugin extends XataPlugin {
|
1803
|
-
constructor(db) {
|
1878
|
+
constructor(db, schemaTables) {
|
1804
1879
|
super();
|
1805
1880
|
this.db = db;
|
1806
1881
|
__privateAdd$1(this, _search);
|
1807
|
-
__privateAdd$1(this,
|
1808
|
-
__privateAdd$1(this,
|
1882
|
+
__privateAdd$1(this, _getSchemaTables);
|
1883
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1884
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1809
1885
|
}
|
1810
1886
|
build({ getFetchProps }) {
|
1811
1887
|
return {
|
1812
1888
|
all: async (query, options = {}) => {
|
1813
1889
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1814
|
-
const
|
1890
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1815
1891
|
return records.map((record) => {
|
1816
1892
|
const { table = "orphan" } = record.xata;
|
1817
|
-
return { table, record: initObject(this.db,
|
1893
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1818
1894
|
});
|
1819
1895
|
},
|
1820
1896
|
byTable: async (query, options = {}) => {
|
1821
1897
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1822
|
-
const
|
1898
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1823
1899
|
return records.reduce((acc, record) => {
|
1824
1900
|
const { table = "orphan" } = record.xata;
|
1825
1901
|
const items = acc[table] ?? [];
|
1826
|
-
const item = initObject(this.db,
|
1902
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1827
1903
|
return { ...acc, [table]: [...items, item] };
|
1828
1904
|
}, {});
|
1829
1905
|
}
|
1830
1906
|
};
|
1831
1907
|
}
|
1832
1908
|
}
|
1833
|
-
|
1909
|
+
_schemaTables = new WeakMap();
|
1834
1910
|
_search = new WeakSet();
|
1835
1911
|
search_fn = async function(query, options, getFetchProps) {
|
1836
1912
|
const fetchProps = await getFetchProps();
|
@@ -1842,38 +1918,32 @@ search_fn = async function(query, options, getFetchProps) {
|
|
1842
1918
|
});
|
1843
1919
|
return records;
|
1844
1920
|
};
|
1845
|
-
|
1846
|
-
|
1847
|
-
if (__privateGet$1(this,
|
1848
|
-
return __privateGet$1(this,
|
1921
|
+
_getSchemaTables = new WeakSet();
|
1922
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1923
|
+
if (__privateGet$1(this, _schemaTables))
|
1924
|
+
return __privateGet$1(this, _schemaTables);
|
1849
1925
|
const fetchProps = await getFetchProps();
|
1850
1926
|
const { schema } = await getBranchDetails({
|
1851
1927
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1852
1928
|
...fetchProps
|
1853
1929
|
});
|
1854
|
-
__privateSet$1(this,
|
1855
|
-
return schema;
|
1930
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1931
|
+
return schema.tables;
|
1856
1932
|
};
|
1857
1933
|
|
1858
1934
|
const isBranchStrategyBuilder = (strategy) => {
|
1859
1935
|
return typeof strategy === "function";
|
1860
1936
|
};
|
1861
1937
|
|
1862
|
-
const envBranchNames = [
|
1863
|
-
"XATA_BRANCH",
|
1864
|
-
"VERCEL_GIT_COMMIT_REF",
|
1865
|
-
"CF_PAGES_BRANCH",
|
1866
|
-
"BRANCH"
|
1867
|
-
];
|
1868
1938
|
async function getCurrentBranchName(options) {
|
1869
|
-
const
|
1870
|
-
if (
|
1871
|
-
const details = await getDatabaseBranch(
|
1939
|
+
const { branch, envBranch } = getEnvironment();
|
1940
|
+
if (branch) {
|
1941
|
+
const details = await getDatabaseBranch(branch, options);
|
1872
1942
|
if (details)
|
1873
|
-
return
|
1874
|
-
console.warn(`Branch ${
|
1943
|
+
return branch;
|
1944
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1875
1945
|
}
|
1876
|
-
const gitBranch = await getGitBranch();
|
1946
|
+
const gitBranch = envBranch || await getGitBranch();
|
1877
1947
|
return resolveXataBranch(gitBranch, options);
|
1878
1948
|
}
|
1879
1949
|
async function getCurrentBranchDetails(options) {
|
@@ -1889,13 +1959,14 @@ async function resolveXataBranch(gitBranch, options) {
|
|
1889
1959
|
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1890
1960
|
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1891
1961
|
const [workspace] = host.split(".");
|
1962
|
+
const { fallbackBranch } = getEnvironment();
|
1892
1963
|
const { branch } = await resolveBranch({
|
1893
1964
|
apiKey,
|
1894
1965
|
apiUrl: databaseURL,
|
1895
1966
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1896
1967
|
workspacesApiUrl: `${protocol}//${host}`,
|
1897
1968
|
pathParams: { dbName, workspace },
|
1898
|
-
queryParams: { gitBranch, fallbackBranch
|
1969
|
+
queryParams: { gitBranch, fallbackBranch }
|
1899
1970
|
});
|
1900
1971
|
return branch;
|
1901
1972
|
}
|
@@ -1923,21 +1994,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1923
1994
|
throw err;
|
1924
1995
|
}
|
1925
1996
|
}
|
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
1997
|
function getDatabaseURL() {
|
1939
1998
|
try {
|
1940
|
-
|
1999
|
+
const { databaseURL } = getEnvironment();
|
2000
|
+
return databaseURL;
|
1941
2001
|
} catch (err) {
|
1942
2002
|
return void 0;
|
1943
2003
|
}
|
@@ -1968,7 +2028,7 @@ var __privateMethod = (obj, member, method) => {
|
|
1968
2028
|
const buildClient = (plugins) => {
|
1969
2029
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1970
2030
|
return _a = class {
|
1971
|
-
constructor(options = {},
|
2031
|
+
constructor(options = {}, schemaTables) {
|
1972
2032
|
__privateAdd(this, _parseOptions);
|
1973
2033
|
__privateAdd(this, _getFetchProps);
|
1974
2034
|
__privateAdd(this, _evaluateBranch);
|
@@ -1978,8 +2038,8 @@ const buildClient = (plugins) => {
|
|
1978
2038
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1979
2039
|
cache: safeOptions.cache
|
1980
2040
|
};
|
1981
|
-
const db = new SchemaPlugin(
|
1982
|
-
const search = new SearchPlugin(db).build(pluginOptions);
|
2041
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2042
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1983
2043
|
this.db = db;
|
1984
2044
|
this.search = search;
|
1985
2045
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
@@ -2150,6 +2210,7 @@ exports.updateRecordWithID = updateRecordWithID;
|
|
2150
2210
|
exports.updateTable = updateTable;
|
2151
2211
|
exports.updateUser = updateUser;
|
2152
2212
|
exports.updateWorkspace = updateWorkspace;
|
2213
|
+
exports.updateWorkspaceMemberInvite = updateWorkspaceMemberInvite;
|
2153
2214
|
exports.updateWorkspaceMemberRole = updateWorkspaceMemberRole;
|
2154
2215
|
exports.upsertRecordWithID = upsertRecordWithID;
|
2155
2216
|
//# sourceMappingURL=index.cjs.map
|