@xata.io/client 0.0.0-alpha.vf6f2567 → 0.0.0-alpha.vf79e7d8
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 +208 -0
- package/README.md +273 -1
- package/Usage.md +449 -0
- package/dist/index.cjs +1003 -485
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1206 -219
- package/dist/index.mjs +966 -486
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -4
- package/tsconfig.json +1 -0
package/dist/index.mjs
CHANGED
@@ -1,3 +1,27 @@
|
|
1
|
+
const defaultTrace = async (_name, fn, _options) => {
|
2
|
+
return await fn({
|
3
|
+
setAttributes: () => {
|
4
|
+
return;
|
5
|
+
},
|
6
|
+
onError: () => {
|
7
|
+
return;
|
8
|
+
}
|
9
|
+
});
|
10
|
+
};
|
11
|
+
const TraceAttributes = {
|
12
|
+
VERSION: "xata.sdk.version",
|
13
|
+
TABLE: "xata.table",
|
14
|
+
HTTP_REQUEST_ID: "http.request_id",
|
15
|
+
HTTP_STATUS_CODE: "http.status_code",
|
16
|
+
HTTP_HOST: "http.host",
|
17
|
+
HTTP_SCHEME: "http.scheme",
|
18
|
+
HTTP_USER_AGENT: "http.user_agent",
|
19
|
+
HTTP_METHOD: "http.method",
|
20
|
+
HTTP_URL: "http.url",
|
21
|
+
HTTP_ROUTE: "http.route",
|
22
|
+
HTTP_TARGET: "http.target"
|
23
|
+
};
|
24
|
+
|
1
25
|
function notEmpty(value) {
|
2
26
|
return value !== null && value !== void 0;
|
3
27
|
}
|
@@ -7,43 +31,101 @@ function compact(arr) {
|
|
7
31
|
function isObject(value) {
|
8
32
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
9
33
|
}
|
34
|
+
function isDefined(value) {
|
35
|
+
return value !== null && value !== void 0;
|
36
|
+
}
|
10
37
|
function isString(value) {
|
11
|
-
return value
|
38
|
+
return isDefined(value) && typeof value === "string";
|
39
|
+
}
|
40
|
+
function isStringArray(value) {
|
41
|
+
return isDefined(value) && Array.isArray(value) && value.every(isString);
|
12
42
|
}
|
13
43
|
function toBase64(value) {
|
14
44
|
try {
|
15
45
|
return btoa(value);
|
16
46
|
} catch (err) {
|
17
|
-
|
47
|
+
const buf = Buffer;
|
48
|
+
return buf.from(value).toString("base64");
|
18
49
|
}
|
19
50
|
}
|
20
51
|
|
21
|
-
function
|
52
|
+
function getEnvironment() {
|
22
53
|
try {
|
23
|
-
if (isObject(process) &&
|
24
|
-
return
|
54
|
+
if (isObject(process) && isObject(process.env)) {
|
55
|
+
return {
|
56
|
+
apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
|
57
|
+
databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
|
58
|
+
branch: process.env.XATA_BRANCH ?? getGlobalBranch(),
|
59
|
+
envBranch: process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH,
|
60
|
+
fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
|
61
|
+
};
|
25
62
|
}
|
26
63
|
} catch (err) {
|
27
64
|
}
|
28
65
|
try {
|
29
|
-
if (isObject(Deno) &&
|
30
|
-
return
|
66
|
+
if (isObject(Deno) && isObject(Deno.env)) {
|
67
|
+
return {
|
68
|
+
apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
|
69
|
+
databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
|
70
|
+
branch: Deno.env.get("XATA_BRANCH") ?? getGlobalBranch(),
|
71
|
+
envBranch: Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH"),
|
72
|
+
fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
|
73
|
+
};
|
31
74
|
}
|
32
75
|
} catch (err) {
|
33
76
|
}
|
77
|
+
return {
|
78
|
+
apiKey: getGlobalApiKey(),
|
79
|
+
databaseURL: getGlobalDatabaseURL(),
|
80
|
+
branch: getGlobalBranch(),
|
81
|
+
envBranch: void 0,
|
82
|
+
fallbackBranch: getGlobalFallbackBranch()
|
83
|
+
};
|
84
|
+
}
|
85
|
+
function getGlobalApiKey() {
|
86
|
+
try {
|
87
|
+
return XATA_API_KEY;
|
88
|
+
} catch (err) {
|
89
|
+
return void 0;
|
90
|
+
}
|
91
|
+
}
|
92
|
+
function getGlobalDatabaseURL() {
|
93
|
+
try {
|
94
|
+
return XATA_DATABASE_URL;
|
95
|
+
} catch (err) {
|
96
|
+
return void 0;
|
97
|
+
}
|
98
|
+
}
|
99
|
+
function getGlobalBranch() {
|
100
|
+
try {
|
101
|
+
return XATA_BRANCH;
|
102
|
+
} catch (err) {
|
103
|
+
return void 0;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
function getGlobalFallbackBranch() {
|
107
|
+
try {
|
108
|
+
return XATA_FALLBACK_BRANCH;
|
109
|
+
} catch (err) {
|
110
|
+
return void 0;
|
111
|
+
}
|
34
112
|
}
|
35
113
|
async function getGitBranch() {
|
114
|
+
const cmd = ["git", "branch", "--show-current"];
|
115
|
+
const fullCmd = cmd.join(" ");
|
116
|
+
const nodeModule = ["child", "process"].join("_");
|
117
|
+
const execOptions = { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] };
|
36
118
|
try {
|
37
|
-
|
119
|
+
if (typeof require === "function") {
|
120
|
+
return require(nodeModule).execSync(fullCmd, execOptions).trim();
|
121
|
+
}
|
122
|
+
const { execSync } = await import(nodeModule);
|
123
|
+
return execSync(fullCmd, execOptions).toString().trim();
|
38
124
|
} catch (err) {
|
39
125
|
}
|
40
126
|
try {
|
41
127
|
if (isObject(Deno)) {
|
42
|
-
const process2 = Deno.run({
|
43
|
-
cmd: ["git", "branch", "--show-current"],
|
44
|
-
stdout: "piped",
|
45
|
-
stderr: "piped"
|
46
|
-
});
|
128
|
+
const process2 = Deno.run({ cmd, stdout: "piped", stderr: "null" });
|
47
129
|
return new TextDecoder().decode(await process2.output()).trim();
|
48
130
|
}
|
49
131
|
} catch (err) {
|
@@ -52,7 +134,8 @@ async function getGitBranch() {
|
|
52
134
|
|
53
135
|
function getAPIKey() {
|
54
136
|
try {
|
55
|
-
|
137
|
+
const { apiKey } = getEnvironment();
|
138
|
+
return apiKey;
|
56
139
|
} catch (err) {
|
57
140
|
return void 0;
|
58
141
|
}
|
@@ -62,21 +145,35 @@ function getFetchImplementation(userFetch) {
|
|
62
145
|
const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
|
63
146
|
const fetchImpl = userFetch ?? globalFetch;
|
64
147
|
if (!fetchImpl) {
|
65
|
-
throw new Error(
|
148
|
+
throw new Error(
|
149
|
+
`Couldn't find \`fetch\`. Install a fetch implementation such as \`node-fetch\` and pass it explicitly.`
|
150
|
+
);
|
66
151
|
}
|
67
152
|
return fetchImpl;
|
68
153
|
}
|
69
154
|
|
70
|
-
|
71
|
-
|
155
|
+
const VERSION = "0.0.0-alpha.vf79e7d8";
|
156
|
+
|
157
|
+
class ErrorWithCause extends Error {
|
158
|
+
constructor(message, options) {
|
159
|
+
super(message, options);
|
160
|
+
}
|
161
|
+
}
|
162
|
+
class FetcherError extends ErrorWithCause {
|
163
|
+
constructor(status, data, requestId) {
|
72
164
|
super(getMessage(data));
|
73
165
|
this.status = status;
|
74
166
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
167
|
+
this.requestId = requestId;
|
75
168
|
if (data instanceof Error) {
|
76
169
|
this.stack = data.stack;
|
77
170
|
this.cause = data.cause;
|
78
171
|
}
|
79
172
|
}
|
173
|
+
toString() {
|
174
|
+
const error = super.toString();
|
175
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
176
|
+
}
|
80
177
|
}
|
81
178
|
function isBulkError(error) {
|
82
179
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -99,7 +196,12 @@ function getMessage(data) {
|
|
99
196
|
}
|
100
197
|
|
101
198
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
102
|
-
const
|
199
|
+
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
200
|
+
if (value === void 0 || value === null)
|
201
|
+
return acc;
|
202
|
+
return { ...acc, [key]: value };
|
203
|
+
}, {});
|
204
|
+
const query = new URLSearchParams(cleanQueryParams).toString();
|
103
205
|
const queryString = query.length > 0 ? `?${query}` : "";
|
104
206
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
105
207
|
};
|
@@ -129,32 +231,62 @@ async function fetch$1({
|
|
129
231
|
fetchImpl,
|
130
232
|
apiKey,
|
131
233
|
apiUrl,
|
132
|
-
workspacesApiUrl
|
234
|
+
workspacesApiUrl,
|
235
|
+
trace
|
133
236
|
}) {
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
237
|
+
return trace(
|
238
|
+
`${method.toUpperCase()} ${path}`,
|
239
|
+
async ({ setAttributes, onError }) => {
|
240
|
+
const baseUrl = buildBaseUrl({ path, workspacesApiUrl, pathParams, apiUrl });
|
241
|
+
const fullUrl = resolveUrl(baseUrl, queryParams, pathParams);
|
242
|
+
const url = fullUrl.includes("localhost") ? fullUrl.replace(/^[^.]+\./, "http://") : fullUrl;
|
243
|
+
setAttributes({
|
244
|
+
[TraceAttributes.HTTP_URL]: url,
|
245
|
+
[TraceAttributes.HTTP_TARGET]: resolveUrl(path, queryParams, pathParams)
|
246
|
+
});
|
247
|
+
const response = await fetchImpl(url, {
|
248
|
+
method: method.toUpperCase(),
|
249
|
+
body: body ? JSON.stringify(body) : void 0,
|
250
|
+
headers: {
|
251
|
+
"Content-Type": "application/json",
|
252
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
253
|
+
...headers,
|
254
|
+
...hostHeader(fullUrl),
|
255
|
+
Authorization: `Bearer ${apiKey}`
|
256
|
+
}
|
257
|
+
});
|
258
|
+
if (response.status === 204) {
|
259
|
+
return {};
|
260
|
+
}
|
261
|
+
const { host, protocol } = parseUrl(response.url);
|
262
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
263
|
+
setAttributes({
|
264
|
+
[TraceAttributes.HTTP_REQUEST_ID]: requestId,
|
265
|
+
[TraceAttributes.HTTP_STATUS_CODE]: response.status,
|
266
|
+
[TraceAttributes.HTTP_HOST]: host,
|
267
|
+
[TraceAttributes.HTTP_SCHEME]: protocol?.replace(":", "")
|
268
|
+
});
|
269
|
+
try {
|
270
|
+
const jsonResponse = await response.json();
|
271
|
+
if (response.ok) {
|
272
|
+
return jsonResponse;
|
273
|
+
}
|
274
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
275
|
+
} catch (error) {
|
276
|
+
const fetcherError = new FetcherError(response.status, error, requestId);
|
277
|
+
onError(fetcherError.message);
|
278
|
+
throw fetcherError;
|
279
|
+
}
|
280
|
+
},
|
281
|
+
{ [TraceAttributes.HTTP_METHOD]: method.toUpperCase(), [TraceAttributes.HTTP_ROUTE]: path }
|
282
|
+
);
|
283
|
+
}
|
284
|
+
function parseUrl(url) {
|
150
285
|
try {
|
151
|
-
const
|
152
|
-
|
153
|
-
return jsonResponse;
|
154
|
-
}
|
155
|
-
throw new FetcherError(response.status, jsonResponse);
|
286
|
+
const { host, protocol } = new URL(url);
|
287
|
+
return { host, protocol };
|
156
288
|
} catch (error) {
|
157
|
-
|
289
|
+
return {};
|
158
290
|
}
|
159
291
|
}
|
160
292
|
|
@@ -213,6 +345,7 @@ const removeWorkspaceMember = (variables) => fetch$1({
|
|
213
345
|
...variables
|
214
346
|
});
|
215
347
|
const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
|
348
|
+
const updateWorkspaceMemberInvite = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites/{inviteId}", method: "patch", ...variables });
|
216
349
|
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
217
350
|
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
218
351
|
method: "delete",
|
@@ -248,16 +381,25 @@ const deleteDatabase = (variables) => fetch$1({
|
|
248
381
|
method: "delete",
|
249
382
|
...variables
|
250
383
|
});
|
251
|
-
const
|
252
|
-
url: "/
|
384
|
+
const getDatabaseMetadata = (variables) => fetch$1({
|
385
|
+
url: "/dbs/{dbName}/metadata",
|
253
386
|
method: "get",
|
254
387
|
...variables
|
255
388
|
});
|
256
|
-
const
|
389
|
+
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
390
|
+
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
391
|
+
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
392
|
+
const resolveBranch = (variables) => fetch$1({
|
393
|
+
url: "/dbs/{dbName}/resolveBranch",
|
394
|
+
method: "get",
|
395
|
+
...variables
|
396
|
+
});
|
397
|
+
const getBranchDetails = (variables) => fetch$1({
|
257
398
|
url: "/db/{dbBranchName}",
|
258
|
-
method: "
|
399
|
+
method: "get",
|
259
400
|
...variables
|
260
401
|
});
|
402
|
+
const createBranch = (variables) => fetch$1({ url: "/db/{dbBranchName}", method: "put", ...variables });
|
261
403
|
const deleteBranch = (variables) => fetch$1({
|
262
404
|
url: "/db/{dbBranchName}",
|
263
405
|
method: "delete",
|
@@ -331,11 +473,7 @@ const updateColumn = (variables) => fetch$1({
|
|
331
473
|
method: "patch",
|
332
474
|
...variables
|
333
475
|
});
|
334
|
-
const insertRecord = (variables) => fetch$1({
|
335
|
-
url: "/db/{dbBranchName}/tables/{tableName}/data",
|
336
|
-
method: "post",
|
337
|
-
...variables
|
338
|
-
});
|
476
|
+
const insertRecord = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data", method: "post", ...variables });
|
339
477
|
const insertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "put", ...variables });
|
340
478
|
const updateRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "patch", ...variables });
|
341
479
|
const upsertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "post", ...variables });
|
@@ -355,6 +493,11 @@ const queryTable = (variables) => fetch$1({
|
|
355
493
|
method: "post",
|
356
494
|
...variables
|
357
495
|
});
|
496
|
+
const searchTable = (variables) => fetch$1({
|
497
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
498
|
+
method: "post",
|
499
|
+
...variables
|
500
|
+
});
|
358
501
|
const searchBranch = (variables) => fetch$1({
|
359
502
|
url: "/db/{dbBranchName}/search",
|
360
503
|
method: "post",
|
@@ -372,11 +515,21 @@ const operationsByTag = {
|
|
372
515
|
updateWorkspaceMemberRole,
|
373
516
|
removeWorkspaceMember,
|
374
517
|
inviteWorkspaceMember,
|
518
|
+
updateWorkspaceMemberInvite,
|
375
519
|
cancelWorkspaceMemberInvite,
|
376
520
|
resendWorkspaceMemberInvite,
|
377
521
|
acceptWorkspaceMemberInvite
|
378
522
|
},
|
379
|
-
database: {
|
523
|
+
database: {
|
524
|
+
getDatabaseList,
|
525
|
+
createDatabase,
|
526
|
+
deleteDatabase,
|
527
|
+
getDatabaseMetadata,
|
528
|
+
getGitBranchesMapping,
|
529
|
+
addGitBranchesEntry,
|
530
|
+
removeGitBranchesEntry,
|
531
|
+
resolveBranch
|
532
|
+
},
|
380
533
|
branch: {
|
381
534
|
getBranchList,
|
382
535
|
getBranchDetails,
|
@@ -410,14 +563,15 @@ const operationsByTag = {
|
|
410
563
|
getRecord,
|
411
564
|
bulkInsertTableRecords,
|
412
565
|
queryTable,
|
566
|
+
searchTable,
|
413
567
|
searchBranch
|
414
568
|
}
|
415
569
|
};
|
416
570
|
|
417
571
|
function getHostUrl(provider, type) {
|
418
|
-
if (
|
572
|
+
if (isHostProviderAlias(provider)) {
|
419
573
|
return providers[provider][type];
|
420
|
-
} else if (
|
574
|
+
} else if (isHostProviderBuilder(provider)) {
|
421
575
|
return provider[type];
|
422
576
|
}
|
423
577
|
throw new Error("Invalid API provider");
|
@@ -432,10 +586,10 @@ const providers = {
|
|
432
586
|
workspaces: "https://{workspaceId}.staging.xatabase.co"
|
433
587
|
}
|
434
588
|
};
|
435
|
-
function
|
589
|
+
function isHostProviderAlias(alias) {
|
436
590
|
return isString(alias) && Object.keys(providers).includes(alias);
|
437
591
|
}
|
438
|
-
function
|
592
|
+
function isHostProviderBuilder(builder) {
|
439
593
|
return isObject(builder) && isString(builder.main) && isString(builder.workspaces);
|
440
594
|
}
|
441
595
|
|
@@ -443,7 +597,7 @@ var __accessCheck$7 = (obj, member, msg) => {
|
|
443
597
|
if (!member.has(obj))
|
444
598
|
throw TypeError("Cannot " + msg);
|
445
599
|
};
|
446
|
-
var __privateGet$
|
600
|
+
var __privateGet$7 = (obj, member, getter) => {
|
447
601
|
__accessCheck$7(obj, member, "read from private field");
|
448
602
|
return getter ? getter.call(obj) : member.get(obj);
|
449
603
|
};
|
@@ -452,7 +606,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
452
606
|
throw TypeError("Cannot add the same private member more than once");
|
453
607
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
454
608
|
};
|
455
|
-
var __privateSet$
|
609
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
456
610
|
__accessCheck$7(obj, member, "write to private field");
|
457
611
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
458
612
|
return value;
|
@@ -463,46 +617,48 @@ class XataApiClient {
|
|
463
617
|
__privateAdd$7(this, _extraProps, void 0);
|
464
618
|
__privateAdd$7(this, _namespaces, {});
|
465
619
|
const provider = options.host ?? "production";
|
466
|
-
const apiKey = options
|
620
|
+
const apiKey = options.apiKey ?? getAPIKey();
|
621
|
+
const trace = options.trace ?? defaultTrace;
|
467
622
|
if (!apiKey) {
|
468
623
|
throw new Error("Could not resolve a valid apiKey");
|
469
624
|
}
|
470
|
-
__privateSet$
|
625
|
+
__privateSet$7(this, _extraProps, {
|
471
626
|
apiUrl: getHostUrl(provider, "main"),
|
472
627
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
473
628
|
fetchImpl: getFetchImplementation(options.fetch),
|
474
|
-
apiKey
|
629
|
+
apiKey,
|
630
|
+
trace
|
475
631
|
});
|
476
632
|
}
|
477
633
|
get user() {
|
478
|
-
if (!__privateGet$
|
479
|
-
__privateGet$
|
480
|
-
return __privateGet$
|
634
|
+
if (!__privateGet$7(this, _namespaces).user)
|
635
|
+
__privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
|
636
|
+
return __privateGet$7(this, _namespaces).user;
|
481
637
|
}
|
482
638
|
get workspaces() {
|
483
|
-
if (!__privateGet$
|
484
|
-
__privateGet$
|
485
|
-
return __privateGet$
|
639
|
+
if (!__privateGet$7(this, _namespaces).workspaces)
|
640
|
+
__privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
|
641
|
+
return __privateGet$7(this, _namespaces).workspaces;
|
486
642
|
}
|
487
643
|
get databases() {
|
488
|
-
if (!__privateGet$
|
489
|
-
__privateGet$
|
490
|
-
return __privateGet$
|
644
|
+
if (!__privateGet$7(this, _namespaces).databases)
|
645
|
+
__privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
|
646
|
+
return __privateGet$7(this, _namespaces).databases;
|
491
647
|
}
|
492
648
|
get branches() {
|
493
|
-
if (!__privateGet$
|
494
|
-
__privateGet$
|
495
|
-
return __privateGet$
|
649
|
+
if (!__privateGet$7(this, _namespaces).branches)
|
650
|
+
__privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
|
651
|
+
return __privateGet$7(this, _namespaces).branches;
|
496
652
|
}
|
497
653
|
get tables() {
|
498
|
-
if (!__privateGet$
|
499
|
-
__privateGet$
|
500
|
-
return __privateGet$
|
654
|
+
if (!__privateGet$7(this, _namespaces).tables)
|
655
|
+
__privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
|
656
|
+
return __privateGet$7(this, _namespaces).tables;
|
501
657
|
}
|
502
658
|
get records() {
|
503
|
-
if (!__privateGet$
|
504
|
-
__privateGet$
|
505
|
-
return __privateGet$
|
659
|
+
if (!__privateGet$7(this, _namespaces).records)
|
660
|
+
__privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
|
661
|
+
return __privateGet$7(this, _namespaces).records;
|
506
662
|
}
|
507
663
|
}
|
508
664
|
_extraProps = new WeakMap();
|
@@ -594,6 +750,13 @@ class WorkspaceApi {
|
|
594
750
|
...this.extraProps
|
595
751
|
});
|
596
752
|
}
|
753
|
+
updateWorkspaceMemberInvite(workspaceId, inviteId, role) {
|
754
|
+
return operationsByTag.workspaces.updateWorkspaceMemberInvite({
|
755
|
+
pathParams: { workspaceId, inviteId },
|
756
|
+
body: { role },
|
757
|
+
...this.extraProps
|
758
|
+
});
|
759
|
+
}
|
597
760
|
cancelWorkspaceMemberInvite(workspaceId, inviteId) {
|
598
761
|
return operationsByTag.workspaces.cancelWorkspaceMemberInvite({
|
599
762
|
pathParams: { workspaceId, inviteId },
|
@@ -636,6 +799,39 @@ class DatabaseApi {
|
|
636
799
|
...this.extraProps
|
637
800
|
});
|
638
801
|
}
|
802
|
+
getDatabaseMetadata(workspace, dbName) {
|
803
|
+
return operationsByTag.database.getDatabaseMetadata({
|
804
|
+
pathParams: { workspace, dbName },
|
805
|
+
...this.extraProps
|
806
|
+
});
|
807
|
+
}
|
808
|
+
getGitBranchesMapping(workspace, dbName) {
|
809
|
+
return operationsByTag.database.getGitBranchesMapping({
|
810
|
+
pathParams: { workspace, dbName },
|
811
|
+
...this.extraProps
|
812
|
+
});
|
813
|
+
}
|
814
|
+
addGitBranchesEntry(workspace, dbName, body) {
|
815
|
+
return operationsByTag.database.addGitBranchesEntry({
|
816
|
+
pathParams: { workspace, dbName },
|
817
|
+
body,
|
818
|
+
...this.extraProps
|
819
|
+
});
|
820
|
+
}
|
821
|
+
removeGitBranchesEntry(workspace, dbName, gitBranch) {
|
822
|
+
return operationsByTag.database.removeGitBranchesEntry({
|
823
|
+
pathParams: { workspace, dbName },
|
824
|
+
queryParams: { gitBranch },
|
825
|
+
...this.extraProps
|
826
|
+
});
|
827
|
+
}
|
828
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
829
|
+
return operationsByTag.database.resolveBranch({
|
830
|
+
pathParams: { workspace, dbName },
|
831
|
+
queryParams: { gitBranch, fallbackBranch },
|
832
|
+
...this.extraProps
|
833
|
+
});
|
834
|
+
}
|
639
835
|
}
|
640
836
|
class BranchApi {
|
641
837
|
constructor(extraProps) {
|
@@ -653,10 +849,10 @@ class BranchApi {
|
|
653
849
|
...this.extraProps
|
654
850
|
});
|
655
851
|
}
|
656
|
-
createBranch(workspace, database, branch, from
|
852
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
657
853
|
return operationsByTag.branch.createBranch({
|
658
854
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
659
|
-
queryParams: { from },
|
855
|
+
queryParams: isString(from) ? { from } : void 0,
|
660
856
|
body: options,
|
661
857
|
...this.extraProps
|
662
858
|
});
|
@@ -781,9 +977,10 @@ class RecordsApi {
|
|
781
977
|
constructor(extraProps) {
|
782
978
|
this.extraProps = extraProps;
|
783
979
|
}
|
784
|
-
insertRecord(workspace, database, branch, tableName, record) {
|
980
|
+
insertRecord(workspace, database, branch, tableName, record, options = {}) {
|
785
981
|
return operationsByTag.records.insertRecord({
|
786
982
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
983
|
+
queryParams: options,
|
787
984
|
body: record,
|
788
985
|
...this.extraProps
|
789
986
|
});
|
@@ -812,21 +1009,24 @@ class RecordsApi {
|
|
812
1009
|
...this.extraProps
|
813
1010
|
});
|
814
1011
|
}
|
815
|
-
deleteRecord(workspace, database, branch, tableName, recordId) {
|
1012
|
+
deleteRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
816
1013
|
return operationsByTag.records.deleteRecord({
|
817
1014
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
1015
|
+
queryParams: options,
|
818
1016
|
...this.extraProps
|
819
1017
|
});
|
820
1018
|
}
|
821
1019
|
getRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
822
1020
|
return operationsByTag.records.getRecord({
|
823
1021
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
1022
|
+
queryParams: options,
|
824
1023
|
...this.extraProps
|
825
1024
|
});
|
826
1025
|
}
|
827
|
-
bulkInsertTableRecords(workspace, database, branch, tableName, records) {
|
1026
|
+
bulkInsertTableRecords(workspace, database, branch, tableName, records, options = {}) {
|
828
1027
|
return operationsByTag.records.bulkInsertTableRecords({
|
829
1028
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
1029
|
+
queryParams: options,
|
830
1030
|
body: { records },
|
831
1031
|
...this.extraProps
|
832
1032
|
});
|
@@ -838,6 +1038,13 @@ class RecordsApi {
|
|
838
1038
|
...this.extraProps
|
839
1039
|
});
|
840
1040
|
}
|
1041
|
+
searchTable(workspace, database, branch, tableName, query) {
|
1042
|
+
return operationsByTag.records.searchTable({
|
1043
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
1044
|
+
body: query,
|
1045
|
+
...this.extraProps
|
1046
|
+
});
|
1047
|
+
}
|
841
1048
|
searchBranch(workspace, database, branch, query) {
|
842
1049
|
return operationsByTag.records.searchBranch({
|
843
1050
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -861,7 +1068,7 @@ var __accessCheck$6 = (obj, member, msg) => {
|
|
861
1068
|
if (!member.has(obj))
|
862
1069
|
throw TypeError("Cannot " + msg);
|
863
1070
|
};
|
864
|
-
var __privateGet$
|
1071
|
+
var __privateGet$6 = (obj, member, getter) => {
|
865
1072
|
__accessCheck$6(obj, member, "read from private field");
|
866
1073
|
return getter ? getter.call(obj) : member.get(obj);
|
867
1074
|
};
|
@@ -870,30 +1077,30 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
870
1077
|
throw TypeError("Cannot add the same private member more than once");
|
871
1078
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
872
1079
|
};
|
873
|
-
var __privateSet$
|
1080
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
874
1081
|
__accessCheck$6(obj, member, "write to private field");
|
875
1082
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
876
1083
|
return value;
|
877
1084
|
};
|
878
|
-
var _query;
|
1085
|
+
var _query, _page;
|
879
1086
|
class Page {
|
880
1087
|
constructor(query, meta, records = []) {
|
881
1088
|
__privateAdd$6(this, _query, void 0);
|
882
|
-
__privateSet$
|
1089
|
+
__privateSet$6(this, _query, query);
|
883
1090
|
this.meta = meta;
|
884
|
-
this.records = records;
|
1091
|
+
this.records = new RecordArray(this, records);
|
885
1092
|
}
|
886
1093
|
async nextPage(size, offset) {
|
887
|
-
return __privateGet$
|
1094
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
888
1095
|
}
|
889
1096
|
async previousPage(size, offset) {
|
890
|
-
return __privateGet$
|
1097
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
891
1098
|
}
|
892
1099
|
async firstPage(size, offset) {
|
893
|
-
return __privateGet$
|
1100
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
|
894
1101
|
}
|
895
1102
|
async lastPage(size, offset) {
|
896
|
-
return __privateGet$
|
1103
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
|
897
1104
|
}
|
898
1105
|
hasNextPage() {
|
899
1106
|
return this.meta.page.more;
|
@@ -901,15 +1108,62 @@ class Page {
|
|
901
1108
|
}
|
902
1109
|
_query = new WeakMap();
|
903
1110
|
const PAGINATION_MAX_SIZE = 200;
|
904
|
-
const PAGINATION_DEFAULT_SIZE =
|
1111
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
905
1112
|
const PAGINATION_MAX_OFFSET = 800;
|
906
1113
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
1114
|
+
function isCursorPaginationOptions(options) {
|
1115
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
1116
|
+
}
|
1117
|
+
const _RecordArray = class extends Array {
|
1118
|
+
constructor(...args) {
|
1119
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
1120
|
+
__privateAdd$6(this, _page, void 0);
|
1121
|
+
__privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
|
1122
|
+
}
|
1123
|
+
static parseConstructorParams(...args) {
|
1124
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1125
|
+
return new Array(args[0]);
|
1126
|
+
}
|
1127
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1128
|
+
const result = args[1] ?? args[0].records ?? [];
|
1129
|
+
return new Array(...result);
|
1130
|
+
}
|
1131
|
+
return new Array(...args);
|
1132
|
+
}
|
1133
|
+
toArray() {
|
1134
|
+
return new Array(...this);
|
1135
|
+
}
|
1136
|
+
map(callbackfn, thisArg) {
|
1137
|
+
return this.toArray().map(callbackfn, thisArg);
|
1138
|
+
}
|
1139
|
+
async nextPage(size, offset) {
|
1140
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1141
|
+
return new _RecordArray(newPage);
|
1142
|
+
}
|
1143
|
+
async previousPage(size, offset) {
|
1144
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1145
|
+
return new _RecordArray(newPage);
|
1146
|
+
}
|
1147
|
+
async firstPage(size, offset) {
|
1148
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1149
|
+
return new _RecordArray(newPage);
|
1150
|
+
}
|
1151
|
+
async lastPage(size, offset) {
|
1152
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1153
|
+
return new _RecordArray(newPage);
|
1154
|
+
}
|
1155
|
+
hasNextPage() {
|
1156
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1157
|
+
}
|
1158
|
+
};
|
1159
|
+
let RecordArray = _RecordArray;
|
1160
|
+
_page = new WeakMap();
|
907
1161
|
|
908
1162
|
var __accessCheck$5 = (obj, member, msg) => {
|
909
1163
|
if (!member.has(obj))
|
910
1164
|
throw TypeError("Cannot " + msg);
|
911
1165
|
};
|
912
|
-
var __privateGet$
|
1166
|
+
var __privateGet$5 = (obj, member, getter) => {
|
913
1167
|
__accessCheck$5(obj, member, "read from private field");
|
914
1168
|
return getter ? getter.call(obj) : member.get(obj);
|
915
1169
|
};
|
@@ -918,34 +1172,35 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
918
1172
|
throw TypeError("Cannot add the same private member more than once");
|
919
1173
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
920
1174
|
};
|
921
|
-
var __privateSet$
|
1175
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
922
1176
|
__accessCheck$5(obj, member, "write to private field");
|
923
1177
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
924
1178
|
return value;
|
925
1179
|
};
|
926
1180
|
var _table$1, _repository, _data;
|
927
1181
|
const _Query = class {
|
928
|
-
constructor(repository, table, data,
|
1182
|
+
constructor(repository, table, data, rawParent) {
|
929
1183
|
__privateAdd$5(this, _table$1, void 0);
|
930
1184
|
__privateAdd$5(this, _repository, void 0);
|
931
1185
|
__privateAdd$5(this, _data, { filter: {} });
|
932
1186
|
this.meta = { page: { cursor: "start", more: true } };
|
933
|
-
this.records = [];
|
934
|
-
__privateSet$
|
1187
|
+
this.records = new RecordArray(this, []);
|
1188
|
+
__privateSet$5(this, _table$1, table);
|
935
1189
|
if (repository) {
|
936
|
-
__privateSet$
|
1190
|
+
__privateSet$5(this, _repository, repository);
|
937
1191
|
} else {
|
938
|
-
__privateSet$
|
1192
|
+
__privateSet$5(this, _repository, this);
|
939
1193
|
}
|
940
|
-
|
941
|
-
__privateGet$
|
942
|
-
__privateGet$
|
943
|
-
__privateGet$
|
944
|
-
__privateGet$
|
945
|
-
__privateGet$
|
946
|
-
__privateGet$
|
947
|
-
__privateGet$
|
948
|
-
__privateGet$
|
1194
|
+
const parent = cleanParent(data, rawParent);
|
1195
|
+
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
1196
|
+
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
1197
|
+
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
1198
|
+
__privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
|
1199
|
+
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
1200
|
+
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
1201
|
+
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
1202
|
+
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
1203
|
+
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
949
1204
|
this.any = this.any.bind(this);
|
950
1205
|
this.all = this.all.bind(this);
|
951
1206
|
this.not = this.not.bind(this);
|
@@ -956,83 +1211,94 @@ const _Query = class {
|
|
956
1211
|
Object.defineProperty(this, "repository", { enumerable: false });
|
957
1212
|
}
|
958
1213
|
getQueryOptions() {
|
959
|
-
return __privateGet$
|
1214
|
+
return __privateGet$5(this, _data);
|
960
1215
|
}
|
961
1216
|
key() {
|
962
|
-
const { columns = [], filter = {}, sort = [],
|
963
|
-
const key = JSON.stringify({ columns, filter, sort,
|
1217
|
+
const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
|
1218
|
+
const key = JSON.stringify({ columns, filter, sort, pagination });
|
964
1219
|
return toBase64(key);
|
965
1220
|
}
|
966
1221
|
any(...queries) {
|
967
1222
|
const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
|
968
|
-
return new _Query(__privateGet$
|
1223
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
|
969
1224
|
}
|
970
1225
|
all(...queries) {
|
971
1226
|
const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
|
972
|
-
return new _Query(__privateGet$
|
1227
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
973
1228
|
}
|
974
1229
|
not(...queries) {
|
975
1230
|
const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
|
976
|
-
return new _Query(__privateGet$
|
1231
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
|
977
1232
|
}
|
978
1233
|
none(...queries) {
|
979
1234
|
const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
|
980
|
-
return new _Query(__privateGet$
|
1235
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
|
981
1236
|
}
|
982
1237
|
filter(a, b) {
|
983
1238
|
if (arguments.length === 1) {
|
984
|
-
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
985
|
-
const $all = compact([__privateGet$
|
986
|
-
return new _Query(__privateGet$
|
1239
|
+
const constraints = Object.entries(a ?? {}).map(([column, constraint]) => ({ [column]: constraint }));
|
1240
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
|
1241
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
987
1242
|
} else {
|
988
|
-
const
|
989
|
-
|
1243
|
+
const constraints = isDefined(a) && isDefined(b) ? [{ [a]: b }] : void 0;
|
1244
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
|
1245
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
990
1246
|
}
|
991
1247
|
}
|
992
|
-
sort(column, direction) {
|
993
|
-
const originalSort = [__privateGet$
|
1248
|
+
sort(column, direction = "asc") {
|
1249
|
+
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
994
1250
|
const sort = [...originalSort, { column, direction }];
|
995
|
-
return new _Query(__privateGet$
|
1251
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
996
1252
|
}
|
997
1253
|
select(columns) {
|
998
|
-
return new _Query(
|
1254
|
+
return new _Query(
|
1255
|
+
__privateGet$5(this, _repository),
|
1256
|
+
__privateGet$5(this, _table$1),
|
1257
|
+
{ columns },
|
1258
|
+
__privateGet$5(this, _data)
|
1259
|
+
);
|
999
1260
|
}
|
1000
1261
|
getPaginated(options = {}) {
|
1001
|
-
const query = new _Query(__privateGet$
|
1002
|
-
return __privateGet$
|
1262
|
+
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
1263
|
+
return __privateGet$5(this, _repository).query(query);
|
1003
1264
|
}
|
1004
1265
|
async *[Symbol.asyncIterator]() {
|
1005
|
-
for await (const [record] of this.getIterator(1)) {
|
1266
|
+
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
1006
1267
|
yield record;
|
1007
1268
|
}
|
1008
1269
|
}
|
1009
|
-
async *getIterator(
|
1010
|
-
|
1011
|
-
let
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1270
|
+
async *getIterator(options = {}) {
|
1271
|
+
const { batchSize = 1 } = options;
|
1272
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1273
|
+
let more = page.hasNextPage();
|
1274
|
+
yield page.records;
|
1275
|
+
while (more) {
|
1276
|
+
page = await page.nextPage();
|
1277
|
+
more = page.hasNextPage();
|
1278
|
+
yield page.records;
|
1017
1279
|
}
|
1018
1280
|
}
|
1019
1281
|
async getMany(options = {}) {
|
1020
|
-
const
|
1021
|
-
|
1282
|
+
const page = await this.getPaginated(options);
|
1283
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1284
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1285
|
+
}
|
1286
|
+
return page.records;
|
1022
1287
|
}
|
1023
|
-
async getAll(
|
1288
|
+
async getAll(options = {}) {
|
1289
|
+
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
1024
1290
|
const results = [];
|
1025
|
-
for await (const page of this.getIterator(
|
1291
|
+
for await (const page of this.getIterator({ ...rest, batchSize })) {
|
1026
1292
|
results.push(...page);
|
1027
1293
|
}
|
1028
1294
|
return results;
|
1029
1295
|
}
|
1030
1296
|
async getFirst(options = {}) {
|
1031
|
-
const records = await this.getMany({ ...options,
|
1032
|
-
return records[0]
|
1297
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1298
|
+
return records[0] ?? null;
|
1033
1299
|
}
|
1034
1300
|
cache(ttl) {
|
1035
|
-
return new _Query(__privateGet$
|
1301
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
1036
1302
|
}
|
1037
1303
|
nextPage(size, offset) {
|
1038
1304
|
return this.firstPage(size, offset);
|
@@ -1041,10 +1307,10 @@ const _Query = class {
|
|
1041
1307
|
return this.firstPage(size, offset);
|
1042
1308
|
}
|
1043
1309
|
firstPage(size, offset) {
|
1044
|
-
return this.getPaginated({
|
1310
|
+
return this.getPaginated({ pagination: { size, offset } });
|
1045
1311
|
}
|
1046
1312
|
lastPage(size, offset) {
|
1047
|
-
return this.getPaginated({
|
1313
|
+
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
1048
1314
|
}
|
1049
1315
|
hasNextPage() {
|
1050
1316
|
return this.meta.page.more;
|
@@ -1054,12 +1320,20 @@ let Query = _Query;
|
|
1054
1320
|
_table$1 = new WeakMap();
|
1055
1321
|
_repository = new WeakMap();
|
1056
1322
|
_data = new WeakMap();
|
1323
|
+
function cleanParent(data, parent) {
|
1324
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1325
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1326
|
+
}
|
1327
|
+
return parent;
|
1328
|
+
}
|
1057
1329
|
|
1058
1330
|
function isIdentifiable(x) {
|
1059
1331
|
return isObject(x) && isString(x?.id);
|
1060
1332
|
}
|
1061
1333
|
function isXataRecord(x) {
|
1062
|
-
|
1334
|
+
const record = x;
|
1335
|
+
const metadata = record?.getMetadata();
|
1336
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1063
1337
|
}
|
1064
1338
|
|
1065
1339
|
function isSortFilterString(value) {
|
@@ -1089,7 +1363,7 @@ var __accessCheck$4 = (obj, member, msg) => {
|
|
1089
1363
|
if (!member.has(obj))
|
1090
1364
|
throw TypeError("Cannot " + msg);
|
1091
1365
|
};
|
1092
|
-
var __privateGet$
|
1366
|
+
var __privateGet$4 = (obj, member, getter) => {
|
1093
1367
|
__accessCheck$4(obj, member, "read from private field");
|
1094
1368
|
return getter ? getter.call(obj) : member.get(obj);
|
1095
1369
|
};
|
@@ -1098,7 +1372,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1098
1372
|
throw TypeError("Cannot add the same private member more than once");
|
1099
1373
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1100
1374
|
};
|
1101
|
-
var __privateSet$
|
1375
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1102
1376
|
__accessCheck$4(obj, member, "write to private field");
|
1103
1377
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1104
1378
|
return value;
|
@@ -1107,7 +1381,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1107
1381
|
__accessCheck$4(obj, member, "access private method");
|
1108
1382
|
return method;
|
1109
1383
|
};
|
1110
|
-
var _table,
|
1384
|
+
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;
|
1111
1385
|
class Repository extends Query {
|
1112
1386
|
}
|
1113
1387
|
class RestRepository extends Query {
|
@@ -1119,292 +1393,338 @@ class RestRepository extends Query {
|
|
1119
1393
|
__privateAdd$4(this, _updateRecordWithID);
|
1120
1394
|
__privateAdd$4(this, _upsertRecordWithID);
|
1121
1395
|
__privateAdd$4(this, _deleteRecord);
|
1122
|
-
__privateAdd$4(this, _invalidateCache);
|
1123
|
-
__privateAdd$4(this, _setCacheRecord);
|
1124
|
-
__privateAdd$4(this, _getCacheRecord);
|
1125
1396
|
__privateAdd$4(this, _setCacheQuery);
|
1126
1397
|
__privateAdd$4(this, _getCacheQuery);
|
1398
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1127
1399
|
__privateAdd$4(this, _table, void 0);
|
1128
|
-
__privateAdd$4(this, _links, void 0);
|
1129
1400
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1401
|
+
__privateAdd$4(this, _db, void 0);
|
1130
1402
|
__privateAdd$4(this, _cache, void 0);
|
1131
|
-
|
1132
|
-
|
1133
|
-
__privateSet$
|
1134
|
-
this
|
1135
|
-
__privateSet$
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1142
|
-
|
1143
|
-
|
1144
|
-
if (a === "")
|
1145
|
-
throw new Error("The id can't be empty");
|
1146
|
-
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b);
|
1147
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1148
|
-
return record;
|
1149
|
-
}
|
1150
|
-
if (isObject(a) && isString(a.id)) {
|
1151
|
-
if (a.id === "")
|
1152
|
-
throw new Error("The id can't be empty");
|
1153
|
-
const record = await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 });
|
1154
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1155
|
-
return record;
|
1156
|
-
}
|
1157
|
-
if (isObject(a)) {
|
1158
|
-
const record = await __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a);
|
1159
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1160
|
-
return record;
|
1161
|
-
}
|
1162
|
-
throw new Error("Invalid arguments for create method");
|
1163
|
-
}
|
1164
|
-
async read(recordId) {
|
1165
|
-
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
|
1166
|
-
if (cacheRecord)
|
1167
|
-
return cacheRecord;
|
1168
|
-
const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
|
1169
|
-
try {
|
1170
|
-
const response = await getRecord({
|
1171
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$3(this, _table), recordId },
|
1172
|
-
...fetchProps
|
1403
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1404
|
+
__privateAdd$4(this, _trace, void 0);
|
1405
|
+
__privateSet$4(this, _table, options.table);
|
1406
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1407
|
+
__privateSet$4(this, _db, options.db);
|
1408
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1409
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1410
|
+
const trace = options.pluginOptions.trace ?? defaultTrace;
|
1411
|
+
__privateSet$4(this, _trace, async (name, fn, options2 = {}) => {
|
1412
|
+
return trace(name, fn, {
|
1413
|
+
...options2,
|
1414
|
+
[TraceAttributes.TABLE]: __privateGet$4(this, _table),
|
1415
|
+
[TraceAttributes.VERSION]: VERSION
|
1173
1416
|
});
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1417
|
+
});
|
1418
|
+
}
|
1419
|
+
async create(a, b, c) {
|
1420
|
+
return __privateGet$4(this, _trace).call(this, "create", async () => {
|
1421
|
+
if (Array.isArray(a)) {
|
1422
|
+
if (a.length === 0)
|
1423
|
+
return [];
|
1424
|
+
const columns = isStringArray(b) ? b : void 0;
|
1425
|
+
return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a, columns);
|
1178
1426
|
}
|
1179
|
-
|
1180
|
-
|
1427
|
+
if (isString(a) && isObject(b)) {
|
1428
|
+
if (a === "")
|
1429
|
+
throw new Error("The id can't be empty");
|
1430
|
+
const columns = isStringArray(c) ? c : void 0;
|
1431
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns);
|
1432
|
+
}
|
1433
|
+
if (isObject(a) && isString(a.id)) {
|
1434
|
+
if (a.id === "")
|
1435
|
+
throw new Error("The id can't be empty");
|
1436
|
+
const columns = isStringArray(b) ? b : void 0;
|
1437
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1438
|
+
}
|
1439
|
+
if (isObject(a)) {
|
1440
|
+
const columns = isStringArray(b) ? b : void 0;
|
1441
|
+
return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a, columns);
|
1442
|
+
}
|
1443
|
+
throw new Error("Invalid arguments for create method");
|
1444
|
+
});
|
1181
1445
|
}
|
1182
|
-
async
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1446
|
+
async read(a, b) {
|
1447
|
+
return __privateGet$4(this, _trace).call(this, "read", async () => {
|
1448
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1449
|
+
if (Array.isArray(a)) {
|
1450
|
+
if (a.length === 0)
|
1451
|
+
return [];
|
1452
|
+
const ids = a.map((item) => extractId(item));
|
1453
|
+
const finalObjects = await this.getAll({ filter: { id: { $any: compact(ids) } }, columns });
|
1454
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1455
|
+
acc[object.id] = object;
|
1456
|
+
return acc;
|
1457
|
+
}, {});
|
1458
|
+
return ids.map((id2) => dictionary[id2 ?? ""] ?? null);
|
1186
1459
|
}
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1460
|
+
const id = extractId(a);
|
1461
|
+
if (id) {
|
1462
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1463
|
+
try {
|
1464
|
+
const response = await getRecord({
|
1465
|
+
pathParams: {
|
1466
|
+
workspace: "{workspaceId}",
|
1467
|
+
dbBranchName: "{dbBranch}",
|
1468
|
+
tableName: __privateGet$4(this, _table),
|
1469
|
+
recordId: id
|
1470
|
+
},
|
1471
|
+
queryParams: { columns },
|
1472
|
+
...fetchProps
|
1473
|
+
});
|
1474
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1475
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1476
|
+
} catch (e) {
|
1477
|
+
if (isObject(e) && e.status === 404) {
|
1478
|
+
return null;
|
1479
|
+
}
|
1480
|
+
throw e;
|
1481
|
+
}
|
1482
|
+
}
|
1483
|
+
return null;
|
1484
|
+
});
|
1202
1485
|
}
|
1203
|
-
async
|
1204
|
-
|
1205
|
-
if (a
|
1206
|
-
|
1486
|
+
async update(a, b, c) {
|
1487
|
+
return __privateGet$4(this, _trace).call(this, "update", async () => {
|
1488
|
+
if (Array.isArray(a)) {
|
1489
|
+
if (a.length === 0)
|
1490
|
+
return [];
|
1491
|
+
if (a.length > 100) {
|
1492
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1493
|
+
}
|
1494
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1495
|
+
return Promise.all(a.map((object) => this.update(object, columns)));
|
1207
1496
|
}
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1219
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1220
|
-
return record;
|
1221
|
-
}
|
1222
|
-
throw new Error("Invalid arguments for createOrUpdate method");
|
1497
|
+
if (isString(a) && isObject(b)) {
|
1498
|
+
const columns = isStringArray(c) ? c : void 0;
|
1499
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns);
|
1500
|
+
}
|
1501
|
+
if (isObject(a) && isString(a.id)) {
|
1502
|
+
const columns = isStringArray(b) ? b : void 0;
|
1503
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1504
|
+
}
|
1505
|
+
throw new Error("Invalid arguments for update method");
|
1506
|
+
});
|
1223
1507
|
}
|
1224
|
-
async
|
1225
|
-
|
1226
|
-
if (a
|
1227
|
-
|
1508
|
+
async createOrUpdate(a, b, c) {
|
1509
|
+
return __privateGet$4(this, _trace).call(this, "createOrUpdate", async () => {
|
1510
|
+
if (Array.isArray(a)) {
|
1511
|
+
if (a.length === 0)
|
1512
|
+
return [];
|
1513
|
+
if (a.length > 100) {
|
1514
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1515
|
+
}
|
1516
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1517
|
+
return Promise.all(a.map((object) => this.createOrUpdate(object, columns)));
|
1228
1518
|
}
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1519
|
+
if (isString(a) && isObject(b)) {
|
1520
|
+
const columns = isStringArray(c) ? c : void 0;
|
1521
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b, columns);
|
1522
|
+
}
|
1523
|
+
if (isObject(a) && isString(a.id)) {
|
1524
|
+
const columns = isStringArray(c) ? c : void 0;
|
1525
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1526
|
+
}
|
1527
|
+
throw new Error("Invalid arguments for createOrUpdate method");
|
1528
|
+
});
|
1529
|
+
}
|
1530
|
+
async delete(a, b) {
|
1531
|
+
return __privateGet$4(this, _trace).call(this, "delete", async () => {
|
1532
|
+
if (Array.isArray(a)) {
|
1533
|
+
if (a.length === 0)
|
1534
|
+
return [];
|
1535
|
+
if (a.length > 100) {
|
1536
|
+
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1537
|
+
}
|
1538
|
+
return Promise.all(a.map((id) => this.delete(id, b)));
|
1539
|
+
}
|
1540
|
+
if (isString(a)) {
|
1541
|
+
return __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a, b);
|
1542
|
+
}
|
1543
|
+
if (isObject(a) && isString(a.id)) {
|
1544
|
+
return __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id, b);
|
1545
|
+
}
|
1546
|
+
throw new Error("Invalid arguments for delete method");
|
1547
|
+
});
|
1243
1548
|
}
|
1244
1549
|
async search(query, options = {}) {
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1550
|
+
return __privateGet$4(this, _trace).call(this, "search", async () => {
|
1551
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1552
|
+
const { records } = await searchTable({
|
1553
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1554
|
+
body: {
|
1555
|
+
query,
|
1556
|
+
fuzziness: options.fuzziness,
|
1557
|
+
prefix: options.prefix,
|
1558
|
+
highlight: options.highlight,
|
1559
|
+
filter: options.filter,
|
1560
|
+
boosters: options.boosters
|
1561
|
+
},
|
1562
|
+
...fetchProps
|
1563
|
+
});
|
1564
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1565
|
+
return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1250
1566
|
});
|
1251
|
-
return records.map((item) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), item));
|
1252
1567
|
}
|
1253
1568
|
async query(query) {
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1569
|
+
return __privateGet$4(this, _trace).call(this, "query", async () => {
|
1570
|
+
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
1571
|
+
if (cacheQuery)
|
1572
|
+
return new Page(query, cacheQuery.meta, cacheQuery.records);
|
1573
|
+
const data = query.getQueryOptions();
|
1574
|
+
const body = {
|
1575
|
+
filter: cleanFilter(data.filter),
|
1576
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1577
|
+
page: data.pagination,
|
1578
|
+
columns: data.columns
|
1579
|
+
};
|
1580
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1581
|
+
const { meta, records: objects } = await queryTable({
|
1582
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1583
|
+
body,
|
1584
|
+
...fetchProps
|
1585
|
+
});
|
1586
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1587
|
+
const records = objects.map((record) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), record));
|
1588
|
+
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1589
|
+
return new Page(query, meta, records);
|
1269
1590
|
});
|
1270
|
-
const records = objects.map((record) => initObject(this.db, __privateGet$3(this, _links), __privateGet$3(this, _table), record));
|
1271
|
-
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1272
|
-
return new Page(query, meta, records);
|
1273
1591
|
}
|
1274
1592
|
}
|
1275
1593
|
_table = new WeakMap();
|
1276
|
-
_links = new WeakMap();
|
1277
1594
|
_getFetchProps = new WeakMap();
|
1595
|
+
_db = new WeakMap();
|
1278
1596
|
_cache = new WeakMap();
|
1597
|
+
_schemaTables$2 = new WeakMap();
|
1598
|
+
_trace = new WeakMap();
|
1279
1599
|
_insertRecordWithoutId = new WeakSet();
|
1280
|
-
insertRecordWithoutId_fn = async function(object) {
|
1281
|
-
const fetchProps = await __privateGet$
|
1600
|
+
insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
|
1601
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1282
1602
|
const record = transformObjectLinks(object);
|
1283
1603
|
const response = await insertRecord({
|
1284
1604
|
pathParams: {
|
1285
1605
|
workspace: "{workspaceId}",
|
1286
1606
|
dbBranchName: "{dbBranch}",
|
1287
|
-
tableName: __privateGet$
|
1607
|
+
tableName: __privateGet$4(this, _table)
|
1288
1608
|
},
|
1609
|
+
queryParams: { columns },
|
1289
1610
|
body: record,
|
1290
1611
|
...fetchProps
|
1291
1612
|
});
|
1292
|
-
const
|
1293
|
-
|
1294
|
-
throw new Error("The server failed to save the record");
|
1295
|
-
}
|
1296
|
-
return finalObject;
|
1613
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1614
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1297
1615
|
};
|
1298
1616
|
_insertRecordWithId = new WeakSet();
|
1299
|
-
insertRecordWithId_fn = async function(recordId, object) {
|
1300
|
-
const fetchProps = await __privateGet$
|
1617
|
+
insertRecordWithId_fn = async function(recordId, object, columns = ["*"]) {
|
1618
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1301
1619
|
const record = transformObjectLinks(object);
|
1302
1620
|
const response = await insertRecordWithID({
|
1303
1621
|
pathParams: {
|
1304
1622
|
workspace: "{workspaceId}",
|
1305
1623
|
dbBranchName: "{dbBranch}",
|
1306
|
-
tableName: __privateGet$
|
1624
|
+
tableName: __privateGet$4(this, _table),
|
1307
1625
|
recordId
|
1308
1626
|
},
|
1309
1627
|
body: record,
|
1310
|
-
queryParams: { createOnly: true },
|
1628
|
+
queryParams: { createOnly: true, columns },
|
1311
1629
|
...fetchProps
|
1312
1630
|
});
|
1313
|
-
const
|
1314
|
-
|
1315
|
-
throw new Error("The server failed to save the record");
|
1316
|
-
}
|
1317
|
-
return finalObject;
|
1631
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1632
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1318
1633
|
};
|
1319
1634
|
_bulkInsertTableRecords = new WeakSet();
|
1320
|
-
bulkInsertTableRecords_fn = async function(objects) {
|
1321
|
-
const fetchProps = await __privateGet$
|
1635
|
+
bulkInsertTableRecords_fn = async function(objects, columns = ["*"]) {
|
1636
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1322
1637
|
const records = objects.map((object) => transformObjectLinks(object));
|
1323
1638
|
const response = await bulkInsertTableRecords({
|
1324
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1639
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1640
|
+
queryParams: { columns },
|
1325
1641
|
body: { records },
|
1326
1642
|
...fetchProps
|
1327
1643
|
});
|
1328
|
-
|
1329
|
-
|
1330
|
-
throw new Error("The server failed to save some records");
|
1644
|
+
if (!isResponseWithRecords(response)) {
|
1645
|
+
throw new Error("Request included columns but server didn't include them");
|
1331
1646
|
}
|
1332
|
-
|
1647
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1648
|
+
return response.records?.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1333
1649
|
};
|
1334
1650
|
_updateRecordWithID = new WeakSet();
|
1335
|
-
updateRecordWithID_fn = async function(recordId, object) {
|
1336
|
-
const fetchProps = await __privateGet$
|
1651
|
+
updateRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1652
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1337
1653
|
const record = transformObjectLinks(object);
|
1338
|
-
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1654
|
+
try {
|
1655
|
+
const response = await updateRecordWithID({
|
1656
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1657
|
+
queryParams: { columns },
|
1658
|
+
body: record,
|
1659
|
+
...fetchProps
|
1660
|
+
});
|
1661
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1662
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1663
|
+
} catch (e) {
|
1664
|
+
if (isObject(e) && e.status === 404) {
|
1665
|
+
return null;
|
1666
|
+
}
|
1667
|
+
throw e;
|
1668
|
+
}
|
1347
1669
|
};
|
1348
1670
|
_upsertRecordWithID = new WeakSet();
|
1349
|
-
upsertRecordWithID_fn = async function(recordId, object) {
|
1350
|
-
const fetchProps = await __privateGet$
|
1671
|
+
upsertRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1672
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1351
1673
|
const response = await upsertRecordWithID({
|
1352
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1674
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1675
|
+
queryParams: { columns },
|
1353
1676
|
body: object,
|
1354
1677
|
...fetchProps
|
1355
1678
|
});
|
1356
|
-
const
|
1357
|
-
|
1358
|
-
throw new Error("The server failed to save the record");
|
1359
|
-
return item;
|
1679
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1680
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1360
1681
|
};
|
1361
1682
|
_deleteRecord = new WeakSet();
|
1362
|
-
deleteRecord_fn = async function(recordId) {
|
1363
|
-
const fetchProps = await __privateGet$
|
1364
|
-
|
1365
|
-
|
1366
|
-
|
1367
|
-
|
1368
|
-
|
1369
|
-
|
1370
|
-
|
1371
|
-
|
1372
|
-
|
1373
|
-
|
1374
|
-
|
1375
|
-
|
1376
|
-
|
1377
|
-
await __privateGet$3(this, _cache).delete(key);
|
1683
|
+
deleteRecord_fn = async function(recordId, columns = ["*"]) {
|
1684
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1685
|
+
try {
|
1686
|
+
const response = await deleteRecord({
|
1687
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1688
|
+
queryParams: { columns },
|
1689
|
+
...fetchProps
|
1690
|
+
});
|
1691
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1692
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1693
|
+
} catch (e) {
|
1694
|
+
if (isObject(e) && e.status === 404) {
|
1695
|
+
return null;
|
1696
|
+
}
|
1697
|
+
throw e;
|
1378
1698
|
}
|
1379
1699
|
};
|
1380
|
-
_setCacheRecord = new WeakSet();
|
1381
|
-
setCacheRecord_fn = async function(record) {
|
1382
|
-
if (!__privateGet$3(this, _cache).cacheRecords)
|
1383
|
-
return;
|
1384
|
-
await __privateGet$3(this, _cache).set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
|
1385
|
-
};
|
1386
|
-
_getCacheRecord = new WeakSet();
|
1387
|
-
getCacheRecord_fn = async function(recordId) {
|
1388
|
-
if (!__privateGet$3(this, _cache).cacheRecords)
|
1389
|
-
return null;
|
1390
|
-
return __privateGet$3(this, _cache).get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
|
1391
|
-
};
|
1392
1700
|
_setCacheQuery = new WeakSet();
|
1393
1701
|
setCacheQuery_fn = async function(query, meta, records) {
|
1394
|
-
await __privateGet$
|
1702
|
+
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
1395
1703
|
};
|
1396
1704
|
_getCacheQuery = new WeakSet();
|
1397
1705
|
getCacheQuery_fn = async function(query) {
|
1398
|
-
const key = `query_${__privateGet$
|
1399
|
-
const result = await __privateGet$
|
1706
|
+
const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
|
1707
|
+
const result = await __privateGet$4(this, _cache).get(key);
|
1400
1708
|
if (!result)
|
1401
1709
|
return null;
|
1402
|
-
const { cache: ttl = __privateGet$
|
1403
|
-
if (
|
1404
|
-
return
|
1710
|
+
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1711
|
+
if (ttl < 0)
|
1712
|
+
return null;
|
1405
1713
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1406
1714
|
return hasExpired ? null : result;
|
1407
1715
|
};
|
1716
|
+
_getSchemaTables$1 = new WeakSet();
|
1717
|
+
getSchemaTables_fn$1 = async function() {
|
1718
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1719
|
+
return __privateGet$4(this, _schemaTables$2);
|
1720
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1721
|
+
const { schema } = await getBranchDetails({
|
1722
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1723
|
+
...fetchProps
|
1724
|
+
});
|
1725
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1726
|
+
return schema.tables;
|
1727
|
+
};
|
1408
1728
|
const transformObjectLinks = (object) => {
|
1409
1729
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
1410
1730
|
if (key === "xata")
|
@@ -1412,47 +1732,76 @@ const transformObjectLinks = (object) => {
|
|
1412
1732
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1413
1733
|
}, {});
|
1414
1734
|
};
|
1415
|
-
const initObject = (db,
|
1735
|
+
const initObject = (db, schemaTables, table, object) => {
|
1416
1736
|
const result = {};
|
1417
|
-
|
1418
|
-
|
1419
|
-
|
1420
|
-
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1737
|
+
const { xata, ...rest } = object ?? {};
|
1738
|
+
Object.assign(result, rest);
|
1739
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1740
|
+
if (!columns)
|
1741
|
+
console.error(`Table ${table} not found in schema`);
|
1742
|
+
for (const column of columns ?? []) {
|
1743
|
+
const value = result[column.name];
|
1744
|
+
switch (column.type) {
|
1745
|
+
case "datetime": {
|
1746
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1747
|
+
if (date && isNaN(date.getTime())) {
|
1748
|
+
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1749
|
+
} else if (date) {
|
1750
|
+
result[column.name] = date;
|
1751
|
+
}
|
1752
|
+
break;
|
1753
|
+
}
|
1754
|
+
case "link": {
|
1755
|
+
const linkTable = column.link?.table;
|
1756
|
+
if (!linkTable) {
|
1757
|
+
console.error(`Failed to parse link for field ${column.name}`);
|
1758
|
+
} else if (isObject(value)) {
|
1759
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1760
|
+
}
|
1761
|
+
break;
|
1762
|
+
}
|
1424
1763
|
}
|
1425
1764
|
}
|
1426
|
-
result.read = function() {
|
1427
|
-
return db[table].read(result["id"]);
|
1765
|
+
result.read = function(columns2) {
|
1766
|
+
return db[table].read(result["id"], columns2);
|
1428
1767
|
};
|
1429
|
-
result.update = function(data) {
|
1430
|
-
return db[table].update(result["id"], data);
|
1768
|
+
result.update = function(data, columns2) {
|
1769
|
+
return db[table].update(result["id"], data, columns2);
|
1431
1770
|
};
|
1432
1771
|
result.delete = function() {
|
1433
1772
|
return db[table].delete(result["id"]);
|
1434
1773
|
};
|
1435
|
-
|
1774
|
+
result.getMetadata = function() {
|
1775
|
+
return xata;
|
1776
|
+
};
|
1777
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1436
1778
|
Object.defineProperty(result, prop, { enumerable: false });
|
1437
1779
|
}
|
1438
1780
|
Object.freeze(result);
|
1439
1781
|
return result;
|
1440
1782
|
};
|
1441
|
-
function
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
if (
|
1446
|
-
return
|
1447
|
-
|
1448
|
-
|
1783
|
+
function isResponseWithRecords(value) {
|
1784
|
+
return isObject(value) && Array.isArray(value.records);
|
1785
|
+
}
|
1786
|
+
function extractId(value) {
|
1787
|
+
if (isString(value))
|
1788
|
+
return value;
|
1789
|
+
if (isObject(value) && isString(value.id))
|
1790
|
+
return value.id;
|
1791
|
+
return void 0;
|
1792
|
+
}
|
1793
|
+
function cleanFilter(filter) {
|
1794
|
+
if (!filter)
|
1795
|
+
return void 0;
|
1796
|
+
const values = Object.values(filter).filter(Boolean).filter((value) => Array.isArray(value) ? value.length > 0 : true);
|
1797
|
+
return values.length > 0 ? filter : void 0;
|
1449
1798
|
}
|
1450
1799
|
|
1451
1800
|
var __accessCheck$3 = (obj, member, msg) => {
|
1452
1801
|
if (!member.has(obj))
|
1453
1802
|
throw TypeError("Cannot " + msg);
|
1454
1803
|
};
|
1455
|
-
var __privateGet$
|
1804
|
+
var __privateGet$3 = (obj, member, getter) => {
|
1456
1805
|
__accessCheck$3(obj, member, "read from private field");
|
1457
1806
|
return getter ? getter.call(obj) : member.get(obj);
|
1458
1807
|
};
|
@@ -1461,7 +1810,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1461
1810
|
throw TypeError("Cannot add the same private member more than once");
|
1462
1811
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1463
1812
|
};
|
1464
|
-
var __privateSet$
|
1813
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1465
1814
|
__accessCheck$3(obj, member, "write to private field");
|
1466
1815
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1467
1816
|
return value;
|
@@ -1470,46 +1819,52 @@ var _map;
|
|
1470
1819
|
class SimpleCache {
|
1471
1820
|
constructor(options = {}) {
|
1472
1821
|
__privateAdd$3(this, _map, void 0);
|
1473
|
-
__privateSet$
|
1822
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1474
1823
|
this.capacity = options.max ?? 500;
|
1475
|
-
this.cacheRecords = options.cacheRecords ?? true;
|
1476
1824
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1477
1825
|
}
|
1478
1826
|
async getAll() {
|
1479
|
-
return Object.fromEntries(__privateGet$
|
1827
|
+
return Object.fromEntries(__privateGet$3(this, _map));
|
1480
1828
|
}
|
1481
1829
|
async get(key) {
|
1482
|
-
return __privateGet$
|
1830
|
+
return __privateGet$3(this, _map).get(key) ?? null;
|
1483
1831
|
}
|
1484
1832
|
async set(key, value) {
|
1485
1833
|
await this.delete(key);
|
1486
|
-
__privateGet$
|
1487
|
-
if (__privateGet$
|
1488
|
-
const leastRecentlyUsed = __privateGet$
|
1834
|
+
__privateGet$3(this, _map).set(key, value);
|
1835
|
+
if (__privateGet$3(this, _map).size > this.capacity) {
|
1836
|
+
const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
|
1489
1837
|
await this.delete(leastRecentlyUsed);
|
1490
1838
|
}
|
1491
1839
|
}
|
1492
1840
|
async delete(key) {
|
1493
|
-
__privateGet$
|
1841
|
+
__privateGet$3(this, _map).delete(key);
|
1494
1842
|
}
|
1495
1843
|
async clear() {
|
1496
|
-
return __privateGet$
|
1844
|
+
return __privateGet$3(this, _map).clear();
|
1497
1845
|
}
|
1498
1846
|
}
|
1499
1847
|
_map = new WeakMap();
|
1500
1848
|
|
1501
|
-
const
|
1502
|
-
const
|
1503
|
-
const
|
1504
|
-
const
|
1505
|
-
const
|
1506
|
-
const
|
1849
|
+
const greaterThan = (value) => ({ $gt: value });
|
1850
|
+
const gt = greaterThan;
|
1851
|
+
const greaterThanEquals = (value) => ({ $ge: value });
|
1852
|
+
const greaterEquals = greaterThanEquals;
|
1853
|
+
const gte = greaterThanEquals;
|
1854
|
+
const ge = greaterThanEquals;
|
1855
|
+
const lessThan = (value) => ({ $lt: value });
|
1856
|
+
const lt = lessThan;
|
1857
|
+
const lessThanEquals = (value) => ({ $le: value });
|
1858
|
+
const lessEquals = lessThanEquals;
|
1859
|
+
const lte = lessThanEquals;
|
1860
|
+
const le = lessThanEquals;
|
1507
1861
|
const exists = (column) => ({ $exists: column });
|
1508
1862
|
const notExists = (column) => ({ $notExists: column });
|
1509
1863
|
const startsWith = (value) => ({ $startsWith: value });
|
1510
1864
|
const endsWith = (value) => ({ $endsWith: value });
|
1511
1865
|
const pattern = (value) => ({ $pattern: value });
|
1512
1866
|
const is = (value) => ({ $is: value });
|
1867
|
+
const equals = is;
|
1513
1868
|
const isNot = (value) => ({ $isNot: value });
|
1514
1869
|
const contains = (value) => ({ $contains: value });
|
1515
1870
|
const includes = (value) => ({ $includes: value });
|
@@ -1521,7 +1876,7 @@ var __accessCheck$2 = (obj, member, msg) => {
|
|
1521
1876
|
if (!member.has(obj))
|
1522
1877
|
throw TypeError("Cannot " + msg);
|
1523
1878
|
};
|
1524
|
-
var __privateGet$
|
1879
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1525
1880
|
__accessCheck$2(obj, member, "read from private field");
|
1526
1881
|
return getter ? getter.call(obj) : member.get(obj);
|
1527
1882
|
};
|
@@ -1530,130 +1885,178 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1530
1885
|
throw TypeError("Cannot add the same private member more than once");
|
1531
1886
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1532
1887
|
};
|
1533
|
-
var
|
1888
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1889
|
+
__accessCheck$2(obj, member, "write to private field");
|
1890
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1891
|
+
return value;
|
1892
|
+
};
|
1893
|
+
var _tables, _schemaTables$1;
|
1534
1894
|
class SchemaPlugin extends XataPlugin {
|
1535
|
-
constructor(
|
1895
|
+
constructor(schemaTables) {
|
1536
1896
|
super();
|
1537
|
-
this.links = links;
|
1538
|
-
this.tableNames = tableNames;
|
1539
1897
|
__privateAdd$2(this, _tables, {});
|
1898
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1899
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1540
1900
|
}
|
1541
1901
|
build(pluginOptions) {
|
1542
|
-
const
|
1543
|
-
|
1544
|
-
|
1545
|
-
|
1546
|
-
|
1547
|
-
|
1548
|
-
__privateGet$
|
1902
|
+
const db = new Proxy(
|
1903
|
+
{},
|
1904
|
+
{
|
1905
|
+
get: (_target, table) => {
|
1906
|
+
if (!isString(table))
|
1907
|
+
throw new Error("Invalid table name");
|
1908
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1909
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1910
|
+
}
|
1911
|
+
return __privateGet$2(this, _tables)[table];
|
1549
1912
|
}
|
1550
|
-
return __privateGet$1(this, _tables)[table];
|
1551
1913
|
}
|
1552
|
-
|
1553
|
-
|
1554
|
-
|
1914
|
+
);
|
1915
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1916
|
+
for (const table of tableNames) {
|
1917
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1555
1918
|
}
|
1556
1919
|
return db;
|
1557
1920
|
}
|
1558
1921
|
}
|
1559
1922
|
_tables = new WeakMap();
|
1923
|
+
_schemaTables$1 = new WeakMap();
|
1560
1924
|
|
1561
1925
|
var __accessCheck$1 = (obj, member, msg) => {
|
1562
1926
|
if (!member.has(obj))
|
1563
1927
|
throw TypeError("Cannot " + msg);
|
1564
1928
|
};
|
1929
|
+
var __privateGet$1 = (obj, member, getter) => {
|
1930
|
+
__accessCheck$1(obj, member, "read from private field");
|
1931
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1932
|
+
};
|
1565
1933
|
var __privateAdd$1 = (obj, member, value) => {
|
1566
1934
|
if (member.has(obj))
|
1567
1935
|
throw TypeError("Cannot add the same private member more than once");
|
1568
1936
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1569
1937
|
};
|
1938
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
1939
|
+
__accessCheck$1(obj, member, "write to private field");
|
1940
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1941
|
+
return value;
|
1942
|
+
};
|
1570
1943
|
var __privateMethod$1 = (obj, member, method) => {
|
1571
1944
|
__accessCheck$1(obj, member, "access private method");
|
1572
1945
|
return method;
|
1573
1946
|
};
|
1574
|
-
var _search, search_fn;
|
1947
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1575
1948
|
class SearchPlugin extends XataPlugin {
|
1576
|
-
constructor(db,
|
1949
|
+
constructor(db, schemaTables) {
|
1577
1950
|
super();
|
1578
1951
|
this.db = db;
|
1579
|
-
this.links = links;
|
1580
1952
|
__privateAdd$1(this, _search);
|
1953
|
+
__privateAdd$1(this, _getSchemaTables);
|
1954
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1955
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1581
1956
|
}
|
1582
1957
|
build({ getFetchProps }) {
|
1583
1958
|
return {
|
1584
1959
|
all: async (query, options = {}) => {
|
1585
1960
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1961
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1586
1962
|
return records.map((record) => {
|
1587
1963
|
const { table = "orphan" } = record.xata;
|
1588
|
-
return { table, record: initObject(this.db,
|
1964
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1589
1965
|
});
|
1590
1966
|
},
|
1591
1967
|
byTable: async (query, options = {}) => {
|
1592
1968
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1969
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1593
1970
|
return records.reduce((acc, record) => {
|
1594
1971
|
const { table = "orphan" } = record.xata;
|
1595
1972
|
const items = acc[table] ?? [];
|
1596
|
-
const item = initObject(this.db,
|
1973
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1597
1974
|
return { ...acc, [table]: [...items, item] };
|
1598
1975
|
}, {});
|
1599
1976
|
}
|
1600
1977
|
};
|
1601
1978
|
}
|
1602
1979
|
}
|
1980
|
+
_schemaTables = new WeakMap();
|
1603
1981
|
_search = new WeakSet();
|
1604
1982
|
search_fn = async function(query, options, getFetchProps) {
|
1605
1983
|
const fetchProps = await getFetchProps();
|
1606
|
-
const { tables, fuzziness } = options ?? {};
|
1984
|
+
const { tables, fuzziness, highlight, prefix } = options ?? {};
|
1607
1985
|
const { records } = await searchBranch({
|
1608
1986
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1609
|
-
body: { tables, query, fuzziness },
|
1987
|
+
body: { tables, query, fuzziness, prefix, highlight },
|
1610
1988
|
...fetchProps
|
1611
1989
|
});
|
1612
1990
|
return records;
|
1613
1991
|
};
|
1992
|
+
_getSchemaTables = new WeakSet();
|
1993
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1994
|
+
if (__privateGet$1(this, _schemaTables))
|
1995
|
+
return __privateGet$1(this, _schemaTables);
|
1996
|
+
const fetchProps = await getFetchProps();
|
1997
|
+
const { schema } = await getBranchDetails({
|
1998
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1999
|
+
...fetchProps
|
2000
|
+
});
|
2001
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
2002
|
+
return schema.tables;
|
2003
|
+
};
|
1614
2004
|
|
1615
2005
|
const isBranchStrategyBuilder = (strategy) => {
|
1616
2006
|
return typeof strategy === "function";
|
1617
2007
|
};
|
1618
2008
|
|
1619
|
-
const envBranchNames = [
|
1620
|
-
"XATA_BRANCH",
|
1621
|
-
"VERCEL_GIT_COMMIT_REF",
|
1622
|
-
"CF_PAGES_BRANCH",
|
1623
|
-
"BRANCH"
|
1624
|
-
];
|
1625
|
-
const defaultBranch = "main";
|
1626
2009
|
async function getCurrentBranchName(options) {
|
1627
|
-
const
|
1628
|
-
if (
|
1629
|
-
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
return defaultBranch;
|
2010
|
+
const { branch, envBranch } = getEnvironment();
|
2011
|
+
if (branch) {
|
2012
|
+
const details = await getDatabaseBranch(branch, options);
|
2013
|
+
if (details)
|
2014
|
+
return branch;
|
2015
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
2016
|
+
}
|
2017
|
+
const gitBranch = envBranch || await getGitBranch();
|
2018
|
+
return resolveXataBranch(gitBranch, options);
|
1637
2019
|
}
|
1638
2020
|
async function getCurrentBranchDetails(options) {
|
1639
|
-
const
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
1645
|
-
|
1646
|
-
|
1647
|
-
|
1648
|
-
|
2021
|
+
const branch = await getCurrentBranchName(options);
|
2022
|
+
return getDatabaseBranch(branch, options);
|
2023
|
+
}
|
2024
|
+
async function resolveXataBranch(gitBranch, options) {
|
2025
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
2026
|
+
const apiKey = options?.apiKey || getAPIKey();
|
2027
|
+
if (!databaseURL)
|
2028
|
+
throw new Error(
|
2029
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
2030
|
+
);
|
2031
|
+
if (!apiKey)
|
2032
|
+
throw new Error(
|
2033
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
2034
|
+
);
|
2035
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
2036
|
+
const [workspace] = host.split(".");
|
2037
|
+
const { fallbackBranch } = getEnvironment();
|
2038
|
+
const { branch } = await resolveBranch({
|
2039
|
+
apiKey,
|
2040
|
+
apiUrl: databaseURL,
|
2041
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
2042
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
2043
|
+
pathParams: { dbName, workspace },
|
2044
|
+
queryParams: { gitBranch, fallbackBranch },
|
2045
|
+
trace: defaultTrace
|
2046
|
+
});
|
2047
|
+
return branch;
|
1649
2048
|
}
|
1650
2049
|
async function getDatabaseBranch(branch, options) {
|
1651
2050
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1652
2051
|
const apiKey = options?.apiKey || getAPIKey();
|
1653
2052
|
if (!databaseURL)
|
1654
|
-
throw new Error(
|
2053
|
+
throw new Error(
|
2054
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
2055
|
+
);
|
1655
2056
|
if (!apiKey)
|
1656
|
-
throw new Error(
|
2057
|
+
throw new Error(
|
2058
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
2059
|
+
);
|
1657
2060
|
const [protocol, , host, , database] = databaseURL.split("/");
|
1658
2061
|
const [workspace] = host.split(".");
|
1659
2062
|
const dbBranchName = `${database}:${branch}`;
|
@@ -1663,10 +2066,8 @@ async function getDatabaseBranch(branch, options) {
|
|
1663
2066
|
apiUrl: databaseURL,
|
1664
2067
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1665
2068
|
workspacesApiUrl: `${protocol}//${host}`,
|
1666
|
-
pathParams: {
|
1667
|
-
|
1668
|
-
workspace
|
1669
|
-
}
|
2069
|
+
pathParams: { dbBranchName, workspace },
|
2070
|
+
trace: defaultTrace
|
1670
2071
|
});
|
1671
2072
|
} catch (err) {
|
1672
2073
|
if (isObject(err) && err.status === 404)
|
@@ -1674,21 +2075,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1674
2075
|
throw err;
|
1675
2076
|
}
|
1676
2077
|
}
|
1677
|
-
function getBranchByEnvVariable() {
|
1678
|
-
for (const name of envBranchNames) {
|
1679
|
-
const value = getEnvVariable(name);
|
1680
|
-
if (value) {
|
1681
|
-
return value;
|
1682
|
-
}
|
1683
|
-
}
|
1684
|
-
try {
|
1685
|
-
return XATA_BRANCH;
|
1686
|
-
} catch (err) {
|
1687
|
-
}
|
1688
|
-
}
|
1689
2078
|
function getDatabaseURL() {
|
1690
2079
|
try {
|
1691
|
-
|
2080
|
+
const { databaseURL } = getEnvironment();
|
2081
|
+
return databaseURL;
|
1692
2082
|
} catch (err) {
|
1693
2083
|
return void 0;
|
1694
2084
|
}
|
@@ -1717,24 +2107,27 @@ var __privateMethod = (obj, member, method) => {
|
|
1717
2107
|
return method;
|
1718
2108
|
};
|
1719
2109
|
const buildClient = (plugins) => {
|
1720
|
-
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
2110
|
+
var _branch, _options, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1721
2111
|
return _a = class {
|
1722
|
-
constructor(options = {},
|
2112
|
+
constructor(options = {}, schemaTables) {
|
1723
2113
|
__privateAdd(this, _parseOptions);
|
1724
2114
|
__privateAdd(this, _getFetchProps);
|
1725
2115
|
__privateAdd(this, _evaluateBranch);
|
1726
2116
|
__privateAdd(this, _branch, void 0);
|
2117
|
+
__privateAdd(this, _options, void 0);
|
1727
2118
|
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
2119
|
+
__privateSet(this, _options, safeOptions);
|
1728
2120
|
const pluginOptions = {
|
1729
2121
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1730
|
-
cache: safeOptions.cache
|
2122
|
+
cache: safeOptions.cache,
|
2123
|
+
trace: safeOptions.trace
|
1731
2124
|
};
|
1732
|
-
const db = new SchemaPlugin(
|
1733
|
-
const search = new SearchPlugin(db,
|
2125
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2126
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1734
2127
|
this.db = db;
|
1735
2128
|
this.search = search;
|
1736
2129
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1737
|
-
if (
|
2130
|
+
if (namespace === void 0)
|
1738
2131
|
continue;
|
1739
2132
|
const result = namespace.build(pluginOptions);
|
1740
2133
|
if (result instanceof Promise) {
|
@@ -1746,22 +2139,26 @@ const buildClient = (plugins) => {
|
|
1746
2139
|
}
|
1747
2140
|
}
|
1748
2141
|
}
|
1749
|
-
|
2142
|
+
async getConfig() {
|
2143
|
+
const databaseURL = __privateGet(this, _options).databaseURL;
|
2144
|
+
const branch = await __privateGet(this, _options).branch();
|
2145
|
+
return { databaseURL, branch };
|
2146
|
+
}
|
2147
|
+
}, _branch = new WeakMap(), _options = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
|
1750
2148
|
const fetch = getFetchImplementation(options?.fetch);
|
1751
2149
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1752
2150
|
const apiKey = options?.apiKey || getAPIKey();
|
1753
|
-
const cache = options?.cache ?? new SimpleCache({
|
1754
|
-
const
|
1755
|
-
|
1756
|
-
|
2151
|
+
const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
|
2152
|
+
const trace = options?.trace ?? defaultTrace;
|
2153
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
2154
|
+
if (!apiKey) {
|
2155
|
+
throw new Error("Option apiKey is required");
|
1757
2156
|
}
|
1758
|
-
|
1759
|
-
|
1760
|
-
|
1761
|
-
apiKey,
|
1762
|
-
|
1763
|
-
branch
|
1764
|
-
}) {
|
2157
|
+
if (!databaseURL) {
|
2158
|
+
throw new Error("Option databaseURL is required");
|
2159
|
+
}
|
2160
|
+
return { fetch, databaseURL, apiKey, branch, cache, trace };
|
2161
|
+
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({ fetch, apiKey, databaseURL, branch, trace }) {
|
1765
2162
|
const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
|
1766
2163
|
if (!branchValue)
|
1767
2164
|
throw new Error("Unable to resolve branch value");
|
@@ -1773,12 +2170,13 @@ const buildClient = (plugins) => {
|
|
1773
2170
|
const hasBranch = params.dbBranchName ?? params.branch;
|
1774
2171
|
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : "");
|
1775
2172
|
return databaseURL + newPath;
|
1776
|
-
}
|
2173
|
+
},
|
2174
|
+
trace
|
1777
2175
|
};
|
1778
2176
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1779
2177
|
if (__privateGet(this, _branch))
|
1780
2178
|
return __privateGet(this, _branch);
|
1781
|
-
if (
|
2179
|
+
if (param === void 0)
|
1782
2180
|
return void 0;
|
1783
2181
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1784
2182
|
const evaluateBranch = async (strategy) => {
|
@@ -1796,6 +2194,88 @@ const buildClient = (plugins) => {
|
|
1796
2194
|
class BaseClient extends buildClient() {
|
1797
2195
|
}
|
1798
2196
|
|
2197
|
+
const META = "__";
|
2198
|
+
const VALUE = "___";
|
2199
|
+
class Serializer {
|
2200
|
+
constructor() {
|
2201
|
+
this.classes = {};
|
2202
|
+
}
|
2203
|
+
add(clazz) {
|
2204
|
+
this.classes[clazz.name] = clazz;
|
2205
|
+
}
|
2206
|
+
toJSON(data) {
|
2207
|
+
function visit(obj) {
|
2208
|
+
if (Array.isArray(obj))
|
2209
|
+
return obj.map(visit);
|
2210
|
+
const type = typeof obj;
|
2211
|
+
if (type === "undefined")
|
2212
|
+
return { [META]: "undefined" };
|
2213
|
+
if (type === "bigint")
|
2214
|
+
return { [META]: "bigint", [VALUE]: obj.toString() };
|
2215
|
+
if (obj === null || type !== "object")
|
2216
|
+
return obj;
|
2217
|
+
const constructor = obj.constructor;
|
2218
|
+
const o = { [META]: constructor.name };
|
2219
|
+
for (const [key, value] of Object.entries(obj)) {
|
2220
|
+
o[key] = visit(value);
|
2221
|
+
}
|
2222
|
+
if (constructor === Date)
|
2223
|
+
o[VALUE] = obj.toISOString();
|
2224
|
+
if (constructor === Map)
|
2225
|
+
o[VALUE] = Object.fromEntries(obj);
|
2226
|
+
if (constructor === Set)
|
2227
|
+
o[VALUE] = [...obj];
|
2228
|
+
return o;
|
2229
|
+
}
|
2230
|
+
return JSON.stringify(visit(data));
|
2231
|
+
}
|
2232
|
+
fromJSON(json) {
|
2233
|
+
return JSON.parse(json, (key, value) => {
|
2234
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
2235
|
+
const { [META]: clazz, [VALUE]: val, ...rest } = value;
|
2236
|
+
const constructor = this.classes[clazz];
|
2237
|
+
if (constructor) {
|
2238
|
+
return Object.assign(Object.create(constructor.prototype), rest);
|
2239
|
+
}
|
2240
|
+
if (clazz === "Date")
|
2241
|
+
return new Date(val);
|
2242
|
+
if (clazz === "Set")
|
2243
|
+
return new Set(val);
|
2244
|
+
if (clazz === "Map")
|
2245
|
+
return new Map(Object.entries(val));
|
2246
|
+
if (clazz === "bigint")
|
2247
|
+
return BigInt(val);
|
2248
|
+
if (clazz === "undefined")
|
2249
|
+
return void 0;
|
2250
|
+
return rest;
|
2251
|
+
}
|
2252
|
+
return value;
|
2253
|
+
});
|
2254
|
+
}
|
2255
|
+
}
|
2256
|
+
const defaultSerializer = new Serializer();
|
2257
|
+
const serialize = (data) => {
|
2258
|
+
return defaultSerializer.toJSON(data);
|
2259
|
+
};
|
2260
|
+
const deserialize = (json) => {
|
2261
|
+
return defaultSerializer.fromJSON(json);
|
2262
|
+
};
|
2263
|
+
|
2264
|
+
function buildWorkerRunner(config) {
|
2265
|
+
return function xataWorker(name, _worker) {
|
2266
|
+
return async (...args) => {
|
2267
|
+
const url = process.env.NODE_ENV === "development" ? `http://localhost:64749/${name}` : `https://dispatcher.xata.workers.dev/${config.workspace}/${config.worker}/${name}`;
|
2268
|
+
const result = await fetch(url, {
|
2269
|
+
method: "POST",
|
2270
|
+
headers: { "Content-Type": "application/json" },
|
2271
|
+
body: serialize({ args })
|
2272
|
+
});
|
2273
|
+
const text = await result.text();
|
2274
|
+
return deserialize(text);
|
2275
|
+
};
|
2276
|
+
};
|
2277
|
+
}
|
2278
|
+
|
1799
2279
|
class XataError extends Error {
|
1800
2280
|
constructor(message, status) {
|
1801
2281
|
super(message);
|
@@ -1803,5 +2283,5 @@ class XataError extends Error {
|
|
1803
2283
|
}
|
1804
2284
|
}
|
1805
2285
|
|
1806
|
-
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeWorkspaceMember, resendWorkspaceMemberInvite, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
2286
|
+
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, RecordArray, Repository, RestRepository, SchemaPlugin, SearchPlugin, Serializer, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, buildWorkerRunner, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, deserialize, endsWith, equals, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseMetadata, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, greaterEquals, greaterThan, greaterThanEquals, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lessEquals, lessThan, lessThanEquals, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, serialize, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberInvite, updateWorkspaceMemberRole, upsertRecordWithID };
|
1807
2287
|
//# sourceMappingURL=index.mjs.map
|