@xata.io/client 0.13.4 → 0.16.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 +40 -0
- package/Usage.md +33 -0
- package/dist/index.cjs +256 -244
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +476 -138
- package/dist/index.mjs +238 -245
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
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
|
}
|
@@ -17,6 +35,9 @@ function isDefined(value) {
|
|
17
35
|
function isString(value) {
|
18
36
|
return isDefined(value) && typeof value === "string";
|
19
37
|
}
|
38
|
+
function isStringArray(value) {
|
39
|
+
return isDefined(value) && Array.isArray(value) && value.every(isString);
|
40
|
+
}
|
20
41
|
function toBase64(value) {
|
21
42
|
try {
|
22
43
|
return btoa(value);
|
@@ -26,35 +47,83 @@ function toBase64(value) {
|
|
26
47
|
}
|
27
48
|
}
|
28
49
|
|
29
|
-
function
|
50
|
+
function getEnvironment() {
|
30
51
|
try {
|
31
|
-
if (isObject(process) &&
|
32
|
-
return
|
52
|
+
if (isObject(process) && isObject(process.env)) {
|
53
|
+
return {
|
54
|
+
apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
|
55
|
+
databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
|
56
|
+
branch: process.env.XATA_BRANCH ?? getGlobalBranch(),
|
57
|
+
envBranch: process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH,
|
58
|
+
fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
|
59
|
+
};
|
33
60
|
}
|
34
61
|
} catch (err) {
|
35
62
|
}
|
36
63
|
try {
|
37
|
-
if (isObject(Deno) &&
|
38
|
-
return
|
64
|
+
if (isObject(Deno) && isObject(Deno.env)) {
|
65
|
+
return {
|
66
|
+
apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
|
67
|
+
databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
|
68
|
+
branch: Deno.env.get("XATA_BRANCH") ?? getGlobalBranch(),
|
69
|
+
envBranch: Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH"),
|
70
|
+
fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
|
71
|
+
};
|
39
72
|
}
|
40
73
|
} catch (err) {
|
41
74
|
}
|
75
|
+
return {
|
76
|
+
apiKey: getGlobalApiKey(),
|
77
|
+
databaseURL: getGlobalDatabaseURL(),
|
78
|
+
branch: getGlobalBranch(),
|
79
|
+
envBranch: void 0,
|
80
|
+
fallbackBranch: getGlobalFallbackBranch()
|
81
|
+
};
|
82
|
+
}
|
83
|
+
function getGlobalApiKey() {
|
84
|
+
try {
|
85
|
+
return XATA_API_KEY;
|
86
|
+
} catch (err) {
|
87
|
+
return void 0;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
function getGlobalDatabaseURL() {
|
91
|
+
try {
|
92
|
+
return XATA_DATABASE_URL;
|
93
|
+
} catch (err) {
|
94
|
+
return void 0;
|
95
|
+
}
|
96
|
+
}
|
97
|
+
function getGlobalBranch() {
|
98
|
+
try {
|
99
|
+
return XATA_BRANCH;
|
100
|
+
} catch (err) {
|
101
|
+
return void 0;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
function getGlobalFallbackBranch() {
|
105
|
+
try {
|
106
|
+
return XATA_FALLBACK_BRANCH;
|
107
|
+
} catch (err) {
|
108
|
+
return void 0;
|
109
|
+
}
|
42
110
|
}
|
43
111
|
async function getGitBranch() {
|
112
|
+
const cmd = ["git", "branch", "--show-current"];
|
113
|
+
const fullCmd = cmd.join(" ");
|
114
|
+
const nodeModule = ["child", "process"].join("_");
|
115
|
+
const execOptions = { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] };
|
44
116
|
try {
|
45
117
|
if (typeof require === "function") {
|
46
|
-
|
47
|
-
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
118
|
+
return require(nodeModule).execSync(fullCmd, execOptions).trim();
|
48
119
|
}
|
120
|
+
const { execSync } = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(nodeModule);
|
121
|
+
return execSync(fullCmd, execOptions).toString().trim();
|
49
122
|
} catch (err) {
|
50
123
|
}
|
51
124
|
try {
|
52
125
|
if (isObject(Deno)) {
|
53
|
-
const process2 = Deno.run({
|
54
|
-
cmd: ["git", "branch", "--show-current"],
|
55
|
-
stdout: "piped",
|
56
|
-
stderr: "piped"
|
57
|
-
});
|
126
|
+
const process2 = Deno.run({ cmd, stdout: "piped", stderr: "null" });
|
58
127
|
return new TextDecoder().decode(await process2.output()).trim();
|
59
128
|
}
|
60
129
|
} catch (err) {
|
@@ -63,7 +132,8 @@ async function getGitBranch() {
|
|
63
132
|
|
64
133
|
function getAPIKey() {
|
65
134
|
try {
|
66
|
-
|
135
|
+
const { apiKey } = getEnvironment();
|
136
|
+
return apiKey;
|
67
137
|
} catch (err) {
|
68
138
|
return void 0;
|
69
139
|
}
|
@@ -78,7 +148,7 @@ function getFetchImplementation(userFetch) {
|
|
78
148
|
return fetchImpl;
|
79
149
|
}
|
80
150
|
|
81
|
-
const VERSION = "0.
|
151
|
+
const VERSION = "0.16.0";
|
82
152
|
|
83
153
|
class ErrorWithCause extends Error {
|
84
154
|
constructor(message, options) {
|
@@ -243,6 +313,7 @@ const removeWorkspaceMember = (variables) => fetch$1({
|
|
243
313
|
...variables
|
244
314
|
});
|
245
315
|
const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
|
316
|
+
const updateWorkspaceMemberInvite = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites/{inviteId}", method: "patch", ...variables });
|
246
317
|
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
247
318
|
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
248
319
|
method: "delete",
|
@@ -291,11 +362,7 @@ const getBranchDetails = (variables) => fetch$1({
|
|
291
362
|
method: "get",
|
292
363
|
...variables
|
293
364
|
});
|
294
|
-
const createBranch = (variables) => fetch$1({
|
295
|
-
url: "/db/{dbBranchName}",
|
296
|
-
method: "put",
|
297
|
-
...variables
|
298
|
-
});
|
365
|
+
const createBranch = (variables) => fetch$1({ url: "/db/{dbBranchName}", method: "put", ...variables });
|
299
366
|
const deleteBranch = (variables) => fetch$1({
|
300
367
|
url: "/db/{dbBranchName}",
|
301
368
|
method: "delete",
|
@@ -369,11 +436,7 @@ const updateColumn = (variables) => fetch$1({
|
|
369
436
|
method: "patch",
|
370
437
|
...variables
|
371
438
|
});
|
372
|
-
const insertRecord = (variables) => fetch$1({
|
373
|
-
url: "/db/{dbBranchName}/tables/{tableName}/data",
|
374
|
-
method: "post",
|
375
|
-
...variables
|
376
|
-
});
|
439
|
+
const insertRecord = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data", method: "post", ...variables });
|
377
440
|
const insertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "put", ...variables });
|
378
441
|
const updateRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "patch", ...variables });
|
379
442
|
const upsertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "post", ...variables });
|
@@ -415,6 +478,7 @@ const operationsByTag = {
|
|
415
478
|
updateWorkspaceMemberRole,
|
416
479
|
removeWorkspaceMember,
|
417
480
|
inviteWorkspaceMember,
|
481
|
+
updateWorkspaceMemberInvite,
|
418
482
|
cancelWorkspaceMemberInvite,
|
419
483
|
resendWorkspaceMemberInvite,
|
420
484
|
acceptWorkspaceMemberInvite
|
@@ -504,7 +568,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
504
568
|
throw TypeError("Cannot add the same private member more than once");
|
505
569
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
506
570
|
};
|
507
|
-
var __privateSet$
|
571
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
508
572
|
__accessCheck$7(obj, member, "write to private field");
|
509
573
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
510
574
|
return value;
|
@@ -519,7 +583,7 @@ class XataApiClient {
|
|
519
583
|
if (!apiKey) {
|
520
584
|
throw new Error("Could not resolve a valid apiKey");
|
521
585
|
}
|
522
|
-
__privateSet$
|
586
|
+
__privateSet$7(this, _extraProps, {
|
523
587
|
apiUrl: getHostUrl(provider, "main"),
|
524
588
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
525
589
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -646,6 +710,13 @@ class WorkspaceApi {
|
|
646
710
|
...this.extraProps
|
647
711
|
});
|
648
712
|
}
|
713
|
+
updateWorkspaceMemberInvite(workspaceId, inviteId, role) {
|
714
|
+
return operationsByTag.workspaces.updateWorkspaceMemberInvite({
|
715
|
+
pathParams: { workspaceId, inviteId },
|
716
|
+
body: { role },
|
717
|
+
...this.extraProps
|
718
|
+
});
|
719
|
+
}
|
649
720
|
cancelWorkspaceMemberInvite(workspaceId, inviteId) {
|
650
721
|
return operationsByTag.workspaces.cancelWorkspaceMemberInvite({
|
651
722
|
pathParams: { workspaceId, inviteId },
|
@@ -860,9 +931,10 @@ class RecordsApi {
|
|
860
931
|
constructor(extraProps) {
|
861
932
|
this.extraProps = extraProps;
|
862
933
|
}
|
863
|
-
insertRecord(workspace, database, branch, tableName, record) {
|
934
|
+
insertRecord(workspace, database, branch, tableName, record, options = {}) {
|
864
935
|
return operationsByTag.records.insertRecord({
|
865
936
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
937
|
+
queryParams: options,
|
866
938
|
body: record,
|
867
939
|
...this.extraProps
|
868
940
|
});
|
@@ -891,21 +963,24 @@ class RecordsApi {
|
|
891
963
|
...this.extraProps
|
892
964
|
});
|
893
965
|
}
|
894
|
-
deleteRecord(workspace, database, branch, tableName, recordId) {
|
966
|
+
deleteRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
895
967
|
return operationsByTag.records.deleteRecord({
|
896
968
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
969
|
+
queryParams: options,
|
897
970
|
...this.extraProps
|
898
971
|
});
|
899
972
|
}
|
900
973
|
getRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
901
974
|
return operationsByTag.records.getRecord({
|
902
975
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
976
|
+
queryParams: options,
|
903
977
|
...this.extraProps
|
904
978
|
});
|
905
979
|
}
|
906
|
-
bulkInsertTableRecords(workspace, database, branch, tableName, records) {
|
980
|
+
bulkInsertTableRecords(workspace, database, branch, tableName, records, options = {}) {
|
907
981
|
return operationsByTag.records.bulkInsertTableRecords({
|
908
982
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
983
|
+
queryParams: options,
|
909
984
|
body: { records },
|
910
985
|
...this.extraProps
|
911
986
|
});
|
@@ -956,7 +1031,7 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
956
1031
|
throw TypeError("Cannot add the same private member more than once");
|
957
1032
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
958
1033
|
};
|
959
|
-
var __privateSet$
|
1034
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
960
1035
|
__accessCheck$6(obj, member, "write to private field");
|
961
1036
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
962
1037
|
return value;
|
@@ -965,7 +1040,7 @@ var _query, _page;
|
|
965
1040
|
class Page {
|
966
1041
|
constructor(query, meta, records = []) {
|
967
1042
|
__privateAdd$6(this, _query, void 0);
|
968
|
-
__privateSet$
|
1043
|
+
__privateSet$6(this, _query, query);
|
969
1044
|
this.meta = meta;
|
970
1045
|
this.records = new RecordArray(this, records);
|
971
1046
|
}
|
@@ -994,10 +1069,10 @@ function isCursorPaginationOptions(options) {
|
|
994
1069
|
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
995
1070
|
}
|
996
1071
|
const _RecordArray = class extends Array {
|
997
|
-
constructor(
|
998
|
-
super(..._RecordArray.parseConstructorParams(
|
1072
|
+
constructor(...args) {
|
1073
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
999
1074
|
__privateAdd$6(this, _page, void 0);
|
1000
|
-
__privateSet$
|
1075
|
+
__privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
|
1001
1076
|
}
|
1002
1077
|
static parseConstructorParams(...args) {
|
1003
1078
|
if (args.length === 1 && typeof args[0] === "number") {
|
@@ -1009,6 +1084,12 @@ const _RecordArray = class extends Array {
|
|
1009
1084
|
}
|
1010
1085
|
return new Array(...args);
|
1011
1086
|
}
|
1087
|
+
toArray() {
|
1088
|
+
return new Array(...this);
|
1089
|
+
}
|
1090
|
+
map(callbackfn, thisArg) {
|
1091
|
+
return this.toArray().map(callbackfn, thisArg);
|
1092
|
+
}
|
1012
1093
|
async nextPage(size, offset) {
|
1013
1094
|
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1014
1095
|
return new _RecordArray(newPage);
|
@@ -1045,7 +1126,7 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
1045
1126
|
throw TypeError("Cannot add the same private member more than once");
|
1046
1127
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1047
1128
|
};
|
1048
|
-
var __privateSet$
|
1129
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
1049
1130
|
__accessCheck$5(obj, member, "write to private field");
|
1050
1131
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1051
1132
|
return value;
|
@@ -1058,11 +1139,11 @@ const _Query = class {
|
|
1058
1139
|
__privateAdd$5(this, _data, { filter: {} });
|
1059
1140
|
this.meta = { page: { cursor: "start", more: true } };
|
1060
1141
|
this.records = new RecordArray(this, []);
|
1061
|
-
__privateSet$
|
1142
|
+
__privateSet$5(this, _table$1, table);
|
1062
1143
|
if (repository) {
|
1063
|
-
__privateSet$
|
1144
|
+
__privateSet$5(this, _repository, repository);
|
1064
1145
|
} else {
|
1065
|
-
__privateSet$
|
1146
|
+
__privateSet$5(this, _repository, this);
|
1066
1147
|
}
|
1067
1148
|
const parent = cleanParent(data, rawParent);
|
1068
1149
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
@@ -1239,7 +1320,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1239
1320
|
throw TypeError("Cannot add the same private member more than once");
|
1240
1321
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1241
1322
|
};
|
1242
|
-
var __privateSet$
|
1323
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1243
1324
|
__accessCheck$4(obj, member, "write to private field");
|
1244
1325
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1245
1326
|
return value;
|
@@ -1248,7 +1329,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1248
1329
|
__accessCheck$4(obj, member, "access private method");
|
1249
1330
|
return method;
|
1250
1331
|
};
|
1251
|
-
var _table, _getFetchProps, _cache,
|
1332
|
+
var _table, _getFetchProps, _cache, _schemaTables$2, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn, _getSchemaTables$1, getSchemaTables_fn$1;
|
1252
1333
|
class Repository extends Query {
|
1253
1334
|
}
|
1254
1335
|
class RestRepository extends Query {
|
@@ -1260,90 +1341,68 @@ class RestRepository extends Query {
|
|
1260
1341
|
__privateAdd$4(this, _updateRecordWithID);
|
1261
1342
|
__privateAdd$4(this, _upsertRecordWithID);
|
1262
1343
|
__privateAdd$4(this, _deleteRecord);
|
1263
|
-
__privateAdd$4(this, _invalidateCache);
|
1264
|
-
__privateAdd$4(this, _setCacheRecord);
|
1265
|
-
__privateAdd$4(this, _getCacheRecord);
|
1266
1344
|
__privateAdd$4(this, _setCacheQuery);
|
1267
1345
|
__privateAdd$4(this, _getCacheQuery);
|
1268
|
-
__privateAdd$4(this,
|
1346
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1269
1347
|
__privateAdd$4(this, _table, void 0);
|
1270
1348
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1271
1349
|
__privateAdd$4(this, _cache, void 0);
|
1272
|
-
__privateAdd$4(this,
|
1273
|
-
__privateSet$
|
1274
|
-
__privateSet$
|
1350
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1351
|
+
__privateSet$4(this, _table, options.table);
|
1352
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1275
1353
|
this.db = options.db;
|
1276
|
-
__privateSet$
|
1354
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1355
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1277
1356
|
}
|
1278
|
-
async create(a, b) {
|
1357
|
+
async create(a, b, c) {
|
1279
1358
|
if (Array.isArray(a)) {
|
1280
1359
|
if (a.length === 0)
|
1281
1360
|
return [];
|
1282
|
-
const
|
1283
|
-
|
1284
|
-
accOrder.push(condition);
|
1285
|
-
if (condition) {
|
1286
|
-
accWithIds.push(item);
|
1287
|
-
} else {
|
1288
|
-
accWithoutIds.push(item);
|
1289
|
-
}
|
1290
|
-
return [accWithoutIds, accWithIds, accOrder];
|
1291
|
-
}, [[], [], []]);
|
1292
|
-
const recordsWithoutId = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, itemsWithoutIds);
|
1293
|
-
await Promise.all(recordsWithoutId.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1294
|
-
if (itemsWithIds.length > 100) {
|
1295
|
-
console.warn("Bulk create operation with id is not optimized in the Xata API yet, this request might be slow");
|
1296
|
-
}
|
1297
|
-
const recordsWithId = await Promise.all(itemsWithIds.map((object) => this.create(object)));
|
1298
|
-
return order.map((condition) => {
|
1299
|
-
if (condition) {
|
1300
|
-
return recordsWithId.shift();
|
1301
|
-
} else {
|
1302
|
-
return recordsWithoutId.shift();
|
1303
|
-
}
|
1304
|
-
}).filter((record) => !!record);
|
1361
|
+
const columns = isStringArray(b) ? b : void 0;
|
1362
|
+
return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a, columns);
|
1305
1363
|
}
|
1306
1364
|
if (isString(a) && isObject(b)) {
|
1307
1365
|
if (a === "")
|
1308
1366
|
throw new Error("The id can't be empty");
|
1309
|
-
const
|
1310
|
-
|
1311
|
-
return record;
|
1367
|
+
const columns = isStringArray(c) ? c : void 0;
|
1368
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns);
|
1312
1369
|
}
|
1313
1370
|
if (isObject(a) && isString(a.id)) {
|
1314
1371
|
if (a.id === "")
|
1315
1372
|
throw new Error("The id can't be empty");
|
1316
|
-
const
|
1317
|
-
|
1318
|
-
return record;
|
1373
|
+
const columns = isStringArray(b) ? b : void 0;
|
1374
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1319
1375
|
}
|
1320
1376
|
if (isObject(a)) {
|
1321
|
-
const
|
1322
|
-
|
1323
|
-
return record;
|
1377
|
+
const columns = isStringArray(b) ? b : void 0;
|
1378
|
+
return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a, columns);
|
1324
1379
|
}
|
1325
1380
|
throw new Error("Invalid arguments for create method");
|
1326
1381
|
}
|
1327
|
-
async read(a) {
|
1382
|
+
async read(a, b) {
|
1383
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1328
1384
|
if (Array.isArray(a)) {
|
1329
1385
|
if (a.length === 0)
|
1330
1386
|
return [];
|
1331
1387
|
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1332
|
-
|
1388
|
+
const finalObjects = await this.getAll({ filter: { id: { $any: ids } }, columns });
|
1389
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1390
|
+
acc[object.id] = object;
|
1391
|
+
return acc;
|
1392
|
+
}, {});
|
1393
|
+
return ids.map((id2) => dictionary[id2] ?? null);
|
1333
1394
|
}
|
1334
1395
|
const id = isString(a) ? a : a.id;
|
1335
1396
|
if (isString(id)) {
|
1336
|
-
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
|
1337
|
-
if (cacheRecord)
|
1338
|
-
return cacheRecord;
|
1339
1397
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1340
1398
|
try {
|
1341
1399
|
const response = await getRecord({
|
1342
1400
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1401
|
+
queryParams: { columns },
|
1343
1402
|
...fetchProps
|
1344
1403
|
});
|
1345
|
-
const
|
1346
|
-
return initObject(this.db,
|
1404
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1405
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1347
1406
|
} catch (e) {
|
1348
1407
|
if (isObject(e) && e.status === 404) {
|
1349
1408
|
return null;
|
@@ -1351,50 +1410,45 @@ class RestRepository extends Query {
|
|
1351
1410
|
throw e;
|
1352
1411
|
}
|
1353
1412
|
}
|
1413
|
+
return null;
|
1354
1414
|
}
|
1355
|
-
async update(a, b) {
|
1415
|
+
async update(a, b, c) {
|
1356
1416
|
if (Array.isArray(a)) {
|
1357
1417
|
if (a.length === 0)
|
1358
1418
|
return [];
|
1359
1419
|
if (a.length > 100) {
|
1360
1420
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1361
1421
|
}
|
1362
|
-
|
1422
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1423
|
+
return Promise.all(a.map((object) => this.update(object, columns)));
|
1363
1424
|
}
|
1364
1425
|
if (isString(a) && isObject(b)) {
|
1365
|
-
|
1366
|
-
|
1367
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1368
|
-
return record;
|
1426
|
+
const columns = isStringArray(c) ? c : void 0;
|
1427
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns);
|
1369
1428
|
}
|
1370
1429
|
if (isObject(a) && isString(a.id)) {
|
1371
|
-
|
1372
|
-
|
1373
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1374
|
-
return record;
|
1430
|
+
const columns = isStringArray(b) ? b : void 0;
|
1431
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1375
1432
|
}
|
1376
1433
|
throw new Error("Invalid arguments for update method");
|
1377
1434
|
}
|
1378
|
-
async createOrUpdate(a, b) {
|
1435
|
+
async createOrUpdate(a, b, c) {
|
1379
1436
|
if (Array.isArray(a)) {
|
1380
1437
|
if (a.length === 0)
|
1381
1438
|
return [];
|
1382
1439
|
if (a.length > 100) {
|
1383
1440
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1384
1441
|
}
|
1385
|
-
|
1442
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1443
|
+
return Promise.all(a.map((object) => this.createOrUpdate(object, columns)));
|
1386
1444
|
}
|
1387
1445
|
if (isString(a) && isObject(b)) {
|
1388
|
-
|
1389
|
-
|
1390
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1391
|
-
return record;
|
1446
|
+
const columns = isStringArray(c) ? c : void 0;
|
1447
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b, columns);
|
1392
1448
|
}
|
1393
1449
|
if (isObject(a) && isString(a.id)) {
|
1394
|
-
|
1395
|
-
|
1396
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1397
|
-
return record;
|
1450
|
+
const columns = isStringArray(c) ? c : void 0;
|
1451
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1398
1452
|
}
|
1399
1453
|
throw new Error("Invalid arguments for createOrUpdate method");
|
1400
1454
|
}
|
@@ -1410,12 +1464,10 @@ class RestRepository extends Query {
|
|
1410
1464
|
}
|
1411
1465
|
if (isString(a)) {
|
1412
1466
|
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
|
1413
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1414
1467
|
return;
|
1415
1468
|
}
|
1416
1469
|
if (isObject(a) && isString(a.id)) {
|
1417
1470
|
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
|
1418
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1419
1471
|
return;
|
1420
1472
|
}
|
1421
1473
|
throw new Error("Invalid arguments for delete method");
|
@@ -1427,13 +1479,15 @@ class RestRepository extends Query {
|
|
1427
1479
|
body: {
|
1428
1480
|
query,
|
1429
1481
|
fuzziness: options.fuzziness,
|
1482
|
+
prefix: options.prefix,
|
1430
1483
|
highlight: options.highlight,
|
1431
|
-
filter: options.filter
|
1484
|
+
filter: options.filter,
|
1485
|
+
boosters: options.boosters
|
1432
1486
|
},
|
1433
1487
|
...fetchProps
|
1434
1488
|
});
|
1435
|
-
const
|
1436
|
-
return records.map((item) => initObject(this.db,
|
1489
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1490
|
+
return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
|
1437
1491
|
}
|
1438
1492
|
async query(query) {
|
1439
1493
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1452,8 +1506,8 @@ class RestRepository extends Query {
|
|
1452
1506
|
body,
|
1453
1507
|
...fetchProps
|
1454
1508
|
});
|
1455
|
-
const
|
1456
|
-
const records = objects.map((record) => initObject(this.db,
|
1509
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1510
|
+
const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
|
1457
1511
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1458
1512
|
return new Page(query, meta, records);
|
1459
1513
|
}
|
@@ -1461,9 +1515,9 @@ class RestRepository extends Query {
|
|
1461
1515
|
_table = new WeakMap();
|
1462
1516
|
_getFetchProps = new WeakMap();
|
1463
1517
|
_cache = new WeakMap();
|
1464
|
-
|
1518
|
+
_schemaTables$2 = new WeakMap();
|
1465
1519
|
_insertRecordWithoutId = new WeakSet();
|
1466
|
-
insertRecordWithoutId_fn = async function(object) {
|
1520
|
+
insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
|
1467
1521
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1468
1522
|
const record = transformObjectLinks(object);
|
1469
1523
|
const response = await insertRecord({
|
@@ -1472,17 +1526,15 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1472
1526
|
dbBranchName: "{dbBranch}",
|
1473
1527
|
tableName: __privateGet$4(this, _table)
|
1474
1528
|
},
|
1529
|
+
queryParams: { columns },
|
1475
1530
|
body: record,
|
1476
1531
|
...fetchProps
|
1477
1532
|
});
|
1478
|
-
const
|
1479
|
-
|
1480
|
-
throw new Error("The server failed to save the record");
|
1481
|
-
}
|
1482
|
-
return finalObject;
|
1533
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1534
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1483
1535
|
};
|
1484
1536
|
_insertRecordWithId = new WeakSet();
|
1485
|
-
insertRecordWithId_fn = async function(recordId, object) {
|
1537
|
+
insertRecordWithId_fn = async function(recordId, object, columns = ["*"]) {
|
1486
1538
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1487
1539
|
const record = transformObjectLinks(object);
|
1488
1540
|
const response = await insertRecordWithID({
|
@@ -1493,56 +1545,52 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1493
1545
|
recordId
|
1494
1546
|
},
|
1495
1547
|
body: record,
|
1496
|
-
queryParams: { createOnly: true },
|
1548
|
+
queryParams: { createOnly: true, columns },
|
1497
1549
|
...fetchProps
|
1498
1550
|
});
|
1499
|
-
const
|
1500
|
-
|
1501
|
-
throw new Error("The server failed to save the record");
|
1502
|
-
}
|
1503
|
-
return finalObject;
|
1551
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1552
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1504
1553
|
};
|
1505
1554
|
_bulkInsertTableRecords = new WeakSet();
|
1506
|
-
bulkInsertTableRecords_fn = async function(objects) {
|
1555
|
+
bulkInsertTableRecords_fn = async function(objects, columns = ["*"]) {
|
1507
1556
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1508
1557
|
const records = objects.map((object) => transformObjectLinks(object));
|
1509
1558
|
const response = await bulkInsertTableRecords({
|
1510
1559
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1560
|
+
queryParams: { columns },
|
1511
1561
|
body: { records },
|
1512
1562
|
...fetchProps
|
1513
1563
|
});
|
1514
|
-
|
1515
|
-
|
1516
|
-
throw new Error("The server failed to save some records");
|
1564
|
+
if (!isResponseWithRecords(response)) {
|
1565
|
+
throw new Error("Request included columns but server didn't include them");
|
1517
1566
|
}
|
1518
|
-
|
1567
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1568
|
+
return response.records?.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
|
1519
1569
|
};
|
1520
1570
|
_updateRecordWithID = new WeakSet();
|
1521
|
-
updateRecordWithID_fn = async function(recordId, object) {
|
1571
|
+
updateRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1522
1572
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1523
1573
|
const record = transformObjectLinks(object);
|
1524
1574
|
const response = await updateRecordWithID({
|
1525
1575
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1576
|
+
queryParams: { columns },
|
1526
1577
|
body: record,
|
1527
1578
|
...fetchProps
|
1528
1579
|
});
|
1529
|
-
const
|
1530
|
-
|
1531
|
-
throw new Error("The server failed to save the record");
|
1532
|
-
return item;
|
1580
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1581
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1533
1582
|
};
|
1534
1583
|
_upsertRecordWithID = new WeakSet();
|
1535
|
-
upsertRecordWithID_fn = async function(recordId, object) {
|
1584
|
+
upsertRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1536
1585
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1537
1586
|
const response = await upsertRecordWithID({
|
1538
1587
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1588
|
+
queryParams: { columns },
|
1539
1589
|
body: object,
|
1540
1590
|
...fetchProps
|
1541
1591
|
});
|
1542
|
-
const
|
1543
|
-
|
1544
|
-
throw new Error("The server failed to save the record");
|
1545
|
-
return item;
|
1592
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1593
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1546
1594
|
};
|
1547
1595
|
_deleteRecord = new WeakSet();
|
1548
1596
|
deleteRecord_fn = async function(recordId) {
|
@@ -1552,29 +1600,6 @@ deleteRecord_fn = async function(recordId) {
|
|
1552
1600
|
...fetchProps
|
1553
1601
|
});
|
1554
1602
|
};
|
1555
|
-
_invalidateCache = new WeakSet();
|
1556
|
-
invalidateCache_fn = async function(recordId) {
|
1557
|
-
await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1558
|
-
const cacheItems = await __privateGet$4(this, _cache).getAll();
|
1559
|
-
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1560
|
-
for (const [key, value] of queries) {
|
1561
|
-
const ids = getIds(value);
|
1562
|
-
if (ids.includes(recordId))
|
1563
|
-
await __privateGet$4(this, _cache).delete(key);
|
1564
|
-
}
|
1565
|
-
};
|
1566
|
-
_setCacheRecord = new WeakSet();
|
1567
|
-
setCacheRecord_fn = async function(record) {
|
1568
|
-
if (!__privateGet$4(this, _cache).cacheRecords)
|
1569
|
-
return;
|
1570
|
-
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1571
|
-
};
|
1572
|
-
_getCacheRecord = new WeakSet();
|
1573
|
-
getCacheRecord_fn = async function(recordId) {
|
1574
|
-
if (!__privateGet$4(this, _cache).cacheRecords)
|
1575
|
-
return null;
|
1576
|
-
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1577
|
-
};
|
1578
1603
|
_setCacheQuery = new WeakSet();
|
1579
1604
|
setCacheQuery_fn = async function(query, meta, records) {
|
1580
1605
|
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
@@ -1591,17 +1616,17 @@ getCacheQuery_fn = async function(query) {
|
|
1591
1616
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1592
1617
|
return hasExpired ? null : result;
|
1593
1618
|
};
|
1594
|
-
|
1595
|
-
|
1596
|
-
if (__privateGet$4(this,
|
1597
|
-
return __privateGet$4(this,
|
1619
|
+
_getSchemaTables$1 = new WeakSet();
|
1620
|
+
getSchemaTables_fn$1 = async function() {
|
1621
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1622
|
+
return __privateGet$4(this, _schemaTables$2);
|
1598
1623
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1599
1624
|
const { schema } = await getBranchDetails({
|
1600
1625
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1601
1626
|
...fetchProps
|
1602
1627
|
});
|
1603
|
-
__privateSet$
|
1604
|
-
return schema;
|
1628
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1629
|
+
return schema.tables;
|
1605
1630
|
};
|
1606
1631
|
const transformObjectLinks = (object) => {
|
1607
1632
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
@@ -1610,11 +1635,11 @@ const transformObjectLinks = (object) => {
|
|
1610
1635
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1611
1636
|
}, {});
|
1612
1637
|
};
|
1613
|
-
const initObject = (db,
|
1638
|
+
const initObject = (db, schemaTables, table, object) => {
|
1614
1639
|
const result = {};
|
1615
1640
|
const { xata, ...rest } = object ?? {};
|
1616
1641
|
Object.assign(result, rest);
|
1617
|
-
const { columns } =
|
1642
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1618
1643
|
if (!columns)
|
1619
1644
|
console.error(`Table ${table} not found in schema`);
|
1620
1645
|
for (const column of columns ?? []) {
|
@@ -1634,17 +1659,17 @@ const initObject = (db, schema, table, object) => {
|
|
1634
1659
|
if (!linkTable) {
|
1635
1660
|
console.error(`Failed to parse link for field ${column.name}`);
|
1636
1661
|
} else if (isObject(value)) {
|
1637
|
-
result[column.name] = initObject(db,
|
1662
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1638
1663
|
}
|
1639
1664
|
break;
|
1640
1665
|
}
|
1641
1666
|
}
|
1642
1667
|
}
|
1643
|
-
result.read = function() {
|
1644
|
-
return db[table].read(result["id"]);
|
1668
|
+
result.read = function(columns2) {
|
1669
|
+
return db[table].read(result["id"], columns2);
|
1645
1670
|
};
|
1646
|
-
result.update = function(data) {
|
1647
|
-
return db[table].update(result["id"], data);
|
1671
|
+
result.update = function(data, columns2) {
|
1672
|
+
return db[table].update(result["id"], data, columns2);
|
1648
1673
|
};
|
1649
1674
|
result.delete = function() {
|
1650
1675
|
return db[table].delete(result["id"]);
|
@@ -1658,14 +1683,8 @@ const initObject = (db, schema, table, object) => {
|
|
1658
1683
|
Object.freeze(result);
|
1659
1684
|
return result;
|
1660
1685
|
};
|
1661
|
-
function
|
1662
|
-
|
1663
|
-
return value.map((item) => getIds(item)).flat();
|
1664
|
-
}
|
1665
|
-
if (!isObject(value))
|
1666
|
-
return [];
|
1667
|
-
const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
|
1668
|
-
return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
|
1686
|
+
function isResponseWithRecords(value) {
|
1687
|
+
return isObject(value) && Array.isArray(value.records);
|
1669
1688
|
}
|
1670
1689
|
|
1671
1690
|
var __accessCheck$3 = (obj, member, msg) => {
|
@@ -1681,7 +1700,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1681
1700
|
throw TypeError("Cannot add the same private member more than once");
|
1682
1701
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1683
1702
|
};
|
1684
|
-
var __privateSet$
|
1703
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1685
1704
|
__accessCheck$3(obj, member, "write to private field");
|
1686
1705
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1687
1706
|
return value;
|
@@ -1690,9 +1709,8 @@ var _map;
|
|
1690
1709
|
class SimpleCache {
|
1691
1710
|
constructor(options = {}) {
|
1692
1711
|
__privateAdd$3(this, _map, void 0);
|
1693
|
-
__privateSet$
|
1712
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1694
1713
|
this.capacity = options.max ?? 500;
|
1695
|
-
this.cacheRecords = options.cacheRecords ?? true;
|
1696
1714
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1697
1715
|
}
|
1698
1716
|
async getAll() {
|
@@ -1750,12 +1768,18 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1750
1768
|
throw TypeError("Cannot add the same private member more than once");
|
1751
1769
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1752
1770
|
};
|
1753
|
-
var
|
1771
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1772
|
+
__accessCheck$2(obj, member, "write to private field");
|
1773
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1774
|
+
return value;
|
1775
|
+
};
|
1776
|
+
var _tables, _schemaTables$1;
|
1754
1777
|
class SchemaPlugin extends XataPlugin {
|
1755
|
-
constructor(
|
1778
|
+
constructor(schemaTables) {
|
1756
1779
|
super();
|
1757
|
-
this.tableNames = tableNames;
|
1758
1780
|
__privateAdd$2(this, _tables, {});
|
1781
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1782
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1759
1783
|
}
|
1760
1784
|
build(pluginOptions) {
|
1761
1785
|
const db = new Proxy({}, {
|
@@ -1763,18 +1787,20 @@ class SchemaPlugin extends XataPlugin {
|
|
1763
1787
|
if (!isString(table))
|
1764
1788
|
throw new Error("Invalid table name");
|
1765
1789
|
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1766
|
-
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1790
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1767
1791
|
}
|
1768
1792
|
return __privateGet$2(this, _tables)[table];
|
1769
1793
|
}
|
1770
1794
|
});
|
1771
|
-
|
1772
|
-
|
1795
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1796
|
+
for (const table of tableNames) {
|
1797
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1773
1798
|
}
|
1774
1799
|
return db;
|
1775
1800
|
}
|
1776
1801
|
}
|
1777
1802
|
_tables = new WeakMap();
|
1803
|
+
_schemaTables$1 = new WeakMap();
|
1778
1804
|
|
1779
1805
|
var __accessCheck$1 = (obj, member, msg) => {
|
1780
1806
|
if (!member.has(obj))
|
@@ -1798,82 +1824,77 @@ var __privateMethod$1 = (obj, member, method) => {
|
|
1798
1824
|
__accessCheck$1(obj, member, "access private method");
|
1799
1825
|
return method;
|
1800
1826
|
};
|
1801
|
-
var
|
1827
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1802
1828
|
class SearchPlugin extends XataPlugin {
|
1803
|
-
constructor(db) {
|
1829
|
+
constructor(db, schemaTables) {
|
1804
1830
|
super();
|
1805
1831
|
this.db = db;
|
1806
1832
|
__privateAdd$1(this, _search);
|
1807
|
-
__privateAdd$1(this,
|
1808
|
-
__privateAdd$1(this,
|
1833
|
+
__privateAdd$1(this, _getSchemaTables);
|
1834
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1835
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1809
1836
|
}
|
1810
1837
|
build({ getFetchProps }) {
|
1811
1838
|
return {
|
1812
1839
|
all: async (query, options = {}) => {
|
1813
1840
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1814
|
-
const
|
1841
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1815
1842
|
return records.map((record) => {
|
1816
1843
|
const { table = "orphan" } = record.xata;
|
1817
|
-
return { table, record: initObject(this.db,
|
1844
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1818
1845
|
});
|
1819
1846
|
},
|
1820
1847
|
byTable: async (query, options = {}) => {
|
1821
1848
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1822
|
-
const
|
1849
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1823
1850
|
return records.reduce((acc, record) => {
|
1824
1851
|
const { table = "orphan" } = record.xata;
|
1825
1852
|
const items = acc[table] ?? [];
|
1826
|
-
const item = initObject(this.db,
|
1853
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1827
1854
|
return { ...acc, [table]: [...items, item] };
|
1828
1855
|
}, {});
|
1829
1856
|
}
|
1830
1857
|
};
|
1831
1858
|
}
|
1832
1859
|
}
|
1833
|
-
|
1860
|
+
_schemaTables = new WeakMap();
|
1834
1861
|
_search = new WeakSet();
|
1835
1862
|
search_fn = async function(query, options, getFetchProps) {
|
1836
1863
|
const fetchProps = await getFetchProps();
|
1837
|
-
const { tables, fuzziness, highlight } = options ?? {};
|
1864
|
+
const { tables, fuzziness, highlight, prefix } = options ?? {};
|
1838
1865
|
const { records } = await searchBranch({
|
1839
1866
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1840
|
-
body: { tables, query, fuzziness, highlight },
|
1867
|
+
body: { tables, query, fuzziness, prefix, highlight },
|
1841
1868
|
...fetchProps
|
1842
1869
|
});
|
1843
1870
|
return records;
|
1844
1871
|
};
|
1845
|
-
|
1846
|
-
|
1847
|
-
if (__privateGet$1(this,
|
1848
|
-
return __privateGet$1(this,
|
1872
|
+
_getSchemaTables = new WeakSet();
|
1873
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1874
|
+
if (__privateGet$1(this, _schemaTables))
|
1875
|
+
return __privateGet$1(this, _schemaTables);
|
1849
1876
|
const fetchProps = await getFetchProps();
|
1850
1877
|
const { schema } = await getBranchDetails({
|
1851
1878
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1852
1879
|
...fetchProps
|
1853
1880
|
});
|
1854
|
-
__privateSet$1(this,
|
1855
|
-
return schema;
|
1881
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1882
|
+
return schema.tables;
|
1856
1883
|
};
|
1857
1884
|
|
1858
1885
|
const isBranchStrategyBuilder = (strategy) => {
|
1859
1886
|
return typeof strategy === "function";
|
1860
1887
|
};
|
1861
1888
|
|
1862
|
-
const envBranchNames = [
|
1863
|
-
"XATA_BRANCH",
|
1864
|
-
"VERCEL_GIT_COMMIT_REF",
|
1865
|
-
"CF_PAGES_BRANCH",
|
1866
|
-
"BRANCH"
|
1867
|
-
];
|
1868
1889
|
async function getCurrentBranchName(options) {
|
1869
|
-
const
|
1870
|
-
if (
|
1871
|
-
const details = await getDatabaseBranch(
|
1890
|
+
const { branch, envBranch } = getEnvironment();
|
1891
|
+
if (branch) {
|
1892
|
+
const details = await getDatabaseBranch(branch, options);
|
1872
1893
|
if (details)
|
1873
|
-
return
|
1874
|
-
console.warn(`Branch ${
|
1894
|
+
return branch;
|
1895
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1875
1896
|
}
|
1876
|
-
const gitBranch = await getGitBranch();
|
1897
|
+
const gitBranch = envBranch || await getGitBranch();
|
1877
1898
|
return resolveXataBranch(gitBranch, options);
|
1878
1899
|
}
|
1879
1900
|
async function getCurrentBranchDetails(options) {
|
@@ -1889,13 +1910,14 @@ async function resolveXataBranch(gitBranch, options) {
|
|
1889
1910
|
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1890
1911
|
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1891
1912
|
const [workspace] = host.split(".");
|
1913
|
+
const { fallbackBranch } = getEnvironment();
|
1892
1914
|
const { branch } = await resolveBranch({
|
1893
1915
|
apiKey,
|
1894
1916
|
apiUrl: databaseURL,
|
1895
1917
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1896
1918
|
workspacesApiUrl: `${protocol}//${host}`,
|
1897
1919
|
pathParams: { dbName, workspace },
|
1898
|
-
queryParams: { gitBranch, fallbackBranch
|
1920
|
+
queryParams: { gitBranch, fallbackBranch }
|
1899
1921
|
});
|
1900
1922
|
return branch;
|
1901
1923
|
}
|
@@ -1923,21 +1945,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1923
1945
|
throw err;
|
1924
1946
|
}
|
1925
1947
|
}
|
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
1948
|
function getDatabaseURL() {
|
1939
1949
|
try {
|
1940
|
-
|
1950
|
+
const { databaseURL } = getEnvironment();
|
1951
|
+
return databaseURL;
|
1941
1952
|
} catch (err) {
|
1942
1953
|
return void 0;
|
1943
1954
|
}
|
@@ -1968,7 +1979,7 @@ var __privateMethod = (obj, member, method) => {
|
|
1968
1979
|
const buildClient = (plugins) => {
|
1969
1980
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1970
1981
|
return _a = class {
|
1971
|
-
constructor(options = {},
|
1982
|
+
constructor(options = {}, schemaTables) {
|
1972
1983
|
__privateAdd(this, _parseOptions);
|
1973
1984
|
__privateAdd(this, _getFetchProps);
|
1974
1985
|
__privateAdd(this, _evaluateBranch);
|
@@ -1978,8 +1989,8 @@ const buildClient = (plugins) => {
|
|
1978
1989
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1979
1990
|
cache: safeOptions.cache
|
1980
1991
|
};
|
1981
|
-
const db = new SchemaPlugin(
|
1982
|
-
const search = new SearchPlugin(db).build(pluginOptions);
|
1992
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
1993
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1983
1994
|
this.db = db;
|
1984
1995
|
this.search = search;
|
1985
1996
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
@@ -1999,7 +2010,7 @@ const buildClient = (plugins) => {
|
|
1999
2010
|
const fetch = getFetchImplementation(options?.fetch);
|
2000
2011
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
2001
2012
|
const apiKey = options?.apiKey || getAPIKey();
|
2002
|
-
const cache = options?.cache ?? new SimpleCache({
|
2013
|
+
const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
|
2003
2014
|
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
2004
2015
|
if (!databaseURL || !apiKey) {
|
2005
2016
|
throw new Error("Options databaseURL and apiKey are required");
|
@@ -2150,6 +2161,7 @@ exports.updateRecordWithID = updateRecordWithID;
|
|
2150
2161
|
exports.updateTable = updateTable;
|
2151
2162
|
exports.updateUser = updateUser;
|
2152
2163
|
exports.updateWorkspace = updateWorkspace;
|
2164
|
+
exports.updateWorkspaceMemberInvite = updateWorkspaceMemberInvite;
|
2153
2165
|
exports.updateWorkspaceMemberRole = updateWorkspaceMemberRole;
|
2154
2166
|
exports.upsertRecordWithID = upsertRecordWithID;
|
2155
2167
|
//# sourceMappingURL=index.cjs.map
|