@xata.io/client 0.0.0-alpha.vfd6aaf3 → 0.0.0-alpha.vfdcf483
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +72 -0
- package/README.md +2 -0
- package/Usage.md +60 -6
- package/dist/index.cjs +674 -415
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +722 -192
- package/dist/index.mjs +644 -416
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
@@ -2,6 +2,48 @@
|
|
2
2
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
4
4
|
|
5
|
+
function _interopNamespace(e) {
|
6
|
+
if (e && e.__esModule) return e;
|
7
|
+
var n = Object.create(null);
|
8
|
+
if (e) {
|
9
|
+
Object.keys(e).forEach(function (k) {
|
10
|
+
if (k !== 'default') {
|
11
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
12
|
+
Object.defineProperty(n, k, d.get ? d : {
|
13
|
+
enumerable: true,
|
14
|
+
get: function () { return e[k]; }
|
15
|
+
});
|
16
|
+
}
|
17
|
+
});
|
18
|
+
}
|
19
|
+
n["default"] = e;
|
20
|
+
return Object.freeze(n);
|
21
|
+
}
|
22
|
+
|
23
|
+
const defaultTrace = async (_name, fn, _options) => {
|
24
|
+
return await fn({
|
25
|
+
setAttributes: () => {
|
26
|
+
return;
|
27
|
+
},
|
28
|
+
onError: () => {
|
29
|
+
return;
|
30
|
+
}
|
31
|
+
});
|
32
|
+
};
|
33
|
+
const TraceAttributes = {
|
34
|
+
VERSION: "xata.sdk.version",
|
35
|
+
TABLE: "xata.table",
|
36
|
+
HTTP_REQUEST_ID: "http.request_id",
|
37
|
+
HTTP_STATUS_CODE: "http.status_code",
|
38
|
+
HTTP_HOST: "http.host",
|
39
|
+
HTTP_SCHEME: "http.scheme",
|
40
|
+
HTTP_USER_AGENT: "http.user_agent",
|
41
|
+
HTTP_METHOD: "http.method",
|
42
|
+
HTTP_URL: "http.url",
|
43
|
+
HTTP_ROUTE: "http.route",
|
44
|
+
HTTP_TARGET: "http.target"
|
45
|
+
};
|
46
|
+
|
5
47
|
function notEmpty(value) {
|
6
48
|
return value !== null && value !== void 0;
|
7
49
|
}
|
@@ -17,6 +59,9 @@ function isDefined(value) {
|
|
17
59
|
function isString(value) {
|
18
60
|
return isDefined(value) && typeof value === "string";
|
19
61
|
}
|
62
|
+
function isStringArray(value) {
|
63
|
+
return isDefined(value) && Array.isArray(value) && value.every(isString);
|
64
|
+
}
|
20
65
|
function toBase64(value) {
|
21
66
|
try {
|
22
67
|
return btoa(value);
|
@@ -26,35 +71,83 @@ function toBase64(value) {
|
|
26
71
|
}
|
27
72
|
}
|
28
73
|
|
29
|
-
function
|
74
|
+
function getEnvironment() {
|
30
75
|
try {
|
31
|
-
if (isObject(process) &&
|
32
|
-
return
|
76
|
+
if (isObject(process) && isObject(process.env)) {
|
77
|
+
return {
|
78
|
+
apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
|
79
|
+
databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
|
80
|
+
branch: process.env.XATA_BRANCH ?? getGlobalBranch(),
|
81
|
+
envBranch: process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH,
|
82
|
+
fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
|
83
|
+
};
|
33
84
|
}
|
34
85
|
} catch (err) {
|
35
86
|
}
|
36
87
|
try {
|
37
|
-
if (isObject(Deno) &&
|
38
|
-
return
|
88
|
+
if (isObject(Deno) && isObject(Deno.env)) {
|
89
|
+
return {
|
90
|
+
apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
|
91
|
+
databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
|
92
|
+
branch: Deno.env.get("XATA_BRANCH") ?? getGlobalBranch(),
|
93
|
+
envBranch: Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH"),
|
94
|
+
fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
|
95
|
+
};
|
39
96
|
}
|
40
97
|
} catch (err) {
|
41
98
|
}
|
99
|
+
return {
|
100
|
+
apiKey: getGlobalApiKey(),
|
101
|
+
databaseURL: getGlobalDatabaseURL(),
|
102
|
+
branch: getGlobalBranch(),
|
103
|
+
envBranch: void 0,
|
104
|
+
fallbackBranch: getGlobalFallbackBranch()
|
105
|
+
};
|
106
|
+
}
|
107
|
+
function getGlobalApiKey() {
|
108
|
+
try {
|
109
|
+
return XATA_API_KEY;
|
110
|
+
} catch (err) {
|
111
|
+
return void 0;
|
112
|
+
}
|
113
|
+
}
|
114
|
+
function getGlobalDatabaseURL() {
|
115
|
+
try {
|
116
|
+
return XATA_DATABASE_URL;
|
117
|
+
} catch (err) {
|
118
|
+
return void 0;
|
119
|
+
}
|
120
|
+
}
|
121
|
+
function getGlobalBranch() {
|
122
|
+
try {
|
123
|
+
return XATA_BRANCH;
|
124
|
+
} catch (err) {
|
125
|
+
return void 0;
|
126
|
+
}
|
127
|
+
}
|
128
|
+
function getGlobalFallbackBranch() {
|
129
|
+
try {
|
130
|
+
return XATA_FALLBACK_BRANCH;
|
131
|
+
} catch (err) {
|
132
|
+
return void 0;
|
133
|
+
}
|
42
134
|
}
|
43
135
|
async function getGitBranch() {
|
136
|
+
const cmd = ["git", "branch", "--show-current"];
|
137
|
+
const fullCmd = cmd.join(" ");
|
138
|
+
const nodeModule = ["child", "process"].join("_");
|
139
|
+
const execOptions = { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] };
|
44
140
|
try {
|
45
141
|
if (typeof require === "function") {
|
46
|
-
|
47
|
-
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
142
|
+
return require(nodeModule).execSync(fullCmd, execOptions).trim();
|
48
143
|
}
|
144
|
+
const { execSync } = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(nodeModule);
|
145
|
+
return execSync(fullCmd, execOptions).toString().trim();
|
49
146
|
} catch (err) {
|
50
147
|
}
|
51
148
|
try {
|
52
149
|
if (isObject(Deno)) {
|
53
|
-
const process2 = Deno.run({
|
54
|
-
cmd: ["git", "branch", "--show-current"],
|
55
|
-
stdout: "piped",
|
56
|
-
stderr: "piped"
|
57
|
-
});
|
150
|
+
const process2 = Deno.run({ cmd, stdout: "piped", stderr: "null" });
|
58
151
|
return new TextDecoder().decode(await process2.output()).trim();
|
59
152
|
}
|
60
153
|
} catch (err) {
|
@@ -63,7 +156,8 @@ async function getGitBranch() {
|
|
63
156
|
|
64
157
|
function getAPIKey() {
|
65
158
|
try {
|
66
|
-
|
159
|
+
const { apiKey } = getEnvironment();
|
160
|
+
return apiKey;
|
67
161
|
} catch (err) {
|
68
162
|
return void 0;
|
69
163
|
}
|
@@ -73,12 +167,14 @@ function getFetchImplementation(userFetch) {
|
|
73
167
|
const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
|
74
168
|
const fetchImpl = userFetch ?? globalFetch;
|
75
169
|
if (!fetchImpl) {
|
76
|
-
throw new Error(
|
170
|
+
throw new Error(
|
171
|
+
`Couldn't find \`fetch\`. Install a fetch implementation such as \`node-fetch\` and pass it explicitly.`
|
172
|
+
);
|
77
173
|
}
|
78
174
|
return fetchImpl;
|
79
175
|
}
|
80
176
|
|
81
|
-
const VERSION = "0.0.0-alpha.
|
177
|
+
const VERSION = "0.0.0-alpha.vfdcf483";
|
82
178
|
|
83
179
|
class ErrorWithCause extends Error {
|
84
180
|
constructor(message, options) {
|
@@ -157,34 +253,62 @@ async function fetch$1({
|
|
157
253
|
fetchImpl,
|
158
254
|
apiKey,
|
159
255
|
apiUrl,
|
160
|
-
workspacesApiUrl
|
256
|
+
workspacesApiUrl,
|
257
|
+
trace
|
161
258
|
}) {
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
259
|
+
return trace(
|
260
|
+
`${method.toUpperCase()} ${path}`,
|
261
|
+
async ({ setAttributes, onError }) => {
|
262
|
+
const baseUrl = buildBaseUrl({ path, workspacesApiUrl, pathParams, apiUrl });
|
263
|
+
const fullUrl = resolveUrl(baseUrl, queryParams, pathParams);
|
264
|
+
const url = fullUrl.includes("localhost") ? fullUrl.replace(/^[^.]+\./, "http://") : fullUrl;
|
265
|
+
setAttributes({
|
266
|
+
[TraceAttributes.HTTP_URL]: url,
|
267
|
+
[TraceAttributes.HTTP_TARGET]: resolveUrl(path, queryParams, pathParams)
|
268
|
+
});
|
269
|
+
const response = await fetchImpl(url, {
|
270
|
+
method: method.toUpperCase(),
|
271
|
+
body: body ? JSON.stringify(body) : void 0,
|
272
|
+
headers: {
|
273
|
+
"Content-Type": "application/json",
|
274
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
275
|
+
...headers,
|
276
|
+
...hostHeader(fullUrl),
|
277
|
+
Authorization: `Bearer ${apiKey}`
|
278
|
+
}
|
279
|
+
});
|
280
|
+
if (response.status === 204) {
|
281
|
+
return {};
|
282
|
+
}
|
283
|
+
const { host, protocol } = parseUrl(response.url);
|
284
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
285
|
+
setAttributes({
|
286
|
+
[TraceAttributes.HTTP_REQUEST_ID]: requestId,
|
287
|
+
[TraceAttributes.HTTP_STATUS_CODE]: response.status,
|
288
|
+
[TraceAttributes.HTTP_HOST]: host,
|
289
|
+
[TraceAttributes.HTTP_SCHEME]: protocol?.replace(":", "")
|
290
|
+
});
|
291
|
+
try {
|
292
|
+
const jsonResponse = await response.json();
|
293
|
+
if (response.ok) {
|
294
|
+
return jsonResponse;
|
295
|
+
}
|
296
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
297
|
+
} catch (error) {
|
298
|
+
const fetcherError = new FetcherError(response.status, error, requestId);
|
299
|
+
onError(fetcherError.message);
|
300
|
+
throw fetcherError;
|
301
|
+
}
|
302
|
+
},
|
303
|
+
{ [TraceAttributes.HTTP_METHOD]: method.toUpperCase(), [TraceAttributes.HTTP_ROUTE]: path }
|
304
|
+
);
|
305
|
+
}
|
306
|
+
function parseUrl(url) {
|
180
307
|
try {
|
181
|
-
const
|
182
|
-
|
183
|
-
return jsonResponse;
|
184
|
-
}
|
185
|
-
throw new FetcherError(response.status, jsonResponse, requestId);
|
308
|
+
const { host, protocol } = new URL(url);
|
309
|
+
return { host, protocol };
|
186
310
|
} catch (error) {
|
187
|
-
|
311
|
+
return {};
|
188
312
|
}
|
189
313
|
}
|
190
314
|
|
@@ -243,6 +367,7 @@ const removeWorkspaceMember = (variables) => fetch$1({
|
|
243
367
|
...variables
|
244
368
|
});
|
245
369
|
const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
|
370
|
+
const updateWorkspaceMemberInvite = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites/{inviteId}", method: "patch", ...variables });
|
246
371
|
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
247
372
|
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
248
373
|
method: "delete",
|
@@ -278,6 +403,11 @@ const deleteDatabase = (variables) => fetch$1({
|
|
278
403
|
method: "delete",
|
279
404
|
...variables
|
280
405
|
});
|
406
|
+
const getDatabaseMetadata = (variables) => fetch$1({
|
407
|
+
url: "/dbs/{dbName}/metadata",
|
408
|
+
method: "get",
|
409
|
+
...variables
|
410
|
+
});
|
281
411
|
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
282
412
|
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
283
413
|
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
@@ -291,11 +421,7 @@ const getBranchDetails = (variables) => fetch$1({
|
|
291
421
|
method: "get",
|
292
422
|
...variables
|
293
423
|
});
|
294
|
-
const createBranch = (variables) => fetch$1({
|
295
|
-
url: "/db/{dbBranchName}",
|
296
|
-
method: "put",
|
297
|
-
...variables
|
298
|
-
});
|
424
|
+
const createBranch = (variables) => fetch$1({ url: "/db/{dbBranchName}", method: "put", ...variables });
|
299
425
|
const deleteBranch = (variables) => fetch$1({
|
300
426
|
url: "/db/{dbBranchName}",
|
301
427
|
method: "delete",
|
@@ -369,11 +495,7 @@ const updateColumn = (variables) => fetch$1({
|
|
369
495
|
method: "patch",
|
370
496
|
...variables
|
371
497
|
});
|
372
|
-
const insertRecord = (variables) => fetch$1({
|
373
|
-
url: "/db/{dbBranchName}/tables/{tableName}/data",
|
374
|
-
method: "post",
|
375
|
-
...variables
|
376
|
-
});
|
498
|
+
const insertRecord = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data", method: "post", ...variables });
|
377
499
|
const insertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "put", ...variables });
|
378
500
|
const updateRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "patch", ...variables });
|
379
501
|
const upsertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "post", ...variables });
|
@@ -415,6 +537,7 @@ const operationsByTag = {
|
|
415
537
|
updateWorkspaceMemberRole,
|
416
538
|
removeWorkspaceMember,
|
417
539
|
inviteWorkspaceMember,
|
540
|
+
updateWorkspaceMemberInvite,
|
418
541
|
cancelWorkspaceMemberInvite,
|
419
542
|
resendWorkspaceMemberInvite,
|
420
543
|
acceptWorkspaceMemberInvite
|
@@ -423,6 +546,7 @@ const operationsByTag = {
|
|
423
546
|
getDatabaseList,
|
424
547
|
createDatabase,
|
425
548
|
deleteDatabase,
|
549
|
+
getDatabaseMetadata,
|
426
550
|
getGitBranchesMapping,
|
427
551
|
addGitBranchesEntry,
|
428
552
|
removeGitBranchesEntry,
|
@@ -504,7 +628,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
504
628
|
throw TypeError("Cannot add the same private member more than once");
|
505
629
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
506
630
|
};
|
507
|
-
var __privateSet$
|
631
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
508
632
|
__accessCheck$7(obj, member, "write to private field");
|
509
633
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
510
634
|
return value;
|
@@ -515,15 +639,17 @@ class XataApiClient {
|
|
515
639
|
__privateAdd$7(this, _extraProps, void 0);
|
516
640
|
__privateAdd$7(this, _namespaces, {});
|
517
641
|
const provider = options.host ?? "production";
|
518
|
-
const apiKey = options
|
642
|
+
const apiKey = options.apiKey ?? getAPIKey();
|
643
|
+
const trace = options.trace ?? defaultTrace;
|
519
644
|
if (!apiKey) {
|
520
645
|
throw new Error("Could not resolve a valid apiKey");
|
521
646
|
}
|
522
|
-
__privateSet$
|
647
|
+
__privateSet$7(this, _extraProps, {
|
523
648
|
apiUrl: getHostUrl(provider, "main"),
|
524
649
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
525
650
|
fetchImpl: getFetchImplementation(options.fetch),
|
526
|
-
apiKey
|
651
|
+
apiKey,
|
652
|
+
trace
|
527
653
|
});
|
528
654
|
}
|
529
655
|
get user() {
|
@@ -646,6 +772,13 @@ class WorkspaceApi {
|
|
646
772
|
...this.extraProps
|
647
773
|
});
|
648
774
|
}
|
775
|
+
updateWorkspaceMemberInvite(workspaceId, inviteId, role) {
|
776
|
+
return operationsByTag.workspaces.updateWorkspaceMemberInvite({
|
777
|
+
pathParams: { workspaceId, inviteId },
|
778
|
+
body: { role },
|
779
|
+
...this.extraProps
|
780
|
+
});
|
781
|
+
}
|
649
782
|
cancelWorkspaceMemberInvite(workspaceId, inviteId) {
|
650
783
|
return operationsByTag.workspaces.cancelWorkspaceMemberInvite({
|
651
784
|
pathParams: { workspaceId, inviteId },
|
@@ -688,6 +821,12 @@ class DatabaseApi {
|
|
688
821
|
...this.extraProps
|
689
822
|
});
|
690
823
|
}
|
824
|
+
getDatabaseMetadata(workspace, dbName) {
|
825
|
+
return operationsByTag.database.getDatabaseMetadata({
|
826
|
+
pathParams: { workspace, dbName },
|
827
|
+
...this.extraProps
|
828
|
+
});
|
829
|
+
}
|
691
830
|
getGitBranchesMapping(workspace, dbName) {
|
692
831
|
return operationsByTag.database.getGitBranchesMapping({
|
693
832
|
pathParams: { workspace, dbName },
|
@@ -860,9 +999,10 @@ class RecordsApi {
|
|
860
999
|
constructor(extraProps) {
|
861
1000
|
this.extraProps = extraProps;
|
862
1001
|
}
|
863
|
-
insertRecord(workspace, database, branch, tableName, record) {
|
1002
|
+
insertRecord(workspace, database, branch, tableName, record, options = {}) {
|
864
1003
|
return operationsByTag.records.insertRecord({
|
865
1004
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
1005
|
+
queryParams: options,
|
866
1006
|
body: record,
|
867
1007
|
...this.extraProps
|
868
1008
|
});
|
@@ -891,21 +1031,24 @@ class RecordsApi {
|
|
891
1031
|
...this.extraProps
|
892
1032
|
});
|
893
1033
|
}
|
894
|
-
deleteRecord(workspace, database, branch, tableName, recordId) {
|
1034
|
+
deleteRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
895
1035
|
return operationsByTag.records.deleteRecord({
|
896
1036
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
1037
|
+
queryParams: options,
|
897
1038
|
...this.extraProps
|
898
1039
|
});
|
899
1040
|
}
|
900
1041
|
getRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
901
1042
|
return operationsByTag.records.getRecord({
|
902
1043
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
1044
|
+
queryParams: options,
|
903
1045
|
...this.extraProps
|
904
1046
|
});
|
905
1047
|
}
|
906
|
-
bulkInsertTableRecords(workspace, database, branch, tableName, records) {
|
1048
|
+
bulkInsertTableRecords(workspace, database, branch, tableName, records, options = {}) {
|
907
1049
|
return operationsByTag.records.bulkInsertTableRecords({
|
908
1050
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
1051
|
+
queryParams: options,
|
909
1052
|
body: { records },
|
910
1053
|
...this.extraProps
|
911
1054
|
});
|
@@ -956,7 +1099,7 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
956
1099
|
throw TypeError("Cannot add the same private member more than once");
|
957
1100
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
958
1101
|
};
|
959
|
-
var __privateSet$
|
1102
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
960
1103
|
__accessCheck$6(obj, member, "write to private field");
|
961
1104
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
962
1105
|
return value;
|
@@ -965,7 +1108,7 @@ var _query, _page;
|
|
965
1108
|
class Page {
|
966
1109
|
constructor(query, meta, records = []) {
|
967
1110
|
__privateAdd$6(this, _query, void 0);
|
968
|
-
__privateSet$
|
1111
|
+
__privateSet$6(this, _query, query);
|
969
1112
|
this.meta = meta;
|
970
1113
|
this.records = new RecordArray(this, records);
|
971
1114
|
}
|
@@ -994,10 +1137,10 @@ function isCursorPaginationOptions(options) {
|
|
994
1137
|
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
995
1138
|
}
|
996
1139
|
const _RecordArray = class extends Array {
|
997
|
-
constructor(
|
998
|
-
super(..._RecordArray.parseConstructorParams(
|
1140
|
+
constructor(...args) {
|
1141
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
999
1142
|
__privateAdd$6(this, _page, void 0);
|
1000
|
-
__privateSet$
|
1143
|
+
__privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
|
1001
1144
|
}
|
1002
1145
|
static parseConstructorParams(...args) {
|
1003
1146
|
if (args.length === 1 && typeof args[0] === "number") {
|
@@ -1009,6 +1152,12 @@ const _RecordArray = class extends Array {
|
|
1009
1152
|
}
|
1010
1153
|
return new Array(...args);
|
1011
1154
|
}
|
1155
|
+
toArray() {
|
1156
|
+
return new Array(...this);
|
1157
|
+
}
|
1158
|
+
map(callbackfn, thisArg) {
|
1159
|
+
return this.toArray().map(callbackfn, thisArg);
|
1160
|
+
}
|
1012
1161
|
async nextPage(size, offset) {
|
1013
1162
|
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1014
1163
|
return new _RecordArray(newPage);
|
@@ -1045,7 +1194,7 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
1045
1194
|
throw TypeError("Cannot add the same private member more than once");
|
1046
1195
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1047
1196
|
};
|
1048
|
-
var __privateSet$
|
1197
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
1049
1198
|
__accessCheck$5(obj, member, "write to private field");
|
1050
1199
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1051
1200
|
return value;
|
@@ -1058,11 +1207,11 @@ const _Query = class {
|
|
1058
1207
|
__privateAdd$5(this, _data, { filter: {} });
|
1059
1208
|
this.meta = { page: { cursor: "start", more: true } };
|
1060
1209
|
this.records = new RecordArray(this, []);
|
1061
|
-
__privateSet$
|
1210
|
+
__privateSet$5(this, _table$1, table);
|
1062
1211
|
if (repository) {
|
1063
|
-
__privateSet$
|
1212
|
+
__privateSet$5(this, _repository, repository);
|
1064
1213
|
} else {
|
1065
|
-
__privateSet$
|
1214
|
+
__privateSet$5(this, _repository, this);
|
1066
1215
|
}
|
1067
1216
|
const parent = cleanParent(data, rawParent);
|
1068
1217
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
@@ -1117,13 +1266,18 @@ const _Query = class {
|
|
1117
1266
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
1118
1267
|
}
|
1119
1268
|
}
|
1120
|
-
sort(column, direction) {
|
1269
|
+
sort(column, direction = "asc") {
|
1121
1270
|
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
1122
1271
|
const sort = [...originalSort, { column, direction }];
|
1123
1272
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
1124
1273
|
}
|
1125
1274
|
select(columns) {
|
1126
|
-
return new _Query(
|
1275
|
+
return new _Query(
|
1276
|
+
__privateGet$5(this, _repository),
|
1277
|
+
__privateGet$5(this, _table$1),
|
1278
|
+
{ columns },
|
1279
|
+
__privateGet$5(this, _data)
|
1280
|
+
);
|
1127
1281
|
}
|
1128
1282
|
getPaginated(options = {}) {
|
1129
1283
|
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
@@ -1239,7 +1393,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1239
1393
|
throw TypeError("Cannot add the same private member more than once");
|
1240
1394
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1241
1395
|
};
|
1242
|
-
var __privateSet$
|
1396
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1243
1397
|
__accessCheck$4(obj, member, "write to private field");
|
1244
1398
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1245
1399
|
return value;
|
@@ -1248,7 +1402,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1248
1402
|
__accessCheck$4(obj, member, "access private method");
|
1249
1403
|
return method;
|
1250
1404
|
};
|
1251
|
-
var _table, _getFetchProps, _cache,
|
1405
|
+
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;
|
1252
1406
|
class Repository extends Query {
|
1253
1407
|
}
|
1254
1408
|
class RestRepository extends Query {
|
@@ -1260,210 +1414,211 @@ class RestRepository extends Query {
|
|
1260
1414
|
__privateAdd$4(this, _updateRecordWithID);
|
1261
1415
|
__privateAdd$4(this, _upsertRecordWithID);
|
1262
1416
|
__privateAdd$4(this, _deleteRecord);
|
1263
|
-
__privateAdd$4(this, _invalidateCache);
|
1264
|
-
__privateAdd$4(this, _setCacheRecord);
|
1265
|
-
__privateAdd$4(this, _getCacheRecord);
|
1266
1417
|
__privateAdd$4(this, _setCacheQuery);
|
1267
1418
|
__privateAdd$4(this, _getCacheQuery);
|
1268
|
-
__privateAdd$4(this,
|
1419
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1269
1420
|
__privateAdd$4(this, _table, void 0);
|
1270
1421
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1422
|
+
__privateAdd$4(this, _db, void 0);
|
1271
1423
|
__privateAdd$4(this, _cache, void 0);
|
1272
|
-
__privateAdd$4(this,
|
1273
|
-
|
1274
|
-
__privateSet$
|
1275
|
-
this
|
1276
|
-
__privateSet$
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
1287
|
-
|
1288
|
-
|
1424
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1425
|
+
__privateAdd$4(this, _trace, void 0);
|
1426
|
+
__privateSet$4(this, _table, options.table);
|
1427
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1428
|
+
__privateSet$4(this, _db, options.db);
|
1429
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1430
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1431
|
+
const trace = options.pluginOptions.trace ?? defaultTrace;
|
1432
|
+
__privateSet$4(this, _trace, async (name, fn, options2 = {}) => {
|
1433
|
+
return trace(name, fn, {
|
1434
|
+
...options2,
|
1435
|
+
[TraceAttributes.TABLE]: __privateGet$4(this, _table),
|
1436
|
+
[TraceAttributes.VERSION]: VERSION
|
1437
|
+
});
|
1438
|
+
});
|
1439
|
+
}
|
1440
|
+
async create(a, b, c) {
|
1441
|
+
return __privateGet$4(this, _trace).call(this, "create", async () => {
|
1442
|
+
if (Array.isArray(a)) {
|
1443
|
+
if (a.length === 0)
|
1444
|
+
return [];
|
1445
|
+
const columns = isStringArray(b) ? b : void 0;
|
1446
|
+
return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a, columns);
|
1447
|
+
}
|
1448
|
+
if (isString(a) && isObject(b)) {
|
1449
|
+
if (a === "")
|
1450
|
+
throw new Error("The id can't be empty");
|
1451
|
+
const columns = isStringArray(c) ? c : void 0;
|
1452
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns);
|
1453
|
+
}
|
1454
|
+
if (isObject(a) && isString(a.id)) {
|
1455
|
+
if (a.id === "")
|
1456
|
+
throw new Error("The id can't be empty");
|
1457
|
+
const columns = isStringArray(b) ? b : void 0;
|
1458
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1459
|
+
}
|
1460
|
+
if (isObject(a)) {
|
1461
|
+
const columns = isStringArray(b) ? b : void 0;
|
1462
|
+
return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a, columns);
|
1463
|
+
}
|
1464
|
+
throw new Error("Invalid arguments for create method");
|
1465
|
+
});
|
1466
|
+
}
|
1467
|
+
async read(a, b) {
|
1468
|
+
return __privateGet$4(this, _trace).call(this, "read", async () => {
|
1469
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1470
|
+
if (Array.isArray(a)) {
|
1471
|
+
if (a.length === 0)
|
1472
|
+
return [];
|
1473
|
+
const ids = a.map((item) => extractId(item));
|
1474
|
+
const finalObjects = await this.getAll({ filter: { id: { $any: compact(ids) } }, columns });
|
1475
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1476
|
+
acc[object.id] = object;
|
1477
|
+
return acc;
|
1478
|
+
}, {});
|
1479
|
+
return ids.map((id2) => dictionary[id2 ?? ""] ?? null);
|
1480
|
+
}
|
1481
|
+
const id = extractId(a);
|
1482
|
+
if (id) {
|
1483
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1484
|
+
try {
|
1485
|
+
const response = await getRecord({
|
1486
|
+
pathParams: {
|
1487
|
+
workspace: "{workspaceId}",
|
1488
|
+
dbBranchName: "{dbBranch}",
|
1489
|
+
tableName: __privateGet$4(this, _table),
|
1490
|
+
recordId: id
|
1491
|
+
},
|
1492
|
+
queryParams: { columns },
|
1493
|
+
...fetchProps
|
1494
|
+
});
|
1495
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1496
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1497
|
+
} catch (e) {
|
1498
|
+
if (isObject(e) && e.status === 404) {
|
1499
|
+
return null;
|
1500
|
+
}
|
1501
|
+
throw e;
|
1289
1502
|
}
|
1290
|
-
return [accWithoutIds, accWithIds, accOrder];
|
1291
|
-
}, [[], [], []]);
|
1292
|
-
const recordsWithoutId = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, itemsWithoutIds);
|
1293
|
-
await Promise.all(recordsWithoutId.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1294
|
-
if (itemsWithIds.length > 100) {
|
1295
|
-
console.warn("Bulk create operation with id is not optimized in the Xata API yet, this request might be slow");
|
1296
1503
|
}
|
1297
|
-
|
1298
|
-
|
1299
|
-
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1504
|
+
return null;
|
1505
|
+
});
|
1506
|
+
}
|
1507
|
+
async update(a, b, c) {
|
1508
|
+
return __privateGet$4(this, _trace).call(this, "update", async () => {
|
1509
|
+
if (Array.isArray(a)) {
|
1510
|
+
if (a.length === 0)
|
1511
|
+
return [];
|
1512
|
+
if (a.length > 100) {
|
1513
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1303
1514
|
}
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
if (a
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1320
|
-
|
1321
|
-
|
1322
|
-
|
1323
|
-
|
1324
|
-
|
1325
|
-
throw new Error("Invalid arguments for create method");
|
1326
|
-
}
|
1327
|
-
async read(a) {
|
1328
|
-
if (Array.isArray(a)) {
|
1329
|
-
if (a.length === 0)
|
1330
|
-
return [];
|
1331
|
-
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1332
|
-
return this.getAll({ filter: { id: { $any: ids } } });
|
1333
|
-
}
|
1334
|
-
const id = isString(a) ? a : a.id;
|
1335
|
-
if (isString(id)) {
|
1336
|
-
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
|
1337
|
-
if (cacheRecord)
|
1338
|
-
return cacheRecord;
|
1339
|
-
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1340
|
-
try {
|
1341
|
-
const response = await getRecord({
|
1342
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1343
|
-
...fetchProps
|
1344
|
-
});
|
1345
|
-
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1346
|
-
return initObject(this.db, schema, __privateGet$4(this, _table), response);
|
1347
|
-
} catch (e) {
|
1348
|
-
if (isObject(e) && e.status === 404) {
|
1349
|
-
return null;
|
1515
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1516
|
+
return Promise.all(a.map((object) => this.update(object, columns)));
|
1517
|
+
}
|
1518
|
+
if (isString(a) && isObject(b)) {
|
1519
|
+
const columns = isStringArray(c) ? c : void 0;
|
1520
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns);
|
1521
|
+
}
|
1522
|
+
if (isObject(a) && isString(a.id)) {
|
1523
|
+
const columns = isStringArray(b) ? b : void 0;
|
1524
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1525
|
+
}
|
1526
|
+
throw new Error("Invalid arguments for update method");
|
1527
|
+
});
|
1528
|
+
}
|
1529
|
+
async createOrUpdate(a, b, c) {
|
1530
|
+
return __privateGet$4(this, _trace).call(this, "createOrUpdate", async () => {
|
1531
|
+
if (Array.isArray(a)) {
|
1532
|
+
if (a.length === 0)
|
1533
|
+
return [];
|
1534
|
+
if (a.length > 100) {
|
1535
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1350
1536
|
}
|
1351
|
-
|
1537
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1538
|
+
return Promise.all(a.map((object) => this.createOrUpdate(object, columns)));
|
1352
1539
|
}
|
1353
|
-
|
1540
|
+
if (isString(a) && isObject(b)) {
|
1541
|
+
const columns = isStringArray(c) ? c : void 0;
|
1542
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b, columns);
|
1543
|
+
}
|
1544
|
+
if (isObject(a) && isString(a.id)) {
|
1545
|
+
const columns = isStringArray(c) ? c : void 0;
|
1546
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1547
|
+
}
|
1548
|
+
throw new Error("Invalid arguments for createOrUpdate method");
|
1549
|
+
});
|
1354
1550
|
}
|
1355
|
-
async
|
1356
|
-
|
1357
|
-
if (a
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1551
|
+
async delete(a, b) {
|
1552
|
+
return __privateGet$4(this, _trace).call(this, "delete", async () => {
|
1553
|
+
if (Array.isArray(a)) {
|
1554
|
+
if (a.length === 0)
|
1555
|
+
return [];
|
1556
|
+
if (a.length > 100) {
|
1557
|
+
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1558
|
+
}
|
1559
|
+
return Promise.all(a.map((id) => this.delete(id, b)));
|
1361
1560
|
}
|
1362
|
-
|
1363
|
-
|
1364
|
-
if (isString(a) && isObject(b)) {
|
1365
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1366
|
-
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
|
1367
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1368
|
-
return record;
|
1369
|
-
}
|
1370
|
-
if (isObject(a) && isString(a.id)) {
|
1371
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1372
|
-
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1373
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1374
|
-
return record;
|
1375
|
-
}
|
1376
|
-
throw new Error("Invalid arguments for update method");
|
1377
|
-
}
|
1378
|
-
async createOrUpdate(a, b) {
|
1379
|
-
if (Array.isArray(a)) {
|
1380
|
-
if (a.length === 0)
|
1381
|
-
return [];
|
1382
|
-
if (a.length > 100) {
|
1383
|
-
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1561
|
+
if (isString(a)) {
|
1562
|
+
return __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a, b);
|
1384
1563
|
}
|
1385
|
-
|
1386
|
-
|
1387
|
-
if (isString(a) && isObject(b)) {
|
1388
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1389
|
-
const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b);
|
1390
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1391
|
-
return record;
|
1392
|
-
}
|
1393
|
-
if (isObject(a) && isString(a.id)) {
|
1394
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1395
|
-
const record = await __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1396
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1397
|
-
return record;
|
1398
|
-
}
|
1399
|
-
throw new Error("Invalid arguments for createOrUpdate method");
|
1400
|
-
}
|
1401
|
-
async delete(a) {
|
1402
|
-
if (Array.isArray(a)) {
|
1403
|
-
if (a.length === 0)
|
1404
|
-
return;
|
1405
|
-
if (a.length > 100) {
|
1406
|
-
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1564
|
+
if (isObject(a) && isString(a.id)) {
|
1565
|
+
return __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id, b);
|
1407
1566
|
}
|
1408
|
-
|
1409
|
-
|
1410
|
-
}
|
1411
|
-
if (isString(a)) {
|
1412
|
-
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
|
1413
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1414
|
-
return;
|
1415
|
-
}
|
1416
|
-
if (isObject(a) && isString(a.id)) {
|
1417
|
-
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
|
1418
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1419
|
-
return;
|
1420
|
-
}
|
1421
|
-
throw new Error("Invalid arguments for delete method");
|
1567
|
+
throw new Error("Invalid arguments for delete method");
|
1568
|
+
});
|
1422
1569
|
}
|
1423
1570
|
async search(query, options = {}) {
|
1424
|
-
|
1425
|
-
|
1426
|
-
|
1427
|
-
|
1428
|
-
|
1429
|
-
|
1430
|
-
|
1431
|
-
|
1432
|
-
|
1433
|
-
|
1571
|
+
return __privateGet$4(this, _trace).call(this, "search", async () => {
|
1572
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1573
|
+
const { records } = await searchTable({
|
1574
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1575
|
+
body: {
|
1576
|
+
query,
|
1577
|
+
fuzziness: options.fuzziness,
|
1578
|
+
prefix: options.prefix,
|
1579
|
+
highlight: options.highlight,
|
1580
|
+
filter: options.filter,
|
1581
|
+
boosters: options.boosters
|
1582
|
+
},
|
1583
|
+
...fetchProps
|
1584
|
+
});
|
1585
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1586
|
+
return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1434
1587
|
});
|
1435
|
-
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1436
|
-
return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
|
1437
1588
|
}
|
1438
1589
|
async query(query) {
|
1439
|
-
|
1440
|
-
|
1441
|
-
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
|
1451
|
-
|
1452
|
-
|
1453
|
-
|
1590
|
+
return __privateGet$4(this, _trace).call(this, "query", async () => {
|
1591
|
+
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
1592
|
+
if (cacheQuery)
|
1593
|
+
return new Page(query, cacheQuery.meta, cacheQuery.records);
|
1594
|
+
const data = query.getQueryOptions();
|
1595
|
+
const body = {
|
1596
|
+
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1597
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1598
|
+
page: data.pagination,
|
1599
|
+
columns: data.columns
|
1600
|
+
};
|
1601
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1602
|
+
const { meta, records: objects } = await queryTable({
|
1603
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1604
|
+
body,
|
1605
|
+
...fetchProps
|
1606
|
+
});
|
1607
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1608
|
+
const records = objects.map((record) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), record));
|
1609
|
+
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1610
|
+
return new Page(query, meta, records);
|
1454
1611
|
});
|
1455
|
-
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1456
|
-
const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
|
1457
|
-
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1458
|
-
return new Page(query, meta, records);
|
1459
1612
|
}
|
1460
1613
|
}
|
1461
1614
|
_table = new WeakMap();
|
1462
1615
|
_getFetchProps = new WeakMap();
|
1616
|
+
_db = new WeakMap();
|
1463
1617
|
_cache = new WeakMap();
|
1464
|
-
|
1618
|
+
_schemaTables$2 = new WeakMap();
|
1619
|
+
_trace = new WeakMap();
|
1465
1620
|
_insertRecordWithoutId = new WeakSet();
|
1466
|
-
insertRecordWithoutId_fn = async function(object) {
|
1621
|
+
insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
|
1467
1622
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1468
1623
|
const record = transformObjectLinks(object);
|
1469
1624
|
const response = await insertRecord({
|
@@ -1472,17 +1627,15 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1472
1627
|
dbBranchName: "{dbBranch}",
|
1473
1628
|
tableName: __privateGet$4(this, _table)
|
1474
1629
|
},
|
1630
|
+
queryParams: { columns },
|
1475
1631
|
body: record,
|
1476
1632
|
...fetchProps
|
1477
1633
|
});
|
1478
|
-
const
|
1479
|
-
|
1480
|
-
throw new Error("The server failed to save the record");
|
1481
|
-
}
|
1482
|
-
return finalObject;
|
1634
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1635
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1483
1636
|
};
|
1484
1637
|
_insertRecordWithId = new WeakSet();
|
1485
|
-
insertRecordWithId_fn = async function(recordId, object) {
|
1638
|
+
insertRecordWithId_fn = async function(recordId, object, columns = ["*"]) {
|
1486
1639
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1487
1640
|
const record = transformObjectLinks(object);
|
1488
1641
|
const response = await insertRecordWithID({
|
@@ -1493,88 +1646,78 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1493
1646
|
recordId
|
1494
1647
|
},
|
1495
1648
|
body: record,
|
1496
|
-
queryParams: { createOnly: true },
|
1649
|
+
queryParams: { createOnly: true, columns },
|
1497
1650
|
...fetchProps
|
1498
1651
|
});
|
1499
|
-
const
|
1500
|
-
|
1501
|
-
throw new Error("The server failed to save the record");
|
1502
|
-
}
|
1503
|
-
return finalObject;
|
1652
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1653
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1504
1654
|
};
|
1505
1655
|
_bulkInsertTableRecords = new WeakSet();
|
1506
|
-
bulkInsertTableRecords_fn = async function(objects) {
|
1656
|
+
bulkInsertTableRecords_fn = async function(objects, columns = ["*"]) {
|
1507
1657
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1508
1658
|
const records = objects.map((object) => transformObjectLinks(object));
|
1509
1659
|
const response = await bulkInsertTableRecords({
|
1510
1660
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1661
|
+
queryParams: { columns },
|
1511
1662
|
body: { records },
|
1512
1663
|
...fetchProps
|
1513
1664
|
});
|
1514
|
-
|
1515
|
-
|
1516
|
-
throw new Error("The server failed to save some records");
|
1665
|
+
if (!isResponseWithRecords(response)) {
|
1666
|
+
throw new Error("Request included columns but server didn't include them");
|
1517
1667
|
}
|
1518
|
-
|
1668
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1669
|
+
return response.records?.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1519
1670
|
};
|
1520
1671
|
_updateRecordWithID = new WeakSet();
|
1521
|
-
updateRecordWithID_fn = async function(recordId, object) {
|
1672
|
+
updateRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1522
1673
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1523
1674
|
const record = transformObjectLinks(object);
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
|
1529
|
-
|
1530
|
-
|
1531
|
-
|
1532
|
-
|
1675
|
+
try {
|
1676
|
+
const response = await updateRecordWithID({
|
1677
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1678
|
+
queryParams: { columns },
|
1679
|
+
body: record,
|
1680
|
+
...fetchProps
|
1681
|
+
});
|
1682
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1683
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1684
|
+
} catch (e) {
|
1685
|
+
if (isObject(e) && e.status === 404) {
|
1686
|
+
return null;
|
1687
|
+
}
|
1688
|
+
throw e;
|
1689
|
+
}
|
1533
1690
|
};
|
1534
1691
|
_upsertRecordWithID = new WeakSet();
|
1535
|
-
upsertRecordWithID_fn = async function(recordId, object) {
|
1692
|
+
upsertRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1536
1693
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1537
1694
|
const response = await upsertRecordWithID({
|
1538
1695
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1696
|
+
queryParams: { columns },
|
1539
1697
|
body: object,
|
1540
1698
|
...fetchProps
|
1541
1699
|
});
|
1542
|
-
const
|
1543
|
-
|
1544
|
-
throw new Error("The server failed to save the record");
|
1545
|
-
return item;
|
1700
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1701
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1546
1702
|
};
|
1547
1703
|
_deleteRecord = new WeakSet();
|
1548
|
-
deleteRecord_fn = async function(recordId) {
|
1704
|
+
deleteRecord_fn = async function(recordId, columns = ["*"]) {
|
1549
1705
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1550
|
-
|
1551
|
-
|
1552
|
-
|
1553
|
-
|
1554
|
-
|
1555
|
-
|
1556
|
-
|
1557
|
-
|
1558
|
-
|
1559
|
-
|
1560
|
-
|
1561
|
-
|
1562
|
-
|
1563
|
-
await __privateGet$4(this, _cache).delete(key);
|
1706
|
+
try {
|
1707
|
+
const response = await deleteRecord({
|
1708
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1709
|
+
queryParams: { columns },
|
1710
|
+
...fetchProps
|
1711
|
+
});
|
1712
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1713
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1714
|
+
} catch (e) {
|
1715
|
+
if (isObject(e) && e.status === 404) {
|
1716
|
+
return null;
|
1717
|
+
}
|
1718
|
+
throw e;
|
1564
1719
|
}
|
1565
1720
|
};
|
1566
|
-
_setCacheRecord = new WeakSet();
|
1567
|
-
setCacheRecord_fn = async function(record) {
|
1568
|
-
if (!__privateGet$4(this, _cache).cacheRecords)
|
1569
|
-
return;
|
1570
|
-
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1571
|
-
};
|
1572
|
-
_getCacheRecord = new WeakSet();
|
1573
|
-
getCacheRecord_fn = async function(recordId) {
|
1574
|
-
if (!__privateGet$4(this, _cache).cacheRecords)
|
1575
|
-
return null;
|
1576
|
-
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1577
|
-
};
|
1578
1721
|
_setCacheQuery = new WeakSet();
|
1579
1722
|
setCacheQuery_fn = async function(query, meta, records) {
|
1580
1723
|
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
@@ -1591,17 +1734,17 @@ getCacheQuery_fn = async function(query) {
|
|
1591
1734
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1592
1735
|
return hasExpired ? null : result;
|
1593
1736
|
};
|
1594
|
-
|
1595
|
-
|
1596
|
-
if (__privateGet$4(this,
|
1597
|
-
return __privateGet$4(this,
|
1737
|
+
_getSchemaTables$1 = new WeakSet();
|
1738
|
+
getSchemaTables_fn$1 = async function() {
|
1739
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1740
|
+
return __privateGet$4(this, _schemaTables$2);
|
1598
1741
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1599
1742
|
const { schema } = await getBranchDetails({
|
1600
1743
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1601
1744
|
...fetchProps
|
1602
1745
|
});
|
1603
|
-
__privateSet$
|
1604
|
-
return schema;
|
1746
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1747
|
+
return schema.tables;
|
1605
1748
|
};
|
1606
1749
|
const transformObjectLinks = (object) => {
|
1607
1750
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
@@ -1610,11 +1753,11 @@ const transformObjectLinks = (object) => {
|
|
1610
1753
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1611
1754
|
}, {});
|
1612
1755
|
};
|
1613
|
-
const initObject = (db,
|
1756
|
+
const initObject = (db, schemaTables, table, object) => {
|
1614
1757
|
const result = {};
|
1615
1758
|
const { xata, ...rest } = object ?? {};
|
1616
1759
|
Object.assign(result, rest);
|
1617
|
-
const { columns } =
|
1760
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1618
1761
|
if (!columns)
|
1619
1762
|
console.error(`Table ${table} not found in schema`);
|
1620
1763
|
for (const column of columns ?? []) {
|
@@ -1634,17 +1777,17 @@ const initObject = (db, schema, table, object) => {
|
|
1634
1777
|
if (!linkTable) {
|
1635
1778
|
console.error(`Failed to parse link for field ${column.name}`);
|
1636
1779
|
} else if (isObject(value)) {
|
1637
|
-
result[column.name] = initObject(db,
|
1780
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1638
1781
|
}
|
1639
1782
|
break;
|
1640
1783
|
}
|
1641
1784
|
}
|
1642
1785
|
}
|
1643
|
-
result.read = function() {
|
1644
|
-
return db[table].read(result["id"]);
|
1786
|
+
result.read = function(columns2) {
|
1787
|
+
return db[table].read(result["id"], columns2);
|
1645
1788
|
};
|
1646
|
-
result.update = function(data) {
|
1647
|
-
return db[table].update(result["id"], data);
|
1789
|
+
result.update = function(data, columns2) {
|
1790
|
+
return db[table].update(result["id"], data, columns2);
|
1648
1791
|
};
|
1649
1792
|
result.delete = function() {
|
1650
1793
|
return db[table].delete(result["id"]);
|
@@ -1658,14 +1801,15 @@ const initObject = (db, schema, table, object) => {
|
|
1658
1801
|
Object.freeze(result);
|
1659
1802
|
return result;
|
1660
1803
|
};
|
1661
|
-
function
|
1662
|
-
|
1663
|
-
|
1664
|
-
|
1665
|
-
if (
|
1666
|
-
return
|
1667
|
-
|
1668
|
-
|
1804
|
+
function isResponseWithRecords(value) {
|
1805
|
+
return isObject(value) && Array.isArray(value.records);
|
1806
|
+
}
|
1807
|
+
function extractId(value) {
|
1808
|
+
if (isString(value))
|
1809
|
+
return value;
|
1810
|
+
if (isObject(value) && isString(value.id))
|
1811
|
+
return value.id;
|
1812
|
+
return void 0;
|
1669
1813
|
}
|
1670
1814
|
|
1671
1815
|
var __accessCheck$3 = (obj, member, msg) => {
|
@@ -1681,7 +1825,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1681
1825
|
throw TypeError("Cannot add the same private member more than once");
|
1682
1826
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1683
1827
|
};
|
1684
|
-
var __privateSet$
|
1828
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1685
1829
|
__accessCheck$3(obj, member, "write to private field");
|
1686
1830
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1687
1831
|
return value;
|
@@ -1690,9 +1834,8 @@ var _map;
|
|
1690
1834
|
class SimpleCache {
|
1691
1835
|
constructor(options = {}) {
|
1692
1836
|
__privateAdd$3(this, _map, void 0);
|
1693
|
-
__privateSet$
|
1837
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1694
1838
|
this.capacity = options.max ?? 500;
|
1695
|
-
this.cacheRecords = options.cacheRecords ?? true;
|
1696
1839
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1697
1840
|
}
|
1698
1841
|
async getAll() {
|
@@ -1718,18 +1861,25 @@ class SimpleCache {
|
|
1718
1861
|
}
|
1719
1862
|
_map = new WeakMap();
|
1720
1863
|
|
1721
|
-
const
|
1722
|
-
const
|
1723
|
-
const
|
1724
|
-
const
|
1725
|
-
const
|
1726
|
-
const
|
1864
|
+
const greaterThan = (value) => ({ $gt: value });
|
1865
|
+
const gt = greaterThan;
|
1866
|
+
const greaterThanEquals = (value) => ({ $ge: value });
|
1867
|
+
const greaterEquals = greaterThanEquals;
|
1868
|
+
const gte = greaterThanEquals;
|
1869
|
+
const ge = greaterThanEquals;
|
1870
|
+
const lessThan = (value) => ({ $lt: value });
|
1871
|
+
const lt = lessThan;
|
1872
|
+
const lessThanEquals = (value) => ({ $le: value });
|
1873
|
+
const lessEquals = lessThanEquals;
|
1874
|
+
const lte = lessThanEquals;
|
1875
|
+
const le = lessThanEquals;
|
1727
1876
|
const exists = (column) => ({ $exists: column });
|
1728
1877
|
const notExists = (column) => ({ $notExists: column });
|
1729
1878
|
const startsWith = (value) => ({ $startsWith: value });
|
1730
1879
|
const endsWith = (value) => ({ $endsWith: value });
|
1731
1880
|
const pattern = (value) => ({ $pattern: value });
|
1732
1881
|
const is = (value) => ({ $is: value });
|
1882
|
+
const equals = is;
|
1733
1883
|
const isNot = (value) => ({ $isNot: value });
|
1734
1884
|
const contains = (value) => ({ $contains: value });
|
1735
1885
|
const includes = (value) => ({ $includes: value });
|
@@ -1750,31 +1900,42 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1750
1900
|
throw TypeError("Cannot add the same private member more than once");
|
1751
1901
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1752
1902
|
};
|
1753
|
-
var
|
1903
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1904
|
+
__accessCheck$2(obj, member, "write to private field");
|
1905
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1906
|
+
return value;
|
1907
|
+
};
|
1908
|
+
var _tables, _schemaTables$1;
|
1754
1909
|
class SchemaPlugin extends XataPlugin {
|
1755
|
-
constructor(
|
1910
|
+
constructor(schemaTables) {
|
1756
1911
|
super();
|
1757
|
-
this.tableNames = tableNames;
|
1758
1912
|
__privateAdd$2(this, _tables, {});
|
1913
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1914
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1759
1915
|
}
|
1760
1916
|
build(pluginOptions) {
|
1761
|
-
const db = new Proxy(
|
1762
|
-
|
1763
|
-
|
1764
|
-
|
1765
|
-
|
1766
|
-
|
1917
|
+
const db = new Proxy(
|
1918
|
+
{},
|
1919
|
+
{
|
1920
|
+
get: (_target, table) => {
|
1921
|
+
if (!isString(table))
|
1922
|
+
throw new Error("Invalid table name");
|
1923
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1924
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1925
|
+
}
|
1926
|
+
return __privateGet$2(this, _tables)[table];
|
1767
1927
|
}
|
1768
|
-
return __privateGet$2(this, _tables)[table];
|
1769
1928
|
}
|
1770
|
-
|
1771
|
-
|
1772
|
-
|
1929
|
+
);
|
1930
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1931
|
+
for (const table of tableNames) {
|
1932
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1773
1933
|
}
|
1774
1934
|
return db;
|
1775
1935
|
}
|
1776
1936
|
}
|
1777
1937
|
_tables = new WeakMap();
|
1938
|
+
_schemaTables$1 = new WeakMap();
|
1778
1939
|
|
1779
1940
|
var __accessCheck$1 = (obj, member, msg) => {
|
1780
1941
|
if (!member.has(obj))
|
@@ -1798,82 +1959,77 @@ var __privateMethod$1 = (obj, member, method) => {
|
|
1798
1959
|
__accessCheck$1(obj, member, "access private method");
|
1799
1960
|
return method;
|
1800
1961
|
};
|
1801
|
-
var
|
1962
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1802
1963
|
class SearchPlugin extends XataPlugin {
|
1803
|
-
constructor(db) {
|
1964
|
+
constructor(db, schemaTables) {
|
1804
1965
|
super();
|
1805
1966
|
this.db = db;
|
1806
1967
|
__privateAdd$1(this, _search);
|
1807
|
-
__privateAdd$1(this,
|
1808
|
-
__privateAdd$1(this,
|
1968
|
+
__privateAdd$1(this, _getSchemaTables);
|
1969
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1970
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1809
1971
|
}
|
1810
1972
|
build({ getFetchProps }) {
|
1811
1973
|
return {
|
1812
1974
|
all: async (query, options = {}) => {
|
1813
1975
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1814
|
-
const
|
1976
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1815
1977
|
return records.map((record) => {
|
1816
1978
|
const { table = "orphan" } = record.xata;
|
1817
|
-
return { table, record: initObject(this.db,
|
1979
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1818
1980
|
});
|
1819
1981
|
},
|
1820
1982
|
byTable: async (query, options = {}) => {
|
1821
1983
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1822
|
-
const
|
1984
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1823
1985
|
return records.reduce((acc, record) => {
|
1824
1986
|
const { table = "orphan" } = record.xata;
|
1825
1987
|
const items = acc[table] ?? [];
|
1826
|
-
const item = initObject(this.db,
|
1988
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1827
1989
|
return { ...acc, [table]: [...items, item] };
|
1828
1990
|
}, {});
|
1829
1991
|
}
|
1830
1992
|
};
|
1831
1993
|
}
|
1832
1994
|
}
|
1833
|
-
|
1995
|
+
_schemaTables = new WeakMap();
|
1834
1996
|
_search = new WeakSet();
|
1835
1997
|
search_fn = async function(query, options, getFetchProps) {
|
1836
1998
|
const fetchProps = await getFetchProps();
|
1837
|
-
const { tables, fuzziness, highlight } = options ?? {};
|
1999
|
+
const { tables, fuzziness, highlight, prefix } = options ?? {};
|
1838
2000
|
const { records } = await searchBranch({
|
1839
2001
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1840
|
-
body: { tables, query, fuzziness, highlight },
|
2002
|
+
body: { tables, query, fuzziness, prefix, highlight },
|
1841
2003
|
...fetchProps
|
1842
2004
|
});
|
1843
2005
|
return records;
|
1844
2006
|
};
|
1845
|
-
|
1846
|
-
|
1847
|
-
if (__privateGet$1(this,
|
1848
|
-
return __privateGet$1(this,
|
2007
|
+
_getSchemaTables = new WeakSet();
|
2008
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
2009
|
+
if (__privateGet$1(this, _schemaTables))
|
2010
|
+
return __privateGet$1(this, _schemaTables);
|
1849
2011
|
const fetchProps = await getFetchProps();
|
1850
2012
|
const { schema } = await getBranchDetails({
|
1851
2013
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1852
2014
|
...fetchProps
|
1853
2015
|
});
|
1854
|
-
__privateSet$1(this,
|
1855
|
-
return schema;
|
2016
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
2017
|
+
return schema.tables;
|
1856
2018
|
};
|
1857
2019
|
|
1858
2020
|
const isBranchStrategyBuilder = (strategy) => {
|
1859
2021
|
return typeof strategy === "function";
|
1860
2022
|
};
|
1861
2023
|
|
1862
|
-
const envBranchNames = [
|
1863
|
-
"XATA_BRANCH",
|
1864
|
-
"VERCEL_GIT_COMMIT_REF",
|
1865
|
-
"CF_PAGES_BRANCH",
|
1866
|
-
"BRANCH"
|
1867
|
-
];
|
1868
2024
|
async function getCurrentBranchName(options) {
|
1869
|
-
const
|
1870
|
-
if (
|
1871
|
-
const details = await getDatabaseBranch(
|
2025
|
+
const { branch, envBranch } = getEnvironment();
|
2026
|
+
if (branch) {
|
2027
|
+
const details = await getDatabaseBranch(branch, options);
|
1872
2028
|
if (details)
|
1873
|
-
return
|
1874
|
-
console.warn(`Branch ${
|
2029
|
+
return branch;
|
2030
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1875
2031
|
}
|
1876
|
-
const gitBranch = await getGitBranch();
|
2032
|
+
const gitBranch = envBranch || await getGitBranch();
|
1877
2033
|
return resolveXataBranch(gitBranch, options);
|
1878
2034
|
}
|
1879
2035
|
async function getCurrentBranchDetails(options) {
|
@@ -1884,18 +2040,24 @@ async function resolveXataBranch(gitBranch, options) {
|
|
1884
2040
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1885
2041
|
const apiKey = options?.apiKey || getAPIKey();
|
1886
2042
|
if (!databaseURL)
|
1887
|
-
throw new Error(
|
2043
|
+
throw new Error(
|
2044
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
2045
|
+
);
|
1888
2046
|
if (!apiKey)
|
1889
|
-
throw new Error(
|
2047
|
+
throw new Error(
|
2048
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
2049
|
+
);
|
1890
2050
|
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1891
2051
|
const [workspace] = host.split(".");
|
2052
|
+
const { fallbackBranch } = getEnvironment();
|
1892
2053
|
const { branch } = await resolveBranch({
|
1893
2054
|
apiKey,
|
1894
2055
|
apiUrl: databaseURL,
|
1895
2056
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1896
2057
|
workspacesApiUrl: `${protocol}//${host}`,
|
1897
2058
|
pathParams: { dbName, workspace },
|
1898
|
-
queryParams: { gitBranch, fallbackBranch
|
2059
|
+
queryParams: { gitBranch, fallbackBranch },
|
2060
|
+
trace: defaultTrace
|
1899
2061
|
});
|
1900
2062
|
return branch;
|
1901
2063
|
}
|
@@ -1903,9 +2065,13 @@ async function getDatabaseBranch(branch, options) {
|
|
1903
2065
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1904
2066
|
const apiKey = options?.apiKey || getAPIKey();
|
1905
2067
|
if (!databaseURL)
|
1906
|
-
throw new Error(
|
2068
|
+
throw new Error(
|
2069
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
2070
|
+
);
|
1907
2071
|
if (!apiKey)
|
1908
|
-
throw new Error(
|
2072
|
+
throw new Error(
|
2073
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
2074
|
+
);
|
1909
2075
|
const [protocol, , host, , database] = databaseURL.split("/");
|
1910
2076
|
const [workspace] = host.split(".");
|
1911
2077
|
const dbBranchName = `${database}:${branch}`;
|
@@ -1915,7 +2081,8 @@ async function getDatabaseBranch(branch, options) {
|
|
1915
2081
|
apiUrl: databaseURL,
|
1916
2082
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1917
2083
|
workspacesApiUrl: `${protocol}//${host}`,
|
1918
|
-
pathParams: { dbBranchName, workspace }
|
2084
|
+
pathParams: { dbBranchName, workspace },
|
2085
|
+
trace: defaultTrace
|
1919
2086
|
});
|
1920
2087
|
} catch (err) {
|
1921
2088
|
if (isObject(err) && err.status === 404)
|
@@ -1923,21 +2090,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1923
2090
|
throw err;
|
1924
2091
|
}
|
1925
2092
|
}
|
1926
|
-
function getBranchByEnvVariable() {
|
1927
|
-
for (const name of envBranchNames) {
|
1928
|
-
const value = getEnvVariable(name);
|
1929
|
-
if (value) {
|
1930
|
-
return value;
|
1931
|
-
}
|
1932
|
-
}
|
1933
|
-
try {
|
1934
|
-
return XATA_BRANCH;
|
1935
|
-
} catch (err) {
|
1936
|
-
}
|
1937
|
-
}
|
1938
2093
|
function getDatabaseURL() {
|
1939
2094
|
try {
|
1940
|
-
|
2095
|
+
const { databaseURL } = getEnvironment();
|
2096
|
+
return databaseURL;
|
1941
2097
|
} catch (err) {
|
1942
2098
|
return void 0;
|
1943
2099
|
}
|
@@ -1966,20 +2122,23 @@ var __privateMethod = (obj, member, method) => {
|
|
1966
2122
|
return method;
|
1967
2123
|
};
|
1968
2124
|
const buildClient = (plugins) => {
|
1969
|
-
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
2125
|
+
var _branch, _options, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1970
2126
|
return _a = class {
|
1971
|
-
constructor(options = {},
|
2127
|
+
constructor(options = {}, schemaTables) {
|
1972
2128
|
__privateAdd(this, _parseOptions);
|
1973
2129
|
__privateAdd(this, _getFetchProps);
|
1974
2130
|
__privateAdd(this, _evaluateBranch);
|
1975
2131
|
__privateAdd(this, _branch, void 0);
|
2132
|
+
__privateAdd(this, _options, void 0);
|
1976
2133
|
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
2134
|
+
__privateSet(this, _options, safeOptions);
|
1977
2135
|
const pluginOptions = {
|
1978
2136
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1979
|
-
cache: safeOptions.cache
|
2137
|
+
cache: safeOptions.cache,
|
2138
|
+
trace: safeOptions.trace
|
1980
2139
|
};
|
1981
|
-
const db = new SchemaPlugin(
|
1982
|
-
const search = new SearchPlugin(db).build(pluginOptions);
|
2140
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2141
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1983
2142
|
this.db = db;
|
1984
2143
|
this.search = search;
|
1985
2144
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
@@ -1995,22 +2154,26 @@ const buildClient = (plugins) => {
|
|
1995
2154
|
}
|
1996
2155
|
}
|
1997
2156
|
}
|
1998
|
-
|
2157
|
+
async getConfig() {
|
2158
|
+
const databaseURL = __privateGet(this, _options).databaseURL;
|
2159
|
+
const branch = await __privateGet(this, _options).branch();
|
2160
|
+
return { databaseURL, branch };
|
2161
|
+
}
|
2162
|
+
}, _branch = new WeakMap(), _options = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
|
1999
2163
|
const fetch = getFetchImplementation(options?.fetch);
|
2000
2164
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
2001
2165
|
const apiKey = options?.apiKey || getAPIKey();
|
2002
|
-
const cache = options?.cache ?? new SimpleCache({
|
2166
|
+
const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
|
2167
|
+
const trace = options?.trace ?? defaultTrace;
|
2003
2168
|
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
2004
|
-
if (!
|
2005
|
-
throw new Error("
|
2169
|
+
if (!apiKey) {
|
2170
|
+
throw new Error("Option apiKey is required");
|
2006
2171
|
}
|
2007
|
-
|
2008
|
-
|
2009
|
-
|
2010
|
-
apiKey,
|
2011
|
-
|
2012
|
-
branch
|
2013
|
-
}) {
|
2172
|
+
if (!databaseURL) {
|
2173
|
+
throw new Error("Option databaseURL is required");
|
2174
|
+
}
|
2175
|
+
return { fetch, databaseURL, apiKey, branch, cache, trace };
|
2176
|
+
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({ fetch, apiKey, databaseURL, branch, trace }) {
|
2014
2177
|
const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
|
2015
2178
|
if (!branchValue)
|
2016
2179
|
throw new Error("Unable to resolve branch value");
|
@@ -2022,7 +2185,8 @@ const buildClient = (plugins) => {
|
|
2022
2185
|
const hasBranch = params.dbBranchName ?? params.branch;
|
2023
2186
|
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : "");
|
2024
2187
|
return databaseURL + newPath;
|
2025
|
-
}
|
2188
|
+
},
|
2189
|
+
trace
|
2026
2190
|
};
|
2027
2191
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
2028
2192
|
if (__privateGet(this, _branch))
|
@@ -2045,6 +2209,88 @@ const buildClient = (plugins) => {
|
|
2045
2209
|
class BaseClient extends buildClient() {
|
2046
2210
|
}
|
2047
2211
|
|
2212
|
+
const META = "__";
|
2213
|
+
const VALUE = "___";
|
2214
|
+
class Serializer {
|
2215
|
+
constructor() {
|
2216
|
+
this.classes = {};
|
2217
|
+
}
|
2218
|
+
add(clazz) {
|
2219
|
+
this.classes[clazz.name] = clazz;
|
2220
|
+
}
|
2221
|
+
toJSON(data) {
|
2222
|
+
function visit(obj) {
|
2223
|
+
if (Array.isArray(obj))
|
2224
|
+
return obj.map(visit);
|
2225
|
+
const type = typeof obj;
|
2226
|
+
if (type === "undefined")
|
2227
|
+
return { [META]: "undefined" };
|
2228
|
+
if (type === "bigint")
|
2229
|
+
return { [META]: "bigint", [VALUE]: obj.toString() };
|
2230
|
+
if (obj === null || type !== "object")
|
2231
|
+
return obj;
|
2232
|
+
const constructor = obj.constructor;
|
2233
|
+
const o = { [META]: constructor.name };
|
2234
|
+
for (const [key, value] of Object.entries(obj)) {
|
2235
|
+
o[key] = visit(value);
|
2236
|
+
}
|
2237
|
+
if (constructor === Date)
|
2238
|
+
o[VALUE] = obj.toISOString();
|
2239
|
+
if (constructor === Map)
|
2240
|
+
o[VALUE] = Object.fromEntries(obj);
|
2241
|
+
if (constructor === Set)
|
2242
|
+
o[VALUE] = [...obj];
|
2243
|
+
return o;
|
2244
|
+
}
|
2245
|
+
return JSON.stringify(visit(data));
|
2246
|
+
}
|
2247
|
+
fromJSON(json) {
|
2248
|
+
return JSON.parse(json, (key, value) => {
|
2249
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
2250
|
+
const { [META]: clazz, [VALUE]: val, ...rest } = value;
|
2251
|
+
const constructor = this.classes[clazz];
|
2252
|
+
if (constructor) {
|
2253
|
+
return Object.assign(Object.create(constructor.prototype), rest);
|
2254
|
+
}
|
2255
|
+
if (clazz === "Date")
|
2256
|
+
return new Date(val);
|
2257
|
+
if (clazz === "Set")
|
2258
|
+
return new Set(val);
|
2259
|
+
if (clazz === "Map")
|
2260
|
+
return new Map(Object.entries(val));
|
2261
|
+
if (clazz === "bigint")
|
2262
|
+
return BigInt(val);
|
2263
|
+
if (clazz === "undefined")
|
2264
|
+
return void 0;
|
2265
|
+
return rest;
|
2266
|
+
}
|
2267
|
+
return value;
|
2268
|
+
});
|
2269
|
+
}
|
2270
|
+
}
|
2271
|
+
const defaultSerializer = new Serializer();
|
2272
|
+
const serialize = (data) => {
|
2273
|
+
return defaultSerializer.toJSON(data);
|
2274
|
+
};
|
2275
|
+
const deserialize = (json) => {
|
2276
|
+
return defaultSerializer.fromJSON(json);
|
2277
|
+
};
|
2278
|
+
|
2279
|
+
function buildWorkerRunner(config) {
|
2280
|
+
return function xataWorker(name, _worker) {
|
2281
|
+
return async (...args) => {
|
2282
|
+
const url = process.env.NODE_ENV === "development" ? `http://localhost:64749/${name}` : `https://dispatcher.xata.workers.dev/${config.workspace}/${config.worker}/${name}`;
|
2283
|
+
const result = await fetch(url, {
|
2284
|
+
method: "POST",
|
2285
|
+
headers: { "Content-Type": "application/json" },
|
2286
|
+
body: serialize({ args })
|
2287
|
+
});
|
2288
|
+
const text = await result.text();
|
2289
|
+
return deserialize(text);
|
2290
|
+
};
|
2291
|
+
};
|
2292
|
+
}
|
2293
|
+
|
2048
2294
|
class XataError extends Error {
|
2049
2295
|
constructor(message, status) {
|
2050
2296
|
super(message);
|
@@ -2065,6 +2311,7 @@ exports.Repository = Repository;
|
|
2065
2311
|
exports.RestRepository = RestRepository;
|
2066
2312
|
exports.SchemaPlugin = SchemaPlugin;
|
2067
2313
|
exports.SearchPlugin = SearchPlugin;
|
2314
|
+
exports.Serializer = Serializer;
|
2068
2315
|
exports.SimpleCache = SimpleCache;
|
2069
2316
|
exports.XataApiClient = XataApiClient;
|
2070
2317
|
exports.XataApiPlugin = XataApiPlugin;
|
@@ -2074,6 +2321,7 @@ exports.acceptWorkspaceMemberInvite = acceptWorkspaceMemberInvite;
|
|
2074
2321
|
exports.addGitBranchesEntry = addGitBranchesEntry;
|
2075
2322
|
exports.addTableColumn = addTableColumn;
|
2076
2323
|
exports.buildClient = buildClient;
|
2324
|
+
exports.buildWorkerRunner = buildWorkerRunner;
|
2077
2325
|
exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
2078
2326
|
exports.cancelWorkspaceMemberInvite = cancelWorkspaceMemberInvite;
|
2079
2327
|
exports.contains = contains;
|
@@ -2090,7 +2338,9 @@ exports.deleteTable = deleteTable;
|
|
2090
2338
|
exports.deleteUser = deleteUser;
|
2091
2339
|
exports.deleteUserAPIKey = deleteUserAPIKey;
|
2092
2340
|
exports.deleteWorkspace = deleteWorkspace;
|
2341
|
+
exports.deserialize = deserialize;
|
2093
2342
|
exports.endsWith = endsWith;
|
2343
|
+
exports.equals = equals;
|
2094
2344
|
exports.executeBranchMigrationPlan = executeBranchMigrationPlan;
|
2095
2345
|
exports.exists = exists;
|
2096
2346
|
exports.ge = ge;
|
@@ -2105,6 +2355,7 @@ exports.getColumn = getColumn;
|
|
2105
2355
|
exports.getCurrentBranchDetails = getCurrentBranchDetails;
|
2106
2356
|
exports.getCurrentBranchName = getCurrentBranchName;
|
2107
2357
|
exports.getDatabaseList = getDatabaseList;
|
2358
|
+
exports.getDatabaseMetadata = getDatabaseMetadata;
|
2108
2359
|
exports.getDatabaseURL = getDatabaseURL;
|
2109
2360
|
exports.getGitBranchesMapping = getGitBranchesMapping;
|
2110
2361
|
exports.getRecord = getRecord;
|
@@ -2115,6 +2366,9 @@ exports.getUserAPIKeys = getUserAPIKeys;
|
|
2115
2366
|
exports.getWorkspace = getWorkspace;
|
2116
2367
|
exports.getWorkspaceMembersList = getWorkspaceMembersList;
|
2117
2368
|
exports.getWorkspacesList = getWorkspacesList;
|
2369
|
+
exports.greaterEquals = greaterEquals;
|
2370
|
+
exports.greaterThan = greaterThan;
|
2371
|
+
exports.greaterThanEquals = greaterThanEquals;
|
2118
2372
|
exports.gt = gt;
|
2119
2373
|
exports.gte = gte;
|
2120
2374
|
exports.includes = includes;
|
@@ -2130,6 +2384,9 @@ exports.isIdentifiable = isIdentifiable;
|
|
2130
2384
|
exports.isNot = isNot;
|
2131
2385
|
exports.isXataRecord = isXataRecord;
|
2132
2386
|
exports.le = le;
|
2387
|
+
exports.lessEquals = lessEquals;
|
2388
|
+
exports.lessThan = lessThan;
|
2389
|
+
exports.lessThanEquals = lessThanEquals;
|
2133
2390
|
exports.lt = lt;
|
2134
2391
|
exports.lte = lte;
|
2135
2392
|
exports.notExists = notExists;
|
@@ -2142,6 +2399,7 @@ exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
|
|
2142
2399
|
exports.resolveBranch = resolveBranch;
|
2143
2400
|
exports.searchBranch = searchBranch;
|
2144
2401
|
exports.searchTable = searchTable;
|
2402
|
+
exports.serialize = serialize;
|
2145
2403
|
exports.setTableSchema = setTableSchema;
|
2146
2404
|
exports.startsWith = startsWith;
|
2147
2405
|
exports.updateBranchMetadata = updateBranchMetadata;
|
@@ -2150,6 +2408,7 @@ exports.updateRecordWithID = updateRecordWithID;
|
|
2150
2408
|
exports.updateTable = updateTable;
|
2151
2409
|
exports.updateUser = updateUser;
|
2152
2410
|
exports.updateWorkspace = updateWorkspace;
|
2411
|
+
exports.updateWorkspaceMemberInvite = updateWorkspaceMemberInvite;
|
2153
2412
|
exports.updateWorkspaceMemberRole = updateWorkspaceMemberRole;
|
2154
2413
|
exports.upsertRecordWithID = upsertRecordWithID;
|
2155
2414
|
//# sourceMappingURL=index.cjs.map
|