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