@xata.io/client 0.0.0-alpha.vf3081bb → 0.0.0-alpha.vf3111df
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 +98 -0
- package/README.md +43 -35
- package/Usage.md +449 -0
- package/dist/index.cjs +645 -380
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +648 -160
- package/dist/index.mjs +622 -381
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -4
package/dist/index.mjs
CHANGED
@@ -1,3 +1,27 @@
|
|
1
|
+
const defaultTrace = async (_name, fn, _options) => {
|
2
|
+
return await fn({
|
3
|
+
setAttributes: () => {
|
4
|
+
return;
|
5
|
+
},
|
6
|
+
onError: () => {
|
7
|
+
return;
|
8
|
+
}
|
9
|
+
});
|
10
|
+
};
|
11
|
+
const TraceAttributes = {
|
12
|
+
VERSION: "xata.sdk.version",
|
13
|
+
TABLE: "xata.table",
|
14
|
+
HTTP_REQUEST_ID: "http.request_id",
|
15
|
+
HTTP_STATUS_CODE: "http.status_code",
|
16
|
+
HTTP_HOST: "http.host",
|
17
|
+
HTTP_SCHEME: "http.scheme",
|
18
|
+
HTTP_USER_AGENT: "http.user_agent",
|
19
|
+
HTTP_METHOD: "http.method",
|
20
|
+
HTTP_URL: "http.url",
|
21
|
+
HTTP_ROUTE: "http.route",
|
22
|
+
HTTP_TARGET: "http.target"
|
23
|
+
};
|
24
|
+
|
1
25
|
function notEmpty(value) {
|
2
26
|
return value !== null && value !== void 0;
|
3
27
|
}
|
@@ -13,43 +37,95 @@ function isDefined(value) {
|
|
13
37
|
function isString(value) {
|
14
38
|
return isDefined(value) && typeof value === "string";
|
15
39
|
}
|
40
|
+
function isStringArray(value) {
|
41
|
+
return isDefined(value) && Array.isArray(value) && value.every(isString);
|
42
|
+
}
|
16
43
|
function toBase64(value) {
|
17
44
|
try {
|
18
45
|
return btoa(value);
|
19
46
|
} catch (err) {
|
20
|
-
|
47
|
+
const buf = Buffer;
|
48
|
+
return buf.from(value).toString("base64");
|
21
49
|
}
|
22
50
|
}
|
23
51
|
|
24
|
-
function
|
52
|
+
function getEnvironment() {
|
25
53
|
try {
|
26
|
-
if (isObject(process) &&
|
27
|
-
return
|
54
|
+
if (isObject(process) && isObject(process.env)) {
|
55
|
+
return {
|
56
|
+
apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
|
57
|
+
databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
|
58
|
+
branch: process.env.XATA_BRANCH ?? getGlobalBranch(),
|
59
|
+
envBranch: process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH,
|
60
|
+
fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
|
61
|
+
};
|
28
62
|
}
|
29
63
|
} catch (err) {
|
30
64
|
}
|
31
65
|
try {
|
32
|
-
if (isObject(Deno) &&
|
33
|
-
return
|
66
|
+
if (isObject(Deno) && isObject(Deno.env)) {
|
67
|
+
return {
|
68
|
+
apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
|
69
|
+
databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
|
70
|
+
branch: Deno.env.get("XATA_BRANCH") ?? getGlobalBranch(),
|
71
|
+
envBranch: Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH"),
|
72
|
+
fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
|
73
|
+
};
|
34
74
|
}
|
35
75
|
} catch (err) {
|
36
76
|
}
|
77
|
+
return {
|
78
|
+
apiKey: getGlobalApiKey(),
|
79
|
+
databaseURL: getGlobalDatabaseURL(),
|
80
|
+
branch: getGlobalBranch(),
|
81
|
+
envBranch: void 0,
|
82
|
+
fallbackBranch: getGlobalFallbackBranch()
|
83
|
+
};
|
84
|
+
}
|
85
|
+
function getGlobalApiKey() {
|
86
|
+
try {
|
87
|
+
return XATA_API_KEY;
|
88
|
+
} catch (err) {
|
89
|
+
return void 0;
|
90
|
+
}
|
91
|
+
}
|
92
|
+
function getGlobalDatabaseURL() {
|
93
|
+
try {
|
94
|
+
return XATA_DATABASE_URL;
|
95
|
+
} catch (err) {
|
96
|
+
return void 0;
|
97
|
+
}
|
98
|
+
}
|
99
|
+
function getGlobalBranch() {
|
100
|
+
try {
|
101
|
+
return XATA_BRANCH;
|
102
|
+
} catch (err) {
|
103
|
+
return void 0;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
function getGlobalFallbackBranch() {
|
107
|
+
try {
|
108
|
+
return XATA_FALLBACK_BRANCH;
|
109
|
+
} catch (err) {
|
110
|
+
return void 0;
|
111
|
+
}
|
37
112
|
}
|
38
113
|
async function getGitBranch() {
|
114
|
+
const cmd = ["git", "branch", "--show-current"];
|
115
|
+
const fullCmd = cmd.join(" ");
|
116
|
+
const nodeModule = ["child", "process"].join("_");
|
117
|
+
const execOptions = { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] };
|
39
118
|
try {
|
40
119
|
if (typeof require === "function") {
|
41
|
-
|
42
|
-
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
120
|
+
return require(nodeModule).execSync(fullCmd, execOptions).trim();
|
43
121
|
}
|
122
|
+
const { execSync } = await import(nodeModule);
|
123
|
+
return execSync(fullCmd, execOptions).toString().trim();
|
44
124
|
} catch (err) {
|
45
125
|
}
|
46
126
|
try {
|
47
127
|
if (isObject(Deno)) {
|
48
|
-
const process2 = Deno.run({
|
49
|
-
cmd: ["git", "branch", "--show-current"],
|
50
|
-
stdout: "piped",
|
51
|
-
stderr: "piped"
|
52
|
-
});
|
128
|
+
const process2 = Deno.run({ cmd, stdout: "piped", stderr: "null" });
|
53
129
|
return new TextDecoder().decode(await process2.output()).trim();
|
54
130
|
}
|
55
131
|
} catch (err) {
|
@@ -58,7 +134,8 @@ async function getGitBranch() {
|
|
58
134
|
|
59
135
|
function getAPIKey() {
|
60
136
|
try {
|
61
|
-
|
137
|
+
const { apiKey } = getEnvironment();
|
138
|
+
return apiKey;
|
62
139
|
} catch (err) {
|
63
140
|
return void 0;
|
64
141
|
}
|
@@ -68,26 +145,35 @@ function getFetchImplementation(userFetch) {
|
|
68
145
|
const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
|
69
146
|
const fetchImpl = userFetch ?? globalFetch;
|
70
147
|
if (!fetchImpl) {
|
71
|
-
throw new Error(
|
148
|
+
throw new Error(
|
149
|
+
`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`
|
150
|
+
);
|
72
151
|
}
|
73
152
|
return fetchImpl;
|
74
153
|
}
|
75
154
|
|
155
|
+
const VERSION = "0.0.0-alpha.vf3111df";
|
156
|
+
|
76
157
|
class ErrorWithCause extends Error {
|
77
158
|
constructor(message, options) {
|
78
159
|
super(message, options);
|
79
160
|
}
|
80
161
|
}
|
81
162
|
class FetcherError extends ErrorWithCause {
|
82
|
-
constructor(status, data) {
|
163
|
+
constructor(status, data, requestId) {
|
83
164
|
super(getMessage(data));
|
84
165
|
this.status = status;
|
85
166
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
167
|
+
this.requestId = requestId;
|
86
168
|
if (data instanceof Error) {
|
87
169
|
this.stack = data.stack;
|
88
170
|
this.cause = data.cause;
|
89
171
|
}
|
90
172
|
}
|
173
|
+
toString() {
|
174
|
+
const error = super.toString();
|
175
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
176
|
+
}
|
91
177
|
}
|
92
178
|
function isBulkError(error) {
|
93
179
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -110,7 +196,12 @@ function getMessage(data) {
|
|
110
196
|
}
|
111
197
|
|
112
198
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
113
|
-
const
|
199
|
+
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
200
|
+
if (value === void 0 || value === null)
|
201
|
+
return acc;
|
202
|
+
return { ...acc, [key]: value };
|
203
|
+
}, {});
|
204
|
+
const query = new URLSearchParams(cleanQueryParams).toString();
|
114
205
|
const queryString = query.length > 0 ? `?${query}` : "";
|
115
206
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
116
207
|
};
|
@@ -140,32 +231,62 @@ async function fetch$1({
|
|
140
231
|
fetchImpl,
|
141
232
|
apiKey,
|
142
233
|
apiUrl,
|
143
|
-
workspacesApiUrl
|
234
|
+
workspacesApiUrl,
|
235
|
+
trace
|
144
236
|
}) {
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
237
|
+
return trace(
|
238
|
+
`${method.toUpperCase()} ${path}`,
|
239
|
+
async ({ setAttributes, onError }) => {
|
240
|
+
const baseUrl = buildBaseUrl({ path, workspacesApiUrl, pathParams, apiUrl });
|
241
|
+
const fullUrl = resolveUrl(baseUrl, queryParams, pathParams);
|
242
|
+
const url = fullUrl.includes("localhost") ? fullUrl.replace(/^[^.]+\./, "http://") : fullUrl;
|
243
|
+
setAttributes({
|
244
|
+
[TraceAttributes.HTTP_URL]: url,
|
245
|
+
[TraceAttributes.HTTP_TARGET]: resolveUrl(path, queryParams, pathParams)
|
246
|
+
});
|
247
|
+
const response = await fetchImpl(url, {
|
248
|
+
method: method.toUpperCase(),
|
249
|
+
body: body ? JSON.stringify(body) : void 0,
|
250
|
+
headers: {
|
251
|
+
"Content-Type": "application/json",
|
252
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
253
|
+
...headers,
|
254
|
+
...hostHeader(fullUrl),
|
255
|
+
Authorization: `Bearer ${apiKey}`
|
256
|
+
}
|
257
|
+
});
|
258
|
+
if (response.status === 204) {
|
259
|
+
return {};
|
260
|
+
}
|
261
|
+
const { host, protocol } = parseUrl(response.url);
|
262
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
263
|
+
setAttributes({
|
264
|
+
[TraceAttributes.HTTP_REQUEST_ID]: requestId,
|
265
|
+
[TraceAttributes.HTTP_STATUS_CODE]: response.status,
|
266
|
+
[TraceAttributes.HTTP_HOST]: host,
|
267
|
+
[TraceAttributes.HTTP_SCHEME]: protocol?.replace(":", "")
|
268
|
+
});
|
269
|
+
try {
|
270
|
+
const jsonResponse = await response.json();
|
271
|
+
if (response.ok) {
|
272
|
+
return jsonResponse;
|
273
|
+
}
|
274
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
275
|
+
} catch (error) {
|
276
|
+
const fetcherError = new FetcherError(response.status, error, requestId);
|
277
|
+
onError(fetcherError.message);
|
278
|
+
throw fetcherError;
|
279
|
+
}
|
280
|
+
},
|
281
|
+
{ [TraceAttributes.HTTP_METHOD]: method.toUpperCase(), [TraceAttributes.HTTP_ROUTE]: path }
|
282
|
+
);
|
283
|
+
}
|
284
|
+
function parseUrl(url) {
|
161
285
|
try {
|
162
|
-
const
|
163
|
-
|
164
|
-
return jsonResponse;
|
165
|
-
}
|
166
|
-
throw new FetcherError(response.status, jsonResponse);
|
286
|
+
const { host, protocol } = new URL(url);
|
287
|
+
return { host, protocol };
|
167
288
|
} catch (error) {
|
168
|
-
|
289
|
+
return {};
|
169
290
|
}
|
170
291
|
}
|
171
292
|
|
@@ -224,6 +345,7 @@ const removeWorkspaceMember = (variables) => fetch$1({
|
|
224
345
|
...variables
|
225
346
|
});
|
226
347
|
const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
|
348
|
+
const updateWorkspaceMemberInvite = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites/{inviteId}", method: "patch", ...variables });
|
227
349
|
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
228
350
|
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
229
351
|
method: "delete",
|
@@ -259,6 +381,11 @@ const deleteDatabase = (variables) => fetch$1({
|
|
259
381
|
method: "delete",
|
260
382
|
...variables
|
261
383
|
});
|
384
|
+
const getDatabaseMetadata = (variables) => fetch$1({
|
385
|
+
url: "/dbs/{dbName}/metadata",
|
386
|
+
method: "get",
|
387
|
+
...variables
|
388
|
+
});
|
262
389
|
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
263
390
|
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
264
391
|
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
@@ -272,11 +399,7 @@ const getBranchDetails = (variables) => fetch$1({
|
|
272
399
|
method: "get",
|
273
400
|
...variables
|
274
401
|
});
|
275
|
-
const createBranch = (variables) => fetch$1({
|
276
|
-
url: "/db/{dbBranchName}",
|
277
|
-
method: "put",
|
278
|
-
...variables
|
279
|
-
});
|
402
|
+
const createBranch = (variables) => fetch$1({ url: "/db/{dbBranchName}", method: "put", ...variables });
|
280
403
|
const deleteBranch = (variables) => fetch$1({
|
281
404
|
url: "/db/{dbBranchName}",
|
282
405
|
method: "delete",
|
@@ -350,11 +473,7 @@ const updateColumn = (variables) => fetch$1({
|
|
350
473
|
method: "patch",
|
351
474
|
...variables
|
352
475
|
});
|
353
|
-
const insertRecord = (variables) => fetch$1({
|
354
|
-
url: "/db/{dbBranchName}/tables/{tableName}/data",
|
355
|
-
method: "post",
|
356
|
-
...variables
|
357
|
-
});
|
476
|
+
const insertRecord = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data", method: "post", ...variables });
|
358
477
|
const insertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "put", ...variables });
|
359
478
|
const updateRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "patch", ...variables });
|
360
479
|
const upsertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "post", ...variables });
|
@@ -396,6 +515,7 @@ const operationsByTag = {
|
|
396
515
|
updateWorkspaceMemberRole,
|
397
516
|
removeWorkspaceMember,
|
398
517
|
inviteWorkspaceMember,
|
518
|
+
updateWorkspaceMemberInvite,
|
399
519
|
cancelWorkspaceMemberInvite,
|
400
520
|
resendWorkspaceMemberInvite,
|
401
521
|
acceptWorkspaceMemberInvite
|
@@ -404,6 +524,7 @@ const operationsByTag = {
|
|
404
524
|
getDatabaseList,
|
405
525
|
createDatabase,
|
406
526
|
deleteDatabase,
|
527
|
+
getDatabaseMetadata,
|
407
528
|
getGitBranchesMapping,
|
408
529
|
addGitBranchesEntry,
|
409
530
|
removeGitBranchesEntry,
|
@@ -485,7 +606,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
485
606
|
throw TypeError("Cannot add the same private member more than once");
|
486
607
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
487
608
|
};
|
488
|
-
var __privateSet$
|
609
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
489
610
|
__accessCheck$7(obj, member, "write to private field");
|
490
611
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
491
612
|
return value;
|
@@ -496,15 +617,17 @@ class XataApiClient {
|
|
496
617
|
__privateAdd$7(this, _extraProps, void 0);
|
497
618
|
__privateAdd$7(this, _namespaces, {});
|
498
619
|
const provider = options.host ?? "production";
|
499
|
-
const apiKey = options
|
620
|
+
const apiKey = options.apiKey ?? getAPIKey();
|
621
|
+
const trace = options.trace ?? defaultTrace;
|
500
622
|
if (!apiKey) {
|
501
623
|
throw new Error("Could not resolve a valid apiKey");
|
502
624
|
}
|
503
|
-
__privateSet$
|
625
|
+
__privateSet$7(this, _extraProps, {
|
504
626
|
apiUrl: getHostUrl(provider, "main"),
|
505
627
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
506
628
|
fetchImpl: getFetchImplementation(options.fetch),
|
507
|
-
apiKey
|
629
|
+
apiKey,
|
630
|
+
trace
|
508
631
|
});
|
509
632
|
}
|
510
633
|
get user() {
|
@@ -627,6 +750,13 @@ class WorkspaceApi {
|
|
627
750
|
...this.extraProps
|
628
751
|
});
|
629
752
|
}
|
753
|
+
updateWorkspaceMemberInvite(workspaceId, inviteId, role) {
|
754
|
+
return operationsByTag.workspaces.updateWorkspaceMemberInvite({
|
755
|
+
pathParams: { workspaceId, inviteId },
|
756
|
+
body: { role },
|
757
|
+
...this.extraProps
|
758
|
+
});
|
759
|
+
}
|
630
760
|
cancelWorkspaceMemberInvite(workspaceId, inviteId) {
|
631
761
|
return operationsByTag.workspaces.cancelWorkspaceMemberInvite({
|
632
762
|
pathParams: { workspaceId, inviteId },
|
@@ -669,6 +799,12 @@ class DatabaseApi {
|
|
669
799
|
...this.extraProps
|
670
800
|
});
|
671
801
|
}
|
802
|
+
getDatabaseMetadata(workspace, dbName) {
|
803
|
+
return operationsByTag.database.getDatabaseMetadata({
|
804
|
+
pathParams: { workspace, dbName },
|
805
|
+
...this.extraProps
|
806
|
+
});
|
807
|
+
}
|
672
808
|
getGitBranchesMapping(workspace, dbName) {
|
673
809
|
return operationsByTag.database.getGitBranchesMapping({
|
674
810
|
pathParams: { workspace, dbName },
|
@@ -689,10 +825,10 @@ class DatabaseApi {
|
|
689
825
|
...this.extraProps
|
690
826
|
});
|
691
827
|
}
|
692
|
-
resolveBranch(workspace, dbName, gitBranch) {
|
828
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
693
829
|
return operationsByTag.database.resolveBranch({
|
694
830
|
pathParams: { workspace, dbName },
|
695
|
-
queryParams: { gitBranch },
|
831
|
+
queryParams: { gitBranch, fallbackBranch },
|
696
832
|
...this.extraProps
|
697
833
|
});
|
698
834
|
}
|
@@ -841,9 +977,10 @@ class RecordsApi {
|
|
841
977
|
constructor(extraProps) {
|
842
978
|
this.extraProps = extraProps;
|
843
979
|
}
|
844
|
-
insertRecord(workspace, database, branch, tableName, record) {
|
980
|
+
insertRecord(workspace, database, branch, tableName, record, options = {}) {
|
845
981
|
return operationsByTag.records.insertRecord({
|
846
982
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
983
|
+
queryParams: options,
|
847
984
|
body: record,
|
848
985
|
...this.extraProps
|
849
986
|
});
|
@@ -872,21 +1009,24 @@ class RecordsApi {
|
|
872
1009
|
...this.extraProps
|
873
1010
|
});
|
874
1011
|
}
|
875
|
-
deleteRecord(workspace, database, branch, tableName, recordId) {
|
1012
|
+
deleteRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
876
1013
|
return operationsByTag.records.deleteRecord({
|
877
1014
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
1015
|
+
queryParams: options,
|
878
1016
|
...this.extraProps
|
879
1017
|
});
|
880
1018
|
}
|
881
1019
|
getRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
882
1020
|
return operationsByTag.records.getRecord({
|
883
1021
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
1022
|
+
queryParams: options,
|
884
1023
|
...this.extraProps
|
885
1024
|
});
|
886
1025
|
}
|
887
|
-
bulkInsertTableRecords(workspace, database, branch, tableName, records) {
|
1026
|
+
bulkInsertTableRecords(workspace, database, branch, tableName, records, options = {}) {
|
888
1027
|
return operationsByTag.records.bulkInsertTableRecords({
|
889
1028
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
1029
|
+
queryParams: options,
|
890
1030
|
body: { records },
|
891
1031
|
...this.extraProps
|
892
1032
|
});
|
@@ -937,7 +1077,7 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
937
1077
|
throw TypeError("Cannot add the same private member more than once");
|
938
1078
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
939
1079
|
};
|
940
|
-
var __privateSet$
|
1080
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
941
1081
|
__accessCheck$6(obj, member, "write to private field");
|
942
1082
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
943
1083
|
return value;
|
@@ -946,7 +1086,7 @@ var _query, _page;
|
|
946
1086
|
class Page {
|
947
1087
|
constructor(query, meta, records = []) {
|
948
1088
|
__privateAdd$6(this, _query, void 0);
|
949
|
-
__privateSet$
|
1089
|
+
__privateSet$6(this, _query, query);
|
950
1090
|
this.meta = meta;
|
951
1091
|
this.records = new RecordArray(this, records);
|
952
1092
|
}
|
@@ -975,10 +1115,26 @@ function isCursorPaginationOptions(options) {
|
|
975
1115
|
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
976
1116
|
}
|
977
1117
|
const _RecordArray = class extends Array {
|
978
|
-
constructor(
|
979
|
-
super(...
|
1118
|
+
constructor(...args) {
|
1119
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
980
1120
|
__privateAdd$6(this, _page, void 0);
|
981
|
-
__privateSet$
|
1121
|
+
__privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
|
1122
|
+
}
|
1123
|
+
static parseConstructorParams(...args) {
|
1124
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1125
|
+
return new Array(args[0]);
|
1126
|
+
}
|
1127
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1128
|
+
const result = args[1] ?? args[0].records ?? [];
|
1129
|
+
return new Array(...result);
|
1130
|
+
}
|
1131
|
+
return new Array(...args);
|
1132
|
+
}
|
1133
|
+
toArray() {
|
1134
|
+
return new Array(...this);
|
1135
|
+
}
|
1136
|
+
map(callbackfn, thisArg) {
|
1137
|
+
return this.toArray().map(callbackfn, thisArg);
|
982
1138
|
}
|
983
1139
|
async nextPage(size, offset) {
|
984
1140
|
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
@@ -1016,7 +1172,7 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
1016
1172
|
throw TypeError("Cannot add the same private member more than once");
|
1017
1173
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1018
1174
|
};
|
1019
|
-
var __privateSet$
|
1175
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
1020
1176
|
__accessCheck$5(obj, member, "write to private field");
|
1021
1177
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1022
1178
|
return value;
|
@@ -1029,11 +1185,11 @@ const _Query = class {
|
|
1029
1185
|
__privateAdd$5(this, _data, { filter: {} });
|
1030
1186
|
this.meta = { page: { cursor: "start", more: true } };
|
1031
1187
|
this.records = new RecordArray(this, []);
|
1032
|
-
__privateSet$
|
1188
|
+
__privateSet$5(this, _table$1, table);
|
1033
1189
|
if (repository) {
|
1034
|
-
__privateSet$
|
1190
|
+
__privateSet$5(this, _repository, repository);
|
1035
1191
|
} else {
|
1036
|
-
__privateSet$
|
1192
|
+
__privateSet$5(this, _repository, this);
|
1037
1193
|
}
|
1038
1194
|
const parent = cleanParent(data, rawParent);
|
1039
1195
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
@@ -1088,13 +1244,18 @@ const _Query = class {
|
|
1088
1244
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
1089
1245
|
}
|
1090
1246
|
}
|
1091
|
-
sort(column, direction) {
|
1247
|
+
sort(column, direction = "asc") {
|
1092
1248
|
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
1093
1249
|
const sort = [...originalSort, { column, direction }];
|
1094
1250
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
1095
1251
|
}
|
1096
1252
|
select(columns) {
|
1097
|
-
return new _Query(
|
1253
|
+
return new _Query(
|
1254
|
+
__privateGet$5(this, _repository),
|
1255
|
+
__privateGet$5(this, _table$1),
|
1256
|
+
{ columns },
|
1257
|
+
__privateGet$5(this, _data)
|
1258
|
+
);
|
1098
1259
|
}
|
1099
1260
|
getPaginated(options = {}) {
|
1100
1261
|
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
@@ -1210,7 +1371,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1210
1371
|
throw TypeError("Cannot add the same private member more than once");
|
1211
1372
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1212
1373
|
};
|
1213
|
-
var __privateSet$
|
1374
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1214
1375
|
__accessCheck$4(obj, member, "write to private field");
|
1215
1376
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1216
1377
|
return value;
|
@@ -1219,7 +1380,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1219
1380
|
__accessCheck$4(obj, member, "access private method");
|
1220
1381
|
return method;
|
1221
1382
|
};
|
1222
|
-
var _table, _getFetchProps, _cache,
|
1383
|
+
var _table, _getFetchProps, _db, _cache, _schemaTables$2, _trace, _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;
|
1223
1384
|
class Repository extends Query {
|
1224
1385
|
}
|
1225
1386
|
class RestRepository extends Query {
|
@@ -1231,188 +1392,214 @@ class RestRepository extends Query {
|
|
1231
1392
|
__privateAdd$4(this, _updateRecordWithID);
|
1232
1393
|
__privateAdd$4(this, _upsertRecordWithID);
|
1233
1394
|
__privateAdd$4(this, _deleteRecord);
|
1234
|
-
__privateAdd$4(this, _invalidateCache);
|
1235
|
-
__privateAdd$4(this, _setCacheRecord);
|
1236
|
-
__privateAdd$4(this, _getCacheRecord);
|
1237
1395
|
__privateAdd$4(this, _setCacheQuery);
|
1238
1396
|
__privateAdd$4(this, _getCacheQuery);
|
1239
|
-
__privateAdd$4(this,
|
1397
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1240
1398
|
__privateAdd$4(this, _table, void 0);
|
1241
1399
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1400
|
+
__privateAdd$4(this, _db, void 0);
|
1242
1401
|
__privateAdd$4(this, _cache, void 0);
|
1243
|
-
__privateAdd$4(this,
|
1244
|
-
|
1245
|
-
__privateSet$
|
1246
|
-
this
|
1247
|
-
__privateSet$
|
1248
|
-
|
1249
|
-
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
if (a === "")
|
1259
|
-
throw new Error("The id can't be empty");
|
1260
|
-
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
|
1261
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1262
|
-
return record;
|
1263
|
-
}
|
1264
|
-
if (isObject(a) && isString(a.id)) {
|
1265
|
-
if (a.id === "")
|
1266
|
-
throw new Error("The id can't be empty");
|
1267
|
-
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
|
1268
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1269
|
-
return record;
|
1270
|
-
}
|
1271
|
-
if (isObject(a)) {
|
1272
|
-
const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
|
1273
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1274
|
-
return record;
|
1275
|
-
}
|
1276
|
-
throw new Error("Invalid arguments for create method");
|
1402
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1403
|
+
__privateAdd$4(this, _trace, void 0);
|
1404
|
+
__privateSet$4(this, _table, options.table);
|
1405
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1406
|
+
__privateSet$4(this, _db, options.db);
|
1407
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1408
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1409
|
+
const trace = options.pluginOptions.trace ?? defaultTrace;
|
1410
|
+
__privateSet$4(this, _trace, async (name, fn, options2 = {}) => {
|
1411
|
+
return trace(name, fn, {
|
1412
|
+
...options2,
|
1413
|
+
[TraceAttributes.TABLE]: __privateGet$4(this, _table),
|
1414
|
+
[TraceAttributes.VERSION]: VERSION
|
1415
|
+
});
|
1416
|
+
});
|
1277
1417
|
}
|
1278
|
-
async
|
1279
|
-
|
1280
|
-
if (a
|
1281
|
-
|
1282
|
-
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
if (
|
1287
|
-
|
1288
|
-
|
1289
|
-
|
1290
|
-
|
1291
|
-
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1298
|
-
|
1418
|
+
async create(a, b, c) {
|
1419
|
+
return __privateGet$4(this, _trace).call(this, "create", async () => {
|
1420
|
+
if (Array.isArray(a)) {
|
1421
|
+
if (a.length === 0)
|
1422
|
+
return [];
|
1423
|
+
const columns = isStringArray(b) ? b : void 0;
|
1424
|
+
return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a, columns);
|
1425
|
+
}
|
1426
|
+
if (isString(a) && isObject(b)) {
|
1427
|
+
if (a === "")
|
1428
|
+
throw new Error("The id can't be empty");
|
1429
|
+
const columns = isStringArray(c) ? c : void 0;
|
1430
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns);
|
1431
|
+
}
|
1432
|
+
if (isObject(a) && isString(a.id)) {
|
1433
|
+
if (a.id === "")
|
1434
|
+
throw new Error("The id can't be empty");
|
1435
|
+
const columns = isStringArray(b) ? b : void 0;
|
1436
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1437
|
+
}
|
1438
|
+
if (isObject(a)) {
|
1439
|
+
const columns = isStringArray(b) ? b : void 0;
|
1440
|
+
return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a, columns);
|
1441
|
+
}
|
1442
|
+
throw new Error("Invalid arguments for create method");
|
1443
|
+
});
|
1444
|
+
}
|
1445
|
+
async read(a, b) {
|
1446
|
+
return __privateGet$4(this, _trace).call(this, "read", async () => {
|
1447
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1448
|
+
if (Array.isArray(a)) {
|
1449
|
+
if (a.length === 0)
|
1450
|
+
return [];
|
1451
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1452
|
+
const finalObjects = await this.getAll({ filter: { id: { $any: ids } }, columns });
|
1453
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1454
|
+
acc[object.id] = object;
|
1455
|
+
return acc;
|
1456
|
+
}, {});
|
1457
|
+
return ids.map((id2) => dictionary[id2] ?? null);
|
1458
|
+
}
|
1459
|
+
const id = isString(a) ? a : a.id;
|
1460
|
+
if (isString(id)) {
|
1461
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1462
|
+
try {
|
1463
|
+
const response = await getRecord({
|
1464
|
+
pathParams: {
|
1465
|
+
workspace: "{workspaceId}",
|
1466
|
+
dbBranchName: "{dbBranch}",
|
1467
|
+
tableName: __privateGet$4(this, _table),
|
1468
|
+
recordId: id
|
1469
|
+
},
|
1470
|
+
queryParams: { columns },
|
1471
|
+
...fetchProps
|
1472
|
+
});
|
1473
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1474
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1475
|
+
} catch (e) {
|
1476
|
+
if (isObject(e) && e.status === 404) {
|
1477
|
+
return null;
|
1478
|
+
}
|
1479
|
+
throw e;
|
1299
1480
|
}
|
1300
|
-
throw e;
|
1301
1481
|
}
|
1302
|
-
|
1482
|
+
return null;
|
1483
|
+
});
|
1303
1484
|
}
|
1304
|
-
async update(a, b) {
|
1305
|
-
|
1306
|
-
if (a
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1485
|
+
async update(a, b, c) {
|
1486
|
+
return __privateGet$4(this, _trace).call(this, "update", async () => {
|
1487
|
+
if (Array.isArray(a)) {
|
1488
|
+
if (a.length === 0)
|
1489
|
+
return [];
|
1490
|
+
if (a.length > 100) {
|
1491
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1492
|
+
}
|
1493
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1494
|
+
return Promise.all(a.map((object) => this.update(object, columns)));
|
1310
1495
|
}
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1315
|
-
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
|
1316
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1317
|
-
return record;
|
1318
|
-
}
|
1319
|
-
if (isObject(a) && isString(a.id)) {
|
1320
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1321
|
-
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1322
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1323
|
-
return record;
|
1324
|
-
}
|
1325
|
-
throw new Error("Invalid arguments for update method");
|
1326
|
-
}
|
1327
|
-
async createOrUpdate(a, b) {
|
1328
|
-
if (Array.isArray(a)) {
|
1329
|
-
if (a.length === 0)
|
1330
|
-
return [];
|
1331
|
-
if (a.length > 100) {
|
1332
|
-
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1496
|
+
if (isString(a) && isObject(b)) {
|
1497
|
+
const columns = isStringArray(c) ? c : void 0;
|
1498
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns);
|
1333
1499
|
}
|
1334
|
-
|
1335
|
-
|
1336
|
-
|
1337
|
-
|
1338
|
-
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
|
1348
|
-
|
1500
|
+
if (isObject(a) && isString(a.id)) {
|
1501
|
+
const columns = isStringArray(b) ? b : void 0;
|
1502
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1503
|
+
}
|
1504
|
+
throw new Error("Invalid arguments for update method");
|
1505
|
+
});
|
1506
|
+
}
|
1507
|
+
async createOrUpdate(a, b, c) {
|
1508
|
+
return __privateGet$4(this, _trace).call(this, "createOrUpdate", async () => {
|
1509
|
+
if (Array.isArray(a)) {
|
1510
|
+
if (a.length === 0)
|
1511
|
+
return [];
|
1512
|
+
if (a.length > 100) {
|
1513
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1514
|
+
}
|
1515
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1516
|
+
return Promise.all(a.map((object) => this.createOrUpdate(object, columns)));
|
1517
|
+
}
|
1518
|
+
if (isString(a) && isObject(b)) {
|
1519
|
+
const columns = isStringArray(c) ? c : void 0;
|
1520
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b, columns);
|
1521
|
+
}
|
1522
|
+
if (isObject(a) && isString(a.id)) {
|
1523
|
+
const columns = isStringArray(c) ? c : void 0;
|
1524
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1525
|
+
}
|
1526
|
+
throw new Error("Invalid arguments for createOrUpdate method");
|
1527
|
+
});
|
1349
1528
|
}
|
1350
1529
|
async delete(a) {
|
1351
|
-
|
1352
|
-
if (a
|
1530
|
+
return __privateGet$4(this, _trace).call(this, "delete", async () => {
|
1531
|
+
if (Array.isArray(a)) {
|
1532
|
+
if (a.length === 0)
|
1533
|
+
return;
|
1534
|
+
if (a.length > 100) {
|
1535
|
+
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1536
|
+
}
|
1537
|
+
await Promise.all(a.map((id) => this.delete(id)));
|
1353
1538
|
return;
|
1354
|
-
if (a.length > 100) {
|
1355
|
-
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1356
1539
|
}
|
1357
|
-
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
1363
|
-
|
1364
|
-
|
1365
|
-
|
1366
|
-
|
1367
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1368
|
-
return;
|
1369
|
-
}
|
1370
|
-
throw new Error("Invalid arguments for delete method");
|
1540
|
+
if (isString(a)) {
|
1541
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
|
1542
|
+
return;
|
1543
|
+
}
|
1544
|
+
if (isObject(a) && isString(a.id)) {
|
1545
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
|
1546
|
+
return;
|
1547
|
+
}
|
1548
|
+
throw new Error("Invalid arguments for delete method");
|
1549
|
+
});
|
1371
1550
|
}
|
1372
1551
|
async search(query, options = {}) {
|
1373
|
-
|
1374
|
-
|
1375
|
-
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
|
1381
|
-
|
1382
|
-
|
1552
|
+
return __privateGet$4(this, _trace).call(this, "search", async () => {
|
1553
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1554
|
+
const { records } = await searchTable({
|
1555
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1556
|
+
body: {
|
1557
|
+
query,
|
1558
|
+
fuzziness: options.fuzziness,
|
1559
|
+
prefix: options.prefix,
|
1560
|
+
highlight: options.highlight,
|
1561
|
+
filter: options.filter,
|
1562
|
+
boosters: options.boosters
|
1563
|
+
},
|
1564
|
+
...fetchProps
|
1565
|
+
});
|
1566
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1567
|
+
return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1383
1568
|
});
|
1384
|
-
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1385
|
-
return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
|
1386
1569
|
}
|
1387
1570
|
async query(query) {
|
1388
|
-
|
1389
|
-
|
1390
|
-
|
1391
|
-
|
1392
|
-
|
1393
|
-
|
1394
|
-
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
|
1399
|
-
|
1400
|
-
|
1401
|
-
|
1402
|
-
|
1571
|
+
return __privateGet$4(this, _trace).call(this, "query", async () => {
|
1572
|
+
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
1573
|
+
if (cacheQuery)
|
1574
|
+
return new Page(query, cacheQuery.meta, cacheQuery.records);
|
1575
|
+
const data = query.getQueryOptions();
|
1576
|
+
const body = {
|
1577
|
+
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1578
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1579
|
+
page: data.pagination,
|
1580
|
+
columns: data.columns
|
1581
|
+
};
|
1582
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1583
|
+
const { meta, records: objects } = await queryTable({
|
1584
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1585
|
+
body,
|
1586
|
+
...fetchProps
|
1587
|
+
});
|
1588
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1589
|
+
const records = objects.map((record) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), record));
|
1590
|
+
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1591
|
+
return new Page(query, meta, records);
|
1403
1592
|
});
|
1404
|
-
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1405
|
-
const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
|
1406
|
-
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1407
|
-
return new Page(query, meta, records);
|
1408
1593
|
}
|
1409
1594
|
}
|
1410
1595
|
_table = new WeakMap();
|
1411
1596
|
_getFetchProps = new WeakMap();
|
1597
|
+
_db = new WeakMap();
|
1412
1598
|
_cache = new WeakMap();
|
1413
|
-
|
1599
|
+
_schemaTables$2 = new WeakMap();
|
1600
|
+
_trace = new WeakMap();
|
1414
1601
|
_insertRecordWithoutId = new WeakSet();
|
1415
|
-
insertRecordWithoutId_fn = async function(object) {
|
1602
|
+
insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
|
1416
1603
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1417
1604
|
const record = transformObjectLinks(object);
|
1418
1605
|
const response = await insertRecord({
|
@@ -1421,17 +1608,15 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1421
1608
|
dbBranchName: "{dbBranch}",
|
1422
1609
|
tableName: __privateGet$4(this, _table)
|
1423
1610
|
},
|
1611
|
+
queryParams: { columns },
|
1424
1612
|
body: record,
|
1425
1613
|
...fetchProps
|
1426
1614
|
});
|
1427
|
-
const
|
1428
|
-
|
1429
|
-
throw new Error("The server failed to save the record");
|
1430
|
-
}
|
1431
|
-
return finalObject;
|
1615
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1616
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1432
1617
|
};
|
1433
1618
|
_insertRecordWithId = new WeakSet();
|
1434
|
-
insertRecordWithId_fn = async function(recordId, object) {
|
1619
|
+
insertRecordWithId_fn = async function(recordId, object, columns = ["*"]) {
|
1435
1620
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1436
1621
|
const record = transformObjectLinks(object);
|
1437
1622
|
const response = await insertRecordWithID({
|
@@ -1442,56 +1627,52 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1442
1627
|
recordId
|
1443
1628
|
},
|
1444
1629
|
body: record,
|
1445
|
-
queryParams: { createOnly: true },
|
1630
|
+
queryParams: { createOnly: true, columns },
|
1446
1631
|
...fetchProps
|
1447
1632
|
});
|
1448
|
-
const
|
1449
|
-
|
1450
|
-
throw new Error("The server failed to save the record");
|
1451
|
-
}
|
1452
|
-
return finalObject;
|
1633
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1634
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1453
1635
|
};
|
1454
1636
|
_bulkInsertTableRecords = new WeakSet();
|
1455
|
-
bulkInsertTableRecords_fn = async function(objects) {
|
1637
|
+
bulkInsertTableRecords_fn = async function(objects, columns = ["*"]) {
|
1456
1638
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1457
1639
|
const records = objects.map((object) => transformObjectLinks(object));
|
1458
1640
|
const response = await bulkInsertTableRecords({
|
1459
1641
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1642
|
+
queryParams: { columns },
|
1460
1643
|
body: { records },
|
1461
1644
|
...fetchProps
|
1462
1645
|
});
|
1463
|
-
|
1464
|
-
|
1465
|
-
throw new Error("The server failed to save some records");
|
1646
|
+
if (!isResponseWithRecords(response)) {
|
1647
|
+
throw new Error("Request included columns but server didn't include them");
|
1466
1648
|
}
|
1467
|
-
|
1649
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1650
|
+
return response.records?.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1468
1651
|
};
|
1469
1652
|
_updateRecordWithID = new WeakSet();
|
1470
|
-
updateRecordWithID_fn = async function(recordId, object) {
|
1653
|
+
updateRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1471
1654
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1472
1655
|
const record = transformObjectLinks(object);
|
1473
1656
|
const response = await updateRecordWithID({
|
1474
1657
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1658
|
+
queryParams: { columns },
|
1475
1659
|
body: record,
|
1476
1660
|
...fetchProps
|
1477
1661
|
});
|
1478
|
-
const
|
1479
|
-
|
1480
|
-
throw new Error("The server failed to save the record");
|
1481
|
-
return item;
|
1662
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1663
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1482
1664
|
};
|
1483
1665
|
_upsertRecordWithID = new WeakSet();
|
1484
|
-
upsertRecordWithID_fn = async function(recordId, object) {
|
1666
|
+
upsertRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1485
1667
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1486
1668
|
const response = await upsertRecordWithID({
|
1487
1669
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1670
|
+
queryParams: { columns },
|
1488
1671
|
body: object,
|
1489
1672
|
...fetchProps
|
1490
1673
|
});
|
1491
|
-
const
|
1492
|
-
|
1493
|
-
throw new Error("The server failed to save the record");
|
1494
|
-
return item;
|
1674
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1675
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1495
1676
|
};
|
1496
1677
|
_deleteRecord = new WeakSet();
|
1497
1678
|
deleteRecord_fn = async function(recordId) {
|
@@ -1501,29 +1682,6 @@ deleteRecord_fn = async function(recordId) {
|
|
1501
1682
|
...fetchProps
|
1502
1683
|
});
|
1503
1684
|
};
|
1504
|
-
_invalidateCache = new WeakSet();
|
1505
|
-
invalidateCache_fn = async function(recordId) {
|
1506
|
-
await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1507
|
-
const cacheItems = await __privateGet$4(this, _cache).getAll();
|
1508
|
-
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1509
|
-
for (const [key, value] of queries) {
|
1510
|
-
const ids = getIds(value);
|
1511
|
-
if (ids.includes(recordId))
|
1512
|
-
await __privateGet$4(this, _cache).delete(key);
|
1513
|
-
}
|
1514
|
-
};
|
1515
|
-
_setCacheRecord = new WeakSet();
|
1516
|
-
setCacheRecord_fn = async function(record) {
|
1517
|
-
if (!__privateGet$4(this, _cache).cacheRecords)
|
1518
|
-
return;
|
1519
|
-
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1520
|
-
};
|
1521
|
-
_getCacheRecord = new WeakSet();
|
1522
|
-
getCacheRecord_fn = async function(recordId) {
|
1523
|
-
if (!__privateGet$4(this, _cache).cacheRecords)
|
1524
|
-
return null;
|
1525
|
-
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1526
|
-
};
|
1527
1685
|
_setCacheQuery = new WeakSet();
|
1528
1686
|
setCacheQuery_fn = async function(query, meta, records) {
|
1529
1687
|
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
@@ -1540,17 +1698,17 @@ getCacheQuery_fn = async function(query) {
|
|
1540
1698
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1541
1699
|
return hasExpired ? null : result;
|
1542
1700
|
};
|
1543
|
-
|
1544
|
-
|
1545
|
-
if (__privateGet$4(this,
|
1546
|
-
return __privateGet$4(this,
|
1701
|
+
_getSchemaTables$1 = new WeakSet();
|
1702
|
+
getSchemaTables_fn$1 = async function() {
|
1703
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1704
|
+
return __privateGet$4(this, _schemaTables$2);
|
1547
1705
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1548
1706
|
const { schema } = await getBranchDetails({
|
1549
1707
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1550
1708
|
...fetchProps
|
1551
1709
|
});
|
1552
|
-
__privateSet$
|
1553
|
-
return schema;
|
1710
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1711
|
+
return schema.tables;
|
1554
1712
|
};
|
1555
1713
|
const transformObjectLinks = (object) => {
|
1556
1714
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
@@ -1559,11 +1717,11 @@ const transformObjectLinks = (object) => {
|
|
1559
1717
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1560
1718
|
}, {});
|
1561
1719
|
};
|
1562
|
-
const initObject = (db,
|
1720
|
+
const initObject = (db, schemaTables, table, object) => {
|
1563
1721
|
const result = {};
|
1564
1722
|
const { xata, ...rest } = object ?? {};
|
1565
1723
|
Object.assign(result, rest);
|
1566
|
-
const { columns } =
|
1724
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1567
1725
|
if (!columns)
|
1568
1726
|
console.error(`Table ${table} not found in schema`);
|
1569
1727
|
for (const column of columns ?? []) {
|
@@ -1583,17 +1741,17 @@ const initObject = (db, schema, table, object) => {
|
|
1583
1741
|
if (!linkTable) {
|
1584
1742
|
console.error(`Failed to parse link for field ${column.name}`);
|
1585
1743
|
} else if (isObject(value)) {
|
1586
|
-
result[column.name] = initObject(db,
|
1744
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1587
1745
|
}
|
1588
1746
|
break;
|
1589
1747
|
}
|
1590
1748
|
}
|
1591
1749
|
}
|
1592
|
-
result.read = function() {
|
1593
|
-
return db[table].read(result["id"]);
|
1750
|
+
result.read = function(columns2) {
|
1751
|
+
return db[table].read(result["id"], columns2);
|
1594
1752
|
};
|
1595
|
-
result.update = function(data) {
|
1596
|
-
return db[table].update(result["id"], data);
|
1753
|
+
result.update = function(data, columns2) {
|
1754
|
+
return db[table].update(result["id"], data, columns2);
|
1597
1755
|
};
|
1598
1756
|
result.delete = function() {
|
1599
1757
|
return db[table].delete(result["id"]);
|
@@ -1607,14 +1765,8 @@ const initObject = (db, schema, table, object) => {
|
|
1607
1765
|
Object.freeze(result);
|
1608
1766
|
return result;
|
1609
1767
|
};
|
1610
|
-
function
|
1611
|
-
|
1612
|
-
return value.map((item) => getIds(item)).flat();
|
1613
|
-
}
|
1614
|
-
if (!isObject(value))
|
1615
|
-
return [];
|
1616
|
-
const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
|
1617
|
-
return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
|
1768
|
+
function isResponseWithRecords(value) {
|
1769
|
+
return isObject(value) && Array.isArray(value.records);
|
1618
1770
|
}
|
1619
1771
|
|
1620
1772
|
var __accessCheck$3 = (obj, member, msg) => {
|
@@ -1630,7 +1782,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1630
1782
|
throw TypeError("Cannot add the same private member more than once");
|
1631
1783
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1632
1784
|
};
|
1633
|
-
var __privateSet$
|
1785
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1634
1786
|
__accessCheck$3(obj, member, "write to private field");
|
1635
1787
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1636
1788
|
return value;
|
@@ -1639,9 +1791,8 @@ var _map;
|
|
1639
1791
|
class SimpleCache {
|
1640
1792
|
constructor(options = {}) {
|
1641
1793
|
__privateAdd$3(this, _map, void 0);
|
1642
|
-
__privateSet$
|
1794
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1643
1795
|
this.capacity = options.max ?? 500;
|
1644
|
-
this.cacheRecords = options.cacheRecords ?? true;
|
1645
1796
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1646
1797
|
}
|
1647
1798
|
async getAll() {
|
@@ -1699,31 +1850,42 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1699
1850
|
throw TypeError("Cannot add the same private member more than once");
|
1700
1851
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1701
1852
|
};
|
1702
|
-
var
|
1853
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1854
|
+
__accessCheck$2(obj, member, "write to private field");
|
1855
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1856
|
+
return value;
|
1857
|
+
};
|
1858
|
+
var _tables, _schemaTables$1;
|
1703
1859
|
class SchemaPlugin extends XataPlugin {
|
1704
|
-
constructor(
|
1860
|
+
constructor(schemaTables) {
|
1705
1861
|
super();
|
1706
|
-
this.tableNames = tableNames;
|
1707
1862
|
__privateAdd$2(this, _tables, {});
|
1863
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1864
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1708
1865
|
}
|
1709
1866
|
build(pluginOptions) {
|
1710
|
-
const db = new Proxy(
|
1711
|
-
|
1712
|
-
|
1713
|
-
|
1714
|
-
|
1715
|
-
|
1867
|
+
const db = new Proxy(
|
1868
|
+
{},
|
1869
|
+
{
|
1870
|
+
get: (_target, table) => {
|
1871
|
+
if (!isString(table))
|
1872
|
+
throw new Error("Invalid table name");
|
1873
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1874
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1875
|
+
}
|
1876
|
+
return __privateGet$2(this, _tables)[table];
|
1716
1877
|
}
|
1717
|
-
return __privateGet$2(this, _tables)[table];
|
1718
1878
|
}
|
1719
|
-
|
1720
|
-
|
1721
|
-
|
1879
|
+
);
|
1880
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1881
|
+
for (const table of tableNames) {
|
1882
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1722
1883
|
}
|
1723
1884
|
return db;
|
1724
1885
|
}
|
1725
1886
|
}
|
1726
1887
|
_tables = new WeakMap();
|
1888
|
+
_schemaTables$1 = new WeakMap();
|
1727
1889
|
|
1728
1890
|
var __accessCheck$1 = (obj, member, msg) => {
|
1729
1891
|
if (!member.has(obj))
|
@@ -1747,82 +1909,77 @@ var __privateMethod$1 = (obj, member, method) => {
|
|
1747
1909
|
__accessCheck$1(obj, member, "access private method");
|
1748
1910
|
return method;
|
1749
1911
|
};
|
1750
|
-
var
|
1912
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1751
1913
|
class SearchPlugin extends XataPlugin {
|
1752
|
-
constructor(db) {
|
1914
|
+
constructor(db, schemaTables) {
|
1753
1915
|
super();
|
1754
1916
|
this.db = db;
|
1755
1917
|
__privateAdd$1(this, _search);
|
1756
|
-
__privateAdd$1(this,
|
1757
|
-
__privateAdd$1(this,
|
1918
|
+
__privateAdd$1(this, _getSchemaTables);
|
1919
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1920
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1758
1921
|
}
|
1759
1922
|
build({ getFetchProps }) {
|
1760
1923
|
return {
|
1761
1924
|
all: async (query, options = {}) => {
|
1762
1925
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1763
|
-
const
|
1926
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1764
1927
|
return records.map((record) => {
|
1765
1928
|
const { table = "orphan" } = record.xata;
|
1766
|
-
return { table, record: initObject(this.db,
|
1929
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1767
1930
|
});
|
1768
1931
|
},
|
1769
1932
|
byTable: async (query, options = {}) => {
|
1770
1933
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1771
|
-
const
|
1934
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1772
1935
|
return records.reduce((acc, record) => {
|
1773
1936
|
const { table = "orphan" } = record.xata;
|
1774
1937
|
const items = acc[table] ?? [];
|
1775
|
-
const item = initObject(this.db,
|
1938
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1776
1939
|
return { ...acc, [table]: [...items, item] };
|
1777
1940
|
}, {});
|
1778
1941
|
}
|
1779
1942
|
};
|
1780
1943
|
}
|
1781
1944
|
}
|
1782
|
-
|
1945
|
+
_schemaTables = new WeakMap();
|
1783
1946
|
_search = new WeakSet();
|
1784
1947
|
search_fn = async function(query, options, getFetchProps) {
|
1785
1948
|
const fetchProps = await getFetchProps();
|
1786
|
-
const { tables, fuzziness, highlight } = options ?? {};
|
1949
|
+
const { tables, fuzziness, highlight, prefix } = options ?? {};
|
1787
1950
|
const { records } = await searchBranch({
|
1788
1951
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1789
|
-
body: { tables, query, fuzziness, highlight },
|
1952
|
+
body: { tables, query, fuzziness, prefix, highlight },
|
1790
1953
|
...fetchProps
|
1791
1954
|
});
|
1792
1955
|
return records;
|
1793
1956
|
};
|
1794
|
-
|
1795
|
-
|
1796
|
-
if (__privateGet$1(this,
|
1797
|
-
return __privateGet$1(this,
|
1957
|
+
_getSchemaTables = new WeakSet();
|
1958
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1959
|
+
if (__privateGet$1(this, _schemaTables))
|
1960
|
+
return __privateGet$1(this, _schemaTables);
|
1798
1961
|
const fetchProps = await getFetchProps();
|
1799
1962
|
const { schema } = await getBranchDetails({
|
1800
1963
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1801
1964
|
...fetchProps
|
1802
1965
|
});
|
1803
|
-
__privateSet$1(this,
|
1804
|
-
return schema;
|
1966
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1967
|
+
return schema.tables;
|
1805
1968
|
};
|
1806
1969
|
|
1807
1970
|
const isBranchStrategyBuilder = (strategy) => {
|
1808
1971
|
return typeof strategy === "function";
|
1809
1972
|
};
|
1810
1973
|
|
1811
|
-
const envBranchNames = [
|
1812
|
-
"XATA_BRANCH",
|
1813
|
-
"VERCEL_GIT_COMMIT_REF",
|
1814
|
-
"CF_PAGES_BRANCH",
|
1815
|
-
"BRANCH"
|
1816
|
-
];
|
1817
1974
|
async function getCurrentBranchName(options) {
|
1818
|
-
const
|
1819
|
-
if (
|
1820
|
-
const details = await getDatabaseBranch(
|
1975
|
+
const { branch, envBranch } = getEnvironment();
|
1976
|
+
if (branch) {
|
1977
|
+
const details = await getDatabaseBranch(branch, options);
|
1821
1978
|
if (details)
|
1822
|
-
return
|
1823
|
-
console.warn(`Branch ${
|
1979
|
+
return branch;
|
1980
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1824
1981
|
}
|
1825
|
-
const gitBranch = await getGitBranch();
|
1982
|
+
const gitBranch = envBranch || await getGitBranch();
|
1826
1983
|
return resolveXataBranch(gitBranch, options);
|
1827
1984
|
}
|
1828
1985
|
async function getCurrentBranchDetails(options) {
|
@@ -1833,18 +1990,24 @@ async function resolveXataBranch(gitBranch, options) {
|
|
1833
1990
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1834
1991
|
const apiKey = options?.apiKey || getAPIKey();
|
1835
1992
|
if (!databaseURL)
|
1836
|
-
throw new Error(
|
1993
|
+
throw new Error(
|
1994
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
1995
|
+
);
|
1837
1996
|
if (!apiKey)
|
1838
|
-
throw new Error(
|
1997
|
+
throw new Error(
|
1998
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
1999
|
+
);
|
1839
2000
|
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1840
2001
|
const [workspace] = host.split(".");
|
2002
|
+
const { fallbackBranch } = getEnvironment();
|
1841
2003
|
const { branch } = await resolveBranch({
|
1842
2004
|
apiKey,
|
1843
2005
|
apiUrl: databaseURL,
|
1844
2006
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1845
2007
|
workspacesApiUrl: `${protocol}//${host}`,
|
1846
2008
|
pathParams: { dbName, workspace },
|
1847
|
-
queryParams: { gitBranch, fallbackBranch
|
2009
|
+
queryParams: { gitBranch, fallbackBranch },
|
2010
|
+
trace: defaultTrace
|
1848
2011
|
});
|
1849
2012
|
return branch;
|
1850
2013
|
}
|
@@ -1852,9 +2015,13 @@ async function getDatabaseBranch(branch, options) {
|
|
1852
2015
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1853
2016
|
const apiKey = options?.apiKey || getAPIKey();
|
1854
2017
|
if (!databaseURL)
|
1855
|
-
throw new Error(
|
2018
|
+
throw new Error(
|
2019
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
2020
|
+
);
|
1856
2021
|
if (!apiKey)
|
1857
|
-
throw new Error(
|
2022
|
+
throw new Error(
|
2023
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
2024
|
+
);
|
1858
2025
|
const [protocol, , host, , database] = databaseURL.split("/");
|
1859
2026
|
const [workspace] = host.split(".");
|
1860
2027
|
const dbBranchName = `${database}:${branch}`;
|
@@ -1864,10 +2031,8 @@ async function getDatabaseBranch(branch, options) {
|
|
1864
2031
|
apiUrl: databaseURL,
|
1865
2032
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1866
2033
|
workspacesApiUrl: `${protocol}//${host}`,
|
1867
|
-
pathParams: {
|
1868
|
-
|
1869
|
-
workspace
|
1870
|
-
}
|
2034
|
+
pathParams: { dbBranchName, workspace },
|
2035
|
+
trace: defaultTrace
|
1871
2036
|
});
|
1872
2037
|
} catch (err) {
|
1873
2038
|
if (isObject(err) && err.status === 404)
|
@@ -1875,21 +2040,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1875
2040
|
throw err;
|
1876
2041
|
}
|
1877
2042
|
}
|
1878
|
-
function getBranchByEnvVariable() {
|
1879
|
-
for (const name of envBranchNames) {
|
1880
|
-
const value = getEnvVariable(name);
|
1881
|
-
if (value) {
|
1882
|
-
return value;
|
1883
|
-
}
|
1884
|
-
}
|
1885
|
-
try {
|
1886
|
-
return XATA_BRANCH;
|
1887
|
-
} catch (err) {
|
1888
|
-
}
|
1889
|
-
}
|
1890
2043
|
function getDatabaseURL() {
|
1891
2044
|
try {
|
1892
|
-
|
2045
|
+
const { databaseURL } = getEnvironment();
|
2046
|
+
return databaseURL;
|
1893
2047
|
} catch (err) {
|
1894
2048
|
return void 0;
|
1895
2049
|
}
|
@@ -1918,20 +2072,23 @@ var __privateMethod = (obj, member, method) => {
|
|
1918
2072
|
return method;
|
1919
2073
|
};
|
1920
2074
|
const buildClient = (plugins) => {
|
1921
|
-
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
2075
|
+
var _branch, _options, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1922
2076
|
return _a = class {
|
1923
|
-
constructor(options = {},
|
2077
|
+
constructor(options = {}, schemaTables) {
|
1924
2078
|
__privateAdd(this, _parseOptions);
|
1925
2079
|
__privateAdd(this, _getFetchProps);
|
1926
2080
|
__privateAdd(this, _evaluateBranch);
|
1927
2081
|
__privateAdd(this, _branch, void 0);
|
2082
|
+
__privateAdd(this, _options, void 0);
|
1928
2083
|
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
2084
|
+
__privateSet(this, _options, safeOptions);
|
1929
2085
|
const pluginOptions = {
|
1930
2086
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1931
|
-
cache: safeOptions.cache
|
2087
|
+
cache: safeOptions.cache,
|
2088
|
+
trace: safeOptions.trace
|
1932
2089
|
};
|
1933
|
-
const db = new SchemaPlugin(
|
1934
|
-
const search = new SearchPlugin(db).build(pluginOptions);
|
2090
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2091
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1935
2092
|
this.db = db;
|
1936
2093
|
this.search = search;
|
1937
2094
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
@@ -1947,22 +2104,23 @@ const buildClient = (plugins) => {
|
|
1947
2104
|
}
|
1948
2105
|
}
|
1949
2106
|
}
|
1950
|
-
|
2107
|
+
async getConfig() {
|
2108
|
+
const databaseURL = __privateGet(this, _options).databaseURL;
|
2109
|
+
const branch = await __privateGet(this, _options).branch();
|
2110
|
+
return { databaseURL, branch };
|
2111
|
+
}
|
2112
|
+
}, _branch = new WeakMap(), _options = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
|
1951
2113
|
const fetch = getFetchImplementation(options?.fetch);
|
1952
2114
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1953
2115
|
const apiKey = options?.apiKey || getAPIKey();
|
1954
|
-
const cache = options?.cache ?? new SimpleCache({
|
2116
|
+
const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
|
2117
|
+
const trace = options?.trace ?? defaultTrace;
|
1955
2118
|
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1956
2119
|
if (!databaseURL || !apiKey) {
|
1957
2120
|
throw new Error("Options databaseURL and apiKey are required");
|
1958
2121
|
}
|
1959
|
-
return { fetch, databaseURL, apiKey, branch, cache };
|
1960
|
-
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
|
1961
|
-
fetch,
|
1962
|
-
apiKey,
|
1963
|
-
databaseURL,
|
1964
|
-
branch
|
1965
|
-
}) {
|
2122
|
+
return { fetch, databaseURL, apiKey, branch, cache, trace };
|
2123
|
+
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({ fetch, apiKey, databaseURL, branch, trace }) {
|
1966
2124
|
const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
|
1967
2125
|
if (!branchValue)
|
1968
2126
|
throw new Error("Unable to resolve branch value");
|
@@ -1974,7 +2132,8 @@ const buildClient = (plugins) => {
|
|
1974
2132
|
const hasBranch = params.dbBranchName ?? params.branch;
|
1975
2133
|
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : "");
|
1976
2134
|
return databaseURL + newPath;
|
1977
|
-
}
|
2135
|
+
},
|
2136
|
+
trace
|
1978
2137
|
};
|
1979
2138
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1980
2139
|
if (__privateGet(this, _branch))
|
@@ -1997,6 +2156,88 @@ const buildClient = (plugins) => {
|
|
1997
2156
|
class BaseClient extends buildClient() {
|
1998
2157
|
}
|
1999
2158
|
|
2159
|
+
const META = "__";
|
2160
|
+
const VALUE = "___";
|
2161
|
+
class Serializer {
|
2162
|
+
constructor() {
|
2163
|
+
this.classes = {};
|
2164
|
+
}
|
2165
|
+
add(clazz) {
|
2166
|
+
this.classes[clazz.name] = clazz;
|
2167
|
+
}
|
2168
|
+
toJSON(data) {
|
2169
|
+
function visit(obj) {
|
2170
|
+
if (Array.isArray(obj))
|
2171
|
+
return obj.map(visit);
|
2172
|
+
const type = typeof obj;
|
2173
|
+
if (type === "undefined")
|
2174
|
+
return { [META]: "undefined" };
|
2175
|
+
if (type === "bigint")
|
2176
|
+
return { [META]: "bigint", [VALUE]: obj.toString() };
|
2177
|
+
if (obj === null || type !== "object")
|
2178
|
+
return obj;
|
2179
|
+
const constructor = obj.constructor;
|
2180
|
+
const o = { [META]: constructor.name };
|
2181
|
+
for (const [key, value] of Object.entries(obj)) {
|
2182
|
+
o[key] = visit(value);
|
2183
|
+
}
|
2184
|
+
if (constructor === Date)
|
2185
|
+
o[VALUE] = obj.toISOString();
|
2186
|
+
if (constructor === Map)
|
2187
|
+
o[VALUE] = Object.fromEntries(obj);
|
2188
|
+
if (constructor === Set)
|
2189
|
+
o[VALUE] = [...obj];
|
2190
|
+
return o;
|
2191
|
+
}
|
2192
|
+
return JSON.stringify(visit(data));
|
2193
|
+
}
|
2194
|
+
fromJSON(json) {
|
2195
|
+
return JSON.parse(json, (key, value) => {
|
2196
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
2197
|
+
const { [META]: clazz, [VALUE]: val, ...rest } = value;
|
2198
|
+
const constructor = this.classes[clazz];
|
2199
|
+
if (constructor) {
|
2200
|
+
return Object.assign(Object.create(constructor.prototype), rest);
|
2201
|
+
}
|
2202
|
+
if (clazz === "Date")
|
2203
|
+
return new Date(val);
|
2204
|
+
if (clazz === "Set")
|
2205
|
+
return new Set(val);
|
2206
|
+
if (clazz === "Map")
|
2207
|
+
return new Map(Object.entries(val));
|
2208
|
+
if (clazz === "bigint")
|
2209
|
+
return BigInt(val);
|
2210
|
+
if (clazz === "undefined")
|
2211
|
+
return void 0;
|
2212
|
+
return rest;
|
2213
|
+
}
|
2214
|
+
return value;
|
2215
|
+
});
|
2216
|
+
}
|
2217
|
+
}
|
2218
|
+
const defaultSerializer = new Serializer();
|
2219
|
+
const serialize = (data) => {
|
2220
|
+
return defaultSerializer.toJSON(data);
|
2221
|
+
};
|
2222
|
+
const deserialize = (json) => {
|
2223
|
+
return defaultSerializer.fromJSON(json);
|
2224
|
+
};
|
2225
|
+
|
2226
|
+
function buildWorkerRunner(config) {
|
2227
|
+
return function xataWorker(name, _worker) {
|
2228
|
+
return async (...args) => {
|
2229
|
+
const url = process.env.NODE_ENV === "development" ? `http://localhost:64749/${name}` : `https://dispatcher.xata.workers.dev/${config.workspace}/${config.worker}/${name}`;
|
2230
|
+
const result = await fetch(url, {
|
2231
|
+
method: "POST",
|
2232
|
+
headers: { "Content-Type": "application/json" },
|
2233
|
+
body: serialize({ args })
|
2234
|
+
});
|
2235
|
+
const text = await result.text();
|
2236
|
+
return deserialize(text);
|
2237
|
+
};
|
2238
|
+
};
|
2239
|
+
}
|
2240
|
+
|
2000
2241
|
class XataError extends Error {
|
2001
2242
|
constructor(message, status) {
|
2002
2243
|
super(message);
|
@@ -2004,5 +2245,5 @@ class XataError extends Error {
|
|
2004
2245
|
}
|
2005
2246
|
}
|
2006
2247
|
|
2007
|
-
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, RecordArray, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
2248
|
+
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 };
|
2008
2249
|
//# sourceMappingURL=index.mjs.map
|