@xata.io/client 0.0.0-alpha.vfb4a018 → 0.0.0-alpha.vfba793c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +1 -2
- package/CHANGELOG.md +236 -0
- package/README.md +273 -1
- package/Usage.md +451 -0
- package/dist/index.cjs +1276 -512
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2708 -371
- package/dist/index.mjs +1227 -513
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -5
- package/tsconfig.json +1 -0
package/dist/index.mjs
CHANGED
@@ -1,3 +1,25 @@
|
|
1
|
+
const defaultTrace = async (_name, fn, _options) => {
|
2
|
+
return await fn({
|
3
|
+
setAttributes: () => {
|
4
|
+
return;
|
5
|
+
}
|
6
|
+
});
|
7
|
+
};
|
8
|
+
const TraceAttributes = {
|
9
|
+
KIND: "xata.trace.kind",
|
10
|
+
VERSION: "xata.sdk.version",
|
11
|
+
TABLE: "xata.table",
|
12
|
+
HTTP_REQUEST_ID: "http.request_id",
|
13
|
+
HTTP_STATUS_CODE: "http.status_code",
|
14
|
+
HTTP_HOST: "http.host",
|
15
|
+
HTTP_SCHEME: "http.scheme",
|
16
|
+
HTTP_USER_AGENT: "http.user_agent",
|
17
|
+
HTTP_METHOD: "http.method",
|
18
|
+
HTTP_URL: "http.url",
|
19
|
+
HTTP_ROUTE: "http.route",
|
20
|
+
HTTP_TARGET: "http.target"
|
21
|
+
};
|
22
|
+
|
1
23
|
function notEmpty(value) {
|
2
24
|
return value !== null && value !== void 0;
|
3
25
|
}
|
@@ -7,46 +29,101 @@ function compact(arr) {
|
|
7
29
|
function isObject(value) {
|
8
30
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
9
31
|
}
|
32
|
+
function isDefined(value) {
|
33
|
+
return value !== null && value !== void 0;
|
34
|
+
}
|
10
35
|
function isString(value) {
|
11
|
-
return value
|
36
|
+
return isDefined(value) && typeof value === "string";
|
37
|
+
}
|
38
|
+
function isStringArray(value) {
|
39
|
+
return isDefined(value) && Array.isArray(value) && value.every(isString);
|
12
40
|
}
|
13
41
|
function toBase64(value) {
|
14
42
|
try {
|
15
43
|
return btoa(value);
|
16
44
|
} catch (err) {
|
17
|
-
|
45
|
+
const buf = Buffer;
|
46
|
+
return buf.from(value).toString("base64");
|
18
47
|
}
|
19
48
|
}
|
20
49
|
|
21
|
-
function
|
50
|
+
function getEnvironment() {
|
22
51
|
try {
|
23
|
-
if (isObject(process) &&
|
24
|
-
return
|
52
|
+
if (isObject(process) && isObject(process.env)) {
|
53
|
+
return {
|
54
|
+
apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
|
55
|
+
databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
|
56
|
+
branch: process.env.XATA_BRANCH ?? getGlobalBranch(),
|
57
|
+
envBranch: process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH,
|
58
|
+
fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
|
59
|
+
};
|
25
60
|
}
|
26
61
|
} catch (err) {
|
27
62
|
}
|
28
63
|
try {
|
29
|
-
if (isObject(Deno) &&
|
30
|
-
return
|
64
|
+
if (isObject(Deno) && isObject(Deno.env)) {
|
65
|
+
return {
|
66
|
+
apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
|
67
|
+
databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
|
68
|
+
branch: Deno.env.get("XATA_BRANCH") ?? getGlobalBranch(),
|
69
|
+
envBranch: Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH"),
|
70
|
+
fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
|
71
|
+
};
|
31
72
|
}
|
32
73
|
} catch (err) {
|
33
74
|
}
|
75
|
+
return {
|
76
|
+
apiKey: getGlobalApiKey(),
|
77
|
+
databaseURL: getGlobalDatabaseURL(),
|
78
|
+
branch: getGlobalBranch(),
|
79
|
+
envBranch: void 0,
|
80
|
+
fallbackBranch: getGlobalFallbackBranch()
|
81
|
+
};
|
82
|
+
}
|
83
|
+
function getGlobalApiKey() {
|
84
|
+
try {
|
85
|
+
return XATA_API_KEY;
|
86
|
+
} catch (err) {
|
87
|
+
return void 0;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
function getGlobalDatabaseURL() {
|
91
|
+
try {
|
92
|
+
return XATA_DATABASE_URL;
|
93
|
+
} catch (err) {
|
94
|
+
return void 0;
|
95
|
+
}
|
96
|
+
}
|
97
|
+
function getGlobalBranch() {
|
98
|
+
try {
|
99
|
+
return XATA_BRANCH;
|
100
|
+
} catch (err) {
|
101
|
+
return void 0;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
function getGlobalFallbackBranch() {
|
105
|
+
try {
|
106
|
+
return XATA_FALLBACK_BRANCH;
|
107
|
+
} catch (err) {
|
108
|
+
return void 0;
|
109
|
+
}
|
34
110
|
}
|
35
111
|
async function getGitBranch() {
|
112
|
+
const cmd = ["git", "branch", "--show-current"];
|
113
|
+
const fullCmd = cmd.join(" ");
|
114
|
+
const nodeModule = ["child", "process"].join("_");
|
115
|
+
const execOptions = { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] };
|
36
116
|
try {
|
37
117
|
if (typeof require === "function") {
|
38
|
-
|
39
|
-
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
118
|
+
return require(nodeModule).execSync(fullCmd, execOptions).trim();
|
40
119
|
}
|
120
|
+
const { execSync } = await import(nodeModule);
|
121
|
+
return execSync(fullCmd, execOptions).toString().trim();
|
41
122
|
} catch (err) {
|
42
123
|
}
|
43
124
|
try {
|
44
125
|
if (isObject(Deno)) {
|
45
|
-
const process2 = Deno.run({
|
46
|
-
cmd: ["git", "branch", "--show-current"],
|
47
|
-
stdout: "piped",
|
48
|
-
stderr: "piped"
|
49
|
-
});
|
126
|
+
const process2 = Deno.run({ cmd, stdout: "piped", stderr: "null" });
|
50
127
|
return new TextDecoder().decode(await process2.output()).trim();
|
51
128
|
}
|
52
129
|
} catch (err) {
|
@@ -55,7 +132,8 @@ async function getGitBranch() {
|
|
55
132
|
|
56
133
|
function getAPIKey() {
|
57
134
|
try {
|
58
|
-
|
135
|
+
const { apiKey } = getEnvironment();
|
136
|
+
return apiKey;
|
59
137
|
} catch (err) {
|
60
138
|
return void 0;
|
61
139
|
}
|
@@ -65,21 +143,35 @@ function getFetchImplementation(userFetch) {
|
|
65
143
|
const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
|
66
144
|
const fetchImpl = userFetch ?? globalFetch;
|
67
145
|
if (!fetchImpl) {
|
68
|
-
throw new Error(
|
146
|
+
throw new Error(
|
147
|
+
`Couldn't find \`fetch\`. Install a fetch implementation such as \`node-fetch\` and pass it explicitly.`
|
148
|
+
);
|
69
149
|
}
|
70
150
|
return fetchImpl;
|
71
151
|
}
|
72
152
|
|
73
|
-
|
74
|
-
|
153
|
+
const VERSION = "0.0.0-alpha.vfba793c";
|
154
|
+
|
155
|
+
class ErrorWithCause extends Error {
|
156
|
+
constructor(message, options) {
|
157
|
+
super(message, options);
|
158
|
+
}
|
159
|
+
}
|
160
|
+
class FetcherError extends ErrorWithCause {
|
161
|
+
constructor(status, data, requestId) {
|
75
162
|
super(getMessage(data));
|
76
163
|
this.status = status;
|
77
164
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
165
|
+
this.requestId = requestId;
|
78
166
|
if (data instanceof Error) {
|
79
167
|
this.stack = data.stack;
|
80
168
|
this.cause = data.cause;
|
81
169
|
}
|
82
170
|
}
|
171
|
+
toString() {
|
172
|
+
const error = super.toString();
|
173
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
174
|
+
}
|
83
175
|
}
|
84
176
|
function isBulkError(error) {
|
85
177
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -102,9 +194,17 @@ function getMessage(data) {
|
|
102
194
|
}
|
103
195
|
|
104
196
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
105
|
-
const
|
197
|
+
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
198
|
+
if (value === void 0 || value === null)
|
199
|
+
return acc;
|
200
|
+
return { ...acc, [key]: value };
|
201
|
+
}, {});
|
202
|
+
const query = new URLSearchParams(cleanQueryParams).toString();
|
106
203
|
const queryString = query.length > 0 ? `?${query}` : "";
|
107
|
-
|
204
|
+
const cleanPathParams = Object.entries(pathParams).reduce((acc, [key, value]) => {
|
205
|
+
return { ...acc, [key]: encodeURIComponent(String(value ?? "")).replace("%3A", ":") };
|
206
|
+
}, {});
|
207
|
+
return url.replace(/\{\w*\}/g, (key) => cleanPathParams[key.slice(1, -1)]) + queryString;
|
108
208
|
};
|
109
209
|
function buildBaseUrl({
|
110
210
|
path,
|
@@ -112,10 +212,10 @@ function buildBaseUrl({
|
|
112
212
|
apiUrl,
|
113
213
|
pathParams
|
114
214
|
}) {
|
115
|
-
if (
|
215
|
+
if (pathParams?.workspace === void 0)
|
116
216
|
return `${apiUrl}${path}`;
|
117
217
|
const url = typeof workspacesApiUrl === "string" ? `${workspacesApiUrl}${path}` : workspacesApiUrl(path, pathParams);
|
118
|
-
return url.replace("{workspaceId}", pathParams.workspace);
|
218
|
+
return url.replace("{workspaceId}", String(pathParams.workspace));
|
119
219
|
}
|
120
220
|
function hostHeader(url) {
|
121
221
|
const pattern = /.*:\/\/(?<host>[^/]+).*/;
|
@@ -132,32 +232,61 @@ async function fetch$1({
|
|
132
232
|
fetchImpl,
|
133
233
|
apiKey,
|
134
234
|
apiUrl,
|
135
|
-
workspacesApiUrl
|
235
|
+
workspacesApiUrl,
|
236
|
+
trace
|
136
237
|
}) {
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
238
|
+
return trace(
|
239
|
+
`${method.toUpperCase()} ${path}`,
|
240
|
+
async ({ setAttributes }) => {
|
241
|
+
const baseUrl = buildBaseUrl({ path, workspacesApiUrl, pathParams, apiUrl });
|
242
|
+
const fullUrl = resolveUrl(baseUrl, queryParams, pathParams);
|
243
|
+
const url = fullUrl.includes("localhost") ? fullUrl.replace(/^[^.]+\./, "http://") : fullUrl;
|
244
|
+
setAttributes({
|
245
|
+
[TraceAttributes.HTTP_URL]: url,
|
246
|
+
[TraceAttributes.HTTP_TARGET]: resolveUrl(path, queryParams, pathParams)
|
247
|
+
});
|
248
|
+
const response = await fetchImpl(url, {
|
249
|
+
method: method.toUpperCase(),
|
250
|
+
body: body ? JSON.stringify(body) : void 0,
|
251
|
+
headers: {
|
252
|
+
"Content-Type": "application/json",
|
253
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
254
|
+
...headers,
|
255
|
+
...hostHeader(fullUrl),
|
256
|
+
Authorization: `Bearer ${apiKey}`
|
257
|
+
}
|
258
|
+
});
|
259
|
+
if (response.status === 204) {
|
260
|
+
return {};
|
261
|
+
}
|
262
|
+
const { host, protocol } = parseUrl(response.url);
|
263
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
264
|
+
setAttributes({
|
265
|
+
[TraceAttributes.KIND]: "http",
|
266
|
+
[TraceAttributes.HTTP_REQUEST_ID]: requestId,
|
267
|
+
[TraceAttributes.HTTP_STATUS_CODE]: response.status,
|
268
|
+
[TraceAttributes.HTTP_HOST]: host,
|
269
|
+
[TraceAttributes.HTTP_SCHEME]: protocol?.replace(":", "")
|
270
|
+
});
|
271
|
+
try {
|
272
|
+
const jsonResponse = await response.json();
|
273
|
+
if (response.ok) {
|
274
|
+
return jsonResponse;
|
275
|
+
}
|
276
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
277
|
+
} catch (error) {
|
278
|
+
throw new FetcherError(response.status, error, requestId);
|
279
|
+
}
|
280
|
+
},
|
281
|
+
{ [TraceAttributes.HTTP_METHOD]: method.toUpperCase(), [TraceAttributes.HTTP_ROUTE]: path }
|
282
|
+
);
|
283
|
+
}
|
284
|
+
function parseUrl(url) {
|
153
285
|
try {
|
154
|
-
const
|
155
|
-
|
156
|
-
return jsonResponse;
|
157
|
-
}
|
158
|
-
throw new FetcherError(response.status, jsonResponse);
|
286
|
+
const { host, protocol } = new URL(url);
|
287
|
+
return { host, protocol };
|
159
288
|
} catch (error) {
|
160
|
-
|
289
|
+
return {};
|
161
290
|
}
|
162
291
|
}
|
163
292
|
|
@@ -216,6 +345,7 @@ const removeWorkspaceMember = (variables) => fetch$1({
|
|
216
345
|
...variables
|
217
346
|
});
|
218
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 });
|
219
349
|
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
220
350
|
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
221
351
|
method: "delete",
|
@@ -251,6 +381,12 @@ const deleteDatabase = (variables) => fetch$1({
|
|
251
381
|
method: "delete",
|
252
382
|
...variables
|
253
383
|
});
|
384
|
+
const getDatabaseMetadata = (variables) => fetch$1({
|
385
|
+
url: "/dbs/{dbName}/metadata",
|
386
|
+
method: "get",
|
387
|
+
...variables
|
388
|
+
});
|
389
|
+
const updateDatabaseMetadata = (variables) => fetch$1({ url: "/dbs/{dbName}/metadata", method: "patch", ...variables });
|
254
390
|
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
255
391
|
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
256
392
|
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
@@ -259,16 +395,28 @@ const resolveBranch = (variables) => fetch$1({
|
|
259
395
|
method: "get",
|
260
396
|
...variables
|
261
397
|
});
|
262
|
-
const
|
263
|
-
|
398
|
+
const listMigrationRequests = (variables) => fetch$1({ url: "/dbs/{dbName}/migrations/list", method: "post", ...variables });
|
399
|
+
const createMigrationRequest = (variables) => fetch$1({ url: "/dbs/{dbName}/migrations", method: "post", ...variables });
|
400
|
+
const getMigrationRequest = (variables) => fetch$1({
|
401
|
+
url: "/dbs/{dbName}/migrations/{mrNumber}",
|
264
402
|
method: "get",
|
265
403
|
...variables
|
266
404
|
});
|
267
|
-
const
|
405
|
+
const updateMigrationRequest = (variables) => fetch$1({ url: "/dbs/{dbName}/migrations/{mrNumber}", method: "patch", ...variables });
|
406
|
+
const listMigrationRequestsCommits = (variables) => fetch$1({ url: "/dbs/{dbName}/migrations/{mrNumber}/commits", method: "post", ...variables });
|
407
|
+
const compareMigrationRequest = (variables) => fetch$1({ url: "/dbs/{dbName}/migrations/{mrNumber}/compare", method: "post", ...variables });
|
408
|
+
const getMigrationRequestIsMerged = (variables) => fetch$1({ url: "/dbs/{dbName}/migrations/{mrNumber}/merge", method: "get", ...variables });
|
409
|
+
const mergeMigrationRequest = (variables) => fetch$1({
|
410
|
+
url: "/dbs/{dbName}/migrations/{mrNumber}/merge",
|
411
|
+
method: "post",
|
412
|
+
...variables
|
413
|
+
});
|
414
|
+
const getBranchDetails = (variables) => fetch$1({
|
268
415
|
url: "/db/{dbBranchName}",
|
269
|
-
method: "
|
416
|
+
method: "get",
|
270
417
|
...variables
|
271
418
|
});
|
419
|
+
const createBranch = (variables) => fetch$1({ url: "/db/{dbBranchName}", method: "put", ...variables });
|
272
420
|
const deleteBranch = (variables) => fetch$1({
|
273
421
|
url: "/db/{dbBranchName}",
|
274
422
|
method: "delete",
|
@@ -287,6 +435,16 @@ const getBranchMetadata = (variables) => fetch$1({
|
|
287
435
|
const getBranchMigrationHistory = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations", method: "get", ...variables });
|
288
436
|
const executeBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/execute", method: "post", ...variables });
|
289
437
|
const getBranchMigrationPlan = (variables) => fetch$1({ url: "/db/{dbBranchName}/migrations/plan", method: "post", ...variables });
|
438
|
+
const compareBranchWithUserSchema = (variables) => fetch$1({ url: "/db/{dbBranchName}/schema/compare", method: "post", ...variables });
|
439
|
+
const compareBranchSchemas = (variables) => fetch$1({ url: "/db/{dbBranchName}/schema/compare/{branchName}", method: "post", ...variables });
|
440
|
+
const updateBranchSchema = (variables) => fetch$1({
|
441
|
+
url: "/db/{dbBranchName}/schema/update",
|
442
|
+
method: "post",
|
443
|
+
...variables
|
444
|
+
});
|
445
|
+
const previewBranchSchemaEdit = (variables) => fetch$1({ url: "/db/{dbBranchName}/schema/preview", method: "post", ...variables });
|
446
|
+
const applyBranchSchemaEdit = (variables) => fetch$1({ url: "/db/{dbBranchName}/schema/apply", method: "post", ...variables });
|
447
|
+
const getBranchSchemaHistory = (variables) => fetch$1({ url: "/db/{dbBranchName}/schema/history", method: "post", ...variables });
|
290
448
|
const getBranchStats = (variables) => fetch$1({
|
291
449
|
url: "/db/{dbBranchName}/stats",
|
292
450
|
method: "get",
|
@@ -342,11 +500,7 @@ const updateColumn = (variables) => fetch$1({
|
|
342
500
|
method: "patch",
|
343
501
|
...variables
|
344
502
|
});
|
345
|
-
const insertRecord = (variables) => fetch$1({
|
346
|
-
url: "/db/{dbBranchName}/tables/{tableName}/data",
|
347
|
-
method: "post",
|
348
|
-
...variables
|
349
|
-
});
|
503
|
+
const insertRecord = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data", method: "post", ...variables });
|
350
504
|
const insertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "put", ...variables });
|
351
505
|
const updateRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "patch", ...variables });
|
352
506
|
const upsertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "post", ...variables });
|
@@ -366,11 +520,21 @@ const queryTable = (variables) => fetch$1({
|
|
366
520
|
method: "post",
|
367
521
|
...variables
|
368
522
|
});
|
523
|
+
const searchTable = (variables) => fetch$1({
|
524
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
525
|
+
method: "post",
|
526
|
+
...variables
|
527
|
+
});
|
369
528
|
const searchBranch = (variables) => fetch$1({
|
370
529
|
url: "/db/{dbBranchName}/search",
|
371
530
|
method: "post",
|
372
531
|
...variables
|
373
532
|
});
|
533
|
+
const summarizeTable = (variables) => fetch$1({
|
534
|
+
url: "/db/{dbBranchName}/tables/{tableName}/summarize",
|
535
|
+
method: "post",
|
536
|
+
...variables
|
537
|
+
});
|
374
538
|
const operationsByTag = {
|
375
539
|
users: { getUser, updateUser, deleteUser, getUserAPIKeys, createUserAPIKey, deleteUserAPIKey },
|
376
540
|
workspaces: {
|
@@ -383,6 +547,7 @@ const operationsByTag = {
|
|
383
547
|
updateWorkspaceMemberRole,
|
384
548
|
removeWorkspaceMember,
|
385
549
|
inviteWorkspaceMember,
|
550
|
+
updateWorkspaceMemberInvite,
|
386
551
|
cancelWorkspaceMemberInvite,
|
387
552
|
resendWorkspaceMemberInvite,
|
388
553
|
acceptWorkspaceMemberInvite
|
@@ -391,6 +556,8 @@ const operationsByTag = {
|
|
391
556
|
getDatabaseList,
|
392
557
|
createDatabase,
|
393
558
|
deleteDatabase,
|
559
|
+
getDatabaseMetadata,
|
560
|
+
updateDatabaseMetadata,
|
394
561
|
getGitBranchesMapping,
|
395
562
|
addGitBranchesEntry,
|
396
563
|
removeGitBranchesEntry,
|
@@ -403,10 +570,28 @@ const operationsByTag = {
|
|
403
570
|
deleteBranch,
|
404
571
|
updateBranchMetadata,
|
405
572
|
getBranchMetadata,
|
573
|
+
getBranchStats
|
574
|
+
},
|
575
|
+
migrationRequests: {
|
576
|
+
listMigrationRequests,
|
577
|
+
createMigrationRequest,
|
578
|
+
getMigrationRequest,
|
579
|
+
updateMigrationRequest,
|
580
|
+
listMigrationRequestsCommits,
|
581
|
+
compareMigrationRequest,
|
582
|
+
getMigrationRequestIsMerged,
|
583
|
+
mergeMigrationRequest
|
584
|
+
},
|
585
|
+
branchSchema: {
|
406
586
|
getBranchMigrationHistory,
|
407
587
|
executeBranchMigrationPlan,
|
408
588
|
getBranchMigrationPlan,
|
409
|
-
|
589
|
+
compareBranchWithUserSchema,
|
590
|
+
compareBranchSchemas,
|
591
|
+
updateBranchSchema,
|
592
|
+
previewBranchSchemaEdit,
|
593
|
+
applyBranchSchemaEdit,
|
594
|
+
getBranchSchemaHistory
|
410
595
|
},
|
411
596
|
table: {
|
412
597
|
createTable,
|
@@ -429,14 +614,16 @@ const operationsByTag = {
|
|
429
614
|
getRecord,
|
430
615
|
bulkInsertTableRecords,
|
431
616
|
queryTable,
|
432
|
-
|
617
|
+
searchTable,
|
618
|
+
searchBranch,
|
619
|
+
summarizeTable
|
433
620
|
}
|
434
621
|
};
|
435
622
|
|
436
623
|
function getHostUrl(provider, type) {
|
437
|
-
if (
|
624
|
+
if (isHostProviderAlias(provider)) {
|
438
625
|
return providers[provider][type];
|
439
|
-
} else if (
|
626
|
+
} else if (isHostProviderBuilder(provider)) {
|
440
627
|
return provider[type];
|
441
628
|
}
|
442
629
|
throw new Error("Invalid API provider");
|
@@ -451,10 +638,10 @@ const providers = {
|
|
451
638
|
workspaces: "https://{workspaceId}.staging.xatabase.co"
|
452
639
|
}
|
453
640
|
};
|
454
|
-
function
|
641
|
+
function isHostProviderAlias(alias) {
|
455
642
|
return isString(alias) && Object.keys(providers).includes(alias);
|
456
643
|
}
|
457
|
-
function
|
644
|
+
function isHostProviderBuilder(builder) {
|
458
645
|
return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
|
459
646
|
}
|
460
647
|
|
@@ -462,7 +649,7 @@ var __accessCheck$7 = (obj, member, msg) => {
|
|
462
649
|
if (!member.has(obj))
|
463
650
|
throw TypeError("Cannot " + msg);
|
464
651
|
};
|
465
|
-
var __privateGet$
|
652
|
+
var __privateGet$7 = (obj, member, getter) => {
|
466
653
|
__accessCheck$7(obj, member, "read from private field");
|
467
654
|
return getter ? getter.call(obj) : member.get(obj);
|
468
655
|
};
|
@@ -471,7 +658,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
471
658
|
throw TypeError("Cannot add the same private member more than once");
|
472
659
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
473
660
|
};
|
474
|
-
var __privateSet$
|
661
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
475
662
|
__accessCheck$7(obj, member, "write to private field");
|
476
663
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
477
664
|
return value;
|
@@ -482,46 +669,58 @@ class XataApiClient {
|
|
482
669
|
__privateAdd$7(this, _extraProps, void 0);
|
483
670
|
__privateAdd$7(this, _namespaces, {});
|
484
671
|
const provider = options.host ?? "production";
|
485
|
-
const apiKey = options
|
672
|
+
const apiKey = options.apiKey ?? getAPIKey();
|
673
|
+
const trace = options.trace ?? defaultTrace;
|
486
674
|
if (!apiKey) {
|
487
675
|
throw new Error("Could not resolve a valid apiKey");
|
488
676
|
}
|
489
|
-
__privateSet$
|
677
|
+
__privateSet$7(this, _extraProps, {
|
490
678
|
apiUrl: getHostUrl(provider, "main"),
|
491
679
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
492
680
|
fetchImpl: getFetchImplementation(options.fetch),
|
493
|
-
apiKey
|
681
|
+
apiKey,
|
682
|
+
trace
|
494
683
|
});
|
495
684
|
}
|
496
685
|
get user() {
|
497
|
-
if (!__privateGet$
|
498
|
-
__privateGet$
|
499
|
-
return __privateGet$
|
686
|
+
if (!__privateGet$7(this, _namespaces).user)
|
687
|
+
__privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
|
688
|
+
return __privateGet$7(this, _namespaces).user;
|
500
689
|
}
|
501
690
|
get workspaces() {
|
502
|
-
if (!__privateGet$
|
503
|
-
__privateGet$
|
504
|
-
return __privateGet$
|
691
|
+
if (!__privateGet$7(this, _namespaces).workspaces)
|
692
|
+
__privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
|
693
|
+
return __privateGet$7(this, _namespaces).workspaces;
|
505
694
|
}
|
506
695
|
get databases() {
|
507
|
-
if (!__privateGet$
|
508
|
-
__privateGet$
|
509
|
-
return __privateGet$
|
696
|
+
if (!__privateGet$7(this, _namespaces).databases)
|
697
|
+
__privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
|
698
|
+
return __privateGet$7(this, _namespaces).databases;
|
510
699
|
}
|
511
700
|
get branches() {
|
512
|
-
if (!__privateGet$
|
513
|
-
__privateGet$
|
514
|
-
return __privateGet$
|
701
|
+
if (!__privateGet$7(this, _namespaces).branches)
|
702
|
+
__privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
|
703
|
+
return __privateGet$7(this, _namespaces).branches;
|
515
704
|
}
|
516
705
|
get tables() {
|
517
|
-
if (!__privateGet$
|
518
|
-
__privateGet$
|
519
|
-
return __privateGet$
|
706
|
+
if (!__privateGet$7(this, _namespaces).tables)
|
707
|
+
__privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
|
708
|
+
return __privateGet$7(this, _namespaces).tables;
|
520
709
|
}
|
521
710
|
get records() {
|
522
|
-
if (!__privateGet$
|
523
|
-
__privateGet$
|
524
|
-
return __privateGet$
|
711
|
+
if (!__privateGet$7(this, _namespaces).records)
|
712
|
+
__privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
|
713
|
+
return __privateGet$7(this, _namespaces).records;
|
714
|
+
}
|
715
|
+
get migrationRequests() {
|
716
|
+
if (!__privateGet$7(this, _namespaces).migrationRequests)
|
717
|
+
__privateGet$7(this, _namespaces).migrationRequests = new MigrationRequestsApi(__privateGet$7(this, _extraProps));
|
718
|
+
return __privateGet$7(this, _namespaces).migrationRequests;
|
719
|
+
}
|
720
|
+
get branchSchema() {
|
721
|
+
if (!__privateGet$7(this, _namespaces).branchSchema)
|
722
|
+
__privateGet$7(this, _namespaces).branchSchema = new BranchSchemaApi(__privateGet$7(this, _extraProps));
|
723
|
+
return __privateGet$7(this, _namespaces).branchSchema;
|
525
724
|
}
|
526
725
|
}
|
527
726
|
_extraProps = new WeakMap();
|
@@ -613,6 +812,13 @@ class WorkspaceApi {
|
|
613
812
|
...this.extraProps
|
614
813
|
});
|
615
814
|
}
|
815
|
+
updateWorkspaceMemberInvite(workspaceId, inviteId, role) {
|
816
|
+
return operationsByTag.workspaces.updateWorkspaceMemberInvite({
|
817
|
+
pathParams: { workspaceId, inviteId },
|
818
|
+
body: { role },
|
819
|
+
...this.extraProps
|
820
|
+
});
|
821
|
+
}
|
616
822
|
cancelWorkspaceMemberInvite(workspaceId, inviteId) {
|
617
823
|
return operationsByTag.workspaces.cancelWorkspaceMemberInvite({
|
618
824
|
pathParams: { workspaceId, inviteId },
|
@@ -655,6 +861,19 @@ class DatabaseApi {
|
|
655
861
|
...this.extraProps
|
656
862
|
});
|
657
863
|
}
|
864
|
+
getDatabaseMetadata(workspace, dbName) {
|
865
|
+
return operationsByTag.database.getDatabaseMetadata({
|
866
|
+
pathParams: { workspace, dbName },
|
867
|
+
...this.extraProps
|
868
|
+
});
|
869
|
+
}
|
870
|
+
updateDatabaseMetadata(workspace, dbName, options = {}) {
|
871
|
+
return operationsByTag.database.updateDatabaseMetadata({
|
872
|
+
pathParams: { workspace, dbName },
|
873
|
+
body: options,
|
874
|
+
...this.extraProps
|
875
|
+
});
|
876
|
+
}
|
658
877
|
getGitBranchesMapping(workspace, dbName) {
|
659
878
|
return operationsByTag.database.getGitBranchesMapping({
|
660
879
|
pathParams: { workspace, dbName },
|
@@ -675,10 +894,10 @@ class DatabaseApi {
|
|
675
894
|
...this.extraProps
|
676
895
|
});
|
677
896
|
}
|
678
|
-
resolveBranch(workspace, dbName, gitBranch) {
|
897
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
679
898
|
return operationsByTag.database.resolveBranch({
|
680
899
|
pathParams: { workspace, dbName },
|
681
|
-
queryParams: { gitBranch },
|
900
|
+
queryParams: { gitBranch, fallbackBranch },
|
682
901
|
...this.extraProps
|
683
902
|
});
|
684
903
|
}
|
@@ -699,10 +918,10 @@ class BranchApi {
|
|
699
918
|
...this.extraProps
|
700
919
|
});
|
701
920
|
}
|
702
|
-
createBranch(workspace, database, branch, from
|
921
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
703
922
|
return operationsByTag.branch.createBranch({
|
704
923
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
705
|
-
queryParams: { from },
|
924
|
+
queryParams: isString(from) ? { from } : void 0,
|
706
925
|
body: options,
|
707
926
|
...this.extraProps
|
708
927
|
});
|
@@ -726,27 +945,6 @@ class BranchApi {
|
|
726
945
|
...this.extraProps
|
727
946
|
});
|
728
947
|
}
|
729
|
-
getBranchMigrationHistory(workspace, database, branch, options = {}) {
|
730
|
-
return operationsByTag.branch.getBranchMigrationHistory({
|
731
|
-
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
732
|
-
body: options,
|
733
|
-
...this.extraProps
|
734
|
-
});
|
735
|
-
}
|
736
|
-
executeBranchMigrationPlan(workspace, database, branch, migrationPlan) {
|
737
|
-
return operationsByTag.branch.executeBranchMigrationPlan({
|
738
|
-
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
739
|
-
body: migrationPlan,
|
740
|
-
...this.extraProps
|
741
|
-
});
|
742
|
-
}
|
743
|
-
getBranchMigrationPlan(workspace, database, branch, schema) {
|
744
|
-
return operationsByTag.branch.getBranchMigrationPlan({
|
745
|
-
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
746
|
-
body: schema,
|
747
|
-
...this.extraProps
|
748
|
-
});
|
749
|
-
}
|
750
948
|
getBranchStats(workspace, database, branch) {
|
751
949
|
return operationsByTag.branch.getBranchStats({
|
752
950
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -827,9 +1025,10 @@ class RecordsApi {
|
|
827
1025
|
constructor(extraProps) {
|
828
1026
|
this.extraProps = extraProps;
|
829
1027
|
}
|
830
|
-
insertRecord(workspace, database, branch, tableName, record) {
|
1028
|
+
insertRecord(workspace, database, branch, tableName, record, options = {}) {
|
831
1029
|
return operationsByTag.records.insertRecord({
|
832
1030
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
1031
|
+
queryParams: options,
|
833
1032
|
body: record,
|
834
1033
|
...this.extraProps
|
835
1034
|
});
|
@@ -858,21 +1057,24 @@ class RecordsApi {
|
|
858
1057
|
...this.extraProps
|
859
1058
|
});
|
860
1059
|
}
|
861
|
-
deleteRecord(workspace, database, branch, tableName, recordId) {
|
1060
|
+
deleteRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
862
1061
|
return operationsByTag.records.deleteRecord({
|
863
1062
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
1063
|
+
queryParams: options,
|
864
1064
|
...this.extraProps
|
865
1065
|
});
|
866
1066
|
}
|
867
1067
|
getRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
868
1068
|
return operationsByTag.records.getRecord({
|
869
1069
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
1070
|
+
queryParams: options,
|
870
1071
|
...this.extraProps
|
871
1072
|
});
|
872
1073
|
}
|
873
|
-
bulkInsertTableRecords(workspace, database, branch, tableName, records) {
|
1074
|
+
bulkInsertTableRecords(workspace, database, branch, tableName, records, options = {}) {
|
874
1075
|
return operationsByTag.records.bulkInsertTableRecords({
|
875
1076
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
1077
|
+
queryParams: options,
|
876
1078
|
body: { records },
|
877
1079
|
...this.extraProps
|
878
1080
|
});
|
@@ -884,6 +1086,13 @@ class RecordsApi {
|
|
884
1086
|
...this.extraProps
|
885
1087
|
});
|
886
1088
|
}
|
1089
|
+
searchTable(workspace, database, branch, tableName, query) {
|
1090
|
+
return operationsByTag.records.searchTable({
|
1091
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
1092
|
+
body: query,
|
1093
|
+
...this.extraProps
|
1094
|
+
});
|
1095
|
+
}
|
887
1096
|
searchBranch(workspace, database, branch, query) {
|
888
1097
|
return operationsByTag.records.searchBranch({
|
889
1098
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -891,6 +1100,138 @@ class RecordsApi {
|
|
891
1100
|
...this.extraProps
|
892
1101
|
});
|
893
1102
|
}
|
1103
|
+
summarizeTable(workspace, database, branch, tableName, query) {
|
1104
|
+
return operationsByTag.records.summarizeTable({
|
1105
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
1106
|
+
body: query,
|
1107
|
+
...this.extraProps
|
1108
|
+
});
|
1109
|
+
}
|
1110
|
+
}
|
1111
|
+
class MigrationRequestsApi {
|
1112
|
+
constructor(extraProps) {
|
1113
|
+
this.extraProps = extraProps;
|
1114
|
+
}
|
1115
|
+
listMigrationRequests(workspace, database, options = {}) {
|
1116
|
+
return operationsByTag.migrationRequests.listMigrationRequests({
|
1117
|
+
pathParams: { workspace, dbName: database },
|
1118
|
+
body: options,
|
1119
|
+
...this.extraProps
|
1120
|
+
});
|
1121
|
+
}
|
1122
|
+
createMigrationRequest(workspace, database, options) {
|
1123
|
+
return operationsByTag.migrationRequests.createMigrationRequest({
|
1124
|
+
pathParams: { workspace, dbName: database },
|
1125
|
+
body: options,
|
1126
|
+
...this.extraProps
|
1127
|
+
});
|
1128
|
+
}
|
1129
|
+
getMigrationRequest(workspace, database, migrationRequest) {
|
1130
|
+
return operationsByTag.migrationRequests.getMigrationRequest({
|
1131
|
+
pathParams: { workspace, dbName: database, mrNumber: migrationRequest },
|
1132
|
+
...this.extraProps
|
1133
|
+
});
|
1134
|
+
}
|
1135
|
+
updateMigrationRequest(workspace, database, migrationRequest, options) {
|
1136
|
+
return operationsByTag.migrationRequests.updateMigrationRequest({
|
1137
|
+
pathParams: { workspace, dbName: database, mrNumber: migrationRequest },
|
1138
|
+
body: options,
|
1139
|
+
...this.extraProps
|
1140
|
+
});
|
1141
|
+
}
|
1142
|
+
listMigrationRequestsCommits(workspace, database, migrationRequest, options = {}) {
|
1143
|
+
return operationsByTag.migrationRequests.listMigrationRequestsCommits({
|
1144
|
+
pathParams: { workspace, dbName: database, mrNumber: migrationRequest },
|
1145
|
+
body: options,
|
1146
|
+
...this.extraProps
|
1147
|
+
});
|
1148
|
+
}
|
1149
|
+
compareMigrationRequest(workspace, database, migrationRequest) {
|
1150
|
+
return operationsByTag.migrationRequests.compareMigrationRequest({
|
1151
|
+
pathParams: { workspace, dbName: database, mrNumber: migrationRequest },
|
1152
|
+
...this.extraProps
|
1153
|
+
});
|
1154
|
+
}
|
1155
|
+
getMigrationRequestIsMerged(workspace, database, migrationRequest) {
|
1156
|
+
return operationsByTag.migrationRequests.getMigrationRequestIsMerged({
|
1157
|
+
pathParams: { workspace, dbName: database, mrNumber: migrationRequest },
|
1158
|
+
...this.extraProps
|
1159
|
+
});
|
1160
|
+
}
|
1161
|
+
mergeMigrationRequest(workspace, database, migrationRequest) {
|
1162
|
+
return operationsByTag.migrationRequests.mergeMigrationRequest({
|
1163
|
+
pathParams: { workspace, dbName: database, mrNumber: migrationRequest },
|
1164
|
+
...this.extraProps
|
1165
|
+
});
|
1166
|
+
}
|
1167
|
+
}
|
1168
|
+
class BranchSchemaApi {
|
1169
|
+
constructor(extraProps) {
|
1170
|
+
this.extraProps = extraProps;
|
1171
|
+
}
|
1172
|
+
getBranchMigrationHistory(workspace, database, branch, options = {}) {
|
1173
|
+
return operationsByTag.branchSchema.getBranchMigrationHistory({
|
1174
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
1175
|
+
body: options,
|
1176
|
+
...this.extraProps
|
1177
|
+
});
|
1178
|
+
}
|
1179
|
+
executeBranchMigrationPlan(workspace, database, branch, migrationPlan) {
|
1180
|
+
return operationsByTag.branchSchema.executeBranchMigrationPlan({
|
1181
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
1182
|
+
body: migrationPlan,
|
1183
|
+
...this.extraProps
|
1184
|
+
});
|
1185
|
+
}
|
1186
|
+
getBranchMigrationPlan(workspace, database, branch, schema) {
|
1187
|
+
return operationsByTag.branchSchema.getBranchMigrationPlan({
|
1188
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
1189
|
+
body: schema,
|
1190
|
+
...this.extraProps
|
1191
|
+
});
|
1192
|
+
}
|
1193
|
+
compareBranchWithUserSchema(workspace, database, branch, schema) {
|
1194
|
+
return operationsByTag.branchSchema.compareBranchWithUserSchema({
|
1195
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
1196
|
+
body: { schema },
|
1197
|
+
...this.extraProps
|
1198
|
+
});
|
1199
|
+
}
|
1200
|
+
compareBranchSchemas(workspace, database, branch, branchName, schema) {
|
1201
|
+
return operationsByTag.branchSchema.compareBranchSchemas({
|
1202
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, branchName },
|
1203
|
+
body: { schema },
|
1204
|
+
...this.extraProps
|
1205
|
+
});
|
1206
|
+
}
|
1207
|
+
updateBranchSchema(workspace, database, branch, migration) {
|
1208
|
+
return operationsByTag.branchSchema.updateBranchSchema({
|
1209
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
1210
|
+
body: migration,
|
1211
|
+
...this.extraProps
|
1212
|
+
});
|
1213
|
+
}
|
1214
|
+
previewBranchSchemaEdit(workspace, database, branch, migration) {
|
1215
|
+
return operationsByTag.branchSchema.previewBranchSchemaEdit({
|
1216
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
1217
|
+
body: migration,
|
1218
|
+
...this.extraProps
|
1219
|
+
});
|
1220
|
+
}
|
1221
|
+
applyBranchSchemaEdit(workspace, database, branch, edits) {
|
1222
|
+
return operationsByTag.branchSchema.applyBranchSchemaEdit({
|
1223
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
1224
|
+
body: { edits },
|
1225
|
+
...this.extraProps
|
1226
|
+
});
|
1227
|
+
}
|
1228
|
+
getBranchSchemaHistory(workspace, database, branch, options = {}) {
|
1229
|
+
return operationsByTag.branchSchema.getBranchSchemaHistory({
|
1230
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
1231
|
+
body: options,
|
1232
|
+
...this.extraProps
|
1233
|
+
});
|
1234
|
+
}
|
894
1235
|
}
|
895
1236
|
|
896
1237
|
class XataApiPlugin {
|
@@ -907,7 +1248,7 @@ var __accessCheck$6 = (obj, member, msg) => {
|
|
907
1248
|
if (!member.has(obj))
|
908
1249
|
throw TypeError("Cannot " + msg);
|
909
1250
|
};
|
910
|
-
var __privateGet$
|
1251
|
+
var __privateGet$6 = (obj, member, getter) => {
|
911
1252
|
__accessCheck$6(obj, member, "read from private field");
|
912
1253
|
return getter ? getter.call(obj) : member.get(obj);
|
913
1254
|
};
|
@@ -916,30 +1257,30 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
916
1257
|
throw TypeError("Cannot add the same private member more than once");
|
917
1258
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
918
1259
|
};
|
919
|
-
var __privateSet$
|
1260
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
920
1261
|
__accessCheck$6(obj, member, "write to private field");
|
921
1262
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
922
1263
|
return value;
|
923
1264
|
};
|
924
|
-
var _query;
|
1265
|
+
var _query, _page;
|
925
1266
|
class Page {
|
926
1267
|
constructor(query, meta, records = []) {
|
927
1268
|
__privateAdd$6(this, _query, void 0);
|
928
|
-
__privateSet$
|
1269
|
+
__privateSet$6(this, _query, query);
|
929
1270
|
this.meta = meta;
|
930
|
-
this.records = records;
|
1271
|
+
this.records = new RecordArray(this, records);
|
931
1272
|
}
|
932
1273
|
async nextPage(size, offset) {
|
933
|
-
return __privateGet$
|
1274
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
934
1275
|
}
|
935
1276
|
async previousPage(size, offset) {
|
936
|
-
return __privateGet$
|
1277
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
937
1278
|
}
|
938
1279
|
async firstPage(size, offset) {
|
939
|
-
return __privateGet$
|
1280
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
|
940
1281
|
}
|
941
1282
|
async lastPage(size, offset) {
|
942
|
-
return __privateGet$
|
1283
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
|
943
1284
|
}
|
944
1285
|
hasNextPage() {
|
945
1286
|
return this.meta.page.more;
|
@@ -947,15 +1288,62 @@ class Page {
|
|
947
1288
|
}
|
948
1289
|
_query = new WeakMap();
|
949
1290
|
const PAGINATION_MAX_SIZE = 200;
|
950
|
-
const PAGINATION_DEFAULT_SIZE =
|
1291
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
951
1292
|
const PAGINATION_MAX_OFFSET = 800;
|
952
1293
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
1294
|
+
function isCursorPaginationOptions(options) {
|
1295
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
1296
|
+
}
|
1297
|
+
const _RecordArray = class extends Array {
|
1298
|
+
constructor(...args) {
|
1299
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
1300
|
+
__privateAdd$6(this, _page, void 0);
|
1301
|
+
__privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
|
1302
|
+
}
|
1303
|
+
static parseConstructorParams(...args) {
|
1304
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1305
|
+
return new Array(args[0]);
|
1306
|
+
}
|
1307
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1308
|
+
const result = args[1] ?? args[0].records ?? [];
|
1309
|
+
return new Array(...result);
|
1310
|
+
}
|
1311
|
+
return new Array(...args);
|
1312
|
+
}
|
1313
|
+
toArray() {
|
1314
|
+
return new Array(...this);
|
1315
|
+
}
|
1316
|
+
map(callbackfn, thisArg) {
|
1317
|
+
return this.toArray().map(callbackfn, thisArg);
|
1318
|
+
}
|
1319
|
+
async nextPage(size, offset) {
|
1320
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1321
|
+
return new _RecordArray(newPage);
|
1322
|
+
}
|
1323
|
+
async previousPage(size, offset) {
|
1324
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1325
|
+
return new _RecordArray(newPage);
|
1326
|
+
}
|
1327
|
+
async firstPage(size, offset) {
|
1328
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1329
|
+
return new _RecordArray(newPage);
|
1330
|
+
}
|
1331
|
+
async lastPage(size, offset) {
|
1332
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1333
|
+
return new _RecordArray(newPage);
|
1334
|
+
}
|
1335
|
+
hasNextPage() {
|
1336
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1337
|
+
}
|
1338
|
+
};
|
1339
|
+
let RecordArray = _RecordArray;
|
1340
|
+
_page = new WeakMap();
|
953
1341
|
|
954
1342
|
var __accessCheck$5 = (obj, member, msg) => {
|
955
1343
|
if (!member.has(obj))
|
956
1344
|
throw TypeError("Cannot " + msg);
|
957
1345
|
};
|
958
|
-
var __privateGet$
|
1346
|
+
var __privateGet$5 = (obj, member, getter) => {
|
959
1347
|
__accessCheck$5(obj, member, "read from private field");
|
960
1348
|
return getter ? getter.call(obj) : member.get(obj);
|
961
1349
|
};
|
@@ -964,34 +1352,40 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
964
1352
|
throw TypeError("Cannot add the same private member more than once");
|
965
1353
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
966
1354
|
};
|
967
|
-
var __privateSet$
|
1355
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
968
1356
|
__accessCheck$5(obj, member, "write to private field");
|
969
1357
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
970
1358
|
return value;
|
971
1359
|
};
|
972
|
-
var
|
1360
|
+
var __privateMethod$3 = (obj, member, method) => {
|
1361
|
+
__accessCheck$5(obj, member, "access private method");
|
1362
|
+
return method;
|
1363
|
+
};
|
1364
|
+
var _table$1, _repository, _data, _cleanFilterConstraint, cleanFilterConstraint_fn;
|
973
1365
|
const _Query = class {
|
974
|
-
constructor(repository, table, data,
|
1366
|
+
constructor(repository, table, data, rawParent) {
|
1367
|
+
__privateAdd$5(this, _cleanFilterConstraint);
|
975
1368
|
__privateAdd$5(this, _table$1, void 0);
|
976
1369
|
__privateAdd$5(this, _repository, void 0);
|
977
1370
|
__privateAdd$5(this, _data, { filter: {} });
|
978
1371
|
this.meta = { page: { cursor: "start", more: true } };
|
979
|
-
this.records = [];
|
980
|
-
__privateSet$
|
1372
|
+
this.records = new RecordArray(this, []);
|
1373
|
+
__privateSet$5(this, _table$1, table);
|
981
1374
|
if (repository) {
|
982
|
-
__privateSet$
|
1375
|
+
__privateSet$5(this, _repository, repository);
|
983
1376
|
} else {
|
984
|
-
__privateSet$
|
1377
|
+
__privateSet$5(this, _repository, this);
|
985
1378
|
}
|
986
|
-
|
987
|
-
__privateGet$
|
988
|
-
__privateGet$
|
989
|
-
__privateGet$
|
990
|
-
__privateGet$
|
991
|
-
__privateGet$
|
992
|
-
__privateGet$
|
993
|
-
__privateGet$
|
994
|
-
__privateGet$
|
1379
|
+
const parent = cleanParent(data, rawParent);
|
1380
|
+
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
1381
|
+
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
1382
|
+
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
1383
|
+
__privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
|
1384
|
+
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
1385
|
+
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
1386
|
+
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
1387
|
+
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
1388
|
+
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
995
1389
|
this.any = this.any.bind(this);
|
996
1390
|
this.all = this.all.bind(this);
|
997
1391
|
this.not = this.not.bind(this);
|
@@ -1002,50 +1396,58 @@ const _Query = class {
|
|
1002
1396
|
Object.defineProperty(this, "repository", { enumerable: false });
|
1003
1397
|
}
|
1004
1398
|
getQueryOptions() {
|
1005
|
-
return __privateGet$
|
1399
|
+
return __privateGet$5(this, _data);
|
1006
1400
|
}
|
1007
1401
|
key() {
|
1008
|
-
const { columns = [], filter = {}, sort = [],
|
1009
|
-
const key = JSON.stringify({ columns, filter, sort,
|
1402
|
+
const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
|
1403
|
+
const key = JSON.stringify({ columns, filter, sort, pagination });
|
1010
1404
|
return toBase64(key);
|
1011
1405
|
}
|
1012
1406
|
any(...queries) {
|
1013
1407
|
const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1014
|
-
return new _Query(__privateGet$
|
1408
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
|
1015
1409
|
}
|
1016
1410
|
all(...queries) {
|
1017
1411
|
const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1018
|
-
return new _Query(__privateGet$
|
1412
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
1019
1413
|
}
|
1020
1414
|
not(...queries) {
|
1021
1415
|
const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1022
|
-
return new _Query(__privateGet$
|
1416
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
|
1023
1417
|
}
|
1024
1418
|
none(...queries) {
|
1025
1419
|
const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
|
1026
|
-
return new _Query(__privateGet$
|
1420
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
|
1027
1421
|
}
|
1028
1422
|
filter(a, b) {
|
1029
1423
|
if (arguments.length === 1) {
|
1030
|
-
const constraints = Object.entries(a).map(([column, constraint]) => ({
|
1031
|
-
|
1032
|
-
|
1424
|
+
const constraints = Object.entries(a ?? {}).map(([column, constraint]) => ({
|
1425
|
+
[column]: __privateMethod$3(this, _cleanFilterConstraint, cleanFilterConstraint_fn).call(this, column, constraint)
|
1426
|
+
}));
|
1427
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
|
1428
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
1033
1429
|
} else {
|
1034
|
-
const
|
1035
|
-
|
1430
|
+
const constraints = isDefined(a) && isDefined(b) ? [{ [a]: __privateMethod$3(this, _cleanFilterConstraint, cleanFilterConstraint_fn).call(this, a, b) }] : void 0;
|
1431
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
|
1432
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
1036
1433
|
}
|
1037
1434
|
}
|
1038
|
-
sort(column, direction) {
|
1039
|
-
const originalSort = [__privateGet$
|
1435
|
+
sort(column, direction = "asc") {
|
1436
|
+
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
1040
1437
|
const sort = [...originalSort, { column, direction }];
|
1041
|
-
return new _Query(__privateGet$
|
1438
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
1042
1439
|
}
|
1043
1440
|
select(columns) {
|
1044
|
-
return new _Query(
|
1441
|
+
return new _Query(
|
1442
|
+
__privateGet$5(this, _repository),
|
1443
|
+
__privateGet$5(this, _table$1),
|
1444
|
+
{ columns },
|
1445
|
+
__privateGet$5(this, _data)
|
1446
|
+
);
|
1045
1447
|
}
|
1046
1448
|
getPaginated(options = {}) {
|
1047
|
-
const query = new _Query(__privateGet$
|
1048
|
-
return __privateGet$
|
1449
|
+
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
1450
|
+
return __privateGet$5(this, _repository).query(query);
|
1049
1451
|
}
|
1050
1452
|
async *[Symbol.asyncIterator]() {
|
1051
1453
|
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
@@ -1054,18 +1456,30 @@ const _Query = class {
|
|
1054
1456
|
}
|
1055
1457
|
async *getIterator(options = {}) {
|
1056
1458
|
const { batchSize = 1 } = options;
|
1057
|
-
let
|
1058
|
-
let
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1459
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1460
|
+
let more = page.hasNextPage();
|
1461
|
+
yield page.records;
|
1462
|
+
while (more) {
|
1463
|
+
page = await page.nextPage();
|
1464
|
+
more = page.hasNextPage();
|
1465
|
+
yield page.records;
|
1064
1466
|
}
|
1065
1467
|
}
|
1066
1468
|
async getMany(options = {}) {
|
1067
|
-
const {
|
1068
|
-
|
1469
|
+
const { pagination = {}, ...rest } = options;
|
1470
|
+
const { size = PAGINATION_DEFAULT_SIZE, offset } = pagination;
|
1471
|
+
const batchSize = size <= PAGINATION_MAX_SIZE ? size : PAGINATION_MAX_SIZE;
|
1472
|
+
let page = await this.getPaginated({ ...rest, pagination: { size: batchSize, offset } });
|
1473
|
+
const results = [...page.records];
|
1474
|
+
while (page.hasNextPage() && results.length < size) {
|
1475
|
+
page = await page.nextPage();
|
1476
|
+
results.push(...page.records);
|
1477
|
+
}
|
1478
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1479
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1480
|
+
}
|
1481
|
+
const array = new RecordArray(page, results.slice(0, size));
|
1482
|
+
return array;
|
1069
1483
|
}
|
1070
1484
|
async getAll(options = {}) {
|
1071
1485
|
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
@@ -1076,11 +1490,17 @@ const _Query = class {
|
|
1076
1490
|
return results;
|
1077
1491
|
}
|
1078
1492
|
async getFirst(options = {}) {
|
1079
|
-
const records = await this.getMany({ ...options,
|
1080
|
-
return records[0]
|
1493
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1494
|
+
return records[0] ?? null;
|
1495
|
+
}
|
1496
|
+
async getFirstOrThrow(options = {}) {
|
1497
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1498
|
+
if (records[0] === void 0)
|
1499
|
+
throw new Error("No results found.");
|
1500
|
+
return records[0];
|
1081
1501
|
}
|
1082
1502
|
cache(ttl) {
|
1083
|
-
return new _Query(__privateGet$
|
1503
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
1084
1504
|
}
|
1085
1505
|
nextPage(size, offset) {
|
1086
1506
|
return this.firstPage(size, offset);
|
@@ -1089,10 +1509,10 @@ const _Query = class {
|
|
1089
1509
|
return this.firstPage(size, offset);
|
1090
1510
|
}
|
1091
1511
|
firstPage(size, offset) {
|
1092
|
-
return this.getPaginated({
|
1512
|
+
return this.getPaginated({ pagination: { size, offset } });
|
1093
1513
|
}
|
1094
1514
|
lastPage(size, offset) {
|
1095
|
-
return this.getPaginated({
|
1515
|
+
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
1096
1516
|
}
|
1097
1517
|
hasNextPage() {
|
1098
1518
|
return this.meta.page.more;
|
@@ -1102,12 +1522,31 @@ let Query = _Query;
|
|
1102
1522
|
_table$1 = new WeakMap();
|
1103
1523
|
_repository = new WeakMap();
|
1104
1524
|
_data = new WeakMap();
|
1525
|
+
_cleanFilterConstraint = new WeakSet();
|
1526
|
+
cleanFilterConstraint_fn = function(column, value) {
|
1527
|
+
const columnType = __privateGet$5(this, _table$1).schema?.columns.find(({ name }) => name === column)?.type;
|
1528
|
+
if (columnType === "multiple" && (isString(value) || isStringArray(value))) {
|
1529
|
+
return { $includes: value };
|
1530
|
+
}
|
1531
|
+
if (columnType === "link" && isObject(value) && isString(value.id)) {
|
1532
|
+
return value.id;
|
1533
|
+
}
|
1534
|
+
return value;
|
1535
|
+
};
|
1536
|
+
function cleanParent(data, parent) {
|
1537
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1538
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1539
|
+
}
|
1540
|
+
return parent;
|
1541
|
+
}
|
1105
1542
|
|
1106
1543
|
function isIdentifiable(x) {
|
1107
1544
|
return isObject(x) && isString(x?.id);
|
1108
1545
|
}
|
1109
1546
|
function isXataRecord(x) {
|
1110
|
-
|
1547
|
+
const record = x;
|
1548
|
+
const metadata = record?.getMetadata();
|
1549
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1111
1550
|
}
|
1112
1551
|
|
1113
1552
|
function isSortFilterString(value) {
|
@@ -1137,7 +1576,7 @@ var __accessCheck$4 = (obj, member, msg) => {
|
|
1137
1576
|
if (!member.has(obj))
|
1138
1577
|
throw TypeError("Cannot " + msg);
|
1139
1578
|
};
|
1140
|
-
var __privateGet$
|
1579
|
+
var __privateGet$4 = (obj, member, getter) => {
|
1141
1580
|
__accessCheck$4(obj, member, "read from private field");
|
1142
1581
|
return getter ? getter.call(obj) : member.get(obj);
|
1143
1582
|
};
|
@@ -1146,7 +1585,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1146
1585
|
throw TypeError("Cannot add the same private member more than once");
|
1147
1586
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1148
1587
|
};
|
1149
|
-
var __privateSet$
|
1588
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1150
1589
|
__accessCheck$4(obj, member, "write to private field");
|
1151
1590
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1152
1591
|
return value;
|
@@ -1155,304 +1594,411 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1155
1594
|
__accessCheck$4(obj, member, "access private method");
|
1156
1595
|
return method;
|
1157
1596
|
};
|
1158
|
-
var _table,
|
1597
|
+
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;
|
1159
1598
|
class Repository extends Query {
|
1160
1599
|
}
|
1161
1600
|
class RestRepository extends Query {
|
1162
1601
|
constructor(options) {
|
1163
|
-
super(
|
1602
|
+
super(
|
1603
|
+
null,
|
1604
|
+
{ name: options.table, schema: options.schemaTables?.find((table) => table.name === options.table) },
|
1605
|
+
{}
|
1606
|
+
);
|
1164
1607
|
__privateAdd$4(this, _insertRecordWithoutId);
|
1165
1608
|
__privateAdd$4(this, _insertRecordWithId);
|
1166
1609
|
__privateAdd$4(this, _bulkInsertTableRecords);
|
1167
1610
|
__privateAdd$4(this, _updateRecordWithID);
|
1168
1611
|
__privateAdd$4(this, _upsertRecordWithID);
|
1169
1612
|
__privateAdd$4(this, _deleteRecord);
|
1170
|
-
__privateAdd$4(this, _invalidateCache);
|
1171
|
-
__privateAdd$4(this, _setCacheRecord);
|
1172
|
-
__privateAdd$4(this, _getCacheRecord);
|
1173
1613
|
__privateAdd$4(this, _setCacheQuery);
|
1174
1614
|
__privateAdd$4(this, _getCacheQuery);
|
1615
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1175
1616
|
__privateAdd$4(this, _table, void 0);
|
1176
|
-
__privateAdd$4(this, _links, void 0);
|
1177
1617
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1618
|
+
__privateAdd$4(this, _db, void 0);
|
1178
1619
|
__privateAdd$4(this, _cache, void 0);
|
1179
|
-
|
1180
|
-
|
1181
|
-
__privateSet$
|
1182
|
-
this
|
1183
|
-
__privateSet$
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
throw new Error("The id can't be empty");
|
1194
|
-
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
|
1195
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1196
|
-
return record;
|
1197
|
-
}
|
1198
|
-
if (isObject(a) && isString(a.id)) {
|
1199
|
-
if (a.id === "")
|
1200
|
-
throw new Error("The id can't be empty");
|
1201
|
-
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
|
1202
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1203
|
-
return record;
|
1204
|
-
}
|
1205
|
-
if (isObject(a)) {
|
1206
|
-
const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
|
1207
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1208
|
-
return record;
|
1209
|
-
}
|
1210
|
-
throw new Error("Invalid arguments for create method");
|
1211
|
-
}
|
1212
|
-
async read(recordId) {
|
1213
|
-
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
|
1214
|
-
if (cacheRecord)
|
1215
|
-
return cacheRecord;
|
1216
|
-
const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
|
1217
|
-
try {
|
1218
|
-
const response = await getRecord({
|
1219
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
|
1220
|
-
...fetchProps
|
1620
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1621
|
+
__privateAdd$4(this, _trace, void 0);
|
1622
|
+
__privateSet$4(this, _table, options.table);
|
1623
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1624
|
+
__privateSet$4(this, _db, options.db);
|
1625
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1626
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1627
|
+
const trace = options.pluginOptions.trace ?? defaultTrace;
|
1628
|
+
__privateSet$4(this, _trace, async (name, fn, options2 = {}) => {
|
1629
|
+
return trace(name, fn, {
|
1630
|
+
...options2,
|
1631
|
+
[TraceAttributes.TABLE]: __privateGet$4(this, _table),
|
1632
|
+
[TraceAttributes.KIND]: "sdk-operation",
|
1633
|
+
[TraceAttributes.VERSION]: VERSION
|
1221
1634
|
});
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1635
|
+
});
|
1636
|
+
}
|
1637
|
+
async create(a, b, c) {
|
1638
|
+
return __privateGet$4(this, _trace).call(this, "create", async () => {
|
1639
|
+
if (Array.isArray(a)) {
|
1640
|
+
if (a.length === 0)
|
1641
|
+
return [];
|
1642
|
+
const columns = isStringArray(b) ? b : void 0;
|
1643
|
+
return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a, columns);
|
1226
1644
|
}
|
1227
|
-
|
1228
|
-
|
1645
|
+
if (isString(a) && isObject(b)) {
|
1646
|
+
if (a === "")
|
1647
|
+
throw new Error("The id can't be empty");
|
1648
|
+
const columns = isStringArray(c) ? c : void 0;
|
1649
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns);
|
1650
|
+
}
|
1651
|
+
if (isObject(a) && isString(a.id)) {
|
1652
|
+
if (a.id === "")
|
1653
|
+
throw new Error("The id can't be empty");
|
1654
|
+
const columns = isStringArray(b) ? b : void 0;
|
1655
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1656
|
+
}
|
1657
|
+
if (isObject(a)) {
|
1658
|
+
const columns = isStringArray(b) ? b : void 0;
|
1659
|
+
return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a, columns);
|
1660
|
+
}
|
1661
|
+
throw new Error("Invalid arguments for create method");
|
1662
|
+
});
|
1229
1663
|
}
|
1230
|
-
async
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1664
|
+
async read(a, b) {
|
1665
|
+
return __privateGet$4(this, _trace).call(this, "read", async () => {
|
1666
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1667
|
+
if (Array.isArray(a)) {
|
1668
|
+
if (a.length === 0)
|
1669
|
+
return [];
|
1670
|
+
const ids = a.map((item) => extractId(item));
|
1671
|
+
const finalObjects = await this.getAll({ filter: { id: { $any: compact(ids) } }, columns });
|
1672
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1673
|
+
acc[object.id] = object;
|
1674
|
+
return acc;
|
1675
|
+
}, {});
|
1676
|
+
return ids.map((id2) => dictionary[id2 ?? ""] ?? null);
|
1234
1677
|
}
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1678
|
+
const id = extractId(a);
|
1679
|
+
if (id) {
|
1680
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1681
|
+
try {
|
1682
|
+
const response = await getRecord({
|
1683
|
+
pathParams: {
|
1684
|
+
workspace: "{workspaceId}",
|
1685
|
+
dbBranchName: "{dbBranch}",
|
1686
|
+
tableName: __privateGet$4(this, _table),
|
1687
|
+
recordId: id
|
1688
|
+
},
|
1689
|
+
queryParams: { columns },
|
1690
|
+
...fetchProps
|
1691
|
+
});
|
1692
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1693
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1694
|
+
} catch (e) {
|
1695
|
+
if (isObject(e) && e.status === 404) {
|
1696
|
+
return null;
|
1697
|
+
}
|
1698
|
+
throw e;
|
1699
|
+
}
|
1700
|
+
}
|
1701
|
+
return null;
|
1702
|
+
});
|
1250
1703
|
}
|
1251
|
-
async
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1704
|
+
async readOrThrow(a, b) {
|
1705
|
+
return __privateGet$4(this, _trace).call(this, "readOrThrow", async () => {
|
1706
|
+
const result = await this.read(a, b);
|
1707
|
+
if (Array.isArray(result)) {
|
1708
|
+
const missingIds = compact(
|
1709
|
+
a.filter((_item, index) => result[index] === null).map((item) => extractId(item))
|
1710
|
+
);
|
1711
|
+
if (missingIds.length > 0) {
|
1712
|
+
throw new Error(`Could not find records with ids: ${missingIds.join(", ")}`);
|
1713
|
+
}
|
1714
|
+
return result;
|
1255
1715
|
}
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
return record;
|
1263
|
-
}
|
1264
|
-
if (isObject(a) && isString(a.id)) {
|
1265
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1266
|
-
const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1267
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1268
|
-
return record;
|
1269
|
-
}
|
1270
|
-
throw new Error("Invalid arguments for createOrUpdate method");
|
1716
|
+
if (result === null) {
|
1717
|
+
const id = extractId(a) ?? "unknown";
|
1718
|
+
throw new Error(`Record with id ${id} not found`);
|
1719
|
+
}
|
1720
|
+
return result;
|
1721
|
+
});
|
1271
1722
|
}
|
1272
|
-
async
|
1273
|
-
|
1274
|
-
if (a
|
1275
|
-
|
1723
|
+
async update(a, b, c) {
|
1724
|
+
return __privateGet$4(this, _trace).call(this, "update", async () => {
|
1725
|
+
if (Array.isArray(a)) {
|
1726
|
+
if (a.length === 0)
|
1727
|
+
return [];
|
1728
|
+
if (a.length > 100) {
|
1729
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1730
|
+
}
|
1731
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1732
|
+
return Promise.all(a.map((object) => this.update(object, columns)));
|
1276
1733
|
}
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
1287
|
-
|
1288
|
-
|
1289
|
-
|
1290
|
-
|
1734
|
+
if (isString(a) && isObject(b)) {
|
1735
|
+
const columns = isStringArray(c) ? c : void 0;
|
1736
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns);
|
1737
|
+
}
|
1738
|
+
if (isObject(a) && isString(a.id)) {
|
1739
|
+
const columns = isStringArray(b) ? b : void 0;
|
1740
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1741
|
+
}
|
1742
|
+
throw new Error("Invalid arguments for update method");
|
1743
|
+
});
|
1744
|
+
}
|
1745
|
+
async updateOrThrow(a, b, c) {
|
1746
|
+
return __privateGet$4(this, _trace).call(this, "updateOrThrow", async () => {
|
1747
|
+
const result = await this.update(a, b, c);
|
1748
|
+
if (Array.isArray(result)) {
|
1749
|
+
const missingIds = compact(
|
1750
|
+
a.filter((_item, index) => result[index] === null).map((item) => extractId(item))
|
1751
|
+
);
|
1752
|
+
if (missingIds.length > 0) {
|
1753
|
+
throw new Error(`Could not find records with ids: ${missingIds.join(", ")}`);
|
1754
|
+
}
|
1755
|
+
return result;
|
1756
|
+
}
|
1757
|
+
if (result === null) {
|
1758
|
+
const id = extractId(a) ?? "unknown";
|
1759
|
+
throw new Error(`Record with id ${id} not found`);
|
1760
|
+
}
|
1761
|
+
return result;
|
1762
|
+
});
|
1763
|
+
}
|
1764
|
+
async createOrUpdate(a, b, c) {
|
1765
|
+
return __privateGet$4(this, _trace).call(this, "createOrUpdate", async () => {
|
1766
|
+
if (Array.isArray(a)) {
|
1767
|
+
if (a.length === 0)
|
1768
|
+
return [];
|
1769
|
+
if (a.length > 100) {
|
1770
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1771
|
+
}
|
1772
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1773
|
+
return Promise.all(a.map((object) => this.createOrUpdate(object, columns)));
|
1774
|
+
}
|
1775
|
+
if (isString(a) && isObject(b)) {
|
1776
|
+
const columns = isStringArray(c) ? c : void 0;
|
1777
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b, columns);
|
1778
|
+
}
|
1779
|
+
if (isObject(a) && isString(a.id)) {
|
1780
|
+
const columns = isStringArray(c) ? c : void 0;
|
1781
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1782
|
+
}
|
1783
|
+
throw new Error("Invalid arguments for createOrUpdate method");
|
1784
|
+
});
|
1785
|
+
}
|
1786
|
+
async delete(a, b) {
|
1787
|
+
return __privateGet$4(this, _trace).call(this, "delete", async () => {
|
1788
|
+
if (Array.isArray(a)) {
|
1789
|
+
if (a.length === 0)
|
1790
|
+
return [];
|
1791
|
+
if (a.length > 100) {
|
1792
|
+
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1793
|
+
}
|
1794
|
+
return Promise.all(a.map((id) => this.delete(id, b)));
|
1795
|
+
}
|
1796
|
+
if (isString(a)) {
|
1797
|
+
return __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a, b);
|
1798
|
+
}
|
1799
|
+
if (isObject(a) && isString(a.id)) {
|
1800
|
+
return __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id, b);
|
1801
|
+
}
|
1802
|
+
throw new Error("Invalid arguments for delete method");
|
1803
|
+
});
|
1804
|
+
}
|
1805
|
+
async deleteOrThrow(a, b) {
|
1806
|
+
return __privateGet$4(this, _trace).call(this, "deleteOrThrow", async () => {
|
1807
|
+
const result = await this.delete(a, b);
|
1808
|
+
if (Array.isArray(result)) {
|
1809
|
+
const missingIds = compact(
|
1810
|
+
a.filter((_item, index) => result[index] === null).map((item) => extractId(item))
|
1811
|
+
);
|
1812
|
+
if (missingIds.length > 0) {
|
1813
|
+
throw new Error(`Could not find records with ids: ${missingIds.join(", ")}`);
|
1814
|
+
}
|
1815
|
+
return result;
|
1816
|
+
} else if (result === null) {
|
1817
|
+
const id = extractId(a) ?? "unknown";
|
1818
|
+
throw new Error(`Record with id ${id} not found`);
|
1819
|
+
}
|
1820
|
+
return result;
|
1821
|
+
});
|
1291
1822
|
}
|
1292
1823
|
async search(query, options = {}) {
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1824
|
+
return __privateGet$4(this, _trace).call(this, "search", async () => {
|
1825
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1826
|
+
const { records } = await searchTable({
|
1827
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1828
|
+
body: {
|
1829
|
+
query,
|
1830
|
+
fuzziness: options.fuzziness,
|
1831
|
+
prefix: options.prefix,
|
1832
|
+
highlight: options.highlight,
|
1833
|
+
filter: options.filter,
|
1834
|
+
boosters: options.boosters
|
1835
|
+
},
|
1836
|
+
...fetchProps
|
1837
|
+
});
|
1838
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1839
|
+
return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1298
1840
|
});
|
1299
|
-
return records.map((item) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), item));
|
1300
1841
|
}
|
1301
1842
|
async query(query) {
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1843
|
+
return __privateGet$4(this, _trace).call(this, "query", async () => {
|
1844
|
+
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
1845
|
+
if (cacheQuery)
|
1846
|
+
return new Page(query, cacheQuery.meta, cacheQuery.records);
|
1847
|
+
const data = query.getQueryOptions();
|
1848
|
+
const body = {
|
1849
|
+
filter: cleanFilter(data.filter),
|
1850
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1851
|
+
page: data.pagination,
|
1852
|
+
columns: data.columns
|
1853
|
+
};
|
1854
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1855
|
+
const { meta, records: objects } = await queryTable({
|
1856
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1857
|
+
body,
|
1858
|
+
...fetchProps
|
1859
|
+
});
|
1860
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1861
|
+
const records = objects.map((record) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), record));
|
1862
|
+
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1863
|
+
return new Page(query, meta, records);
|
1317
1864
|
});
|
1318
|
-
const records = objects.map((record) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), record));
|
1319
|
-
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1320
|
-
return new Page(query, meta, records);
|
1321
1865
|
}
|
1322
1866
|
}
|
1323
1867
|
_table = new WeakMap();
|
1324
|
-
_links = new WeakMap();
|
1325
1868
|
_getFetchProps = new WeakMap();
|
1869
|
+
_db = new WeakMap();
|
1326
1870
|
_cache = new WeakMap();
|
1871
|
+
_schemaTables$2 = new WeakMap();
|
1872
|
+
_trace = new WeakMap();
|
1327
1873
|
_insertRecordWithoutId = new WeakSet();
|
1328
|
-
insertRecordWithoutId_fn = async function(object) {
|
1329
|
-
const fetchProps = await __privateGet$
|
1874
|
+
insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
|
1875
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1330
1876
|
const record = transformObjectLinks(object);
|
1331
1877
|
const response = await insertRecord({
|
1332
1878
|
pathParams: {
|
1333
1879
|
workspace: "{workspaceId}",
|
1334
1880
|
dbBranchName: "{dbBranch}",
|
1335
|
-
tableName: __privateGet$
|
1881
|
+
tableName: __privateGet$4(this, _table)
|
1336
1882
|
},
|
1883
|
+
queryParams: { columns },
|
1337
1884
|
body: record,
|
1338
1885
|
...fetchProps
|
1339
1886
|
});
|
1340
|
-
const
|
1341
|
-
|
1342
|
-
throw new Error("The server failed to save the record");
|
1343
|
-
}
|
1344
|
-
return finalObject;
|
1887
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1888
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1345
1889
|
};
|
1346
1890
|
_insertRecordWithId = new WeakSet();
|
1347
|
-
insertRecordWithId_fn = async function(recordId, object) {
|
1348
|
-
const fetchProps = await __privateGet$
|
1891
|
+
insertRecordWithId_fn = async function(recordId, object, columns = ["*"]) {
|
1892
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1349
1893
|
const record = transformObjectLinks(object);
|
1350
1894
|
const response = await insertRecordWithID({
|
1351
1895
|
pathParams: {
|
1352
1896
|
workspace: "{workspaceId}",
|
1353
1897
|
dbBranchName: "{dbBranch}",
|
1354
|
-
tableName: __privateGet$
|
1898
|
+
tableName: __privateGet$4(this, _table),
|
1355
1899
|
recordId
|
1356
1900
|
},
|
1357
1901
|
body: record,
|
1358
|
-
queryParams: { createOnly: true },
|
1902
|
+
queryParams: { createOnly: true, columns },
|
1359
1903
|
...fetchProps
|
1360
1904
|
});
|
1361
|
-
const
|
1362
|
-
|
1363
|
-
throw new Error("The server failed to save the record");
|
1364
|
-
}
|
1365
|
-
return finalObject;
|
1905
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1906
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1366
1907
|
};
|
1367
1908
|
_bulkInsertTableRecords = new WeakSet();
|
1368
|
-
bulkInsertTableRecords_fn = async function(objects) {
|
1369
|
-
const fetchProps = await __privateGet$
|
1909
|
+
bulkInsertTableRecords_fn = async function(objects, columns = ["*"]) {
|
1910
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1370
1911
|
const records = objects.map((object) => transformObjectLinks(object));
|
1371
1912
|
const response = await bulkInsertTableRecords({
|
1372
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1913
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1914
|
+
queryParams: { columns },
|
1373
1915
|
body: { records },
|
1374
1916
|
...fetchProps
|
1375
1917
|
});
|
1376
|
-
|
1377
|
-
|
1378
|
-
throw new Error("The server failed to save some records");
|
1918
|
+
if (!isResponseWithRecords(response)) {
|
1919
|
+
throw new Error("Request included columns but server didn't include them");
|
1379
1920
|
}
|
1380
|
-
|
1921
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1922
|
+
return response.records?.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1381
1923
|
};
|
1382
1924
|
_updateRecordWithID = new WeakSet();
|
1383
|
-
updateRecordWithID_fn = async function(recordId, object) {
|
1384
|
-
const fetchProps = await __privateGet$
|
1925
|
+
updateRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1926
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1385
1927
|
const record = transformObjectLinks(object);
|
1386
|
-
|
1387
|
-
|
1388
|
-
|
1389
|
-
|
1390
|
-
|
1391
|
-
|
1392
|
-
|
1393
|
-
|
1394
|
-
|
1928
|
+
try {
|
1929
|
+
const response = await updateRecordWithID({
|
1930
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1931
|
+
queryParams: { columns },
|
1932
|
+
body: record,
|
1933
|
+
...fetchProps
|
1934
|
+
});
|
1935
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1936
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1937
|
+
} catch (e) {
|
1938
|
+
if (isObject(e) && e.status === 404) {
|
1939
|
+
return null;
|
1940
|
+
}
|
1941
|
+
throw e;
|
1942
|
+
}
|
1395
1943
|
};
|
1396
1944
|
_upsertRecordWithID = new WeakSet();
|
1397
|
-
upsertRecordWithID_fn = async function(recordId, object) {
|
1398
|
-
const fetchProps = await __privateGet$
|
1945
|
+
upsertRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1946
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1399
1947
|
const response = await upsertRecordWithID({
|
1400
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1948
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1949
|
+
queryParams: { columns },
|
1401
1950
|
body: object,
|
1402
1951
|
...fetchProps
|
1403
1952
|
});
|
1404
|
-
const
|
1405
|
-
|
1406
|
-
throw new Error("The server failed to save the record");
|
1407
|
-
return item;
|
1953
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1954
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1408
1955
|
};
|
1409
1956
|
_deleteRecord = new WeakSet();
|
1410
|
-
deleteRecord_fn = async function(recordId) {
|
1411
|
-
const fetchProps = await __privateGet$
|
1412
|
-
|
1413
|
-
|
1414
|
-
|
1415
|
-
|
1416
|
-
|
1417
|
-
|
1418
|
-
|
1419
|
-
|
1420
|
-
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1424
|
-
|
1425
|
-
await __privateGet$3(this, _cache).delete(key);
|
1957
|
+
deleteRecord_fn = async function(recordId, columns = ["*"]) {
|
1958
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1959
|
+
try {
|
1960
|
+
const response = await deleteRecord({
|
1961
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1962
|
+
queryParams: { columns },
|
1963
|
+
...fetchProps
|
1964
|
+
});
|
1965
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1966
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1967
|
+
} catch (e) {
|
1968
|
+
if (isObject(e) && e.status === 404) {
|
1969
|
+
return null;
|
1970
|
+
}
|
1971
|
+
throw e;
|
1426
1972
|
}
|
1427
1973
|
};
|
1428
|
-
_setCacheRecord = new WeakSet();
|
1429
|
-
setCacheRecord_fn = async function(record) {
|
1430
|
-
if (!__privateGet$3(this, _cache).cacheRecords)
|
1431
|
-
return;
|
1432
|
-
await __privateGet$3(this, _cache).set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
|
1433
|
-
};
|
1434
|
-
_getCacheRecord = new WeakSet();
|
1435
|
-
getCacheRecord_fn = async function(recordId) {
|
1436
|
-
if (!__privateGet$3(this, _cache).cacheRecords)
|
1437
|
-
return null;
|
1438
|
-
return __privateGet$3(this, _cache).get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
|
1439
|
-
};
|
1440
1974
|
_setCacheQuery = new WeakSet();
|
1441
1975
|
setCacheQuery_fn = async function(query, meta, records) {
|
1442
|
-
await __privateGet$
|
1976
|
+
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
1443
1977
|
};
|
1444
1978
|
_getCacheQuery = new WeakSet();
|
1445
1979
|
getCacheQuery_fn = async function(query) {
|
1446
|
-
const key = `query_${__privateGet$
|
1447
|
-
const result = await __privateGet$
|
1980
|
+
const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
|
1981
|
+
const result = await __privateGet$4(this, _cache).get(key);
|
1448
1982
|
if (!result)
|
1449
1983
|
return null;
|
1450
|
-
const { cache: ttl = __privateGet$
|
1451
|
-
if (
|
1452
|
-
return
|
1984
|
+
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1985
|
+
if (ttl < 0)
|
1986
|
+
return null;
|
1453
1987
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1454
1988
|
return hasExpired ? null : result;
|
1455
1989
|
};
|
1990
|
+
_getSchemaTables$1 = new WeakSet();
|
1991
|
+
getSchemaTables_fn$1 = async function() {
|
1992
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1993
|
+
return __privateGet$4(this, _schemaTables$2);
|
1994
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1995
|
+
const { schema } = await getBranchDetails({
|
1996
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1997
|
+
...fetchProps
|
1998
|
+
});
|
1999
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
2000
|
+
return schema.tables;
|
2001
|
+
};
|
1456
2002
|
const transformObjectLinks = (object) => {
|
1457
2003
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
1458
2004
|
if (key === "xata")
|
@@ -1460,47 +2006,84 @@ const transformObjectLinks = (object) => {
|
|
1460
2006
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1461
2007
|
}, {});
|
1462
2008
|
};
|
1463
|
-
const initObject = (db,
|
2009
|
+
const initObject = (db, schemaTables, table, object) => {
|
1464
2010
|
const result = {};
|
1465
|
-
|
1466
|
-
|
1467
|
-
|
1468
|
-
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
2011
|
+
const { xata, ...rest } = object ?? {};
|
2012
|
+
Object.assign(result, rest);
|
2013
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
2014
|
+
if (!columns)
|
2015
|
+
console.error(`Table ${table} not found in schema`);
|
2016
|
+
for (const column of columns ?? []) {
|
2017
|
+
const value = result[column.name];
|
2018
|
+
switch (column.type) {
|
2019
|
+
case "datetime": {
|
2020
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
2021
|
+
if (date && isNaN(date.getTime())) {
|
2022
|
+
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
2023
|
+
} else if (date) {
|
2024
|
+
result[column.name] = date;
|
2025
|
+
}
|
2026
|
+
break;
|
2027
|
+
}
|
2028
|
+
case "link": {
|
2029
|
+
const linkTable = column.link?.table;
|
2030
|
+
if (!linkTable) {
|
2031
|
+
console.error(`Failed to parse link for field ${column.name}`);
|
2032
|
+
} else if (isObject(value)) {
|
2033
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
2034
|
+
} else {
|
2035
|
+
result[column.name] = null;
|
2036
|
+
}
|
2037
|
+
break;
|
2038
|
+
}
|
2039
|
+
default:
|
2040
|
+
result[column.name] = value ?? null;
|
2041
|
+
if (column.notNull === true && value === null) {
|
2042
|
+
console.error(`Parse error, column ${column.name} is non nullable and value resolves null`);
|
2043
|
+
}
|
2044
|
+
break;
|
1472
2045
|
}
|
1473
2046
|
}
|
1474
|
-
result.read = function() {
|
1475
|
-
return db[table].read(result["id"]);
|
2047
|
+
result.read = function(columns2) {
|
2048
|
+
return db[table].read(result["id"], columns2);
|
1476
2049
|
};
|
1477
|
-
result.update = function(data) {
|
1478
|
-
return db[table].update(result["id"], data);
|
2050
|
+
result.update = function(data, columns2) {
|
2051
|
+
return db[table].update(result["id"], data, columns2);
|
1479
2052
|
};
|
1480
2053
|
result.delete = function() {
|
1481
2054
|
return db[table].delete(result["id"]);
|
1482
2055
|
};
|
1483
|
-
|
2056
|
+
result.getMetadata = function() {
|
2057
|
+
return xata;
|
2058
|
+
};
|
2059
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1484
2060
|
Object.defineProperty(result, prop, { enumerable: false });
|
1485
2061
|
}
|
1486
2062
|
Object.freeze(result);
|
1487
2063
|
return result;
|
1488
2064
|
};
|
1489
|
-
function
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
if (
|
1494
|
-
return
|
1495
|
-
|
1496
|
-
|
2065
|
+
function isResponseWithRecords(value) {
|
2066
|
+
return isObject(value) && Array.isArray(value.records);
|
2067
|
+
}
|
2068
|
+
function extractId(value) {
|
2069
|
+
if (isString(value))
|
2070
|
+
return value;
|
2071
|
+
if (isObject(value) && isString(value.id))
|
2072
|
+
return value.id;
|
2073
|
+
return void 0;
|
2074
|
+
}
|
2075
|
+
function cleanFilter(filter) {
|
2076
|
+
if (!filter)
|
2077
|
+
return void 0;
|
2078
|
+
const values = Object.values(filter).filter(Boolean).filter((value) => Array.isArray(value) ? value.length > 0 : true);
|
2079
|
+
return values.length > 0 ? filter : void 0;
|
1497
2080
|
}
|
1498
2081
|
|
1499
2082
|
var __accessCheck$3 = (obj, member, msg) => {
|
1500
2083
|
if (!member.has(obj))
|
1501
2084
|
throw TypeError("Cannot " + msg);
|
1502
2085
|
};
|
1503
|
-
var __privateGet$
|
2086
|
+
var __privateGet$3 = (obj, member, getter) => {
|
1504
2087
|
__accessCheck$3(obj, member, "read from private field");
|
1505
2088
|
return getter ? getter.call(obj) : member.get(obj);
|
1506
2089
|
};
|
@@ -1509,7 +2092,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1509
2092
|
throw TypeError("Cannot add the same private member more than once");
|
1510
2093
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1511
2094
|
};
|
1512
|
-
var __privateSet$
|
2095
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1513
2096
|
__accessCheck$3(obj, member, "write to private field");
|
1514
2097
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1515
2098
|
return value;
|
@@ -1518,46 +2101,52 @@ var _map;
|
|
1518
2101
|
class SimpleCache {
|
1519
2102
|
constructor(options = {}) {
|
1520
2103
|
__privateAdd$3(this, _map, void 0);
|
1521
|
-
__privateSet$
|
2104
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1522
2105
|
this.capacity = options.max ?? 500;
|
1523
|
-
this.cacheRecords = options.cacheRecords ?? true;
|
1524
2106
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1525
2107
|
}
|
1526
2108
|
async getAll() {
|
1527
|
-
return Object.fromEntries(__privateGet$
|
2109
|
+
return Object.fromEntries(__privateGet$3(this, _map));
|
1528
2110
|
}
|
1529
2111
|
async get(key) {
|
1530
|
-
return __privateGet$
|
2112
|
+
return __privateGet$3(this, _map).get(key) ?? null;
|
1531
2113
|
}
|
1532
2114
|
async set(key, value) {
|
1533
2115
|
await this.delete(key);
|
1534
|
-
__privateGet$
|
1535
|
-
if (__privateGet$
|
1536
|
-
const leastRecentlyUsed = __privateGet$
|
2116
|
+
__privateGet$3(this, _map).set(key, value);
|
2117
|
+
if (__privateGet$3(this, _map).size > this.capacity) {
|
2118
|
+
const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
|
1537
2119
|
await this.delete(leastRecentlyUsed);
|
1538
2120
|
}
|
1539
2121
|
}
|
1540
2122
|
async delete(key) {
|
1541
|
-
__privateGet$
|
2123
|
+
__privateGet$3(this, _map).delete(key);
|
1542
2124
|
}
|
1543
2125
|
async clear() {
|
1544
|
-
return __privateGet$
|
2126
|
+
return __privateGet$3(this, _map).clear();
|
1545
2127
|
}
|
1546
2128
|
}
|
1547
2129
|
_map = new WeakMap();
|
1548
2130
|
|
1549
|
-
const
|
1550
|
-
const
|
1551
|
-
const
|
1552
|
-
const
|
1553
|
-
const
|
1554
|
-
const
|
2131
|
+
const greaterThan = (value) => ({ $gt: value });
|
2132
|
+
const gt = greaterThan;
|
2133
|
+
const greaterThanEquals = (value) => ({ $ge: value });
|
2134
|
+
const greaterEquals = greaterThanEquals;
|
2135
|
+
const gte = greaterThanEquals;
|
2136
|
+
const ge = greaterThanEquals;
|
2137
|
+
const lessThan = (value) => ({ $lt: value });
|
2138
|
+
const lt = lessThan;
|
2139
|
+
const lessThanEquals = (value) => ({ $le: value });
|
2140
|
+
const lessEquals = lessThanEquals;
|
2141
|
+
const lte = lessThanEquals;
|
2142
|
+
const le = lessThanEquals;
|
1555
2143
|
const exists = (column) => ({ $exists: column });
|
1556
2144
|
const notExists = (column) => ({ $notExists: column });
|
1557
2145
|
const startsWith = (value) => ({ $startsWith: value });
|
1558
2146
|
const endsWith = (value) => ({ $endsWith: value });
|
1559
2147
|
const pattern = (value) => ({ $pattern: value });
|
1560
2148
|
const is = (value) => ({ $is: value });
|
2149
|
+
const equals = is;
|
1561
2150
|
const isNot = (value) => ({ $isNot: value });
|
1562
2151
|
const contains = (value) => ({ $contains: value });
|
1563
2152
|
const includes = (value) => ({ $includes: value });
|
@@ -1569,7 +2158,7 @@ var __accessCheck$2 = (obj, member, msg) => {
|
|
1569
2158
|
if (!member.has(obj))
|
1570
2159
|
throw TypeError("Cannot " + msg);
|
1571
2160
|
};
|
1572
|
-
var __privateGet$
|
2161
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1573
2162
|
__accessCheck$2(obj, member, "read from private field");
|
1574
2163
|
return getter ? getter.call(obj) : member.get(obj);
|
1575
2164
|
};
|
@@ -1578,130 +2167,178 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1578
2167
|
throw TypeError("Cannot add the same private member more than once");
|
1579
2168
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1580
2169
|
};
|
1581
|
-
var
|
2170
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
2171
|
+
__accessCheck$2(obj, member, "write to private field");
|
2172
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
2173
|
+
return value;
|
2174
|
+
};
|
2175
|
+
var _tables, _schemaTables$1;
|
1582
2176
|
class SchemaPlugin extends XataPlugin {
|
1583
|
-
constructor(
|
2177
|
+
constructor(schemaTables) {
|
1584
2178
|
super();
|
1585
|
-
this.links = links;
|
1586
|
-
this.tableNames = tableNames;
|
1587
2179
|
__privateAdd$2(this, _tables, {});
|
2180
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
2181
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1588
2182
|
}
|
1589
2183
|
build(pluginOptions) {
|
1590
|
-
const
|
1591
|
-
|
1592
|
-
|
1593
|
-
|
1594
|
-
|
1595
|
-
|
1596
|
-
__privateGet$
|
2184
|
+
const db = new Proxy(
|
2185
|
+
{},
|
2186
|
+
{
|
2187
|
+
get: (_target, table) => {
|
2188
|
+
if (!isString(table))
|
2189
|
+
throw new Error("Invalid table name");
|
2190
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
2191
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
2192
|
+
}
|
2193
|
+
return __privateGet$2(this, _tables)[table];
|
1597
2194
|
}
|
1598
|
-
return __privateGet$1(this, _tables)[table];
|
1599
2195
|
}
|
1600
|
-
|
1601
|
-
|
1602
|
-
|
2196
|
+
);
|
2197
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
2198
|
+
for (const table of tableNames) {
|
2199
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1603
2200
|
}
|
1604
2201
|
return db;
|
1605
2202
|
}
|
1606
2203
|
}
|
1607
2204
|
_tables = new WeakMap();
|
2205
|
+
_schemaTables$1 = new WeakMap();
|
1608
2206
|
|
1609
2207
|
var __accessCheck$1 = (obj, member, msg) => {
|
1610
2208
|
if (!member.has(obj))
|
1611
2209
|
throw TypeError("Cannot " + msg);
|
1612
2210
|
};
|
2211
|
+
var __privateGet$1 = (obj, member, getter) => {
|
2212
|
+
__accessCheck$1(obj, member, "read from private field");
|
2213
|
+
return getter ? getter.call(obj) : member.get(obj);
|
2214
|
+
};
|
1613
2215
|
var __privateAdd$1 = (obj, member, value) => {
|
1614
2216
|
if (member.has(obj))
|
1615
2217
|
throw TypeError("Cannot add the same private member more than once");
|
1616
2218
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1617
2219
|
};
|
2220
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
2221
|
+
__accessCheck$1(obj, member, "write to private field");
|
2222
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
2223
|
+
return value;
|
2224
|
+
};
|
1618
2225
|
var __privateMethod$1 = (obj, member, method) => {
|
1619
2226
|
__accessCheck$1(obj, member, "access private method");
|
1620
2227
|
return method;
|
1621
2228
|
};
|
1622
|
-
var _search, search_fn;
|
2229
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1623
2230
|
class SearchPlugin extends XataPlugin {
|
1624
|
-
constructor(db,
|
2231
|
+
constructor(db, schemaTables) {
|
1625
2232
|
super();
|
1626
2233
|
this.db = db;
|
1627
|
-
this.links = links;
|
1628
2234
|
__privateAdd$1(this, _search);
|
2235
|
+
__privateAdd$1(this, _getSchemaTables);
|
2236
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
2237
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1629
2238
|
}
|
1630
2239
|
build({ getFetchProps }) {
|
1631
2240
|
return {
|
1632
2241
|
all: async (query, options = {}) => {
|
1633
2242
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
2243
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1634
2244
|
return records.map((record) => {
|
1635
2245
|
const { table = "orphan" } = record.xata;
|
1636
|
-
return { table, record: initObject(this.db,
|
2246
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1637
2247
|
});
|
1638
2248
|
},
|
1639
2249
|
byTable: async (query, options = {}) => {
|
1640
2250
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
2251
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1641
2252
|
return records.reduce((acc, record) => {
|
1642
2253
|
const { table = "orphan" } = record.xata;
|
1643
2254
|
const items = acc[table] ?? [];
|
1644
|
-
const item = initObject(this.db,
|
2255
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1645
2256
|
return { ...acc, [table]: [...items, item] };
|
1646
2257
|
}, {});
|
1647
2258
|
}
|
1648
2259
|
};
|
1649
2260
|
}
|
1650
2261
|
}
|
2262
|
+
_schemaTables = new WeakMap();
|
1651
2263
|
_search = new WeakSet();
|
1652
2264
|
search_fn = async function(query, options, getFetchProps) {
|
1653
2265
|
const fetchProps = await getFetchProps();
|
1654
|
-
const { tables, fuzziness } = options ?? {};
|
2266
|
+
const { tables, fuzziness, highlight, prefix } = options ?? {};
|
1655
2267
|
const { records } = await searchBranch({
|
1656
2268
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1657
|
-
body: { tables, query, fuzziness },
|
2269
|
+
body: { tables, query, fuzziness, prefix, highlight },
|
1658
2270
|
...fetchProps
|
1659
2271
|
});
|
1660
2272
|
return records;
|
1661
2273
|
};
|
2274
|
+
_getSchemaTables = new WeakSet();
|
2275
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
2276
|
+
if (__privateGet$1(this, _schemaTables))
|
2277
|
+
return __privateGet$1(this, _schemaTables);
|
2278
|
+
const fetchProps = await getFetchProps();
|
2279
|
+
const { schema } = await getBranchDetails({
|
2280
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
2281
|
+
...fetchProps
|
2282
|
+
});
|
2283
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
2284
|
+
return schema.tables;
|
2285
|
+
};
|
1662
2286
|
|
1663
2287
|
const isBranchStrategyBuilder = (strategy) => {
|
1664
2288
|
return typeof strategy === "function";
|
1665
2289
|
};
|
1666
2290
|
|
1667
|
-
const envBranchNames = [
|
1668
|
-
"XATA_BRANCH",
|
1669
|
-
"VERCEL_GIT_COMMIT_REF",
|
1670
|
-
"CF_PAGES_BRANCH",
|
1671
|
-
"BRANCH"
|
1672
|
-
];
|
1673
|
-
const defaultBranch = "main";
|
1674
2291
|
async function getCurrentBranchName(options) {
|
1675
|
-
const
|
1676
|
-
if (
|
1677
|
-
|
1678
|
-
|
1679
|
-
|
1680
|
-
|
1681
|
-
|
1682
|
-
|
1683
|
-
|
1684
|
-
return defaultBranch;
|
2292
|
+
const { branch, envBranch } = getEnvironment();
|
2293
|
+
if (branch) {
|
2294
|
+
const details = await getDatabaseBranch(branch, options);
|
2295
|
+
if (details)
|
2296
|
+
return branch;
|
2297
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
2298
|
+
}
|
2299
|
+
const gitBranch = envBranch || await getGitBranch();
|
2300
|
+
return resolveXataBranch(gitBranch, options);
|
1685
2301
|
}
|
1686
2302
|
async function getCurrentBranchDetails(options) {
|
1687
|
-
const
|
1688
|
-
|
1689
|
-
|
1690
|
-
|
1691
|
-
|
1692
|
-
|
1693
|
-
|
1694
|
-
|
1695
|
-
|
1696
|
-
|
2303
|
+
const branch = await getCurrentBranchName(options);
|
2304
|
+
return getDatabaseBranch(branch, options);
|
2305
|
+
}
|
2306
|
+
async function resolveXataBranch(gitBranch, options) {
|
2307
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
2308
|
+
const apiKey = options?.apiKey || getAPIKey();
|
2309
|
+
if (!databaseURL)
|
2310
|
+
throw new Error(
|
2311
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
2312
|
+
);
|
2313
|
+
if (!apiKey)
|
2314
|
+
throw new Error(
|
2315
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
2316
|
+
);
|
2317
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
2318
|
+
const [workspace] = host.split(".");
|
2319
|
+
const { fallbackBranch } = getEnvironment();
|
2320
|
+
const { branch } = await resolveBranch({
|
2321
|
+
apiKey,
|
2322
|
+
apiUrl: databaseURL,
|
2323
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
2324
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
2325
|
+
pathParams: { dbName, workspace },
|
2326
|
+
queryParams: { gitBranch, fallbackBranch },
|
2327
|
+
trace: defaultTrace
|
2328
|
+
});
|
2329
|
+
return branch;
|
1697
2330
|
}
|
1698
2331
|
async function getDatabaseBranch(branch, options) {
|
1699
2332
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1700
2333
|
const apiKey = options?.apiKey || getAPIKey();
|
1701
2334
|
if (!databaseURL)
|
1702
|
-
throw new Error(
|
2335
|
+
throw new Error(
|
2336
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
2337
|
+
);
|
1703
2338
|
if (!apiKey)
|
1704
|
-
throw new Error(
|
2339
|
+
throw new Error(
|
2340
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
2341
|
+
);
|
1705
2342
|
const [protocol, , host, , database] = databaseURL.split("/");
|
1706
2343
|
const [workspace] = host.split(".");
|
1707
2344
|
const dbBranchName = `${database}:${branch}`;
|
@@ -1711,10 +2348,8 @@ async function getDatabaseBranch(branch, options) {
|
|
1711
2348
|
apiUrl: databaseURL,
|
1712
2349
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1713
2350
|
workspacesApiUrl: `${protocol}//${host}`,
|
1714
|
-
pathParams: {
|
1715
|
-
|
1716
|
-
workspace
|
1717
|
-
}
|
2351
|
+
pathParams: { dbBranchName, workspace },
|
2352
|
+
trace: defaultTrace
|
1718
2353
|
});
|
1719
2354
|
} catch (err) {
|
1720
2355
|
if (isObject(err) && err.status === 404)
|
@@ -1722,21 +2357,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1722
2357
|
throw err;
|
1723
2358
|
}
|
1724
2359
|
}
|
1725
|
-
function getBranchByEnvVariable() {
|
1726
|
-
for (const name of envBranchNames) {
|
1727
|
-
const value = getEnvVariable(name);
|
1728
|
-
if (value) {
|
1729
|
-
return value;
|
1730
|
-
}
|
1731
|
-
}
|
1732
|
-
try {
|
1733
|
-
return XATA_BRANCH;
|
1734
|
-
} catch (err) {
|
1735
|
-
}
|
1736
|
-
}
|
1737
2360
|
function getDatabaseURL() {
|
1738
2361
|
try {
|
1739
|
-
|
2362
|
+
const { databaseURL } = getEnvironment();
|
2363
|
+
return databaseURL;
|
1740
2364
|
} catch (err) {
|
1741
2365
|
return void 0;
|
1742
2366
|
}
|
@@ -1765,24 +2389,27 @@ var __privateMethod = (obj, member, method) => {
|
|
1765
2389
|
return method;
|
1766
2390
|
};
|
1767
2391
|
const buildClient = (plugins) => {
|
1768
|
-
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
2392
|
+
var _branch, _options, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1769
2393
|
return _a = class {
|
1770
|
-
constructor(options = {},
|
2394
|
+
constructor(options = {}, schemaTables) {
|
1771
2395
|
__privateAdd(this, _parseOptions);
|
1772
2396
|
__privateAdd(this, _getFetchProps);
|
1773
2397
|
__privateAdd(this, _evaluateBranch);
|
1774
2398
|
__privateAdd(this, _branch, void 0);
|
2399
|
+
__privateAdd(this, _options, void 0);
|
1775
2400
|
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
2401
|
+
__privateSet(this, _options, safeOptions);
|
1776
2402
|
const pluginOptions = {
|
1777
2403
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1778
|
-
cache: safeOptions.cache
|
2404
|
+
cache: safeOptions.cache,
|
2405
|
+
trace: safeOptions.trace
|
1779
2406
|
};
|
1780
|
-
const db = new SchemaPlugin(
|
1781
|
-
const search = new SearchPlugin(db,
|
2407
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2408
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1782
2409
|
this.db = db;
|
1783
2410
|
this.search = search;
|
1784
2411
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1785
|
-
if (
|
2412
|
+
if (namespace === void 0)
|
1786
2413
|
continue;
|
1787
2414
|
const result = namespace.build(pluginOptions);
|
1788
2415
|
if (result instanceof Promise) {
|
@@ -1794,22 +2421,26 @@ const buildClient = (plugins) => {
|
|
1794
2421
|
}
|
1795
2422
|
}
|
1796
2423
|
}
|
1797
|
-
|
2424
|
+
async getConfig() {
|
2425
|
+
const databaseURL = __privateGet(this, _options).databaseURL;
|
2426
|
+
const branch = await __privateGet(this, _options).branch();
|
2427
|
+
return { databaseURL, branch };
|
2428
|
+
}
|
2429
|
+
}, _branch = new WeakMap(), _options = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
|
1798
2430
|
const fetch = getFetchImplementation(options?.fetch);
|
1799
2431
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1800
2432
|
const apiKey = options?.apiKey || getAPIKey();
|
1801
|
-
const cache = options?.cache ?? new SimpleCache({
|
1802
|
-
const
|
1803
|
-
|
1804
|
-
|
2433
|
+
const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
|
2434
|
+
const trace = options?.trace ?? defaultTrace;
|
2435
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
2436
|
+
if (!apiKey) {
|
2437
|
+
throw new Error("Option apiKey is required");
|
1805
2438
|
}
|
1806
|
-
|
1807
|
-
|
1808
|
-
|
1809
|
-
apiKey,
|
1810
|
-
|
1811
|
-
branch
|
1812
|
-
}) {
|
2439
|
+
if (!databaseURL) {
|
2440
|
+
throw new Error("Option databaseURL is required");
|
2441
|
+
}
|
2442
|
+
return { fetch, databaseURL, apiKey, branch, cache, trace };
|
2443
|
+
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({ fetch, apiKey, databaseURL, branch, trace }) {
|
1813
2444
|
const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
|
1814
2445
|
if (!branchValue)
|
1815
2446
|
throw new Error("Unable to resolve branch value");
|
@@ -1819,14 +2450,15 @@ const buildClient = (plugins) => {
|
|
1819
2450
|
apiUrl: "",
|
1820
2451
|
workspacesApiUrl: (path, params) => {
|
1821
2452
|
const hasBranch = params.dbBranchName ?? params.branch;
|
1822
|
-
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : "");
|
2453
|
+
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch !== void 0 ? `:${branchValue}` : "");
|
1823
2454
|
return databaseURL + newPath;
|
1824
|
-
}
|
2455
|
+
},
|
2456
|
+
trace
|
1825
2457
|
};
|
1826
2458
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1827
2459
|
if (__privateGet(this, _branch))
|
1828
2460
|
return __privateGet(this, _branch);
|
1829
|
-
if (
|
2461
|
+
if (param === void 0)
|
1830
2462
|
return void 0;
|
1831
2463
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1832
2464
|
const evaluateBranch = async (strategy) => {
|
@@ -1844,6 +2476,88 @@ const buildClient = (plugins) => {
|
|
1844
2476
|
class BaseClient extends buildClient() {
|
1845
2477
|
}
|
1846
2478
|
|
2479
|
+
const META = "__";
|
2480
|
+
const VALUE = "___";
|
2481
|
+
class Serializer {
|
2482
|
+
constructor() {
|
2483
|
+
this.classes = {};
|
2484
|
+
}
|
2485
|
+
add(clazz) {
|
2486
|
+
this.classes[clazz.name] = clazz;
|
2487
|
+
}
|
2488
|
+
toJSON(data) {
|
2489
|
+
function visit(obj) {
|
2490
|
+
if (Array.isArray(obj))
|
2491
|
+
return obj.map(visit);
|
2492
|
+
const type = typeof obj;
|
2493
|
+
if (type === "undefined")
|
2494
|
+
return { [META]: "undefined" };
|
2495
|
+
if (type === "bigint")
|
2496
|
+
return { [META]: "bigint", [VALUE]: obj.toString() };
|
2497
|
+
if (obj === null || type !== "object")
|
2498
|
+
return obj;
|
2499
|
+
const constructor = obj.constructor;
|
2500
|
+
const o = { [META]: constructor.name };
|
2501
|
+
for (const [key, value] of Object.entries(obj)) {
|
2502
|
+
o[key] = visit(value);
|
2503
|
+
}
|
2504
|
+
if (constructor === Date)
|
2505
|
+
o[VALUE] = obj.toISOString();
|
2506
|
+
if (constructor === Map)
|
2507
|
+
o[VALUE] = Object.fromEntries(obj);
|
2508
|
+
if (constructor === Set)
|
2509
|
+
o[VALUE] = [...obj];
|
2510
|
+
return o;
|
2511
|
+
}
|
2512
|
+
return JSON.stringify(visit(data));
|
2513
|
+
}
|
2514
|
+
fromJSON(json) {
|
2515
|
+
return JSON.parse(json, (key, value) => {
|
2516
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
2517
|
+
const { [META]: clazz, [VALUE]: val, ...rest } = value;
|
2518
|
+
const constructor = this.classes[clazz];
|
2519
|
+
if (constructor) {
|
2520
|
+
return Object.assign(Object.create(constructor.prototype), rest);
|
2521
|
+
}
|
2522
|
+
if (clazz === "Date")
|
2523
|
+
return new Date(val);
|
2524
|
+
if (clazz === "Set")
|
2525
|
+
return new Set(val);
|
2526
|
+
if (clazz === "Map")
|
2527
|
+
return new Map(Object.entries(val));
|
2528
|
+
if (clazz === "bigint")
|
2529
|
+
return BigInt(val);
|
2530
|
+
if (clazz === "undefined")
|
2531
|
+
return void 0;
|
2532
|
+
return rest;
|
2533
|
+
}
|
2534
|
+
return value;
|
2535
|
+
});
|
2536
|
+
}
|
2537
|
+
}
|
2538
|
+
const defaultSerializer = new Serializer();
|
2539
|
+
const serialize = (data) => {
|
2540
|
+
return defaultSerializer.toJSON(data);
|
2541
|
+
};
|
2542
|
+
const deserialize = (json) => {
|
2543
|
+
return defaultSerializer.fromJSON(json);
|
2544
|
+
};
|
2545
|
+
|
2546
|
+
function buildWorkerRunner(config) {
|
2547
|
+
return function xataWorker(name, _worker) {
|
2548
|
+
return async (...args) => {
|
2549
|
+
const url = process.env.NODE_ENV === "development" ? `http://localhost:64749/${name}` : `https://dispatcher.xata.workers.dev/${config.workspace}/${config.worker}/${name}`;
|
2550
|
+
const result = await fetch(url, {
|
2551
|
+
method: "POST",
|
2552
|
+
headers: { "Content-Type": "application/json" },
|
2553
|
+
body: serialize({ args })
|
2554
|
+
});
|
2555
|
+
const text = await result.text();
|
2556
|
+
return deserialize(text);
|
2557
|
+
};
|
2558
|
+
};
|
2559
|
+
}
|
2560
|
+
|
1847
2561
|
class XataError extends Error {
|
1848
2562
|
constructor(message, status) {
|
1849
2563
|
super(message);
|
@@ -1851,5 +2565,5 @@ class XataError extends Error {
|
|
1851
2565
|
}
|
1852
2566
|
}
|
1853
2567
|
|
1854
|
-
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
2568
|
+
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, applyBranchSchemaEdit, buildClient, buildWorkerRunner, bulkInsertTableRecords, cancelWorkspaceMemberInvite, compareBranchSchemas, compareBranchWithUserSchema, compareMigrationRequest, contains, createBranch, createDatabase, createMigrationRequest, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, deserialize, endsWith, equals, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchSchemaHistory, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseMetadata, getDatabaseURL, getGitBranchesMapping, getMigrationRequest, getMigrationRequestIsMerged, 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, listMigrationRequests, listMigrationRequestsCommits, lt, lte, mergeMigrationRequest, notExists, operationsByTag, pattern, previewBranchSchemaEdit, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, serialize, setTableSchema, startsWith, summarizeTable, updateBranchMetadata, updateBranchSchema, updateColumn, updateDatabaseMetadata, updateMigrationRequest, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberInvite, updateWorkspaceMemberRole, upsertRecordWithID };
|
1855
2569
|
//# sourceMappingURL=index.mjs.map
|