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