@xata.io/client 0.0.0-alpha.vf9819fa → 0.0.0-alpha.vf9f8ac6
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/.eslintrc.cjs +1 -2
- package/CHANGELOG.md +144 -0
- package/README.md +271 -1
- package/Usage.md +428 -0
- package/dist/index.cjs +582 -305
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +873 -163
- package/dist/index.mjs +556 -306
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -4
- package/tsconfig.json +1 -0
package/dist/index.mjs
CHANGED
@@ -7,46 +7,101 @@ function compact(arr) {
|
|
7
7
|
function isObject(value) {
|
8
8
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
9
9
|
}
|
10
|
+
function isDefined(value) {
|
11
|
+
return value !== null && value !== void 0;
|
12
|
+
}
|
10
13
|
function isString(value) {
|
11
|
-
return value
|
14
|
+
return isDefined(value) && typeof value === "string";
|
15
|
+
}
|
16
|
+
function isStringArray(value) {
|
17
|
+
return isDefined(value) && Array.isArray(value) && value.every(isString);
|
12
18
|
}
|
13
19
|
function toBase64(value) {
|
14
20
|
try {
|
15
21
|
return btoa(value);
|
16
22
|
} catch (err) {
|
17
|
-
|
23
|
+
const buf = Buffer;
|
24
|
+
return buf.from(value).toString("base64");
|
18
25
|
}
|
19
26
|
}
|
20
27
|
|
21
|
-
function
|
28
|
+
function getEnvironment() {
|
22
29
|
try {
|
23
|
-
if (isObject(process) &&
|
24
|
-
return
|
30
|
+
if (isObject(process) && isObject(process.env)) {
|
31
|
+
return {
|
32
|
+
apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
|
33
|
+
databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
|
34
|
+
branch: process.env.XATA_BRANCH ?? getGlobalBranch(),
|
35
|
+
envBranch: process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH,
|
36
|
+
fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
|
37
|
+
};
|
25
38
|
}
|
26
39
|
} catch (err) {
|
27
40
|
}
|
28
41
|
try {
|
29
|
-
if (isObject(Deno) &&
|
30
|
-
return
|
42
|
+
if (isObject(Deno) && isObject(Deno.env)) {
|
43
|
+
return {
|
44
|
+
apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
|
45
|
+
databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
|
46
|
+
branch: Deno.env.get("XATA_BRANCH") ?? getGlobalBranch(),
|
47
|
+
envBranch: Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH"),
|
48
|
+
fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
|
49
|
+
};
|
31
50
|
}
|
32
51
|
} catch (err) {
|
33
52
|
}
|
53
|
+
return {
|
54
|
+
apiKey: getGlobalApiKey(),
|
55
|
+
databaseURL: getGlobalDatabaseURL(),
|
56
|
+
branch: getGlobalBranch(),
|
57
|
+
envBranch: void 0,
|
58
|
+
fallbackBranch: getGlobalFallbackBranch()
|
59
|
+
};
|
60
|
+
}
|
61
|
+
function getGlobalApiKey() {
|
62
|
+
try {
|
63
|
+
return XATA_API_KEY;
|
64
|
+
} catch (err) {
|
65
|
+
return void 0;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
function getGlobalDatabaseURL() {
|
69
|
+
try {
|
70
|
+
return XATA_DATABASE_URL;
|
71
|
+
} catch (err) {
|
72
|
+
return void 0;
|
73
|
+
}
|
74
|
+
}
|
75
|
+
function getGlobalBranch() {
|
76
|
+
try {
|
77
|
+
return XATA_BRANCH;
|
78
|
+
} catch (err) {
|
79
|
+
return void 0;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
function getGlobalFallbackBranch() {
|
83
|
+
try {
|
84
|
+
return XATA_FALLBACK_BRANCH;
|
85
|
+
} catch (err) {
|
86
|
+
return void 0;
|
87
|
+
}
|
34
88
|
}
|
35
89
|
async function getGitBranch() {
|
90
|
+
const cmd = ["git", "branch", "--show-current"];
|
91
|
+
const fullCmd = cmd.join(" ");
|
92
|
+
const nodeModule = ["child", "process"].join("_");
|
93
|
+
const execOptions = { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] };
|
36
94
|
try {
|
37
95
|
if (typeof require === "function") {
|
38
|
-
|
39
|
-
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
96
|
+
return require(nodeModule).execSync(fullCmd, execOptions).trim();
|
40
97
|
}
|
98
|
+
const { execSync } = await import(nodeModule);
|
99
|
+
return execSync(fullCmd, execOptions).toString().trim();
|
41
100
|
} catch (err) {
|
42
101
|
}
|
43
102
|
try {
|
44
103
|
if (isObject(Deno)) {
|
45
|
-
const process2 = Deno.run({
|
46
|
-
cmd: ["git", "branch", "--show-current"],
|
47
|
-
stdout: "piped",
|
48
|
-
stderr: "piped"
|
49
|
-
});
|
104
|
+
const process2 = Deno.run({ cmd, stdout: "piped", stderr: "null" });
|
50
105
|
return new TextDecoder().decode(await process2.output()).trim();
|
51
106
|
}
|
52
107
|
} catch (err) {
|
@@ -55,7 +110,8 @@ async function getGitBranch() {
|
|
55
110
|
|
56
111
|
function getAPIKey() {
|
57
112
|
try {
|
58
|
-
|
113
|
+
const { apiKey } = getEnvironment();
|
114
|
+
return apiKey;
|
59
115
|
} catch (err) {
|
60
116
|
return void 0;
|
61
117
|
}
|
@@ -65,21 +121,35 @@ function getFetchImplementation(userFetch) {
|
|
65
121
|
const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
|
66
122
|
const fetchImpl = userFetch ?? globalFetch;
|
67
123
|
if (!fetchImpl) {
|
68
|
-
throw new Error(
|
124
|
+
throw new Error(
|
125
|
+
`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`
|
126
|
+
);
|
69
127
|
}
|
70
128
|
return fetchImpl;
|
71
129
|
}
|
72
130
|
|
73
|
-
|
74
|
-
|
131
|
+
const VERSION = "0.0.0-alpha.vf9f8ac6";
|
132
|
+
|
133
|
+
class ErrorWithCause extends Error {
|
134
|
+
constructor(message, options) {
|
135
|
+
super(message, options);
|
136
|
+
}
|
137
|
+
}
|
138
|
+
class FetcherError extends ErrorWithCause {
|
139
|
+
constructor(status, data, requestId) {
|
75
140
|
super(getMessage(data));
|
76
141
|
this.status = status;
|
77
142
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
143
|
+
this.requestId = requestId;
|
78
144
|
if (data instanceof Error) {
|
79
145
|
this.stack = data.stack;
|
80
146
|
this.cause = data.cause;
|
81
147
|
}
|
82
148
|
}
|
149
|
+
toString() {
|
150
|
+
const error = super.toString();
|
151
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
152
|
+
}
|
83
153
|
}
|
84
154
|
function isBulkError(error) {
|
85
155
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -102,7 +172,12 @@ function getMessage(data) {
|
|
102
172
|
}
|
103
173
|
|
104
174
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
105
|
-
const
|
175
|
+
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
176
|
+
if (value === void 0 || value === null)
|
177
|
+
return acc;
|
178
|
+
return { ...acc, [key]: value };
|
179
|
+
}, {});
|
180
|
+
const query = new URLSearchParams(cleanQueryParams).toString();
|
106
181
|
const queryString = query.length > 0 ? `?${query}` : "";
|
107
182
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
108
183
|
};
|
@@ -142,6 +217,7 @@ async function fetch$1({
|
|
142
217
|
body: body ? JSON.stringify(body) : void 0,
|
143
218
|
headers: {
|
144
219
|
"Content-Type": "application/json",
|
220
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
145
221
|
...headers,
|
146
222
|
...hostHeader(fullUrl),
|
147
223
|
Authorization: `Bearer ${apiKey}`
|
@@ -150,14 +226,15 @@ async function fetch$1({
|
|
150
226
|
if (response.status === 204) {
|
151
227
|
return {};
|
152
228
|
}
|
229
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
153
230
|
try {
|
154
231
|
const jsonResponse = await response.json();
|
155
232
|
if (response.ok) {
|
156
233
|
return jsonResponse;
|
157
234
|
}
|
158
|
-
throw new FetcherError(response.status, jsonResponse);
|
235
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
159
236
|
} catch (error) {
|
160
|
-
throw new FetcherError(response.status, error);
|
237
|
+
throw new FetcherError(response.status, error, requestId);
|
161
238
|
}
|
162
239
|
}
|
163
240
|
|
@@ -216,6 +293,7 @@ const removeWorkspaceMember = (variables) => fetch$1({
|
|
216
293
|
...variables
|
217
294
|
});
|
218
295
|
const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
|
296
|
+
const updateWorkspaceMemberInvite = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites/{inviteId}", method: "patch", ...variables });
|
219
297
|
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
220
298
|
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
221
299
|
method: "delete",
|
@@ -251,6 +329,11 @@ const deleteDatabase = (variables) => fetch$1({
|
|
251
329
|
method: "delete",
|
252
330
|
...variables
|
253
331
|
});
|
332
|
+
const getDatabaseMetadata = (variables) => fetch$1({
|
333
|
+
url: "/dbs/{dbName}/metadata",
|
334
|
+
method: "get",
|
335
|
+
...variables
|
336
|
+
});
|
254
337
|
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
255
338
|
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
256
339
|
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
@@ -264,11 +347,7 @@ const getBranchDetails = (variables) => fetch$1({
|
|
264
347
|
method: "get",
|
265
348
|
...variables
|
266
349
|
});
|
267
|
-
const createBranch = (variables) => fetch$1({
|
268
|
-
url: "/db/{dbBranchName}",
|
269
|
-
method: "put",
|
270
|
-
...variables
|
271
|
-
});
|
350
|
+
const createBranch = (variables) => fetch$1({ url: "/db/{dbBranchName}", method: "put", ...variables });
|
272
351
|
const deleteBranch = (variables) => fetch$1({
|
273
352
|
url: "/db/{dbBranchName}",
|
274
353
|
method: "delete",
|
@@ -342,11 +421,7 @@ const updateColumn = (variables) => fetch$1({
|
|
342
421
|
method: "patch",
|
343
422
|
...variables
|
344
423
|
});
|
345
|
-
const insertRecord = (variables) => fetch$1({
|
346
|
-
url: "/db/{dbBranchName}/tables/{tableName}/data",
|
347
|
-
method: "post",
|
348
|
-
...variables
|
349
|
-
});
|
424
|
+
const insertRecord = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data", method: "post", ...variables });
|
350
425
|
const insertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "put", ...variables });
|
351
426
|
const updateRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "patch", ...variables });
|
352
427
|
const upsertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "post", ...variables });
|
@@ -366,6 +441,11 @@ const queryTable = (variables) => fetch$1({
|
|
366
441
|
method: "post",
|
367
442
|
...variables
|
368
443
|
});
|
444
|
+
const searchTable = (variables) => fetch$1({
|
445
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
446
|
+
method: "post",
|
447
|
+
...variables
|
448
|
+
});
|
369
449
|
const searchBranch = (variables) => fetch$1({
|
370
450
|
url: "/db/{dbBranchName}/search",
|
371
451
|
method: "post",
|
@@ -383,6 +463,7 @@ const operationsByTag = {
|
|
383
463
|
updateWorkspaceMemberRole,
|
384
464
|
removeWorkspaceMember,
|
385
465
|
inviteWorkspaceMember,
|
466
|
+
updateWorkspaceMemberInvite,
|
386
467
|
cancelWorkspaceMemberInvite,
|
387
468
|
resendWorkspaceMemberInvite,
|
388
469
|
acceptWorkspaceMemberInvite
|
@@ -391,6 +472,7 @@ const operationsByTag = {
|
|
391
472
|
getDatabaseList,
|
392
473
|
createDatabase,
|
393
474
|
deleteDatabase,
|
475
|
+
getDatabaseMetadata,
|
394
476
|
getGitBranchesMapping,
|
395
477
|
addGitBranchesEntry,
|
396
478
|
removeGitBranchesEntry,
|
@@ -429,6 +511,7 @@ const operationsByTag = {
|
|
429
511
|
getRecord,
|
430
512
|
bulkInsertTableRecords,
|
431
513
|
queryTable,
|
514
|
+
searchTable,
|
432
515
|
searchBranch
|
433
516
|
}
|
434
517
|
};
|
@@ -471,7 +554,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
471
554
|
throw TypeError("Cannot add the same private member more than once");
|
472
555
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
473
556
|
};
|
474
|
-
var __privateSet$
|
557
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
475
558
|
__accessCheck$7(obj, member, "write to private field");
|
476
559
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
477
560
|
return value;
|
@@ -486,7 +569,7 @@ class XataApiClient {
|
|
486
569
|
if (!apiKey) {
|
487
570
|
throw new Error("Could not resolve a valid apiKey");
|
488
571
|
}
|
489
|
-
__privateSet$
|
572
|
+
__privateSet$7(this, _extraProps, {
|
490
573
|
apiUrl: getHostUrl(provider, "main"),
|
491
574
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
492
575
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -613,6 +696,13 @@ class WorkspaceApi {
|
|
613
696
|
...this.extraProps
|
614
697
|
});
|
615
698
|
}
|
699
|
+
updateWorkspaceMemberInvite(workspaceId, inviteId, role) {
|
700
|
+
return operationsByTag.workspaces.updateWorkspaceMemberInvite({
|
701
|
+
pathParams: { workspaceId, inviteId },
|
702
|
+
body: { role },
|
703
|
+
...this.extraProps
|
704
|
+
});
|
705
|
+
}
|
616
706
|
cancelWorkspaceMemberInvite(workspaceId, inviteId) {
|
617
707
|
return operationsByTag.workspaces.cancelWorkspaceMemberInvite({
|
618
708
|
pathParams: { workspaceId, inviteId },
|
@@ -655,6 +745,12 @@ class DatabaseApi {
|
|
655
745
|
...this.extraProps
|
656
746
|
});
|
657
747
|
}
|
748
|
+
getDatabaseMetadata(workspace, dbName) {
|
749
|
+
return operationsByTag.database.getDatabaseMetadata({
|
750
|
+
pathParams: { workspace, dbName },
|
751
|
+
...this.extraProps
|
752
|
+
});
|
753
|
+
}
|
658
754
|
getGitBranchesMapping(workspace, dbName) {
|
659
755
|
return operationsByTag.database.getGitBranchesMapping({
|
660
756
|
pathParams: { workspace, dbName },
|
@@ -675,10 +771,10 @@ class DatabaseApi {
|
|
675
771
|
...this.extraProps
|
676
772
|
});
|
677
773
|
}
|
678
|
-
resolveBranch(workspace, dbName, gitBranch) {
|
774
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
679
775
|
return operationsByTag.database.resolveBranch({
|
680
776
|
pathParams: { workspace, dbName },
|
681
|
-
queryParams: { gitBranch },
|
777
|
+
queryParams: { gitBranch, fallbackBranch },
|
682
778
|
...this.extraProps
|
683
779
|
});
|
684
780
|
}
|
@@ -827,9 +923,10 @@ class RecordsApi {
|
|
827
923
|
constructor(extraProps) {
|
828
924
|
this.extraProps = extraProps;
|
829
925
|
}
|
830
|
-
insertRecord(workspace, database, branch, tableName, record) {
|
926
|
+
insertRecord(workspace, database, branch, tableName, record, options = {}) {
|
831
927
|
return operationsByTag.records.insertRecord({
|
832
928
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
929
|
+
queryParams: options,
|
833
930
|
body: record,
|
834
931
|
...this.extraProps
|
835
932
|
});
|
@@ -858,21 +955,24 @@ class RecordsApi {
|
|
858
955
|
...this.extraProps
|
859
956
|
});
|
860
957
|
}
|
861
|
-
deleteRecord(workspace, database, branch, tableName, recordId) {
|
958
|
+
deleteRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
862
959
|
return operationsByTag.records.deleteRecord({
|
863
960
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
961
|
+
queryParams: options,
|
864
962
|
...this.extraProps
|
865
963
|
});
|
866
964
|
}
|
867
965
|
getRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
868
966
|
return operationsByTag.records.getRecord({
|
869
967
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
968
|
+
queryParams: options,
|
870
969
|
...this.extraProps
|
871
970
|
});
|
872
971
|
}
|
873
|
-
bulkInsertTableRecords(workspace, database, branch, tableName, records) {
|
972
|
+
bulkInsertTableRecords(workspace, database, branch, tableName, records, options = {}) {
|
874
973
|
return operationsByTag.records.bulkInsertTableRecords({
|
875
974
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
975
|
+
queryParams: options,
|
876
976
|
body: { records },
|
877
977
|
...this.extraProps
|
878
978
|
});
|
@@ -884,6 +984,13 @@ class RecordsApi {
|
|
884
984
|
...this.extraProps
|
885
985
|
});
|
886
986
|
}
|
987
|
+
searchTable(workspace, database, branch, tableName, query) {
|
988
|
+
return operationsByTag.records.searchTable({
|
989
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
990
|
+
body: query,
|
991
|
+
...this.extraProps
|
992
|
+
});
|
993
|
+
}
|
887
994
|
searchBranch(workspace, database, branch, query) {
|
888
995
|
return operationsByTag.records.searchBranch({
|
889
996
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -916,18 +1023,18 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
916
1023
|
throw TypeError("Cannot add the same private member more than once");
|
917
1024
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
918
1025
|
};
|
919
|
-
var __privateSet$
|
1026
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
920
1027
|
__accessCheck$6(obj, member, "write to private field");
|
921
1028
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
922
1029
|
return value;
|
923
1030
|
};
|
924
|
-
var _query;
|
1031
|
+
var _query, _page;
|
925
1032
|
class Page {
|
926
1033
|
constructor(query, meta, records = []) {
|
927
1034
|
__privateAdd$6(this, _query, void 0);
|
928
|
-
__privateSet$
|
1035
|
+
__privateSet$6(this, _query, query);
|
929
1036
|
this.meta = meta;
|
930
|
-
this.records = records;
|
1037
|
+
this.records = new RecordArray(this, records);
|
931
1038
|
}
|
932
1039
|
async nextPage(size, offset) {
|
933
1040
|
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
@@ -947,9 +1054,56 @@ class Page {
|
|
947
1054
|
}
|
948
1055
|
_query = new WeakMap();
|
949
1056
|
const PAGINATION_MAX_SIZE = 200;
|
950
|
-
const PAGINATION_DEFAULT_SIZE =
|
1057
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
951
1058
|
const PAGINATION_MAX_OFFSET = 800;
|
952
1059
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
1060
|
+
function isCursorPaginationOptions(options) {
|
1061
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
1062
|
+
}
|
1063
|
+
const _RecordArray = class extends Array {
|
1064
|
+
constructor(...args) {
|
1065
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
1066
|
+
__privateAdd$6(this, _page, void 0);
|
1067
|
+
__privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
|
1068
|
+
}
|
1069
|
+
static parseConstructorParams(...args) {
|
1070
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1071
|
+
return new Array(args[0]);
|
1072
|
+
}
|
1073
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1074
|
+
const result = args[1] ?? args[0].records ?? [];
|
1075
|
+
return new Array(...result);
|
1076
|
+
}
|
1077
|
+
return new Array(...args);
|
1078
|
+
}
|
1079
|
+
toArray() {
|
1080
|
+
return new Array(...this);
|
1081
|
+
}
|
1082
|
+
map(callbackfn, thisArg) {
|
1083
|
+
return this.toArray().map(callbackfn, thisArg);
|
1084
|
+
}
|
1085
|
+
async nextPage(size, offset) {
|
1086
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1087
|
+
return new _RecordArray(newPage);
|
1088
|
+
}
|
1089
|
+
async previousPage(size, offset) {
|
1090
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1091
|
+
return new _RecordArray(newPage);
|
1092
|
+
}
|
1093
|
+
async firstPage(size, offset) {
|
1094
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1095
|
+
return new _RecordArray(newPage);
|
1096
|
+
}
|
1097
|
+
async lastPage(size, offset) {
|
1098
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1099
|
+
return new _RecordArray(newPage);
|
1100
|
+
}
|
1101
|
+
hasNextPage() {
|
1102
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1103
|
+
}
|
1104
|
+
};
|
1105
|
+
let RecordArray = _RecordArray;
|
1106
|
+
_page = new WeakMap();
|
953
1107
|
|
954
1108
|
var __accessCheck$5 = (obj, member, msg) => {
|
955
1109
|
if (!member.has(obj))
|
@@ -964,25 +1118,26 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
964
1118
|
throw TypeError("Cannot add the same private member more than once");
|
965
1119
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
966
1120
|
};
|
967
|
-
var __privateSet$
|
1121
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
968
1122
|
__accessCheck$5(obj, member, "write to private field");
|
969
1123
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
970
1124
|
return value;
|
971
1125
|
};
|
972
1126
|
var _table$1, _repository, _data;
|
973
1127
|
const _Query = class {
|
974
|
-
constructor(repository, table, data,
|
1128
|
+
constructor(repository, table, data, rawParent) {
|
975
1129
|
__privateAdd$5(this, _table$1, void 0);
|
976
1130
|
__privateAdd$5(this, _repository, void 0);
|
977
1131
|
__privateAdd$5(this, _data, { filter: {} });
|
978
1132
|
this.meta = { page: { cursor: "start", more: true } };
|
979
|
-
this.records = [];
|
980
|
-
__privateSet$
|
1133
|
+
this.records = new RecordArray(this, []);
|
1134
|
+
__privateSet$5(this, _table$1, table);
|
981
1135
|
if (repository) {
|
982
|
-
__privateSet$
|
1136
|
+
__privateSet$5(this, _repository, repository);
|
983
1137
|
} else {
|
984
|
-
__privateSet$
|
1138
|
+
__privateSet$5(this, _repository, this);
|
985
1139
|
}
|
1140
|
+
const parent = cleanParent(data, rawParent);
|
986
1141
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
987
1142
|
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
988
1143
|
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
@@ -1041,7 +1196,12 @@ const _Query = class {
|
|
1041
1196
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
1042
1197
|
}
|
1043
1198
|
select(columns) {
|
1044
|
-
return new _Query(
|
1199
|
+
return new _Query(
|
1200
|
+
__privateGet$5(this, _repository),
|
1201
|
+
__privateGet$5(this, _table$1),
|
1202
|
+
{ columns },
|
1203
|
+
__privateGet$5(this, _data)
|
1204
|
+
);
|
1045
1205
|
}
|
1046
1206
|
getPaginated(options = {}) {
|
1047
1207
|
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
@@ -1054,18 +1214,21 @@ const _Query = class {
|
|
1054
1214
|
}
|
1055
1215
|
async *getIterator(options = {}) {
|
1056
1216
|
const { batchSize = 1 } = options;
|
1057
|
-
let
|
1058
|
-
let
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1217
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1218
|
+
let more = page.hasNextPage();
|
1219
|
+
yield page.records;
|
1220
|
+
while (more) {
|
1221
|
+
page = await page.nextPage();
|
1222
|
+
more = page.hasNextPage();
|
1223
|
+
yield page.records;
|
1064
1224
|
}
|
1065
1225
|
}
|
1066
1226
|
async getMany(options = {}) {
|
1067
|
-
const
|
1068
|
-
|
1227
|
+
const page = await this.getPaginated(options);
|
1228
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1229
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1230
|
+
}
|
1231
|
+
return page.records;
|
1069
1232
|
}
|
1070
1233
|
async getAll(options = {}) {
|
1071
1234
|
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
@@ -1077,7 +1240,7 @@ const _Query = class {
|
|
1077
1240
|
}
|
1078
1241
|
async getFirst(options = {}) {
|
1079
1242
|
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1080
|
-
return records[0]
|
1243
|
+
return records[0] ?? null;
|
1081
1244
|
}
|
1082
1245
|
cache(ttl) {
|
1083
1246
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
@@ -1102,12 +1265,20 @@ let Query = _Query;
|
|
1102
1265
|
_table$1 = new WeakMap();
|
1103
1266
|
_repository = new WeakMap();
|
1104
1267
|
_data = new WeakMap();
|
1268
|
+
function cleanParent(data, parent) {
|
1269
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1270
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1271
|
+
}
|
1272
|
+
return parent;
|
1273
|
+
}
|
1105
1274
|
|
1106
1275
|
function isIdentifiable(x) {
|
1107
1276
|
return isObject(x) && isString(x?.id);
|
1108
1277
|
}
|
1109
1278
|
function isXataRecord(x) {
|
1110
|
-
|
1279
|
+
const record = x;
|
1280
|
+
const metadata = record?.getMetadata();
|
1281
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1111
1282
|
}
|
1112
1283
|
|
1113
1284
|
function isSortFilterString(value) {
|
@@ -1146,7 +1317,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1146
1317
|
throw TypeError("Cannot add the same private member more than once");
|
1147
1318
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1148
1319
|
};
|
1149
|
-
var __privateSet$
|
1320
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1150
1321
|
__accessCheck$4(obj, member, "write to private field");
|
1151
1322
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1152
1323
|
return value;
|
@@ -1155,7 +1326,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1155
1326
|
__accessCheck$4(obj, member, "access private method");
|
1156
1327
|
return method;
|
1157
1328
|
};
|
1158
|
-
var _table, _getFetchProps, _cache,
|
1329
|
+
var _table, _getFetchProps, _db, _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;
|
1159
1330
|
class Repository extends Query {
|
1160
1331
|
}
|
1161
1332
|
class RestRepository extends Query {
|
@@ -1167,111 +1338,122 @@ class RestRepository extends Query {
|
|
1167
1338
|
__privateAdd$4(this, _updateRecordWithID);
|
1168
1339
|
__privateAdd$4(this, _upsertRecordWithID);
|
1169
1340
|
__privateAdd$4(this, _deleteRecord);
|
1170
|
-
__privateAdd$4(this, _invalidateCache);
|
1171
|
-
__privateAdd$4(this, _setCacheRecord);
|
1172
|
-
__privateAdd$4(this, _getCacheRecord);
|
1173
1341
|
__privateAdd$4(this, _setCacheQuery);
|
1174
1342
|
__privateAdd$4(this, _getCacheQuery);
|
1175
|
-
__privateAdd$4(this,
|
1343
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1176
1344
|
__privateAdd$4(this, _table, void 0);
|
1177
1345
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1346
|
+
__privateAdd$4(this, _db, void 0);
|
1178
1347
|
__privateAdd$4(this, _cache, void 0);
|
1179
|
-
__privateAdd$4(this,
|
1180
|
-
__privateSet$
|
1181
|
-
__privateSet$
|
1182
|
-
this
|
1183
|
-
__privateSet$
|
1184
|
-
|
1185
|
-
|
1348
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1349
|
+
__privateSet$4(this, _table, options.table);
|
1350
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1351
|
+
__privateSet$4(this, _db, options.db);
|
1352
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1353
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1354
|
+
}
|
1355
|
+
async create(a, b, c) {
|
1186
1356
|
if (Array.isArray(a)) {
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1357
|
+
if (a.length === 0)
|
1358
|
+
return [];
|
1359
|
+
const columns = isStringArray(b) ? b : void 0;
|
1360
|
+
return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a, columns);
|
1190
1361
|
}
|
1191
1362
|
if (isString(a) && isObject(b)) {
|
1192
1363
|
if (a === "")
|
1193
1364
|
throw new Error("The id can't be empty");
|
1194
|
-
const
|
1195
|
-
|
1196
|
-
return record;
|
1365
|
+
const columns = isStringArray(c) ? c : void 0;
|
1366
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns);
|
1197
1367
|
}
|
1198
1368
|
if (isObject(a) && isString(a.id)) {
|
1199
1369
|
if (a.id === "")
|
1200
1370
|
throw new Error("The id can't be empty");
|
1201
|
-
const
|
1202
|
-
|
1203
|
-
return record;
|
1371
|
+
const columns = isStringArray(b) ? b : void 0;
|
1372
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1204
1373
|
}
|
1205
1374
|
if (isObject(a)) {
|
1206
|
-
const
|
1207
|
-
|
1208
|
-
return record;
|
1375
|
+
const columns = isStringArray(b) ? b : void 0;
|
1376
|
+
return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a, columns);
|
1209
1377
|
}
|
1210
1378
|
throw new Error("Invalid arguments for create method");
|
1211
1379
|
}
|
1212
|
-
async read(
|
1213
|
-
const
|
1214
|
-
if (
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
const
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
return
|
1224
|
-
}
|
1225
|
-
|
1226
|
-
|
1380
|
+
async read(a, b) {
|
1381
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1382
|
+
if (Array.isArray(a)) {
|
1383
|
+
if (a.length === 0)
|
1384
|
+
return [];
|
1385
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1386
|
+
const finalObjects = await this.getAll({ filter: { id: { $any: ids } }, columns });
|
1387
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1388
|
+
acc[object.id] = object;
|
1389
|
+
return acc;
|
1390
|
+
}, {});
|
1391
|
+
return ids.map((id2) => dictionary[id2] ?? null);
|
1392
|
+
}
|
1393
|
+
const id = isString(a) ? a : a.id;
|
1394
|
+
if (isString(id)) {
|
1395
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1396
|
+
try {
|
1397
|
+
const response = await getRecord({
|
1398
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1399
|
+
queryParams: { columns },
|
1400
|
+
...fetchProps
|
1401
|
+
});
|
1402
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1403
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1404
|
+
} catch (e) {
|
1405
|
+
if (isObject(e) && e.status === 404) {
|
1406
|
+
return null;
|
1407
|
+
}
|
1408
|
+
throw e;
|
1227
1409
|
}
|
1228
|
-
throw e;
|
1229
1410
|
}
|
1411
|
+
return null;
|
1230
1412
|
}
|
1231
|
-
async update(a, b) {
|
1413
|
+
async update(a, b, c) {
|
1232
1414
|
if (Array.isArray(a)) {
|
1415
|
+
if (a.length === 0)
|
1416
|
+
return [];
|
1233
1417
|
if (a.length > 100) {
|
1234
1418
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1235
1419
|
}
|
1236
|
-
|
1420
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1421
|
+
return Promise.all(a.map((object) => this.update(object, columns)));
|
1237
1422
|
}
|
1238
1423
|
if (isString(a) && isObject(b)) {
|
1239
|
-
|
1240
|
-
|
1241
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1242
|
-
return record;
|
1424
|
+
const columns = isStringArray(c) ? c : void 0;
|
1425
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns);
|
1243
1426
|
}
|
1244
1427
|
if (isObject(a) && isString(a.id)) {
|
1245
|
-
|
1246
|
-
|
1247
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1248
|
-
return record;
|
1428
|
+
const columns = isStringArray(b) ? b : void 0;
|
1429
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1249
1430
|
}
|
1250
1431
|
throw new Error("Invalid arguments for update method");
|
1251
1432
|
}
|
1252
|
-
async createOrUpdate(a, b) {
|
1433
|
+
async createOrUpdate(a, b, c) {
|
1253
1434
|
if (Array.isArray(a)) {
|
1435
|
+
if (a.length === 0)
|
1436
|
+
return [];
|
1254
1437
|
if (a.length > 100) {
|
1255
1438
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1256
1439
|
}
|
1257
|
-
|
1440
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1441
|
+
return Promise.all(a.map((object) => this.createOrUpdate(object, columns)));
|
1258
1442
|
}
|
1259
1443
|
if (isString(a) && isObject(b)) {
|
1260
|
-
|
1261
|
-
|
1262
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1263
|
-
return record;
|
1444
|
+
const columns = isStringArray(c) ? c : void 0;
|
1445
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b, columns);
|
1264
1446
|
}
|
1265
1447
|
if (isObject(a) && isString(a.id)) {
|
1266
|
-
|
1267
|
-
|
1268
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1269
|
-
return record;
|
1448
|
+
const columns = isStringArray(c) ? c : void 0;
|
1449
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1270
1450
|
}
|
1271
1451
|
throw new Error("Invalid arguments for createOrUpdate method");
|
1272
1452
|
}
|
1273
1453
|
async delete(a) {
|
1274
1454
|
if (Array.isArray(a)) {
|
1455
|
+
if (a.length === 0)
|
1456
|
+
return;
|
1275
1457
|
if (a.length > 100) {
|
1276
1458
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1277
1459
|
}
|
@@ -1280,25 +1462,30 @@ class RestRepository extends Query {
|
|
1280
1462
|
}
|
1281
1463
|
if (isString(a)) {
|
1282
1464
|
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
|
1283
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1284
1465
|
return;
|
1285
1466
|
}
|
1286
1467
|
if (isObject(a) && isString(a.id)) {
|
1287
1468
|
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
|
1288
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1289
1469
|
return;
|
1290
1470
|
}
|
1291
1471
|
throw new Error("Invalid arguments for delete method");
|
1292
1472
|
}
|
1293
1473
|
async search(query, options = {}) {
|
1294
1474
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1295
|
-
const { records } = await
|
1296
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1297
|
-
body: {
|
1475
|
+
const { records } = await searchTable({
|
1476
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1477
|
+
body: {
|
1478
|
+
query,
|
1479
|
+
fuzziness: options.fuzziness,
|
1480
|
+
prefix: options.prefix,
|
1481
|
+
highlight: options.highlight,
|
1482
|
+
filter: options.filter,
|
1483
|
+
boosters: options.boosters
|
1484
|
+
},
|
1298
1485
|
...fetchProps
|
1299
1486
|
});
|
1300
|
-
const
|
1301
|
-
return records.map((item) => initObject(this
|
1487
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1488
|
+
return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1302
1489
|
}
|
1303
1490
|
async query(query) {
|
1304
1491
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1307,7 +1494,7 @@ class RestRepository extends Query {
|
|
1307
1494
|
const data = query.getQueryOptions();
|
1308
1495
|
const body = {
|
1309
1496
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1310
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1497
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1311
1498
|
page: data.pagination,
|
1312
1499
|
columns: data.columns
|
1313
1500
|
};
|
@@ -1317,18 +1504,19 @@ class RestRepository extends Query {
|
|
1317
1504
|
body,
|
1318
1505
|
...fetchProps
|
1319
1506
|
});
|
1320
|
-
const
|
1321
|
-
const records = objects.map((record) => initObject(this
|
1507
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1508
|
+
const records = objects.map((record) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), record));
|
1322
1509
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1323
1510
|
return new Page(query, meta, records);
|
1324
1511
|
}
|
1325
1512
|
}
|
1326
1513
|
_table = new WeakMap();
|
1327
1514
|
_getFetchProps = new WeakMap();
|
1515
|
+
_db = new WeakMap();
|
1328
1516
|
_cache = new WeakMap();
|
1329
|
-
|
1517
|
+
_schemaTables$2 = new WeakMap();
|
1330
1518
|
_insertRecordWithoutId = new WeakSet();
|
1331
|
-
insertRecordWithoutId_fn = async function(object) {
|
1519
|
+
insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
|
1332
1520
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1333
1521
|
const record = transformObjectLinks(object);
|
1334
1522
|
const response = await insertRecord({
|
@@ -1337,17 +1525,15 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1337
1525
|
dbBranchName: "{dbBranch}",
|
1338
1526
|
tableName: __privateGet$4(this, _table)
|
1339
1527
|
},
|
1528
|
+
queryParams: { columns },
|
1340
1529
|
body: record,
|
1341
1530
|
...fetchProps
|
1342
1531
|
});
|
1343
|
-
const
|
1344
|
-
|
1345
|
-
throw new Error("The server failed to save the record");
|
1346
|
-
}
|
1347
|
-
return finalObject;
|
1532
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1533
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1348
1534
|
};
|
1349
1535
|
_insertRecordWithId = new WeakSet();
|
1350
|
-
insertRecordWithId_fn = async function(recordId, object) {
|
1536
|
+
insertRecordWithId_fn = async function(recordId, object, columns = ["*"]) {
|
1351
1537
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1352
1538
|
const record = transformObjectLinks(object);
|
1353
1539
|
const response = await insertRecordWithID({
|
@@ -1358,56 +1544,52 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1358
1544
|
recordId
|
1359
1545
|
},
|
1360
1546
|
body: record,
|
1361
|
-
queryParams: { createOnly: true },
|
1547
|
+
queryParams: { createOnly: true, columns },
|
1362
1548
|
...fetchProps
|
1363
1549
|
});
|
1364
|
-
const
|
1365
|
-
|
1366
|
-
throw new Error("The server failed to save the record");
|
1367
|
-
}
|
1368
|
-
return finalObject;
|
1550
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1551
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1369
1552
|
};
|
1370
1553
|
_bulkInsertTableRecords = new WeakSet();
|
1371
|
-
bulkInsertTableRecords_fn = async function(objects) {
|
1554
|
+
bulkInsertTableRecords_fn = async function(objects, columns = ["*"]) {
|
1372
1555
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1373
1556
|
const records = objects.map((object) => transformObjectLinks(object));
|
1374
1557
|
const response = await bulkInsertTableRecords({
|
1375
1558
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1559
|
+
queryParams: { columns },
|
1376
1560
|
body: { records },
|
1377
1561
|
...fetchProps
|
1378
1562
|
});
|
1379
|
-
|
1380
|
-
|
1381
|
-
throw new Error("The server failed to save some records");
|
1563
|
+
if (!isResponseWithRecords(response)) {
|
1564
|
+
throw new Error("Request included columns but server didn't include them");
|
1382
1565
|
}
|
1383
|
-
|
1566
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1567
|
+
return response.records?.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1384
1568
|
};
|
1385
1569
|
_updateRecordWithID = new WeakSet();
|
1386
|
-
updateRecordWithID_fn = async function(recordId, object) {
|
1570
|
+
updateRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1387
1571
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1388
1572
|
const record = transformObjectLinks(object);
|
1389
1573
|
const response = await updateRecordWithID({
|
1390
1574
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1575
|
+
queryParams: { columns },
|
1391
1576
|
body: record,
|
1392
1577
|
...fetchProps
|
1393
1578
|
});
|
1394
|
-
const
|
1395
|
-
|
1396
|
-
throw new Error("The server failed to save the record");
|
1397
|
-
return item;
|
1579
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1580
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1398
1581
|
};
|
1399
1582
|
_upsertRecordWithID = new WeakSet();
|
1400
|
-
upsertRecordWithID_fn = async function(recordId, object) {
|
1583
|
+
upsertRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1401
1584
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1402
1585
|
const response = await upsertRecordWithID({
|
1403
1586
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1587
|
+
queryParams: { columns },
|
1404
1588
|
body: object,
|
1405
1589
|
...fetchProps
|
1406
1590
|
});
|
1407
|
-
const
|
1408
|
-
|
1409
|
-
throw new Error("The server failed to save the record");
|
1410
|
-
return item;
|
1591
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1592
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1411
1593
|
};
|
1412
1594
|
_deleteRecord = new WeakSet();
|
1413
1595
|
deleteRecord_fn = async function(recordId) {
|
@@ -1417,29 +1599,6 @@ deleteRecord_fn = async function(recordId) {
|
|
1417
1599
|
...fetchProps
|
1418
1600
|
});
|
1419
1601
|
};
|
1420
|
-
_invalidateCache = new WeakSet();
|
1421
|
-
invalidateCache_fn = async function(recordId) {
|
1422
|
-
await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1423
|
-
const cacheItems = await __privateGet$4(this, _cache).getAll();
|
1424
|
-
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1425
|
-
for (const [key, value] of queries) {
|
1426
|
-
const ids = getIds(value);
|
1427
|
-
if (ids.includes(recordId))
|
1428
|
-
await __privateGet$4(this, _cache).delete(key);
|
1429
|
-
}
|
1430
|
-
};
|
1431
|
-
_setCacheRecord = new WeakSet();
|
1432
|
-
setCacheRecord_fn = async function(record) {
|
1433
|
-
if (!__privateGet$4(this, _cache).cacheRecords)
|
1434
|
-
return;
|
1435
|
-
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1436
|
-
};
|
1437
|
-
_getCacheRecord = new WeakSet();
|
1438
|
-
getCacheRecord_fn = async function(recordId) {
|
1439
|
-
if (!__privateGet$4(this, _cache).cacheRecords)
|
1440
|
-
return null;
|
1441
|
-
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1442
|
-
};
|
1443
1602
|
_setCacheQuery = new WeakSet();
|
1444
1603
|
setCacheQuery_fn = async function(query, meta, records) {
|
1445
1604
|
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
@@ -1456,17 +1615,17 @@ getCacheQuery_fn = async function(query) {
|
|
1456
1615
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1457
1616
|
return hasExpired ? null : result;
|
1458
1617
|
};
|
1459
|
-
|
1460
|
-
|
1461
|
-
if (__privateGet$4(this,
|
1462
|
-
return __privateGet$4(this,
|
1618
|
+
_getSchemaTables$1 = new WeakSet();
|
1619
|
+
getSchemaTables_fn$1 = async function() {
|
1620
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1621
|
+
return __privateGet$4(this, _schemaTables$2);
|
1463
1622
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1464
1623
|
const { schema } = await getBranchDetails({
|
1465
1624
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1466
1625
|
...fetchProps
|
1467
1626
|
});
|
1468
|
-
__privateSet$
|
1469
|
-
return schema;
|
1627
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1628
|
+
return schema.tables;
|
1470
1629
|
};
|
1471
1630
|
const transformObjectLinks = (object) => {
|
1472
1631
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
@@ -1475,20 +1634,21 @@ const transformObjectLinks = (object) => {
|
|
1475
1634
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1476
1635
|
}, {});
|
1477
1636
|
};
|
1478
|
-
const initObject = (db,
|
1637
|
+
const initObject = (db, schemaTables, table, object) => {
|
1479
1638
|
const result = {};
|
1480
|
-
|
1481
|
-
|
1639
|
+
const { xata, ...rest } = object ?? {};
|
1640
|
+
Object.assign(result, rest);
|
1641
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1482
1642
|
if (!columns)
|
1483
1643
|
console.error(`Table ${table} not found in schema`);
|
1484
1644
|
for (const column of columns ?? []) {
|
1485
1645
|
const value = result[column.name];
|
1486
1646
|
switch (column.type) {
|
1487
1647
|
case "datetime": {
|
1488
|
-
const date = new Date(value);
|
1489
|
-
if (isNaN(date.getTime())) {
|
1648
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1649
|
+
if (date && isNaN(date.getTime())) {
|
1490
1650
|
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1491
|
-
} else {
|
1651
|
+
} else if (date) {
|
1492
1652
|
result[column.name] = date;
|
1493
1653
|
}
|
1494
1654
|
break;
|
@@ -1497,36 +1657,33 @@ const initObject = (db, schema, table, object) => {
|
|
1497
1657
|
const linkTable = column.link?.table;
|
1498
1658
|
if (!linkTable) {
|
1499
1659
|
console.error(`Failed to parse link for field ${column.name}`);
|
1500
|
-
} else if (
|
1501
|
-
result[column.name] = initObject(db,
|
1660
|
+
} else if (isObject(value)) {
|
1661
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1502
1662
|
}
|
1503
1663
|
break;
|
1504
1664
|
}
|
1505
1665
|
}
|
1506
1666
|
}
|
1507
|
-
result.read = function() {
|
1508
|
-
return db[table].read(result["id"]);
|
1667
|
+
result.read = function(columns2) {
|
1668
|
+
return db[table].read(result["id"], columns2);
|
1509
1669
|
};
|
1510
|
-
result.update = function(data) {
|
1511
|
-
return db[table].update(result["id"], data);
|
1670
|
+
result.update = function(data, columns2) {
|
1671
|
+
return db[table].update(result["id"], data, columns2);
|
1512
1672
|
};
|
1513
1673
|
result.delete = function() {
|
1514
1674
|
return db[table].delete(result["id"]);
|
1515
1675
|
};
|
1516
|
-
|
1676
|
+
result.getMetadata = function() {
|
1677
|
+
return xata;
|
1678
|
+
};
|
1679
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1517
1680
|
Object.defineProperty(result, prop, { enumerable: false });
|
1518
1681
|
}
|
1519
1682
|
Object.freeze(result);
|
1520
1683
|
return result;
|
1521
1684
|
};
|
1522
|
-
function
|
1523
|
-
|
1524
|
-
return value.map((item) => getIds(item)).flat();
|
1525
|
-
}
|
1526
|
-
if (!isObject(value))
|
1527
|
-
return [];
|
1528
|
-
const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
|
1529
|
-
return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
|
1685
|
+
function isResponseWithRecords(value) {
|
1686
|
+
return isObject(value) && Array.isArray(value.records);
|
1530
1687
|
}
|
1531
1688
|
|
1532
1689
|
var __accessCheck$3 = (obj, member, msg) => {
|
@@ -1542,7 +1699,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1542
1699
|
throw TypeError("Cannot add the same private member more than once");
|
1543
1700
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1544
1701
|
};
|
1545
|
-
var __privateSet$
|
1702
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1546
1703
|
__accessCheck$3(obj, member, "write to private field");
|
1547
1704
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1548
1705
|
return value;
|
@@ -1551,9 +1708,8 @@ var _map;
|
|
1551
1708
|
class SimpleCache {
|
1552
1709
|
constructor(options = {}) {
|
1553
1710
|
__privateAdd$3(this, _map, void 0);
|
1554
|
-
__privateSet$
|
1711
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1555
1712
|
this.capacity = options.max ?? 500;
|
1556
|
-
this.cacheRecords = options.cacheRecords ?? true;
|
1557
1713
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1558
1714
|
}
|
1559
1715
|
async getAll() {
|
@@ -1611,31 +1767,42 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1611
1767
|
throw TypeError("Cannot add the same private member more than once");
|
1612
1768
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1613
1769
|
};
|
1614
|
-
var
|
1770
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1771
|
+
__accessCheck$2(obj, member, "write to private field");
|
1772
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1773
|
+
return value;
|
1774
|
+
};
|
1775
|
+
var _tables, _schemaTables$1;
|
1615
1776
|
class SchemaPlugin extends XataPlugin {
|
1616
|
-
constructor(
|
1777
|
+
constructor(schemaTables) {
|
1617
1778
|
super();
|
1618
|
-
this.tableNames = tableNames;
|
1619
1779
|
__privateAdd$2(this, _tables, {});
|
1780
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1781
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1620
1782
|
}
|
1621
1783
|
build(pluginOptions) {
|
1622
|
-
const db = new Proxy(
|
1623
|
-
|
1624
|
-
|
1625
|
-
|
1626
|
-
|
1627
|
-
|
1784
|
+
const db = new Proxy(
|
1785
|
+
{},
|
1786
|
+
{
|
1787
|
+
get: (_target, table) => {
|
1788
|
+
if (!isString(table))
|
1789
|
+
throw new Error("Invalid table name");
|
1790
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1791
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1792
|
+
}
|
1793
|
+
return __privateGet$2(this, _tables)[table];
|
1628
1794
|
}
|
1629
|
-
return __privateGet$2(this, _tables)[table];
|
1630
1795
|
}
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1796
|
+
);
|
1797
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1798
|
+
for (const table of tableNames) {
|
1799
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1634
1800
|
}
|
1635
1801
|
return db;
|
1636
1802
|
}
|
1637
1803
|
}
|
1638
1804
|
_tables = new WeakMap();
|
1805
|
+
_schemaTables$1 = new WeakMap();
|
1639
1806
|
|
1640
1807
|
var __accessCheck$1 = (obj, member, msg) => {
|
1641
1808
|
if (!member.has(obj))
|
@@ -1659,105 +1826,118 @@ var __privateMethod$1 = (obj, member, method) => {
|
|
1659
1826
|
__accessCheck$1(obj, member, "access private method");
|
1660
1827
|
return method;
|
1661
1828
|
};
|
1662
|
-
var
|
1829
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1663
1830
|
class SearchPlugin extends XataPlugin {
|
1664
|
-
constructor(db) {
|
1831
|
+
constructor(db, schemaTables) {
|
1665
1832
|
super();
|
1666
1833
|
this.db = db;
|
1667
1834
|
__privateAdd$1(this, _search);
|
1668
|
-
__privateAdd$1(this,
|
1669
|
-
__privateAdd$1(this,
|
1835
|
+
__privateAdd$1(this, _getSchemaTables);
|
1836
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1837
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1670
1838
|
}
|
1671
1839
|
build({ getFetchProps }) {
|
1672
1840
|
return {
|
1673
1841
|
all: async (query, options = {}) => {
|
1674
1842
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1675
|
-
const
|
1843
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1676
1844
|
return records.map((record) => {
|
1677
1845
|
const { table = "orphan" } = record.xata;
|
1678
|
-
return { table, record: initObject(this.db,
|
1846
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1679
1847
|
});
|
1680
1848
|
},
|
1681
1849
|
byTable: async (query, options = {}) => {
|
1682
1850
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1683
|
-
const
|
1851
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1684
1852
|
return records.reduce((acc, record) => {
|
1685
1853
|
const { table = "orphan" } = record.xata;
|
1686
1854
|
const items = acc[table] ?? [];
|
1687
|
-
const item = initObject(this.db,
|
1855
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1688
1856
|
return { ...acc, [table]: [...items, item] };
|
1689
1857
|
}, {});
|
1690
1858
|
}
|
1691
1859
|
};
|
1692
1860
|
}
|
1693
1861
|
}
|
1694
|
-
|
1862
|
+
_schemaTables = new WeakMap();
|
1695
1863
|
_search = new WeakSet();
|
1696
1864
|
search_fn = async function(query, options, getFetchProps) {
|
1697
1865
|
const fetchProps = await getFetchProps();
|
1698
|
-
const { tables, fuzziness } = options ?? {};
|
1866
|
+
const { tables, fuzziness, highlight, prefix } = options ?? {};
|
1699
1867
|
const { records } = await searchBranch({
|
1700
1868
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1701
|
-
body: { tables, query, fuzziness },
|
1869
|
+
body: { tables, query, fuzziness, prefix, highlight },
|
1702
1870
|
...fetchProps
|
1703
1871
|
});
|
1704
1872
|
return records;
|
1705
1873
|
};
|
1706
|
-
|
1707
|
-
|
1708
|
-
if (__privateGet$1(this,
|
1709
|
-
return __privateGet$1(this,
|
1874
|
+
_getSchemaTables = new WeakSet();
|
1875
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1876
|
+
if (__privateGet$1(this, _schemaTables))
|
1877
|
+
return __privateGet$1(this, _schemaTables);
|
1710
1878
|
const fetchProps = await getFetchProps();
|
1711
1879
|
const { schema } = await getBranchDetails({
|
1712
1880
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1713
1881
|
...fetchProps
|
1714
1882
|
});
|
1715
|
-
__privateSet$1(this,
|
1716
|
-
return schema;
|
1883
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1884
|
+
return schema.tables;
|
1717
1885
|
};
|
1718
1886
|
|
1719
1887
|
const isBranchStrategyBuilder = (strategy) => {
|
1720
1888
|
return typeof strategy === "function";
|
1721
1889
|
};
|
1722
1890
|
|
1723
|
-
const envBranchNames = [
|
1724
|
-
"XATA_BRANCH",
|
1725
|
-
"VERCEL_GIT_COMMIT_REF",
|
1726
|
-
"CF_PAGES_BRANCH",
|
1727
|
-
"BRANCH"
|
1728
|
-
];
|
1729
|
-
const defaultBranch = "main";
|
1730
1891
|
async function getCurrentBranchName(options) {
|
1731
|
-
const
|
1732
|
-
if (
|
1733
|
-
|
1734
|
-
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1738
|
-
|
1739
|
-
|
1740
|
-
return defaultBranch;
|
1892
|
+
const { branch, envBranch } = getEnvironment();
|
1893
|
+
if (branch) {
|
1894
|
+
const details = await getDatabaseBranch(branch, options);
|
1895
|
+
if (details)
|
1896
|
+
return branch;
|
1897
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1898
|
+
}
|
1899
|
+
const gitBranch = envBranch || await getGitBranch();
|
1900
|
+
return resolveXataBranch(gitBranch, options);
|
1741
1901
|
}
|
1742
1902
|
async function getCurrentBranchDetails(options) {
|
1743
|
-
const
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1747
|
-
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1751
|
-
|
1752
|
-
|
1903
|
+
const branch = await getCurrentBranchName(options);
|
1904
|
+
return getDatabaseBranch(branch, options);
|
1905
|
+
}
|
1906
|
+
async function resolveXataBranch(gitBranch, options) {
|
1907
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1908
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1909
|
+
if (!databaseURL)
|
1910
|
+
throw new Error(
|
1911
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
1912
|
+
);
|
1913
|
+
if (!apiKey)
|
1914
|
+
throw new Error(
|
1915
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
1916
|
+
);
|
1917
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1918
|
+
const [workspace] = host.split(".");
|
1919
|
+
const { fallbackBranch } = getEnvironment();
|
1920
|
+
const { branch } = await resolveBranch({
|
1921
|
+
apiKey,
|
1922
|
+
apiUrl: databaseURL,
|
1923
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1924
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1925
|
+
pathParams: { dbName, workspace },
|
1926
|
+
queryParams: { gitBranch, fallbackBranch }
|
1927
|
+
});
|
1928
|
+
return branch;
|
1753
1929
|
}
|
1754
1930
|
async function getDatabaseBranch(branch, options) {
|
1755
1931
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1756
1932
|
const apiKey = options?.apiKey || getAPIKey();
|
1757
1933
|
if (!databaseURL)
|
1758
|
-
throw new Error(
|
1934
|
+
throw new Error(
|
1935
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
1936
|
+
);
|
1759
1937
|
if (!apiKey)
|
1760
|
-
throw new Error(
|
1938
|
+
throw new Error(
|
1939
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
1940
|
+
);
|
1761
1941
|
const [protocol, , host, , database] = databaseURL.split("/");
|
1762
1942
|
const [workspace] = host.split(".");
|
1763
1943
|
const dbBranchName = `${database}:${branch}`;
|
@@ -1767,10 +1947,7 @@ async function getDatabaseBranch(branch, options) {
|
|
1767
1947
|
apiUrl: databaseURL,
|
1768
1948
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1769
1949
|
workspacesApiUrl: `${protocol}//${host}`,
|
1770
|
-
pathParams: {
|
1771
|
-
dbBranchName,
|
1772
|
-
workspace
|
1773
|
-
}
|
1950
|
+
pathParams: { dbBranchName, workspace }
|
1774
1951
|
});
|
1775
1952
|
} catch (err) {
|
1776
1953
|
if (isObject(err) && err.status === 404)
|
@@ -1778,21 +1955,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1778
1955
|
throw err;
|
1779
1956
|
}
|
1780
1957
|
}
|
1781
|
-
function getBranchByEnvVariable() {
|
1782
|
-
for (const name of envBranchNames) {
|
1783
|
-
const value = getEnvVariable(name);
|
1784
|
-
if (value) {
|
1785
|
-
return value;
|
1786
|
-
}
|
1787
|
-
}
|
1788
|
-
try {
|
1789
|
-
return XATA_BRANCH;
|
1790
|
-
} catch (err) {
|
1791
|
-
}
|
1792
|
-
}
|
1793
1958
|
function getDatabaseURL() {
|
1794
1959
|
try {
|
1795
|
-
|
1960
|
+
const { databaseURL } = getEnvironment();
|
1961
|
+
return databaseURL;
|
1796
1962
|
} catch (err) {
|
1797
1963
|
return void 0;
|
1798
1964
|
}
|
@@ -1821,24 +1987,26 @@ var __privateMethod = (obj, member, method) => {
|
|
1821
1987
|
return method;
|
1822
1988
|
};
|
1823
1989
|
const buildClient = (plugins) => {
|
1824
|
-
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1990
|
+
var _branch, _options, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1825
1991
|
return _a = class {
|
1826
|
-
constructor(options = {},
|
1992
|
+
constructor(options = {}, schemaTables) {
|
1827
1993
|
__privateAdd(this, _parseOptions);
|
1828
1994
|
__privateAdd(this, _getFetchProps);
|
1829
1995
|
__privateAdd(this, _evaluateBranch);
|
1830
1996
|
__privateAdd(this, _branch, void 0);
|
1997
|
+
__privateAdd(this, _options, void 0);
|
1831
1998
|
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
1999
|
+
__privateSet(this, _options, safeOptions);
|
1832
2000
|
const pluginOptions = {
|
1833
2001
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1834
2002
|
cache: safeOptions.cache
|
1835
2003
|
};
|
1836
|
-
const db = new SchemaPlugin(
|
1837
|
-
const search = new SearchPlugin(db).build(pluginOptions);
|
2004
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2005
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1838
2006
|
this.db = db;
|
1839
2007
|
this.search = search;
|
1840
2008
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1841
|
-
if (
|
2009
|
+
if (namespace === void 0)
|
1842
2010
|
continue;
|
1843
2011
|
const result = namespace.build(pluginOptions);
|
1844
2012
|
if (result instanceof Promise) {
|
@@ -1850,22 +2018,22 @@ const buildClient = (plugins) => {
|
|
1850
2018
|
}
|
1851
2019
|
}
|
1852
2020
|
}
|
1853
|
-
|
2021
|
+
async getConfig() {
|
2022
|
+
const databaseURL = __privateGet(this, _options).databaseURL;
|
2023
|
+
const branch = await __privateGet(this, _options).branch();
|
2024
|
+
return { databaseURL, branch };
|
2025
|
+
}
|
2026
|
+
}, _branch = new WeakMap(), _options = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
|
1854
2027
|
const fetch = getFetchImplementation(options?.fetch);
|
1855
2028
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1856
2029
|
const apiKey = options?.apiKey || getAPIKey();
|
1857
|
-
const cache = options?.cache ?? new SimpleCache({
|
1858
|
-
const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
2030
|
+
const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
|
2031
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1859
2032
|
if (!databaseURL || !apiKey) {
|
1860
2033
|
throw new Error("Options databaseURL and apiKey are required");
|
1861
2034
|
}
|
1862
2035
|
return { fetch, databaseURL, apiKey, branch, cache };
|
1863
|
-
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
|
1864
|
-
fetch,
|
1865
|
-
apiKey,
|
1866
|
-
databaseURL,
|
1867
|
-
branch
|
1868
|
-
}) {
|
2036
|
+
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({ fetch, apiKey, databaseURL, branch }) {
|
1869
2037
|
const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
|
1870
2038
|
if (!branchValue)
|
1871
2039
|
throw new Error("Unable to resolve branch value");
|
@@ -1882,7 +2050,7 @@ const buildClient = (plugins) => {
|
|
1882
2050
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1883
2051
|
if (__privateGet(this, _branch))
|
1884
2052
|
return __privateGet(this, _branch);
|
1885
|
-
if (
|
2053
|
+
if (param === void 0)
|
1886
2054
|
return void 0;
|
1887
2055
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1888
2056
|
const evaluateBranch = async (strategy) => {
|
@@ -1900,6 +2068,88 @@ const buildClient = (plugins) => {
|
|
1900
2068
|
class BaseClient extends buildClient() {
|
1901
2069
|
}
|
1902
2070
|
|
2071
|
+
const META = "__";
|
2072
|
+
const VALUE = "___";
|
2073
|
+
class Serializer {
|
2074
|
+
constructor() {
|
2075
|
+
this.classes = {};
|
2076
|
+
}
|
2077
|
+
add(clazz) {
|
2078
|
+
this.classes[clazz.name] = clazz;
|
2079
|
+
}
|
2080
|
+
toJSON(data) {
|
2081
|
+
function visit(obj) {
|
2082
|
+
if (Array.isArray(obj))
|
2083
|
+
return obj.map(visit);
|
2084
|
+
const type = typeof obj;
|
2085
|
+
if (type === "undefined")
|
2086
|
+
return { [META]: "undefined" };
|
2087
|
+
if (type === "bigint")
|
2088
|
+
return { [META]: "bigint", [VALUE]: obj.toString() };
|
2089
|
+
if (obj === null || type !== "object")
|
2090
|
+
return obj;
|
2091
|
+
const constructor = obj.constructor;
|
2092
|
+
const o = { [META]: constructor.name };
|
2093
|
+
for (const [key, value] of Object.entries(obj)) {
|
2094
|
+
o[key] = visit(value);
|
2095
|
+
}
|
2096
|
+
if (constructor === Date)
|
2097
|
+
o[VALUE] = obj.toISOString();
|
2098
|
+
if (constructor === Map)
|
2099
|
+
o[VALUE] = Object.fromEntries(obj);
|
2100
|
+
if (constructor === Set)
|
2101
|
+
o[VALUE] = [...obj];
|
2102
|
+
return o;
|
2103
|
+
}
|
2104
|
+
return JSON.stringify(visit(data));
|
2105
|
+
}
|
2106
|
+
fromJSON(json) {
|
2107
|
+
return JSON.parse(json, (key, value) => {
|
2108
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
2109
|
+
const { [META]: clazz, [VALUE]: val, ...rest } = value;
|
2110
|
+
const constructor = this.classes[clazz];
|
2111
|
+
if (constructor) {
|
2112
|
+
return Object.assign(Object.create(constructor.prototype), rest);
|
2113
|
+
}
|
2114
|
+
if (clazz === "Date")
|
2115
|
+
return new Date(val);
|
2116
|
+
if (clazz === "Set")
|
2117
|
+
return new Set(val);
|
2118
|
+
if (clazz === "Map")
|
2119
|
+
return new Map(Object.entries(val));
|
2120
|
+
if (clazz === "bigint")
|
2121
|
+
return BigInt(val);
|
2122
|
+
if (clazz === "undefined")
|
2123
|
+
return void 0;
|
2124
|
+
return rest;
|
2125
|
+
}
|
2126
|
+
return value;
|
2127
|
+
});
|
2128
|
+
}
|
2129
|
+
}
|
2130
|
+
const defaultSerializer = new Serializer();
|
2131
|
+
const serialize = (data) => {
|
2132
|
+
return defaultSerializer.toJSON(data);
|
2133
|
+
};
|
2134
|
+
const deserialize = (json) => {
|
2135
|
+
return defaultSerializer.fromJSON(json);
|
2136
|
+
};
|
2137
|
+
|
2138
|
+
function buildWorkerRunner(config) {
|
2139
|
+
return function xataWorker(name, _worker) {
|
2140
|
+
return async (...args) => {
|
2141
|
+
const url = process.env.NODE_ENV === "development" ? `http://localhost:64749/${name}` : `https://dispatcher.xata.workers.dev/${config.workspace}/${config.worker}/${name}`;
|
2142
|
+
const result = await fetch(url, {
|
2143
|
+
method: "POST",
|
2144
|
+
headers: { "Content-Type": "application/json" },
|
2145
|
+
body: serialize({ args })
|
2146
|
+
});
|
2147
|
+
const text = await result.text();
|
2148
|
+
return deserialize(text);
|
2149
|
+
};
|
2150
|
+
};
|
2151
|
+
}
|
2152
|
+
|
1903
2153
|
class XataError extends Error {
|
1904
2154
|
constructor(message, status) {
|
1905
2155
|
super(message);
|
@@ -1907,5 +2157,5 @@ class XataError extends Error {
|
|
1907
2157
|
}
|
1908
2158
|
}
|
1909
2159
|
|
1910
|
-
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, 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, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
2160
|
+
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, RecordArray, Repository, RestRepository, SchemaPlugin, SearchPlugin, Serializer, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, buildWorkerRunner, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, deserialize, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseMetadata, 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, serialize, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberInvite, updateWorkspaceMemberRole, upsertRecordWithID };
|
1911
2161
|
//# sourceMappingURL=index.mjs.map
|