@xata.io/client 0.0.0-alpha.vfde9dcf → 0.0.0-alpha.vfe4a947
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 +130 -0
- package/README.md +273 -1
- package/Usage.md +449 -0
- package/dist/index.cjs +732 -387
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +853 -164
- package/dist/index.mjs +701 -388
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -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,21 +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
|
|
76
|
-
|
77
|
-
|
155
|
+
const VERSION = "0.0.0-alpha.vfe4a947";
|
156
|
+
|
157
|
+
class ErrorWithCause extends Error {
|
158
|
+
constructor(message, options) {
|
159
|
+
super(message, options);
|
160
|
+
}
|
161
|
+
}
|
162
|
+
class FetcherError extends ErrorWithCause {
|
163
|
+
constructor(status, data, requestId) {
|
78
164
|
super(getMessage(data));
|
79
165
|
this.status = status;
|
80
166
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
167
|
+
this.requestId = requestId;
|
81
168
|
if (data instanceof Error) {
|
82
169
|
this.stack = data.stack;
|
83
170
|
this.cause = data.cause;
|
84
171
|
}
|
85
172
|
}
|
173
|
+
toString() {
|
174
|
+
const error = super.toString();
|
175
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
176
|
+
}
|
86
177
|
}
|
87
178
|
function isBulkError(error) {
|
88
179
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -105,7 +196,12 @@ function getMessage(data) {
|
|
105
196
|
}
|
106
197
|
|
107
198
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
108
|
-
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();
|
109
205
|
const queryString = query.length > 0 ? `?${query}` : "";
|
110
206
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
111
207
|
};
|
@@ -135,32 +231,62 @@ async function fetch$1({
|
|
135
231
|
fetchImpl,
|
136
232
|
apiKey,
|
137
233
|
apiUrl,
|
138
|
-
workspacesApiUrl
|
234
|
+
workspacesApiUrl,
|
235
|
+
trace
|
139
236
|
}) {
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
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) {
|
156
285
|
try {
|
157
|
-
const
|
158
|
-
|
159
|
-
return jsonResponse;
|
160
|
-
}
|
161
|
-
throw new FetcherError(response.status, jsonResponse);
|
286
|
+
const { host, protocol } = new URL(url);
|
287
|
+
return { host, protocol };
|
162
288
|
} catch (error) {
|
163
|
-
|
289
|
+
return {};
|
164
290
|
}
|
165
291
|
}
|
166
292
|
|
@@ -219,6 +345,7 @@ const removeWorkspaceMember = (variables) => fetch$1({
|
|
219
345
|
...variables
|
220
346
|
});
|
221
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 });
|
222
349
|
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
223
350
|
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
224
351
|
method: "delete",
|
@@ -254,6 +381,11 @@ const deleteDatabase = (variables) => fetch$1({
|
|
254
381
|
method: "delete",
|
255
382
|
...variables
|
256
383
|
});
|
384
|
+
const getDatabaseMetadata = (variables) => fetch$1({
|
385
|
+
url: "/dbs/{dbName}/metadata",
|
386
|
+
method: "get",
|
387
|
+
...variables
|
388
|
+
});
|
257
389
|
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
258
390
|
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
259
391
|
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
@@ -267,11 +399,7 @@ const getBranchDetails = (variables) => fetch$1({
|
|
267
399
|
method: "get",
|
268
400
|
...variables
|
269
401
|
});
|
270
|
-
const createBranch = (variables) => fetch$1({
|
271
|
-
url: "/db/{dbBranchName}",
|
272
|
-
method: "put",
|
273
|
-
...variables
|
274
|
-
});
|
402
|
+
const createBranch = (variables) => fetch$1({ url: "/db/{dbBranchName}", method: "put", ...variables });
|
275
403
|
const deleteBranch = (variables) => fetch$1({
|
276
404
|
url: "/db/{dbBranchName}",
|
277
405
|
method: "delete",
|
@@ -345,11 +473,7 @@ const updateColumn = (variables) => fetch$1({
|
|
345
473
|
method: "patch",
|
346
474
|
...variables
|
347
475
|
});
|
348
|
-
const insertRecord = (variables) => fetch$1({
|
349
|
-
url: "/db/{dbBranchName}/tables/{tableName}/data",
|
350
|
-
method: "post",
|
351
|
-
...variables
|
352
|
-
});
|
476
|
+
const insertRecord = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data", method: "post", ...variables });
|
353
477
|
const insertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "put", ...variables });
|
354
478
|
const updateRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "patch", ...variables });
|
355
479
|
const upsertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "post", ...variables });
|
@@ -391,6 +515,7 @@ const operationsByTag = {
|
|
391
515
|
updateWorkspaceMemberRole,
|
392
516
|
removeWorkspaceMember,
|
393
517
|
inviteWorkspaceMember,
|
518
|
+
updateWorkspaceMemberInvite,
|
394
519
|
cancelWorkspaceMemberInvite,
|
395
520
|
resendWorkspaceMemberInvite,
|
396
521
|
acceptWorkspaceMemberInvite
|
@@ -399,6 +524,7 @@ const operationsByTag = {
|
|
399
524
|
getDatabaseList,
|
400
525
|
createDatabase,
|
401
526
|
deleteDatabase,
|
527
|
+
getDatabaseMetadata,
|
402
528
|
getGitBranchesMapping,
|
403
529
|
addGitBranchesEntry,
|
404
530
|
removeGitBranchesEntry,
|
@@ -480,7 +606,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
480
606
|
throw TypeError("Cannot add the same private member more than once");
|
481
607
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
482
608
|
};
|
483
|
-
var __privateSet$
|
609
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
484
610
|
__accessCheck$7(obj, member, "write to private field");
|
485
611
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
486
612
|
return value;
|
@@ -491,15 +617,17 @@ class XataApiClient {
|
|
491
617
|
__privateAdd$7(this, _extraProps, void 0);
|
492
618
|
__privateAdd$7(this, _namespaces, {});
|
493
619
|
const provider = options.host ?? "production";
|
494
|
-
const apiKey = options
|
620
|
+
const apiKey = options.apiKey ?? getAPIKey();
|
621
|
+
const trace = options.trace ?? defaultTrace;
|
495
622
|
if (!apiKey) {
|
496
623
|
throw new Error("Could not resolve a valid apiKey");
|
497
624
|
}
|
498
|
-
__privateSet$
|
625
|
+
__privateSet$7(this, _extraProps, {
|
499
626
|
apiUrl: getHostUrl(provider, "main"),
|
500
627
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
501
628
|
fetchImpl: getFetchImplementation(options.fetch),
|
502
|
-
apiKey
|
629
|
+
apiKey,
|
630
|
+
trace
|
503
631
|
});
|
504
632
|
}
|
505
633
|
get user() {
|
@@ -622,6 +750,13 @@ class WorkspaceApi {
|
|
622
750
|
...this.extraProps
|
623
751
|
});
|
624
752
|
}
|
753
|
+
updateWorkspaceMemberInvite(workspaceId, inviteId, role) {
|
754
|
+
return operationsByTag.workspaces.updateWorkspaceMemberInvite({
|
755
|
+
pathParams: { workspaceId, inviteId },
|
756
|
+
body: { role },
|
757
|
+
...this.extraProps
|
758
|
+
});
|
759
|
+
}
|
625
760
|
cancelWorkspaceMemberInvite(workspaceId, inviteId) {
|
626
761
|
return operationsByTag.workspaces.cancelWorkspaceMemberInvite({
|
627
762
|
pathParams: { workspaceId, inviteId },
|
@@ -664,6 +799,12 @@ class DatabaseApi {
|
|
664
799
|
...this.extraProps
|
665
800
|
});
|
666
801
|
}
|
802
|
+
getDatabaseMetadata(workspace, dbName) {
|
803
|
+
return operationsByTag.database.getDatabaseMetadata({
|
804
|
+
pathParams: { workspace, dbName },
|
805
|
+
...this.extraProps
|
806
|
+
});
|
807
|
+
}
|
667
808
|
getGitBranchesMapping(workspace, dbName) {
|
668
809
|
return operationsByTag.database.getGitBranchesMapping({
|
669
810
|
pathParams: { workspace, dbName },
|
@@ -684,10 +825,10 @@ class DatabaseApi {
|
|
684
825
|
...this.extraProps
|
685
826
|
});
|
686
827
|
}
|
687
|
-
resolveBranch(workspace, dbName, gitBranch) {
|
828
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
688
829
|
return operationsByTag.database.resolveBranch({
|
689
830
|
pathParams: { workspace, dbName },
|
690
|
-
queryParams: { gitBranch },
|
831
|
+
queryParams: { gitBranch, fallbackBranch },
|
691
832
|
...this.extraProps
|
692
833
|
});
|
693
834
|
}
|
@@ -836,9 +977,10 @@ class RecordsApi {
|
|
836
977
|
constructor(extraProps) {
|
837
978
|
this.extraProps = extraProps;
|
838
979
|
}
|
839
|
-
insertRecord(workspace, database, branch, tableName, record) {
|
980
|
+
insertRecord(workspace, database, branch, tableName, record, options = {}) {
|
840
981
|
return operationsByTag.records.insertRecord({
|
841
982
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
983
|
+
queryParams: options,
|
842
984
|
body: record,
|
843
985
|
...this.extraProps
|
844
986
|
});
|
@@ -867,21 +1009,24 @@ class RecordsApi {
|
|
867
1009
|
...this.extraProps
|
868
1010
|
});
|
869
1011
|
}
|
870
|
-
deleteRecord(workspace, database, branch, tableName, recordId) {
|
1012
|
+
deleteRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
871
1013
|
return operationsByTag.records.deleteRecord({
|
872
1014
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
1015
|
+
queryParams: options,
|
873
1016
|
...this.extraProps
|
874
1017
|
});
|
875
1018
|
}
|
876
1019
|
getRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
877
1020
|
return operationsByTag.records.getRecord({
|
878
1021
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
1022
|
+
queryParams: options,
|
879
1023
|
...this.extraProps
|
880
1024
|
});
|
881
1025
|
}
|
882
|
-
bulkInsertTableRecords(workspace, database, branch, tableName, records) {
|
1026
|
+
bulkInsertTableRecords(workspace, database, branch, tableName, records, options = {}) {
|
883
1027
|
return operationsByTag.records.bulkInsertTableRecords({
|
884
1028
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
1029
|
+
queryParams: options,
|
885
1030
|
body: { records },
|
886
1031
|
...this.extraProps
|
887
1032
|
});
|
@@ -932,18 +1077,18 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
932
1077
|
throw TypeError("Cannot add the same private member more than once");
|
933
1078
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
934
1079
|
};
|
935
|
-
var __privateSet$
|
1080
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
936
1081
|
__accessCheck$6(obj, member, "write to private field");
|
937
1082
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
938
1083
|
return value;
|
939
1084
|
};
|
940
|
-
var _query;
|
1085
|
+
var _query, _page;
|
941
1086
|
class Page {
|
942
1087
|
constructor(query, meta, records = []) {
|
943
1088
|
__privateAdd$6(this, _query, void 0);
|
944
|
-
__privateSet$
|
1089
|
+
__privateSet$6(this, _query, query);
|
945
1090
|
this.meta = meta;
|
946
|
-
this.records = records;
|
1091
|
+
this.records = new RecordArray(this, records);
|
947
1092
|
}
|
948
1093
|
async nextPage(size, offset) {
|
949
1094
|
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
@@ -963,12 +1108,56 @@ class Page {
|
|
963
1108
|
}
|
964
1109
|
_query = new WeakMap();
|
965
1110
|
const PAGINATION_MAX_SIZE = 200;
|
966
|
-
const PAGINATION_DEFAULT_SIZE =
|
1111
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
967
1112
|
const PAGINATION_MAX_OFFSET = 800;
|
968
1113
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
969
1114
|
function isCursorPaginationOptions(options) {
|
970
1115
|
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
971
1116
|
}
|
1117
|
+
const _RecordArray = class extends Array {
|
1118
|
+
constructor(...args) {
|
1119
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
1120
|
+
__privateAdd$6(this, _page, void 0);
|
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);
|
1138
|
+
}
|
1139
|
+
async nextPage(size, offset) {
|
1140
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1141
|
+
return new _RecordArray(newPage);
|
1142
|
+
}
|
1143
|
+
async previousPage(size, offset) {
|
1144
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1145
|
+
return new _RecordArray(newPage);
|
1146
|
+
}
|
1147
|
+
async firstPage(size, offset) {
|
1148
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1149
|
+
return new _RecordArray(newPage);
|
1150
|
+
}
|
1151
|
+
async lastPage(size, offset) {
|
1152
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1153
|
+
return new _RecordArray(newPage);
|
1154
|
+
}
|
1155
|
+
hasNextPage() {
|
1156
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1157
|
+
}
|
1158
|
+
};
|
1159
|
+
let RecordArray = _RecordArray;
|
1160
|
+
_page = new WeakMap();
|
972
1161
|
|
973
1162
|
var __accessCheck$5 = (obj, member, msg) => {
|
974
1163
|
if (!member.has(obj))
|
@@ -983,25 +1172,26 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
983
1172
|
throw TypeError("Cannot add the same private member more than once");
|
984
1173
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
985
1174
|
};
|
986
|
-
var __privateSet$
|
1175
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
987
1176
|
__accessCheck$5(obj, member, "write to private field");
|
988
1177
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
989
1178
|
return value;
|
990
1179
|
};
|
991
1180
|
var _table$1, _repository, _data;
|
992
1181
|
const _Query = class {
|
993
|
-
constructor(repository, table, data,
|
1182
|
+
constructor(repository, table, data, rawParent) {
|
994
1183
|
__privateAdd$5(this, _table$1, void 0);
|
995
1184
|
__privateAdd$5(this, _repository, void 0);
|
996
1185
|
__privateAdd$5(this, _data, { filter: {} });
|
997
1186
|
this.meta = { page: { cursor: "start", more: true } };
|
998
|
-
this.records = [];
|
999
|
-
__privateSet$
|
1187
|
+
this.records = new RecordArray(this, []);
|
1188
|
+
__privateSet$5(this, _table$1, table);
|
1000
1189
|
if (repository) {
|
1001
|
-
__privateSet$
|
1190
|
+
__privateSet$5(this, _repository, repository);
|
1002
1191
|
} else {
|
1003
|
-
__privateSet$
|
1192
|
+
__privateSet$5(this, _repository, this);
|
1004
1193
|
}
|
1194
|
+
const parent = cleanParent(data, rawParent);
|
1005
1195
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
1006
1196
|
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
1007
1197
|
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
@@ -1054,13 +1244,18 @@ const _Query = class {
|
|
1054
1244
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
1055
1245
|
}
|
1056
1246
|
}
|
1057
|
-
sort(column, direction) {
|
1247
|
+
sort(column, direction = "asc") {
|
1058
1248
|
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
1059
1249
|
const sort = [...originalSort, { column, direction }];
|
1060
1250
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
1061
1251
|
}
|
1062
1252
|
select(columns) {
|
1063
|
-
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
|
+
);
|
1064
1259
|
}
|
1065
1260
|
getPaginated(options = {}) {
|
1066
1261
|
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
@@ -1083,8 +1278,11 @@ const _Query = class {
|
|
1083
1278
|
}
|
1084
1279
|
}
|
1085
1280
|
async getMany(options = {}) {
|
1086
|
-
const
|
1087
|
-
|
1281
|
+
const page = await this.getPaginated(options);
|
1282
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1283
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1284
|
+
}
|
1285
|
+
return page.records;
|
1088
1286
|
}
|
1089
1287
|
async getAll(options = {}) {
|
1090
1288
|
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
@@ -1121,12 +1319,20 @@ let Query = _Query;
|
|
1121
1319
|
_table$1 = new WeakMap();
|
1122
1320
|
_repository = new WeakMap();
|
1123
1321
|
_data = new WeakMap();
|
1322
|
+
function cleanParent(data, parent) {
|
1323
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1324
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1325
|
+
}
|
1326
|
+
return parent;
|
1327
|
+
}
|
1124
1328
|
|
1125
1329
|
function isIdentifiable(x) {
|
1126
1330
|
return isObject(x) && isString(x?.id);
|
1127
1331
|
}
|
1128
1332
|
function isXataRecord(x) {
|
1129
|
-
|
1333
|
+
const record = x;
|
1334
|
+
const metadata = record?.getMetadata();
|
1335
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1130
1336
|
}
|
1131
1337
|
|
1132
1338
|
function isSortFilterString(value) {
|
@@ -1165,7 +1371,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1165
1371
|
throw TypeError("Cannot add the same private member more than once");
|
1166
1372
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1167
1373
|
};
|
1168
|
-
var __privateSet$
|
1374
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1169
1375
|
__accessCheck$4(obj, member, "write to private field");
|
1170
1376
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1171
1377
|
return value;
|
@@ -1174,7 +1380,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1174
1380
|
__accessCheck$4(obj, member, "access private method");
|
1175
1381
|
return method;
|
1176
1382
|
};
|
1177
|
-
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;
|
1178
1384
|
class Repository extends Query {
|
1179
1385
|
}
|
1180
1386
|
class RestRepository extends Query {
|
@@ -1186,175 +1392,214 @@ class RestRepository extends Query {
|
|
1186
1392
|
__privateAdd$4(this, _updateRecordWithID);
|
1187
1393
|
__privateAdd$4(this, _upsertRecordWithID);
|
1188
1394
|
__privateAdd$4(this, _deleteRecord);
|
1189
|
-
__privateAdd$4(this, _invalidateCache);
|
1190
|
-
__privateAdd$4(this, _setCacheRecord);
|
1191
|
-
__privateAdd$4(this, _getCacheRecord);
|
1192
1395
|
__privateAdd$4(this, _setCacheQuery);
|
1193
1396
|
__privateAdd$4(this, _getCacheQuery);
|
1194
|
-
__privateAdd$4(this,
|
1397
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1195
1398
|
__privateAdd$4(this, _table, void 0);
|
1196
1399
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1400
|
+
__privateAdd$4(this, _db, void 0);
|
1197
1401
|
__privateAdd$4(this, _cache, void 0);
|
1198
|
-
__privateAdd$4(this,
|
1199
|
-
|
1200
|
-
__privateSet$
|
1201
|
-
this
|
1202
|
-
__privateSet$
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
if (a === "")
|
1212
|
-
throw new Error("The id can't be empty");
|
1213
|
-
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
|
1214
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1215
|
-
return record;
|
1216
|
-
}
|
1217
|
-
if (isObject(a) && isString(a.id)) {
|
1218
|
-
if (a.id === "")
|
1219
|
-
throw new Error("The id can't be empty");
|
1220
|
-
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
|
1221
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1222
|
-
return record;
|
1223
|
-
}
|
1224
|
-
if (isObject(a)) {
|
1225
|
-
const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
|
1226
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1227
|
-
return record;
|
1228
|
-
}
|
1229
|
-
throw new Error("Invalid arguments for create method");
|
1230
|
-
}
|
1231
|
-
async read(recordId) {
|
1232
|
-
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
|
1233
|
-
if (cacheRecord)
|
1234
|
-
return cacheRecord;
|
1235
|
-
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1236
|
-
try {
|
1237
|
-
const response = await getRecord({
|
1238
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1239
|
-
...fetchProps
|
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
|
1240
1415
|
});
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1416
|
+
});
|
1417
|
+
}
|
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);
|
1246
1425
|
}
|
1247
|
-
|
1248
|
-
|
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
|
+
});
|
1249
1444
|
}
|
1250
|
-
async
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
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);
|
1254
1458
|
}
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
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;
|
1480
|
+
}
|
1481
|
+
}
|
1482
|
+
return null;
|
1483
|
+
});
|
1484
|
+
}
|
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)));
|
1495
|
+
}
|
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);
|
1499
|
+
}
|
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
|
+
});
|
1270
1506
|
}
|
1271
|
-
async createOrUpdate(a, b) {
|
1272
|
-
|
1273
|
-
if (a
|
1274
|
-
|
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)));
|
1275
1517
|
}
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1287
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1288
|
-
return record;
|
1289
|
-
}
|
1290
|
-
throw new Error("Invalid arguments for createOrUpdate method");
|
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
|
+
});
|
1291
1528
|
}
|
1292
1529
|
async delete(a) {
|
1293
|
-
|
1294
|
-
if (a
|
1295
|
-
|
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)));
|
1538
|
+
return;
|
1296
1539
|
}
|
1297
|
-
|
1298
|
-
|
1299
|
-
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1308
|
-
return;
|
1309
|
-
}
|
1310
|
-
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
|
+
});
|
1311
1550
|
}
|
1312
1551
|
async search(query, options = {}) {
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1320
|
-
|
1321
|
-
|
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));
|
1322
1568
|
});
|
1323
|
-
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1324
|
-
return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
|
1325
1569
|
}
|
1326
1570
|
async query(query) {
|
1327
|
-
|
1328
|
-
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
1332
|
-
|
1333
|
-
|
1334
|
-
|
1335
|
-
|
1336
|
-
|
1337
|
-
|
1338
|
-
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
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);
|
1345
1592
|
});
|
1346
|
-
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1347
|
-
const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
|
1348
|
-
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1349
|
-
return new Page(query, meta, records);
|
1350
1593
|
}
|
1351
1594
|
}
|
1352
1595
|
_table = new WeakMap();
|
1353
1596
|
_getFetchProps = new WeakMap();
|
1597
|
+
_db = new WeakMap();
|
1354
1598
|
_cache = new WeakMap();
|
1355
|
-
|
1599
|
+
_schemaTables$2 = new WeakMap();
|
1600
|
+
_trace = new WeakMap();
|
1356
1601
|
_insertRecordWithoutId = new WeakSet();
|
1357
|
-
insertRecordWithoutId_fn = async function(object) {
|
1602
|
+
insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
|
1358
1603
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1359
1604
|
const record = transformObjectLinks(object);
|
1360
1605
|
const response = await insertRecord({
|
@@ -1363,17 +1608,15 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1363
1608
|
dbBranchName: "{dbBranch}",
|
1364
1609
|
tableName: __privateGet$4(this, _table)
|
1365
1610
|
},
|
1611
|
+
queryParams: { columns },
|
1366
1612
|
body: record,
|
1367
1613
|
...fetchProps
|
1368
1614
|
});
|
1369
|
-
const
|
1370
|
-
|
1371
|
-
throw new Error("The server failed to save the record");
|
1372
|
-
}
|
1373
|
-
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);
|
1374
1617
|
};
|
1375
1618
|
_insertRecordWithId = new WeakSet();
|
1376
|
-
insertRecordWithId_fn = async function(recordId, object) {
|
1619
|
+
insertRecordWithId_fn = async function(recordId, object, columns = ["*"]) {
|
1377
1620
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1378
1621
|
const record = transformObjectLinks(object);
|
1379
1622
|
const response = await insertRecordWithID({
|
@@ -1384,56 +1627,52 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1384
1627
|
recordId
|
1385
1628
|
},
|
1386
1629
|
body: record,
|
1387
|
-
queryParams: { createOnly: true },
|
1630
|
+
queryParams: { createOnly: true, columns },
|
1388
1631
|
...fetchProps
|
1389
1632
|
});
|
1390
|
-
const
|
1391
|
-
|
1392
|
-
throw new Error("The server failed to save the record");
|
1393
|
-
}
|
1394
|
-
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);
|
1395
1635
|
};
|
1396
1636
|
_bulkInsertTableRecords = new WeakSet();
|
1397
|
-
bulkInsertTableRecords_fn = async function(objects) {
|
1637
|
+
bulkInsertTableRecords_fn = async function(objects, columns = ["*"]) {
|
1398
1638
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1399
1639
|
const records = objects.map((object) => transformObjectLinks(object));
|
1400
1640
|
const response = await bulkInsertTableRecords({
|
1401
1641
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1642
|
+
queryParams: { columns },
|
1402
1643
|
body: { records },
|
1403
1644
|
...fetchProps
|
1404
1645
|
});
|
1405
|
-
|
1406
|
-
|
1407
|
-
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");
|
1408
1648
|
}
|
1409
|
-
|
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));
|
1410
1651
|
};
|
1411
1652
|
_updateRecordWithID = new WeakSet();
|
1412
|
-
updateRecordWithID_fn = async function(recordId, object) {
|
1653
|
+
updateRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1413
1654
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1414
1655
|
const record = transformObjectLinks(object);
|
1415
1656
|
const response = await updateRecordWithID({
|
1416
1657
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1658
|
+
queryParams: { columns },
|
1417
1659
|
body: record,
|
1418
1660
|
...fetchProps
|
1419
1661
|
});
|
1420
|
-
const
|
1421
|
-
|
1422
|
-
throw new Error("The server failed to save the record");
|
1423
|
-
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);
|
1424
1664
|
};
|
1425
1665
|
_upsertRecordWithID = new WeakSet();
|
1426
|
-
upsertRecordWithID_fn = async function(recordId, object) {
|
1666
|
+
upsertRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1427
1667
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1428
1668
|
const response = await upsertRecordWithID({
|
1429
1669
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1670
|
+
queryParams: { columns },
|
1430
1671
|
body: object,
|
1431
1672
|
...fetchProps
|
1432
1673
|
});
|
1433
|
-
const
|
1434
|
-
|
1435
|
-
throw new Error("The server failed to save the record");
|
1436
|
-
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);
|
1437
1676
|
};
|
1438
1677
|
_deleteRecord = new WeakSet();
|
1439
1678
|
deleteRecord_fn = async function(recordId) {
|
@@ -1443,29 +1682,6 @@ deleteRecord_fn = async function(recordId) {
|
|
1443
1682
|
...fetchProps
|
1444
1683
|
});
|
1445
1684
|
};
|
1446
|
-
_invalidateCache = new WeakSet();
|
1447
|
-
invalidateCache_fn = async function(recordId) {
|
1448
|
-
await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1449
|
-
const cacheItems = await __privateGet$4(this, _cache).getAll();
|
1450
|
-
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1451
|
-
for (const [key, value] of queries) {
|
1452
|
-
const ids = getIds(value);
|
1453
|
-
if (ids.includes(recordId))
|
1454
|
-
await __privateGet$4(this, _cache).delete(key);
|
1455
|
-
}
|
1456
|
-
};
|
1457
|
-
_setCacheRecord = new WeakSet();
|
1458
|
-
setCacheRecord_fn = async function(record) {
|
1459
|
-
if (!__privateGet$4(this, _cache).cacheRecords)
|
1460
|
-
return;
|
1461
|
-
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1462
|
-
};
|
1463
|
-
_getCacheRecord = new WeakSet();
|
1464
|
-
getCacheRecord_fn = async function(recordId) {
|
1465
|
-
if (!__privateGet$4(this, _cache).cacheRecords)
|
1466
|
-
return null;
|
1467
|
-
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1468
|
-
};
|
1469
1685
|
_setCacheQuery = new WeakSet();
|
1470
1686
|
setCacheQuery_fn = async function(query, meta, records) {
|
1471
1687
|
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
@@ -1482,17 +1698,17 @@ getCacheQuery_fn = async function(query) {
|
|
1482
1698
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1483
1699
|
return hasExpired ? null : result;
|
1484
1700
|
};
|
1485
|
-
|
1486
|
-
|
1487
|
-
if (__privateGet$4(this,
|
1488
|
-
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);
|
1489
1705
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1490
1706
|
const { schema } = await getBranchDetails({
|
1491
1707
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1492
1708
|
...fetchProps
|
1493
1709
|
});
|
1494
|
-
__privateSet$
|
1495
|
-
return schema;
|
1710
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1711
|
+
return schema.tables;
|
1496
1712
|
};
|
1497
1713
|
const transformObjectLinks = (object) => {
|
1498
1714
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
@@ -1501,20 +1717,21 @@ const transformObjectLinks = (object) => {
|
|
1501
1717
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1502
1718
|
}, {});
|
1503
1719
|
};
|
1504
|
-
const initObject = (db,
|
1720
|
+
const initObject = (db, schemaTables, table, object) => {
|
1505
1721
|
const result = {};
|
1506
|
-
|
1507
|
-
|
1722
|
+
const { xata, ...rest } = object ?? {};
|
1723
|
+
Object.assign(result, rest);
|
1724
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1508
1725
|
if (!columns)
|
1509
1726
|
console.error(`Table ${table} not found in schema`);
|
1510
1727
|
for (const column of columns ?? []) {
|
1511
1728
|
const value = result[column.name];
|
1512
1729
|
switch (column.type) {
|
1513
1730
|
case "datetime": {
|
1514
|
-
const date = new Date(value);
|
1515
|
-
if (isNaN(date.getTime())) {
|
1731
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1732
|
+
if (date && isNaN(date.getTime())) {
|
1516
1733
|
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1517
|
-
} else {
|
1734
|
+
} else if (date) {
|
1518
1735
|
result[column.name] = date;
|
1519
1736
|
}
|
1520
1737
|
break;
|
@@ -1524,35 +1741,32 @@ const initObject = (db, schema, table, object) => {
|
|
1524
1741
|
if (!linkTable) {
|
1525
1742
|
console.error(`Failed to parse link for field ${column.name}`);
|
1526
1743
|
} else if (isObject(value)) {
|
1527
|
-
result[column.name] = initObject(db,
|
1744
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1528
1745
|
}
|
1529
1746
|
break;
|
1530
1747
|
}
|
1531
1748
|
}
|
1532
1749
|
}
|
1533
|
-
result.read = function() {
|
1534
|
-
return db[table].read(result["id"]);
|
1750
|
+
result.read = function(columns2) {
|
1751
|
+
return db[table].read(result["id"], columns2);
|
1535
1752
|
};
|
1536
|
-
result.update = function(data) {
|
1537
|
-
return db[table].update(result["id"], data);
|
1753
|
+
result.update = function(data, columns2) {
|
1754
|
+
return db[table].update(result["id"], data, columns2);
|
1538
1755
|
};
|
1539
1756
|
result.delete = function() {
|
1540
1757
|
return db[table].delete(result["id"]);
|
1541
1758
|
};
|
1542
|
-
|
1759
|
+
result.getMetadata = function() {
|
1760
|
+
return xata;
|
1761
|
+
};
|
1762
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1543
1763
|
Object.defineProperty(result, prop, { enumerable: false });
|
1544
1764
|
}
|
1545
1765
|
Object.freeze(result);
|
1546
1766
|
return result;
|
1547
1767
|
};
|
1548
|
-
function
|
1549
|
-
|
1550
|
-
return value.map((item) => getIds(item)).flat();
|
1551
|
-
}
|
1552
|
-
if (!isObject(value))
|
1553
|
-
return [];
|
1554
|
-
const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
|
1555
|
-
return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
|
1768
|
+
function isResponseWithRecords(value) {
|
1769
|
+
return isObject(value) && Array.isArray(value.records);
|
1556
1770
|
}
|
1557
1771
|
|
1558
1772
|
var __accessCheck$3 = (obj, member, msg) => {
|
@@ -1568,7 +1782,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1568
1782
|
throw TypeError("Cannot add the same private member more than once");
|
1569
1783
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1570
1784
|
};
|
1571
|
-
var __privateSet$
|
1785
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1572
1786
|
__accessCheck$3(obj, member, "write to private field");
|
1573
1787
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1574
1788
|
return value;
|
@@ -1577,9 +1791,8 @@ var _map;
|
|
1577
1791
|
class SimpleCache {
|
1578
1792
|
constructor(options = {}) {
|
1579
1793
|
__privateAdd$3(this, _map, void 0);
|
1580
|
-
__privateSet$
|
1794
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1581
1795
|
this.capacity = options.max ?? 500;
|
1582
|
-
this.cacheRecords = options.cacheRecords ?? true;
|
1583
1796
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1584
1797
|
}
|
1585
1798
|
async getAll() {
|
@@ -1605,18 +1818,25 @@ class SimpleCache {
|
|
1605
1818
|
}
|
1606
1819
|
_map = new WeakMap();
|
1607
1820
|
|
1608
|
-
const
|
1609
|
-
const
|
1610
|
-
const
|
1611
|
-
const
|
1612
|
-
const
|
1613
|
-
const
|
1821
|
+
const greaterThan = (value) => ({ $gt: value });
|
1822
|
+
const gt = greaterThan;
|
1823
|
+
const greaterThanEquals = (value) => ({ $ge: value });
|
1824
|
+
const greaterEquals = greaterThanEquals;
|
1825
|
+
const gte = greaterThanEquals;
|
1826
|
+
const ge = greaterThanEquals;
|
1827
|
+
const lessThan = (value) => ({ $lt: value });
|
1828
|
+
const lt = lessThan;
|
1829
|
+
const lessThanEquals = (value) => ({ $le: value });
|
1830
|
+
const lessEquals = lessThanEquals;
|
1831
|
+
const lte = lessThanEquals;
|
1832
|
+
const le = lessThanEquals;
|
1614
1833
|
const exists = (column) => ({ $exists: column });
|
1615
1834
|
const notExists = (column) => ({ $notExists: column });
|
1616
1835
|
const startsWith = (value) => ({ $startsWith: value });
|
1617
1836
|
const endsWith = (value) => ({ $endsWith: value });
|
1618
1837
|
const pattern = (value) => ({ $pattern: value });
|
1619
1838
|
const is = (value) => ({ $is: value });
|
1839
|
+
const equals = is;
|
1620
1840
|
const isNot = (value) => ({ $isNot: value });
|
1621
1841
|
const contains = (value) => ({ $contains: value });
|
1622
1842
|
const includes = (value) => ({ $includes: value });
|
@@ -1637,31 +1857,42 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1637
1857
|
throw TypeError("Cannot add the same private member more than once");
|
1638
1858
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1639
1859
|
};
|
1640
|
-
var
|
1860
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1861
|
+
__accessCheck$2(obj, member, "write to private field");
|
1862
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1863
|
+
return value;
|
1864
|
+
};
|
1865
|
+
var _tables, _schemaTables$1;
|
1641
1866
|
class SchemaPlugin extends XataPlugin {
|
1642
|
-
constructor(
|
1867
|
+
constructor(schemaTables) {
|
1643
1868
|
super();
|
1644
|
-
this.tableNames = tableNames;
|
1645
1869
|
__privateAdd$2(this, _tables, {});
|
1870
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1871
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1646
1872
|
}
|
1647
1873
|
build(pluginOptions) {
|
1648
|
-
const db = new Proxy(
|
1649
|
-
|
1650
|
-
|
1651
|
-
|
1652
|
-
|
1653
|
-
|
1874
|
+
const db = new Proxy(
|
1875
|
+
{},
|
1876
|
+
{
|
1877
|
+
get: (_target, table) => {
|
1878
|
+
if (!isString(table))
|
1879
|
+
throw new Error("Invalid table name");
|
1880
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1881
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1882
|
+
}
|
1883
|
+
return __privateGet$2(this, _tables)[table];
|
1654
1884
|
}
|
1655
|
-
return __privateGet$2(this, _tables)[table];
|
1656
1885
|
}
|
1657
|
-
|
1658
|
-
|
1659
|
-
|
1886
|
+
);
|
1887
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1888
|
+
for (const table of tableNames) {
|
1889
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1660
1890
|
}
|
1661
1891
|
return db;
|
1662
1892
|
}
|
1663
1893
|
}
|
1664
1894
|
_tables = new WeakMap();
|
1895
|
+
_schemaTables$1 = new WeakMap();
|
1665
1896
|
|
1666
1897
|
var __accessCheck$1 = (obj, member, msg) => {
|
1667
1898
|
if (!member.has(obj))
|
@@ -1685,82 +1916,77 @@ var __privateMethod$1 = (obj, member, method) => {
|
|
1685
1916
|
__accessCheck$1(obj, member, "access private method");
|
1686
1917
|
return method;
|
1687
1918
|
};
|
1688
|
-
var
|
1919
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1689
1920
|
class SearchPlugin extends XataPlugin {
|
1690
|
-
constructor(db) {
|
1921
|
+
constructor(db, schemaTables) {
|
1691
1922
|
super();
|
1692
1923
|
this.db = db;
|
1693
1924
|
__privateAdd$1(this, _search);
|
1694
|
-
__privateAdd$1(this,
|
1695
|
-
__privateAdd$1(this,
|
1925
|
+
__privateAdd$1(this, _getSchemaTables);
|
1926
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1927
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1696
1928
|
}
|
1697
1929
|
build({ getFetchProps }) {
|
1698
1930
|
return {
|
1699
1931
|
all: async (query, options = {}) => {
|
1700
1932
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1701
|
-
const
|
1933
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1702
1934
|
return records.map((record) => {
|
1703
1935
|
const { table = "orphan" } = record.xata;
|
1704
|
-
return { table, record: initObject(this.db,
|
1936
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1705
1937
|
});
|
1706
1938
|
},
|
1707
1939
|
byTable: async (query, options = {}) => {
|
1708
1940
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1709
|
-
const
|
1941
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1710
1942
|
return records.reduce((acc, record) => {
|
1711
1943
|
const { table = "orphan" } = record.xata;
|
1712
1944
|
const items = acc[table] ?? [];
|
1713
|
-
const item = initObject(this.db,
|
1945
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1714
1946
|
return { ...acc, [table]: [...items, item] };
|
1715
1947
|
}, {});
|
1716
1948
|
}
|
1717
1949
|
};
|
1718
1950
|
}
|
1719
1951
|
}
|
1720
|
-
|
1952
|
+
_schemaTables = new WeakMap();
|
1721
1953
|
_search = new WeakSet();
|
1722
1954
|
search_fn = async function(query, options, getFetchProps) {
|
1723
1955
|
const fetchProps = await getFetchProps();
|
1724
|
-
const { tables, fuzziness } = options ?? {};
|
1956
|
+
const { tables, fuzziness, highlight, prefix } = options ?? {};
|
1725
1957
|
const { records } = await searchBranch({
|
1726
1958
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1727
|
-
body: { tables, query, fuzziness },
|
1959
|
+
body: { tables, query, fuzziness, prefix, highlight },
|
1728
1960
|
...fetchProps
|
1729
1961
|
});
|
1730
1962
|
return records;
|
1731
1963
|
};
|
1732
|
-
|
1733
|
-
|
1734
|
-
if (__privateGet$1(this,
|
1735
|
-
return __privateGet$1(this,
|
1964
|
+
_getSchemaTables = new WeakSet();
|
1965
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1966
|
+
if (__privateGet$1(this, _schemaTables))
|
1967
|
+
return __privateGet$1(this, _schemaTables);
|
1736
1968
|
const fetchProps = await getFetchProps();
|
1737
1969
|
const { schema } = await getBranchDetails({
|
1738
1970
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1739
1971
|
...fetchProps
|
1740
1972
|
});
|
1741
|
-
__privateSet$1(this,
|
1742
|
-
return schema;
|
1973
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1974
|
+
return schema.tables;
|
1743
1975
|
};
|
1744
1976
|
|
1745
1977
|
const isBranchStrategyBuilder = (strategy) => {
|
1746
1978
|
return typeof strategy === "function";
|
1747
1979
|
};
|
1748
1980
|
|
1749
|
-
const envBranchNames = [
|
1750
|
-
"XATA_BRANCH",
|
1751
|
-
"VERCEL_GIT_COMMIT_REF",
|
1752
|
-
"CF_PAGES_BRANCH",
|
1753
|
-
"BRANCH"
|
1754
|
-
];
|
1755
1981
|
async function getCurrentBranchName(options) {
|
1756
|
-
const
|
1757
|
-
if (
|
1758
|
-
const details = await getDatabaseBranch(
|
1982
|
+
const { branch, envBranch } = getEnvironment();
|
1983
|
+
if (branch) {
|
1984
|
+
const details = await getDatabaseBranch(branch, options);
|
1759
1985
|
if (details)
|
1760
|
-
return
|
1761
|
-
console.warn(`Branch ${
|
1986
|
+
return branch;
|
1987
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1762
1988
|
}
|
1763
|
-
const gitBranch = await getGitBranch();
|
1989
|
+
const gitBranch = envBranch || await getGitBranch();
|
1764
1990
|
return resolveXataBranch(gitBranch, options);
|
1765
1991
|
}
|
1766
1992
|
async function getCurrentBranchDetails(options) {
|
@@ -1771,18 +1997,24 @@ async function resolveXataBranch(gitBranch, options) {
|
|
1771
1997
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1772
1998
|
const apiKey = options?.apiKey || getAPIKey();
|
1773
1999
|
if (!databaseURL)
|
1774
|
-
throw new Error(
|
2000
|
+
throw new Error(
|
2001
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
2002
|
+
);
|
1775
2003
|
if (!apiKey)
|
1776
|
-
throw new Error(
|
2004
|
+
throw new Error(
|
2005
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
2006
|
+
);
|
1777
2007
|
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1778
2008
|
const [workspace] = host.split(".");
|
2009
|
+
const { fallbackBranch } = getEnvironment();
|
1779
2010
|
const { branch } = await resolveBranch({
|
1780
2011
|
apiKey,
|
1781
2012
|
apiUrl: databaseURL,
|
1782
2013
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1783
2014
|
workspacesApiUrl: `${protocol}//${host}`,
|
1784
2015
|
pathParams: { dbName, workspace },
|
1785
|
-
queryParams: { gitBranch, fallbackBranch
|
2016
|
+
queryParams: { gitBranch, fallbackBranch },
|
2017
|
+
trace: defaultTrace
|
1786
2018
|
});
|
1787
2019
|
return branch;
|
1788
2020
|
}
|
@@ -1790,9 +2022,13 @@ async function getDatabaseBranch(branch, options) {
|
|
1790
2022
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1791
2023
|
const apiKey = options?.apiKey || getAPIKey();
|
1792
2024
|
if (!databaseURL)
|
1793
|
-
throw new Error(
|
2025
|
+
throw new Error(
|
2026
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
2027
|
+
);
|
1794
2028
|
if (!apiKey)
|
1795
|
-
throw new Error(
|
2029
|
+
throw new Error(
|
2030
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
2031
|
+
);
|
1796
2032
|
const [protocol, , host, , database] = databaseURL.split("/");
|
1797
2033
|
const [workspace] = host.split(".");
|
1798
2034
|
const dbBranchName = `${database}:${branch}`;
|
@@ -1802,10 +2038,8 @@ async function getDatabaseBranch(branch, options) {
|
|
1802
2038
|
apiUrl: databaseURL,
|
1803
2039
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1804
2040
|
workspacesApiUrl: `${protocol}//${host}`,
|
1805
|
-
pathParams: {
|
1806
|
-
|
1807
|
-
workspace
|
1808
|
-
}
|
2041
|
+
pathParams: { dbBranchName, workspace },
|
2042
|
+
trace: defaultTrace
|
1809
2043
|
});
|
1810
2044
|
} catch (err) {
|
1811
2045
|
if (isObject(err) && err.status === 404)
|
@@ -1813,21 +2047,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1813
2047
|
throw err;
|
1814
2048
|
}
|
1815
2049
|
}
|
1816
|
-
function getBranchByEnvVariable() {
|
1817
|
-
for (const name of envBranchNames) {
|
1818
|
-
const value = getEnvVariable(name);
|
1819
|
-
if (value) {
|
1820
|
-
return value;
|
1821
|
-
}
|
1822
|
-
}
|
1823
|
-
try {
|
1824
|
-
return XATA_BRANCH;
|
1825
|
-
} catch (err) {
|
1826
|
-
}
|
1827
|
-
}
|
1828
2050
|
function getDatabaseURL() {
|
1829
2051
|
try {
|
1830
|
-
|
2052
|
+
const { databaseURL } = getEnvironment();
|
2053
|
+
return databaseURL;
|
1831
2054
|
} catch (err) {
|
1832
2055
|
return void 0;
|
1833
2056
|
}
|
@@ -1856,20 +2079,23 @@ var __privateMethod = (obj, member, method) => {
|
|
1856
2079
|
return method;
|
1857
2080
|
};
|
1858
2081
|
const buildClient = (plugins) => {
|
1859
|
-
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
2082
|
+
var _branch, _options, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1860
2083
|
return _a = class {
|
1861
|
-
constructor(options = {},
|
2084
|
+
constructor(options = {}, schemaTables) {
|
1862
2085
|
__privateAdd(this, _parseOptions);
|
1863
2086
|
__privateAdd(this, _getFetchProps);
|
1864
2087
|
__privateAdd(this, _evaluateBranch);
|
1865
2088
|
__privateAdd(this, _branch, void 0);
|
2089
|
+
__privateAdd(this, _options, void 0);
|
1866
2090
|
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
2091
|
+
__privateSet(this, _options, safeOptions);
|
1867
2092
|
const pluginOptions = {
|
1868
2093
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1869
|
-
cache: safeOptions.cache
|
2094
|
+
cache: safeOptions.cache,
|
2095
|
+
trace: safeOptions.trace
|
1870
2096
|
};
|
1871
|
-
const db = new SchemaPlugin(
|
1872
|
-
const search = new SearchPlugin(db).build(pluginOptions);
|
2097
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2098
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1873
2099
|
this.db = db;
|
1874
2100
|
this.search = search;
|
1875
2101
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
@@ -1885,22 +2111,26 @@ const buildClient = (plugins) => {
|
|
1885
2111
|
}
|
1886
2112
|
}
|
1887
2113
|
}
|
1888
|
-
|
2114
|
+
async getConfig() {
|
2115
|
+
const databaseURL = __privateGet(this, _options).databaseURL;
|
2116
|
+
const branch = await __privateGet(this, _options).branch();
|
2117
|
+
return { databaseURL, branch };
|
2118
|
+
}
|
2119
|
+
}, _branch = new WeakMap(), _options = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
|
1889
2120
|
const fetch = getFetchImplementation(options?.fetch);
|
1890
2121
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1891
2122
|
const apiKey = options?.apiKey || getAPIKey();
|
1892
|
-
const cache = options?.cache ?? new SimpleCache({
|
2123
|
+
const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
|
2124
|
+
const trace = options?.trace ?? defaultTrace;
|
1893
2125
|
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1894
|
-
if (!
|
1895
|
-
throw new Error("
|
2126
|
+
if (!apiKey) {
|
2127
|
+
throw new Error("Option apiKey is required");
|
1896
2128
|
}
|
1897
|
-
|
1898
|
-
|
1899
|
-
|
1900
|
-
apiKey,
|
1901
|
-
|
1902
|
-
branch
|
1903
|
-
}) {
|
2129
|
+
if (!databaseURL) {
|
2130
|
+
throw new Error("Option databaseURL is required");
|
2131
|
+
}
|
2132
|
+
return { fetch, databaseURL, apiKey, branch, cache, trace };
|
2133
|
+
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({ fetch, apiKey, databaseURL, branch, trace }) {
|
1904
2134
|
const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
|
1905
2135
|
if (!branchValue)
|
1906
2136
|
throw new Error("Unable to resolve branch value");
|
@@ -1912,7 +2142,8 @@ const buildClient = (plugins) => {
|
|
1912
2142
|
const hasBranch = params.dbBranchName ?? params.branch;
|
1913
2143
|
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : "");
|
1914
2144
|
return databaseURL + newPath;
|
1915
|
-
}
|
2145
|
+
},
|
2146
|
+
trace
|
1916
2147
|
};
|
1917
2148
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1918
2149
|
if (__privateGet(this, _branch))
|
@@ -1935,6 +2166,88 @@ const buildClient = (plugins) => {
|
|
1935
2166
|
class BaseClient extends buildClient() {
|
1936
2167
|
}
|
1937
2168
|
|
2169
|
+
const META = "__";
|
2170
|
+
const VALUE = "___";
|
2171
|
+
class Serializer {
|
2172
|
+
constructor() {
|
2173
|
+
this.classes = {};
|
2174
|
+
}
|
2175
|
+
add(clazz) {
|
2176
|
+
this.classes[clazz.name] = clazz;
|
2177
|
+
}
|
2178
|
+
toJSON(data) {
|
2179
|
+
function visit(obj) {
|
2180
|
+
if (Array.isArray(obj))
|
2181
|
+
return obj.map(visit);
|
2182
|
+
const type = typeof obj;
|
2183
|
+
if (type === "undefined")
|
2184
|
+
return { [META]: "undefined" };
|
2185
|
+
if (type === "bigint")
|
2186
|
+
return { [META]: "bigint", [VALUE]: obj.toString() };
|
2187
|
+
if (obj === null || type !== "object")
|
2188
|
+
return obj;
|
2189
|
+
const constructor = obj.constructor;
|
2190
|
+
const o = { [META]: constructor.name };
|
2191
|
+
for (const [key, value] of Object.entries(obj)) {
|
2192
|
+
o[key] = visit(value);
|
2193
|
+
}
|
2194
|
+
if (constructor === Date)
|
2195
|
+
o[VALUE] = obj.toISOString();
|
2196
|
+
if (constructor === Map)
|
2197
|
+
o[VALUE] = Object.fromEntries(obj);
|
2198
|
+
if (constructor === Set)
|
2199
|
+
o[VALUE] = [...obj];
|
2200
|
+
return o;
|
2201
|
+
}
|
2202
|
+
return JSON.stringify(visit(data));
|
2203
|
+
}
|
2204
|
+
fromJSON(json) {
|
2205
|
+
return JSON.parse(json, (key, value) => {
|
2206
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
2207
|
+
const { [META]: clazz, [VALUE]: val, ...rest } = value;
|
2208
|
+
const constructor = this.classes[clazz];
|
2209
|
+
if (constructor) {
|
2210
|
+
return Object.assign(Object.create(constructor.prototype), rest);
|
2211
|
+
}
|
2212
|
+
if (clazz === "Date")
|
2213
|
+
return new Date(val);
|
2214
|
+
if (clazz === "Set")
|
2215
|
+
return new Set(val);
|
2216
|
+
if (clazz === "Map")
|
2217
|
+
return new Map(Object.entries(val));
|
2218
|
+
if (clazz === "bigint")
|
2219
|
+
return BigInt(val);
|
2220
|
+
if (clazz === "undefined")
|
2221
|
+
return void 0;
|
2222
|
+
return rest;
|
2223
|
+
}
|
2224
|
+
return value;
|
2225
|
+
});
|
2226
|
+
}
|
2227
|
+
}
|
2228
|
+
const defaultSerializer = new Serializer();
|
2229
|
+
const serialize = (data) => {
|
2230
|
+
return defaultSerializer.toJSON(data);
|
2231
|
+
};
|
2232
|
+
const deserialize = (json) => {
|
2233
|
+
return defaultSerializer.fromJSON(json);
|
2234
|
+
};
|
2235
|
+
|
2236
|
+
function buildWorkerRunner(config) {
|
2237
|
+
return function xataWorker(name, _worker) {
|
2238
|
+
return async (...args) => {
|
2239
|
+
const url = process.env.NODE_ENV === "development" ? `http://localhost:64749/${name}` : `https://dispatcher.xata.workers.dev/${config.workspace}/${config.worker}/${name}`;
|
2240
|
+
const result = await fetch(url, {
|
2241
|
+
method: "POST",
|
2242
|
+
headers: { "Content-Type": "application/json" },
|
2243
|
+
body: serialize({ args })
|
2244
|
+
});
|
2245
|
+
const text = await result.text();
|
2246
|
+
return deserialize(text);
|
2247
|
+
};
|
2248
|
+
};
|
2249
|
+
}
|
2250
|
+
|
1938
2251
|
class XataError extends Error {
|
1939
2252
|
constructor(message, status) {
|
1940
2253
|
super(message);
|
@@ -1942,5 +2255,5 @@ class XataError extends Error {
|
|
1942
2255
|
}
|
1943
2256
|
}
|
1944
2257
|
|
1945
|
-
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, 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 };
|
2258
|
+
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, equals, 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, greaterEquals, greaterThan, greaterThanEquals, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lessEquals, lessThan, lessThanEquals, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, serialize, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberInvite, updateWorkspaceMemberRole, upsertRecordWithID };
|
1946
2259
|
//# sourceMappingURL=index.mjs.map
|