@xata.io/client 0.0.0-alpha.vf4b92f1 → 0.0.0-alpha.vf603f80
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 +154 -0
- package/README.md +271 -1
- package/Usage.md +395 -0
- package/dist/index.cjs +652 -253
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +724 -89
- package/dist/index.mjs +623 -254
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -4
- package/tsconfig.json +1 -0
package/dist/index.mjs
CHANGED
@@ -7,43 +7,98 @@ function compact(arr) {
|
|
7
7
|
function isObject(value) {
|
8
8
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
9
9
|
}
|
10
|
+
function isDefined(value) {
|
11
|
+
return value !== null && value !== void 0;
|
12
|
+
}
|
10
13
|
function isString(value) {
|
11
|
-
return value
|
14
|
+
return isDefined(value) && typeof value === "string";
|
12
15
|
}
|
13
16
|
function toBase64(value) {
|
14
17
|
try {
|
15
18
|
return btoa(value);
|
16
19
|
} catch (err) {
|
17
|
-
|
20
|
+
const buf = Buffer;
|
21
|
+
return buf.from(value).toString("base64");
|
18
22
|
}
|
19
23
|
}
|
20
24
|
|
21
|
-
function
|
25
|
+
function getEnvironment() {
|
22
26
|
try {
|
23
|
-
if (isObject(process) &&
|
24
|
-
return
|
27
|
+
if (isObject(process) && isObject(process.env)) {
|
28
|
+
return {
|
29
|
+
apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
|
30
|
+
databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
|
31
|
+
branch: process.env.XATA_BRANCH ?? getGlobalBranch(),
|
32
|
+
envBranch: process.env.VERCEL_GIT_COMMIT_REF ?? process.env.CF_PAGES_BRANCH ?? process.env.BRANCH,
|
33
|
+
fallbackBranch: process.env.XATA_FALLBACK_BRANCH ?? getGlobalFallbackBranch()
|
34
|
+
};
|
25
35
|
}
|
26
36
|
} catch (err) {
|
27
37
|
}
|
28
38
|
try {
|
29
|
-
if (isObject(Deno) &&
|
30
|
-
return
|
39
|
+
if (isObject(Deno) && isObject(Deno.env)) {
|
40
|
+
return {
|
41
|
+
apiKey: Deno.env.get("XATA_API_KEY") ?? getGlobalApiKey(),
|
42
|
+
databaseURL: Deno.env.get("XATA_DATABASE_URL") ?? getGlobalDatabaseURL(),
|
43
|
+
branch: Deno.env.get("XATA_BRANCH") ?? getGlobalBranch(),
|
44
|
+
envBranch: Deno.env.get("VERCEL_GIT_COMMIT_REF") ?? Deno.env.get("CF_PAGES_BRANCH") ?? Deno.env.get("BRANCH"),
|
45
|
+
fallbackBranch: Deno.env.get("XATA_FALLBACK_BRANCH") ?? getGlobalFallbackBranch()
|
46
|
+
};
|
31
47
|
}
|
32
48
|
} catch (err) {
|
33
49
|
}
|
50
|
+
return {
|
51
|
+
apiKey: getGlobalApiKey(),
|
52
|
+
databaseURL: getGlobalDatabaseURL(),
|
53
|
+
branch: getGlobalBranch(),
|
54
|
+
envBranch: void 0,
|
55
|
+
fallbackBranch: getGlobalFallbackBranch()
|
56
|
+
};
|
57
|
+
}
|
58
|
+
function getGlobalApiKey() {
|
59
|
+
try {
|
60
|
+
return XATA_API_KEY;
|
61
|
+
} catch (err) {
|
62
|
+
return void 0;
|
63
|
+
}
|
64
|
+
}
|
65
|
+
function getGlobalDatabaseURL() {
|
66
|
+
try {
|
67
|
+
return XATA_DATABASE_URL;
|
68
|
+
} catch (err) {
|
69
|
+
return void 0;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
function getGlobalBranch() {
|
73
|
+
try {
|
74
|
+
return XATA_BRANCH;
|
75
|
+
} catch (err) {
|
76
|
+
return void 0;
|
77
|
+
}
|
78
|
+
}
|
79
|
+
function getGlobalFallbackBranch() {
|
80
|
+
try {
|
81
|
+
return XATA_FALLBACK_BRANCH;
|
82
|
+
} catch (err) {
|
83
|
+
return void 0;
|
84
|
+
}
|
34
85
|
}
|
35
86
|
async function getGitBranch() {
|
87
|
+
const cmd = ["git", "branch", "--show-current"];
|
88
|
+
const fullCmd = cmd.join(" ");
|
89
|
+
const nodeModule = ["child", "process"].join("_");
|
90
|
+
const execOptions = { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] };
|
36
91
|
try {
|
37
|
-
|
92
|
+
if (typeof require === "function") {
|
93
|
+
return require(nodeModule).execSync(fullCmd, execOptions).trim();
|
94
|
+
}
|
95
|
+
const { execSync } = await import(nodeModule);
|
96
|
+
return execSync(fullCmd, execOptions).toString().trim();
|
38
97
|
} catch (err) {
|
39
98
|
}
|
40
99
|
try {
|
41
100
|
if (isObject(Deno)) {
|
42
|
-
const process2 = Deno.run({
|
43
|
-
cmd: ["git", "branch", "--show-current"],
|
44
|
-
stdout: "piped",
|
45
|
-
stderr: "piped"
|
46
|
-
});
|
101
|
+
const process2 = Deno.run({ cmd, stdout: "piped", stderr: "null" });
|
47
102
|
return new TextDecoder().decode(await process2.output()).trim();
|
48
103
|
}
|
49
104
|
} catch (err) {
|
@@ -52,7 +107,8 @@ async function getGitBranch() {
|
|
52
107
|
|
53
108
|
function getAPIKey() {
|
54
109
|
try {
|
55
|
-
|
110
|
+
const { apiKey } = getEnvironment();
|
111
|
+
return apiKey;
|
56
112
|
} catch (err) {
|
57
113
|
return void 0;
|
58
114
|
}
|
@@ -67,16 +123,28 @@ function getFetchImplementation(userFetch) {
|
|
67
123
|
return fetchImpl;
|
68
124
|
}
|
69
125
|
|
70
|
-
|
71
|
-
|
126
|
+
const VERSION = "0.0.0-alpha.vf603f80";
|
127
|
+
|
128
|
+
class ErrorWithCause extends Error {
|
129
|
+
constructor(message, options) {
|
130
|
+
super(message, options);
|
131
|
+
}
|
132
|
+
}
|
133
|
+
class FetcherError extends ErrorWithCause {
|
134
|
+
constructor(status, data, requestId) {
|
72
135
|
super(getMessage(data));
|
73
136
|
this.status = status;
|
74
137
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
138
|
+
this.requestId = requestId;
|
75
139
|
if (data instanceof Error) {
|
76
140
|
this.stack = data.stack;
|
77
141
|
this.cause = data.cause;
|
78
142
|
}
|
79
143
|
}
|
144
|
+
toString() {
|
145
|
+
const error = super.toString();
|
146
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
147
|
+
}
|
80
148
|
}
|
81
149
|
function isBulkError(error) {
|
82
150
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -99,7 +167,12 @@ function getMessage(data) {
|
|
99
167
|
}
|
100
168
|
|
101
169
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
102
|
-
const
|
170
|
+
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
171
|
+
if (value === void 0 || value === null)
|
172
|
+
return acc;
|
173
|
+
return { ...acc, [key]: value };
|
174
|
+
}, {});
|
175
|
+
const query = new URLSearchParams(cleanQueryParams).toString();
|
103
176
|
const queryString = query.length > 0 ? `?${query}` : "";
|
104
177
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
105
178
|
};
|
@@ -139,6 +212,7 @@ async function fetch$1({
|
|
139
212
|
body: body ? JSON.stringify(body) : void 0,
|
140
213
|
headers: {
|
141
214
|
"Content-Type": "application/json",
|
215
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
142
216
|
...headers,
|
143
217
|
...hostHeader(fullUrl),
|
144
218
|
Authorization: `Bearer ${apiKey}`
|
@@ -147,14 +221,15 @@ async function fetch$1({
|
|
147
221
|
if (response.status === 204) {
|
148
222
|
return {};
|
149
223
|
}
|
224
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
150
225
|
try {
|
151
226
|
const jsonResponse = await response.json();
|
152
227
|
if (response.ok) {
|
153
228
|
return jsonResponse;
|
154
229
|
}
|
155
|
-
throw new FetcherError(response.status, jsonResponse);
|
230
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
156
231
|
} catch (error) {
|
157
|
-
throw new FetcherError(response.status, error);
|
232
|
+
throw new FetcherError(response.status, error, requestId);
|
158
233
|
}
|
159
234
|
}
|
160
235
|
|
@@ -213,6 +288,7 @@ const removeWorkspaceMember = (variables) => fetch$1({
|
|
213
288
|
...variables
|
214
289
|
});
|
215
290
|
const inviteWorkspaceMember = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites", method: "post", ...variables });
|
291
|
+
const updateWorkspaceMemberInvite = (variables) => fetch$1({ url: "/workspaces/{workspaceId}/invites/{inviteId}", method: "patch", ...variables });
|
216
292
|
const cancelWorkspaceMemberInvite = (variables) => fetch$1({
|
217
293
|
url: "/workspaces/{workspaceId}/invites/{inviteId}",
|
218
294
|
method: "delete",
|
@@ -248,6 +324,14 @@ const deleteDatabase = (variables) => fetch$1({
|
|
248
324
|
method: "delete",
|
249
325
|
...variables
|
250
326
|
});
|
327
|
+
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
328
|
+
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
329
|
+
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
330
|
+
const resolveBranch = (variables) => fetch$1({
|
331
|
+
url: "/dbs/{dbName}/resolveBranch",
|
332
|
+
method: "get",
|
333
|
+
...variables
|
334
|
+
});
|
251
335
|
const getBranchDetails = (variables) => fetch$1({
|
252
336
|
url: "/db/{dbBranchName}",
|
253
337
|
method: "get",
|
@@ -355,6 +439,11 @@ const queryTable = (variables) => fetch$1({
|
|
355
439
|
method: "post",
|
356
440
|
...variables
|
357
441
|
});
|
442
|
+
const searchTable = (variables) => fetch$1({
|
443
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
444
|
+
method: "post",
|
445
|
+
...variables
|
446
|
+
});
|
358
447
|
const searchBranch = (variables) => fetch$1({
|
359
448
|
url: "/db/{dbBranchName}/search",
|
360
449
|
method: "post",
|
@@ -372,11 +461,20 @@ const operationsByTag = {
|
|
372
461
|
updateWorkspaceMemberRole,
|
373
462
|
removeWorkspaceMember,
|
374
463
|
inviteWorkspaceMember,
|
464
|
+
updateWorkspaceMemberInvite,
|
375
465
|
cancelWorkspaceMemberInvite,
|
376
466
|
resendWorkspaceMemberInvite,
|
377
467
|
acceptWorkspaceMemberInvite
|
378
468
|
},
|
379
|
-
database: {
|
469
|
+
database: {
|
470
|
+
getDatabaseList,
|
471
|
+
createDatabase,
|
472
|
+
deleteDatabase,
|
473
|
+
getGitBranchesMapping,
|
474
|
+
addGitBranchesEntry,
|
475
|
+
removeGitBranchesEntry,
|
476
|
+
resolveBranch
|
477
|
+
},
|
380
478
|
branch: {
|
381
479
|
getBranchList,
|
382
480
|
getBranchDetails,
|
@@ -410,6 +508,7 @@ const operationsByTag = {
|
|
410
508
|
getRecord,
|
411
509
|
bulkInsertTableRecords,
|
412
510
|
queryTable,
|
511
|
+
searchTable,
|
413
512
|
searchBranch
|
414
513
|
}
|
415
514
|
};
|
@@ -443,7 +542,7 @@ var __accessCheck$7 = (obj, member, msg) => {
|
|
443
542
|
if (!member.has(obj))
|
444
543
|
throw TypeError("Cannot " + msg);
|
445
544
|
};
|
446
|
-
var __privateGet$
|
545
|
+
var __privateGet$7 = (obj, member, getter) => {
|
447
546
|
__accessCheck$7(obj, member, "read from private field");
|
448
547
|
return getter ? getter.call(obj) : member.get(obj);
|
449
548
|
};
|
@@ -452,7 +551,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
452
551
|
throw TypeError("Cannot add the same private member more than once");
|
453
552
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
454
553
|
};
|
455
|
-
var __privateSet$
|
554
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
456
555
|
__accessCheck$7(obj, member, "write to private field");
|
457
556
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
458
557
|
return value;
|
@@ -467,7 +566,7 @@ class XataApiClient {
|
|
467
566
|
if (!apiKey) {
|
468
567
|
throw new Error("Could not resolve a valid apiKey");
|
469
568
|
}
|
470
|
-
__privateSet$
|
569
|
+
__privateSet$7(this, _extraProps, {
|
471
570
|
apiUrl: getHostUrl(provider, "main"),
|
472
571
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
473
572
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -475,34 +574,34 @@ class XataApiClient {
|
|
475
574
|
});
|
476
575
|
}
|
477
576
|
get user() {
|
478
|
-
if (!__privateGet$
|
479
|
-
__privateGet$
|
480
|
-
return __privateGet$
|
577
|
+
if (!__privateGet$7(this, _namespaces).user)
|
578
|
+
__privateGet$7(this, _namespaces).user = new UserApi(__privateGet$7(this, _extraProps));
|
579
|
+
return __privateGet$7(this, _namespaces).user;
|
481
580
|
}
|
482
581
|
get workspaces() {
|
483
|
-
if (!__privateGet$
|
484
|
-
__privateGet$
|
485
|
-
return __privateGet$
|
582
|
+
if (!__privateGet$7(this, _namespaces).workspaces)
|
583
|
+
__privateGet$7(this, _namespaces).workspaces = new WorkspaceApi(__privateGet$7(this, _extraProps));
|
584
|
+
return __privateGet$7(this, _namespaces).workspaces;
|
486
585
|
}
|
487
586
|
get databases() {
|
488
|
-
if (!__privateGet$
|
489
|
-
__privateGet$
|
490
|
-
return __privateGet$
|
587
|
+
if (!__privateGet$7(this, _namespaces).databases)
|
588
|
+
__privateGet$7(this, _namespaces).databases = new DatabaseApi(__privateGet$7(this, _extraProps));
|
589
|
+
return __privateGet$7(this, _namespaces).databases;
|
491
590
|
}
|
492
591
|
get branches() {
|
493
|
-
if (!__privateGet$
|
494
|
-
__privateGet$
|
495
|
-
return __privateGet$
|
592
|
+
if (!__privateGet$7(this, _namespaces).branches)
|
593
|
+
__privateGet$7(this, _namespaces).branches = new BranchApi(__privateGet$7(this, _extraProps));
|
594
|
+
return __privateGet$7(this, _namespaces).branches;
|
496
595
|
}
|
497
596
|
get tables() {
|
498
|
-
if (!__privateGet$
|
499
|
-
__privateGet$
|
500
|
-
return __privateGet$
|
597
|
+
if (!__privateGet$7(this, _namespaces).tables)
|
598
|
+
__privateGet$7(this, _namespaces).tables = new TableApi(__privateGet$7(this, _extraProps));
|
599
|
+
return __privateGet$7(this, _namespaces).tables;
|
501
600
|
}
|
502
601
|
get records() {
|
503
|
-
if (!__privateGet$
|
504
|
-
__privateGet$
|
505
|
-
return __privateGet$
|
602
|
+
if (!__privateGet$7(this, _namespaces).records)
|
603
|
+
__privateGet$7(this, _namespaces).records = new RecordsApi(__privateGet$7(this, _extraProps));
|
604
|
+
return __privateGet$7(this, _namespaces).records;
|
506
605
|
}
|
507
606
|
}
|
508
607
|
_extraProps = new WeakMap();
|
@@ -594,6 +693,13 @@ class WorkspaceApi {
|
|
594
693
|
...this.extraProps
|
595
694
|
});
|
596
695
|
}
|
696
|
+
updateWorkspaceMemberInvite(workspaceId, inviteId, role) {
|
697
|
+
return operationsByTag.workspaces.updateWorkspaceMemberInvite({
|
698
|
+
pathParams: { workspaceId, inviteId },
|
699
|
+
body: { role },
|
700
|
+
...this.extraProps
|
701
|
+
});
|
702
|
+
}
|
597
703
|
cancelWorkspaceMemberInvite(workspaceId, inviteId) {
|
598
704
|
return operationsByTag.workspaces.cancelWorkspaceMemberInvite({
|
599
705
|
pathParams: { workspaceId, inviteId },
|
@@ -636,6 +742,33 @@ class DatabaseApi {
|
|
636
742
|
...this.extraProps
|
637
743
|
});
|
638
744
|
}
|
745
|
+
getGitBranchesMapping(workspace, dbName) {
|
746
|
+
return operationsByTag.database.getGitBranchesMapping({
|
747
|
+
pathParams: { workspace, dbName },
|
748
|
+
...this.extraProps
|
749
|
+
});
|
750
|
+
}
|
751
|
+
addGitBranchesEntry(workspace, dbName, body) {
|
752
|
+
return operationsByTag.database.addGitBranchesEntry({
|
753
|
+
pathParams: { workspace, dbName },
|
754
|
+
body,
|
755
|
+
...this.extraProps
|
756
|
+
});
|
757
|
+
}
|
758
|
+
removeGitBranchesEntry(workspace, dbName, gitBranch) {
|
759
|
+
return operationsByTag.database.removeGitBranchesEntry({
|
760
|
+
pathParams: { workspace, dbName },
|
761
|
+
queryParams: { gitBranch },
|
762
|
+
...this.extraProps
|
763
|
+
});
|
764
|
+
}
|
765
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
766
|
+
return operationsByTag.database.resolveBranch({
|
767
|
+
pathParams: { workspace, dbName },
|
768
|
+
queryParams: { gitBranch, fallbackBranch },
|
769
|
+
...this.extraProps
|
770
|
+
});
|
771
|
+
}
|
639
772
|
}
|
640
773
|
class BranchApi {
|
641
774
|
constructor(extraProps) {
|
@@ -653,10 +786,10 @@ class BranchApi {
|
|
653
786
|
...this.extraProps
|
654
787
|
});
|
655
788
|
}
|
656
|
-
createBranch(workspace, database, branch, from
|
789
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
657
790
|
return operationsByTag.branch.createBranch({
|
658
791
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
659
|
-
queryParams: { from },
|
792
|
+
queryParams: isString(from) ? { from } : void 0,
|
660
793
|
body: options,
|
661
794
|
...this.extraProps
|
662
795
|
});
|
@@ -838,6 +971,13 @@ class RecordsApi {
|
|
838
971
|
...this.extraProps
|
839
972
|
});
|
840
973
|
}
|
974
|
+
searchTable(workspace, database, branch, tableName, query) {
|
975
|
+
return operationsByTag.records.searchTable({
|
976
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
977
|
+
body: query,
|
978
|
+
...this.extraProps
|
979
|
+
});
|
980
|
+
}
|
841
981
|
searchBranch(workspace, database, branch, query) {
|
842
982
|
return operationsByTag.records.searchBranch({
|
843
983
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -861,7 +1001,7 @@ var __accessCheck$6 = (obj, member, msg) => {
|
|
861
1001
|
if (!member.has(obj))
|
862
1002
|
throw TypeError("Cannot " + msg);
|
863
1003
|
};
|
864
|
-
var __privateGet$
|
1004
|
+
var __privateGet$6 = (obj, member, getter) => {
|
865
1005
|
__accessCheck$6(obj, member, "read from private field");
|
866
1006
|
return getter ? getter.call(obj) : member.get(obj);
|
867
1007
|
};
|
@@ -870,30 +1010,30 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
870
1010
|
throw TypeError("Cannot add the same private member more than once");
|
871
1011
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
872
1012
|
};
|
873
|
-
var __privateSet$
|
1013
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
874
1014
|
__accessCheck$6(obj, member, "write to private field");
|
875
1015
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
876
1016
|
return value;
|
877
1017
|
};
|
878
|
-
var _query;
|
1018
|
+
var _query, _page;
|
879
1019
|
class Page {
|
880
1020
|
constructor(query, meta, records = []) {
|
881
1021
|
__privateAdd$6(this, _query, void 0);
|
882
|
-
__privateSet$
|
1022
|
+
__privateSet$6(this, _query, query);
|
883
1023
|
this.meta = meta;
|
884
|
-
this.records = records;
|
1024
|
+
this.records = new RecordArray(this, records);
|
885
1025
|
}
|
886
1026
|
async nextPage(size, offset) {
|
887
|
-
return __privateGet$
|
1027
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
888
1028
|
}
|
889
1029
|
async previousPage(size, offset) {
|
890
|
-
return __privateGet$
|
1030
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
891
1031
|
}
|
892
1032
|
async firstPage(size, offset) {
|
893
|
-
return __privateGet$
|
1033
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
|
894
1034
|
}
|
895
1035
|
async lastPage(size, offset) {
|
896
|
-
return __privateGet$
|
1036
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
|
897
1037
|
}
|
898
1038
|
hasNextPage() {
|
899
1039
|
return this.meta.page.more;
|
@@ -901,15 +1041,62 @@ class Page {
|
|
901
1041
|
}
|
902
1042
|
_query = new WeakMap();
|
903
1043
|
const PAGINATION_MAX_SIZE = 200;
|
904
|
-
const PAGINATION_DEFAULT_SIZE =
|
1044
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
905
1045
|
const PAGINATION_MAX_OFFSET = 800;
|
906
1046
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
1047
|
+
function isCursorPaginationOptions(options) {
|
1048
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
1049
|
+
}
|
1050
|
+
const _RecordArray = class extends Array {
|
1051
|
+
constructor(...args) {
|
1052
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
1053
|
+
__privateAdd$6(this, _page, void 0);
|
1054
|
+
__privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
|
1055
|
+
}
|
1056
|
+
static parseConstructorParams(...args) {
|
1057
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1058
|
+
return new Array(args[0]);
|
1059
|
+
}
|
1060
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1061
|
+
const result = args[1] ?? args[0].records ?? [];
|
1062
|
+
return new Array(...result);
|
1063
|
+
}
|
1064
|
+
return new Array(...args);
|
1065
|
+
}
|
1066
|
+
toArray() {
|
1067
|
+
return new Array(...this);
|
1068
|
+
}
|
1069
|
+
map(callbackfn, thisArg) {
|
1070
|
+
return this.toArray().map(callbackfn, thisArg);
|
1071
|
+
}
|
1072
|
+
async nextPage(size, offset) {
|
1073
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1074
|
+
return new _RecordArray(newPage);
|
1075
|
+
}
|
1076
|
+
async previousPage(size, offset) {
|
1077
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1078
|
+
return new _RecordArray(newPage);
|
1079
|
+
}
|
1080
|
+
async firstPage(size, offset) {
|
1081
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1082
|
+
return new _RecordArray(newPage);
|
1083
|
+
}
|
1084
|
+
async lastPage(size, offset) {
|
1085
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1086
|
+
return new _RecordArray(newPage);
|
1087
|
+
}
|
1088
|
+
hasNextPage() {
|
1089
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1090
|
+
}
|
1091
|
+
};
|
1092
|
+
let RecordArray = _RecordArray;
|
1093
|
+
_page = new WeakMap();
|
907
1094
|
|
908
1095
|
var __accessCheck$5 = (obj, member, msg) => {
|
909
1096
|
if (!member.has(obj))
|
910
1097
|
throw TypeError("Cannot " + msg);
|
911
1098
|
};
|
912
|
-
var __privateGet$
|
1099
|
+
var __privateGet$5 = (obj, member, getter) => {
|
913
1100
|
__accessCheck$5(obj, member, "read from private field");
|
914
1101
|
return getter ? getter.call(obj) : member.get(obj);
|
915
1102
|
};
|
@@ -918,34 +1105,35 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
918
1105
|
throw TypeError("Cannot add the same private member more than once");
|
919
1106
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
920
1107
|
};
|
921
|
-
var __privateSet$
|
1108
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
922
1109
|
__accessCheck$5(obj, member, "write to private field");
|
923
1110
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
924
1111
|
return value;
|
925
1112
|
};
|
926
1113
|
var _table$1, _repository, _data;
|
927
1114
|
const _Query = class {
|
928
|
-
constructor(repository, table, data,
|
1115
|
+
constructor(repository, table, data, rawParent) {
|
929
1116
|
__privateAdd$5(this, _table$1, void 0);
|
930
1117
|
__privateAdd$5(this, _repository, void 0);
|
931
1118
|
__privateAdd$5(this, _data, { filter: {} });
|
932
1119
|
this.meta = { page: { cursor: "start", more: true } };
|
933
|
-
this.records = [];
|
934
|
-
__privateSet$
|
1120
|
+
this.records = new RecordArray(this, []);
|
1121
|
+
__privateSet$5(this, _table$1, table);
|
935
1122
|
if (repository) {
|
936
|
-
__privateSet$
|
1123
|
+
__privateSet$5(this, _repository, repository);
|
937
1124
|
} else {
|
938
|
-
__privateSet$
|
1125
|
+
__privateSet$5(this, _repository, this);
|
939
1126
|
}
|
940
|
-
|
941
|
-
__privateGet$
|
942
|
-
__privateGet$
|
943
|
-
__privateGet$
|
944
|
-
__privateGet$
|
945
|
-
__privateGet$
|
946
|
-
__privateGet$
|
947
|
-
__privateGet$
|
948
|
-
__privateGet$
|
1127
|
+
const parent = cleanParent(data, rawParent);
|
1128
|
+
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
1129
|
+
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
1130
|
+
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
1131
|
+
__privateGet$5(this, _data).filter.$not = data.filter?.$not ?? parent?.filter?.$not;
|
1132
|
+
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
1133
|
+
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
1134
|
+
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
1135
|
+
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
1136
|
+
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
949
1137
|
this.any = this.any.bind(this);
|
950
1138
|
this.all = this.all.bind(this);
|
951
1139
|
this.not = this.not.bind(this);
|
@@ -956,83 +1144,88 @@ const _Query = class {
|
|
956
1144
|
Object.defineProperty(this, "repository", { enumerable: false });
|
957
1145
|
}
|
958
1146
|
getQueryOptions() {
|
959
|
-
return __privateGet$
|
1147
|
+
return __privateGet$5(this, _data);
|
960
1148
|
}
|
961
1149
|
key() {
|
962
|
-
const { columns = [], filter = {}, sort = [],
|
963
|
-
const key = JSON.stringify({ columns, filter, sort,
|
1150
|
+
const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
|
1151
|
+
const key = JSON.stringify({ columns, filter, sort, pagination });
|
964
1152
|
return toBase64(key);
|
965
1153
|
}
|
966
1154
|
any(...queries) {
|
967
1155
|
const $any = queries.map((query) => query.getQueryOptions().filter ?? {});
|
968
|
-
return new _Query(__privateGet$
|
1156
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $any } }, __privateGet$5(this, _data));
|
969
1157
|
}
|
970
1158
|
all(...queries) {
|
971
1159
|
const $all = queries.map((query) => query.getQueryOptions().filter ?? {});
|
972
|
-
return new _Query(__privateGet$
|
1160
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
973
1161
|
}
|
974
1162
|
not(...queries) {
|
975
1163
|
const $not = queries.map((query) => query.getQueryOptions().filter ?? {});
|
976
|
-
return new _Query(__privateGet$
|
1164
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $not } }, __privateGet$5(this, _data));
|
977
1165
|
}
|
978
1166
|
none(...queries) {
|
979
1167
|
const $none = queries.map((query) => query.getQueryOptions().filter ?? {});
|
980
|
-
return new _Query(__privateGet$
|
1168
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $none } }, __privateGet$5(this, _data));
|
981
1169
|
}
|
982
1170
|
filter(a, b) {
|
983
1171
|
if (arguments.length === 1) {
|
984
1172
|
const constraints = Object.entries(a).map(([column, constraint]) => ({ [column]: constraint }));
|
985
|
-
const $all = compact([__privateGet$
|
986
|
-
return new _Query(__privateGet$
|
1173
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat(constraints));
|
1174
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
987
1175
|
} else {
|
988
|
-
const $all = compact([__privateGet$
|
989
|
-
return new _Query(__privateGet$
|
1176
|
+
const $all = compact([__privateGet$5(this, _data).filter?.$all].flat().concat([{ [a]: b }]));
|
1177
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
990
1178
|
}
|
991
1179
|
}
|
992
1180
|
sort(column, direction) {
|
993
|
-
const originalSort = [__privateGet$
|
1181
|
+
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
994
1182
|
const sort = [...originalSort, { column, direction }];
|
995
|
-
return new _Query(__privateGet$
|
1183
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
996
1184
|
}
|
997
1185
|
select(columns) {
|
998
|
-
return new _Query(__privateGet$
|
1186
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { columns }, __privateGet$5(this, _data));
|
999
1187
|
}
|
1000
1188
|
getPaginated(options = {}) {
|
1001
|
-
const query = new _Query(__privateGet$
|
1002
|
-
return __privateGet$
|
1189
|
+
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
1190
|
+
return __privateGet$5(this, _repository).query(query);
|
1003
1191
|
}
|
1004
1192
|
async *[Symbol.asyncIterator]() {
|
1005
|
-
for await (const [record] of this.getIterator(1)) {
|
1193
|
+
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
1006
1194
|
yield record;
|
1007
1195
|
}
|
1008
1196
|
}
|
1009
|
-
async *getIterator(
|
1010
|
-
|
1011
|
-
let
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1197
|
+
async *getIterator(options = {}) {
|
1198
|
+
const { batchSize = 1 } = options;
|
1199
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1200
|
+
let more = page.hasNextPage();
|
1201
|
+
yield page.records;
|
1202
|
+
while (more) {
|
1203
|
+
page = await page.nextPage();
|
1204
|
+
more = page.hasNextPage();
|
1205
|
+
yield page.records;
|
1017
1206
|
}
|
1018
1207
|
}
|
1019
1208
|
async getMany(options = {}) {
|
1020
|
-
const
|
1021
|
-
|
1209
|
+
const page = await this.getPaginated(options);
|
1210
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1211
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1212
|
+
}
|
1213
|
+
return page.records;
|
1022
1214
|
}
|
1023
|
-
async getAll(
|
1215
|
+
async getAll(options = {}) {
|
1216
|
+
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
1024
1217
|
const results = [];
|
1025
|
-
for await (const page of this.getIterator(
|
1218
|
+
for await (const page of this.getIterator({ ...rest, batchSize })) {
|
1026
1219
|
results.push(...page);
|
1027
1220
|
}
|
1028
1221
|
return results;
|
1029
1222
|
}
|
1030
1223
|
async getFirst(options = {}) {
|
1031
|
-
const records = await this.getMany({ ...options,
|
1032
|
-
return records[0]
|
1224
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1225
|
+
return records[0] ?? null;
|
1033
1226
|
}
|
1034
1227
|
cache(ttl) {
|
1035
|
-
return new _Query(__privateGet$
|
1228
|
+
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
1036
1229
|
}
|
1037
1230
|
nextPage(size, offset) {
|
1038
1231
|
return this.firstPage(size, offset);
|
@@ -1041,10 +1234,10 @@ const _Query = class {
|
|
1041
1234
|
return this.firstPage(size, offset);
|
1042
1235
|
}
|
1043
1236
|
firstPage(size, offset) {
|
1044
|
-
return this.getPaginated({
|
1237
|
+
return this.getPaginated({ pagination: { size, offset } });
|
1045
1238
|
}
|
1046
1239
|
lastPage(size, offset) {
|
1047
|
-
return this.getPaginated({
|
1240
|
+
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
1048
1241
|
}
|
1049
1242
|
hasNextPage() {
|
1050
1243
|
return this.meta.page.more;
|
@@ -1054,12 +1247,20 @@ let Query = _Query;
|
|
1054
1247
|
_table$1 = new WeakMap();
|
1055
1248
|
_repository = new WeakMap();
|
1056
1249
|
_data = new WeakMap();
|
1250
|
+
function cleanParent(data, parent) {
|
1251
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1252
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1253
|
+
}
|
1254
|
+
return parent;
|
1255
|
+
}
|
1057
1256
|
|
1058
1257
|
function isIdentifiable(x) {
|
1059
1258
|
return isObject(x) && isString(x?.id);
|
1060
1259
|
}
|
1061
1260
|
function isXataRecord(x) {
|
1062
|
-
|
1261
|
+
const record = x;
|
1262
|
+
const metadata = record?.getMetadata();
|
1263
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1063
1264
|
}
|
1064
1265
|
|
1065
1266
|
function isSortFilterString(value) {
|
@@ -1089,7 +1290,7 @@ var __accessCheck$4 = (obj, member, msg) => {
|
|
1089
1290
|
if (!member.has(obj))
|
1090
1291
|
throw TypeError("Cannot " + msg);
|
1091
1292
|
};
|
1092
|
-
var __privateGet$
|
1293
|
+
var __privateGet$4 = (obj, member, getter) => {
|
1093
1294
|
__accessCheck$4(obj, member, "read from private field");
|
1094
1295
|
return getter ? getter.call(obj) : member.get(obj);
|
1095
1296
|
};
|
@@ -1098,7 +1299,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1098
1299
|
throw TypeError("Cannot add the same private member more than once");
|
1099
1300
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1100
1301
|
};
|
1101
|
-
var __privateSet$
|
1302
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1102
1303
|
__accessCheck$4(obj, member, "write to private field");
|
1103
1304
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1104
1305
|
return value;
|
@@ -1107,7 +1308,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1107
1308
|
__accessCheck$4(obj, member, "access private method");
|
1108
1309
|
return method;
|
1109
1310
|
};
|
1110
|
-
var _table,
|
1311
|
+
var _table, _getFetchProps, _cache, _schemaTables$2, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _invalidateCache, invalidateCache_fn, _setCacheRecord, setCacheRecord_fn, _getCacheRecord, getCacheRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn, _getSchemaTables$1, getSchemaTables_fn$1;
|
1111
1312
|
class Repository extends Query {
|
1112
1313
|
}
|
1113
1314
|
class RestRepository extends Query {
|
@@ -1124,18 +1325,21 @@ class RestRepository extends Query {
|
|
1124
1325
|
__privateAdd$4(this, _getCacheRecord);
|
1125
1326
|
__privateAdd$4(this, _setCacheQuery);
|
1126
1327
|
__privateAdd$4(this, _getCacheQuery);
|
1328
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1127
1329
|
__privateAdd$4(this, _table, void 0);
|
1128
|
-
__privateAdd$4(this, _links, void 0);
|
1129
1330
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1130
1331
|
__privateAdd$4(this, _cache, void 0);
|
1131
|
-
|
1132
|
-
__privateSet$
|
1133
|
-
__privateSet$
|
1332
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1333
|
+
__privateSet$4(this, _table, options.table);
|
1334
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1134
1335
|
this.db = options.db;
|
1135
|
-
__privateSet$
|
1336
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1337
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1136
1338
|
}
|
1137
1339
|
async create(a, b) {
|
1138
1340
|
if (Array.isArray(a)) {
|
1341
|
+
if (a.length === 0)
|
1342
|
+
return [];
|
1139
1343
|
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1140
1344
|
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1141
1345
|
return records;
|
@@ -1161,26 +1365,38 @@ class RestRepository extends Query {
|
|
1161
1365
|
}
|
1162
1366
|
throw new Error("Invalid arguments for create method");
|
1163
1367
|
}
|
1164
|
-
async read(
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1368
|
+
async read(a) {
|
1369
|
+
if (Array.isArray(a)) {
|
1370
|
+
if (a.length === 0)
|
1371
|
+
return [];
|
1372
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1373
|
+
return this.getAll({ filter: { id: { $any: ids } } });
|
1374
|
+
}
|
1375
|
+
const id = isString(a) ? a : a.id;
|
1376
|
+
if (isString(id)) {
|
1377
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
|
1378
|
+
if (cacheRecord)
|
1379
|
+
return cacheRecord;
|
1380
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1381
|
+
try {
|
1382
|
+
const response = await getRecord({
|
1383
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1384
|
+
...fetchProps
|
1385
|
+
});
|
1386
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1387
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1388
|
+
} catch (e) {
|
1389
|
+
if (isObject(e) && e.status === 404) {
|
1390
|
+
return null;
|
1391
|
+
}
|
1392
|
+
throw e;
|
1178
1393
|
}
|
1179
|
-
throw e;
|
1180
1394
|
}
|
1181
1395
|
}
|
1182
1396
|
async update(a, b) {
|
1183
1397
|
if (Array.isArray(a)) {
|
1398
|
+
if (a.length === 0)
|
1399
|
+
return [];
|
1184
1400
|
if (a.length > 100) {
|
1185
1401
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1186
1402
|
}
|
@@ -1202,6 +1418,8 @@ class RestRepository extends Query {
|
|
1202
1418
|
}
|
1203
1419
|
async createOrUpdate(a, b) {
|
1204
1420
|
if (Array.isArray(a)) {
|
1421
|
+
if (a.length === 0)
|
1422
|
+
return [];
|
1205
1423
|
if (a.length > 100) {
|
1206
1424
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1207
1425
|
}
|
@@ -1223,6 +1441,8 @@ class RestRepository extends Query {
|
|
1223
1441
|
}
|
1224
1442
|
async delete(a) {
|
1225
1443
|
if (Array.isArray(a)) {
|
1444
|
+
if (a.length === 0)
|
1445
|
+
return;
|
1226
1446
|
if (a.length > 100) {
|
1227
1447
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1228
1448
|
}
|
@@ -1242,13 +1462,19 @@ class RestRepository extends Query {
|
|
1242
1462
|
throw new Error("Invalid arguments for delete method");
|
1243
1463
|
}
|
1244
1464
|
async search(query, options = {}) {
|
1245
|
-
const fetchProps = await __privateGet$
|
1246
|
-
const { records } = await
|
1247
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1248
|
-
body: {
|
1465
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1466
|
+
const { records } = await searchTable({
|
1467
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1468
|
+
body: {
|
1469
|
+
query,
|
1470
|
+
fuzziness: options.fuzziness,
|
1471
|
+
highlight: options.highlight,
|
1472
|
+
filter: options.filter
|
1473
|
+
},
|
1249
1474
|
...fetchProps
|
1250
1475
|
});
|
1251
|
-
|
1476
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1477
|
+
return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
|
1252
1478
|
}
|
1253
1479
|
async query(query) {
|
1254
1480
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1257,34 +1483,35 @@ class RestRepository extends Query {
|
|
1257
1483
|
const data = query.getQueryOptions();
|
1258
1484
|
const body = {
|
1259
1485
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1260
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1261
|
-
page: data.
|
1486
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1487
|
+
page: data.pagination,
|
1262
1488
|
columns: data.columns
|
1263
1489
|
};
|
1264
|
-
const fetchProps = await __privateGet$
|
1490
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1265
1491
|
const { meta, records: objects } = await queryTable({
|
1266
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1492
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1267
1493
|
body,
|
1268
1494
|
...fetchProps
|
1269
1495
|
});
|
1270
|
-
const
|
1496
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1497
|
+
const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
|
1271
1498
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1272
1499
|
return new Page(query, meta, records);
|
1273
1500
|
}
|
1274
1501
|
}
|
1275
1502
|
_table = new WeakMap();
|
1276
|
-
_links = new WeakMap();
|
1277
1503
|
_getFetchProps = new WeakMap();
|
1278
1504
|
_cache = new WeakMap();
|
1505
|
+
_schemaTables$2 = new WeakMap();
|
1279
1506
|
_insertRecordWithoutId = new WeakSet();
|
1280
1507
|
insertRecordWithoutId_fn = async function(object) {
|
1281
|
-
const fetchProps = await __privateGet$
|
1508
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1282
1509
|
const record = transformObjectLinks(object);
|
1283
1510
|
const response = await insertRecord({
|
1284
1511
|
pathParams: {
|
1285
1512
|
workspace: "{workspaceId}",
|
1286
1513
|
dbBranchName: "{dbBranch}",
|
1287
|
-
tableName: __privateGet$
|
1514
|
+
tableName: __privateGet$4(this, _table)
|
1288
1515
|
},
|
1289
1516
|
body: record,
|
1290
1517
|
...fetchProps
|
@@ -1297,13 +1524,13 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1297
1524
|
};
|
1298
1525
|
_insertRecordWithId = new WeakSet();
|
1299
1526
|
insertRecordWithId_fn = async function(recordId, object) {
|
1300
|
-
const fetchProps = await __privateGet$
|
1527
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1301
1528
|
const record = transformObjectLinks(object);
|
1302
1529
|
const response = await insertRecordWithID({
|
1303
1530
|
pathParams: {
|
1304
1531
|
workspace: "{workspaceId}",
|
1305
1532
|
dbBranchName: "{dbBranch}",
|
1306
|
-
tableName: __privateGet$
|
1533
|
+
tableName: __privateGet$4(this, _table),
|
1307
1534
|
recordId
|
1308
1535
|
},
|
1309
1536
|
body: record,
|
@@ -1318,25 +1545,29 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1318
1545
|
};
|
1319
1546
|
_bulkInsertTableRecords = new WeakSet();
|
1320
1547
|
bulkInsertTableRecords_fn = async function(objects) {
|
1321
|
-
const fetchProps = await __privateGet$
|
1548
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1322
1549
|
const records = objects.map((object) => transformObjectLinks(object));
|
1323
|
-
const
|
1324
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1550
|
+
const { recordIDs } = await bulkInsertTableRecords({
|
1551
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1325
1552
|
body: { records },
|
1326
1553
|
...fetchProps
|
1327
1554
|
});
|
1328
|
-
const finalObjects = await this.
|
1555
|
+
const finalObjects = await this.read(recordIDs);
|
1329
1556
|
if (finalObjects.length !== objects.length) {
|
1330
1557
|
throw new Error("The server failed to save some records");
|
1331
1558
|
}
|
1332
|
-
|
1559
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1560
|
+
acc[object.id] = object;
|
1561
|
+
return acc;
|
1562
|
+
}, {});
|
1563
|
+
return recordIDs.map((id) => dictionary[id]);
|
1333
1564
|
};
|
1334
1565
|
_updateRecordWithID = new WeakSet();
|
1335
1566
|
updateRecordWithID_fn = async function(recordId, object) {
|
1336
|
-
const fetchProps = await __privateGet$
|
1567
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1337
1568
|
const record = transformObjectLinks(object);
|
1338
1569
|
const response = await updateRecordWithID({
|
1339
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1570
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1340
1571
|
body: record,
|
1341
1572
|
...fetchProps
|
1342
1573
|
});
|
@@ -1347,9 +1578,9 @@ updateRecordWithID_fn = async function(recordId, object) {
|
|
1347
1578
|
};
|
1348
1579
|
_upsertRecordWithID = new WeakSet();
|
1349
1580
|
upsertRecordWithID_fn = async function(recordId, object) {
|
1350
|
-
const fetchProps = await __privateGet$
|
1581
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1351
1582
|
const response = await upsertRecordWithID({
|
1352
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1583
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1353
1584
|
body: object,
|
1354
1585
|
...fetchProps
|
1355
1586
|
});
|
@@ -1360,51 +1591,63 @@ upsertRecordWithID_fn = async function(recordId, object) {
|
|
1360
1591
|
};
|
1361
1592
|
_deleteRecord = new WeakSet();
|
1362
1593
|
deleteRecord_fn = async function(recordId) {
|
1363
|
-
const fetchProps = await __privateGet$
|
1594
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1364
1595
|
await deleteRecord({
|
1365
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$
|
1596
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1366
1597
|
...fetchProps
|
1367
1598
|
});
|
1368
1599
|
};
|
1369
1600
|
_invalidateCache = new WeakSet();
|
1370
1601
|
invalidateCache_fn = async function(recordId) {
|
1371
|
-
await __privateGet$
|
1372
|
-
const cacheItems = await __privateGet$
|
1602
|
+
await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1603
|
+
const cacheItems = await __privateGet$4(this, _cache).getAll();
|
1373
1604
|
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1374
1605
|
for (const [key, value] of queries) {
|
1375
1606
|
const ids = getIds(value);
|
1376
1607
|
if (ids.includes(recordId))
|
1377
|
-
await __privateGet$
|
1608
|
+
await __privateGet$4(this, _cache).delete(key);
|
1378
1609
|
}
|
1379
1610
|
};
|
1380
1611
|
_setCacheRecord = new WeakSet();
|
1381
1612
|
setCacheRecord_fn = async function(record) {
|
1382
|
-
if (!__privateGet$
|
1613
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1383
1614
|
return;
|
1384
|
-
await __privateGet$
|
1615
|
+
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1385
1616
|
};
|
1386
1617
|
_getCacheRecord = new WeakSet();
|
1387
1618
|
getCacheRecord_fn = async function(recordId) {
|
1388
|
-
if (!__privateGet$
|
1619
|
+
if (!__privateGet$4(this, _cache).cacheRecords)
|
1389
1620
|
return null;
|
1390
|
-
return __privateGet$
|
1621
|
+
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1391
1622
|
};
|
1392
1623
|
_setCacheQuery = new WeakSet();
|
1393
1624
|
setCacheQuery_fn = async function(query, meta, records) {
|
1394
|
-
await __privateGet$
|
1625
|
+
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
1395
1626
|
};
|
1396
1627
|
_getCacheQuery = new WeakSet();
|
1397
1628
|
getCacheQuery_fn = async function(query) {
|
1398
|
-
const key = `query_${__privateGet$
|
1399
|
-
const result = await __privateGet$
|
1629
|
+
const key = `query_${__privateGet$4(this, _table)}:${query.key()}`;
|
1630
|
+
const result = await __privateGet$4(this, _cache).get(key);
|
1400
1631
|
if (!result)
|
1401
1632
|
return null;
|
1402
|
-
const { cache: ttl = __privateGet$
|
1403
|
-
if (
|
1404
|
-
return
|
1633
|
+
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1634
|
+
if (ttl < 0)
|
1635
|
+
return null;
|
1405
1636
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1406
1637
|
return hasExpired ? null : result;
|
1407
1638
|
};
|
1639
|
+
_getSchemaTables$1 = new WeakSet();
|
1640
|
+
getSchemaTables_fn$1 = async function() {
|
1641
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1642
|
+
return __privateGet$4(this, _schemaTables$2);
|
1643
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1644
|
+
const { schema } = await getBranchDetails({
|
1645
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1646
|
+
...fetchProps
|
1647
|
+
});
|
1648
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1649
|
+
return schema.tables;
|
1650
|
+
};
|
1408
1651
|
const transformObjectLinks = (object) => {
|
1409
1652
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
1410
1653
|
if (key === "xata")
|
@@ -1412,15 +1655,34 @@ const transformObjectLinks = (object) => {
|
|
1412
1655
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1413
1656
|
}, {});
|
1414
1657
|
};
|
1415
|
-
const initObject = (db,
|
1658
|
+
const initObject = (db, schemaTables, table, object) => {
|
1416
1659
|
const result = {};
|
1417
|
-
|
1418
|
-
|
1419
|
-
|
1420
|
-
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1660
|
+
const { xata, ...rest } = object ?? {};
|
1661
|
+
Object.assign(result, rest);
|
1662
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1663
|
+
if (!columns)
|
1664
|
+
console.error(`Table ${table} not found in schema`);
|
1665
|
+
for (const column of columns ?? []) {
|
1666
|
+
const value = result[column.name];
|
1667
|
+
switch (column.type) {
|
1668
|
+
case "datetime": {
|
1669
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1670
|
+
if (date && isNaN(date.getTime())) {
|
1671
|
+
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1672
|
+
} else if (date) {
|
1673
|
+
result[column.name] = date;
|
1674
|
+
}
|
1675
|
+
break;
|
1676
|
+
}
|
1677
|
+
case "link": {
|
1678
|
+
const linkTable = column.link?.table;
|
1679
|
+
if (!linkTable) {
|
1680
|
+
console.error(`Failed to parse link for field ${column.name}`);
|
1681
|
+
} else if (isObject(value)) {
|
1682
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1683
|
+
}
|
1684
|
+
break;
|
1685
|
+
}
|
1424
1686
|
}
|
1425
1687
|
}
|
1426
1688
|
result.read = function() {
|
@@ -1432,7 +1694,10 @@ const initObject = (db, links, table, object) => {
|
|
1432
1694
|
result.delete = function() {
|
1433
1695
|
return db[table].delete(result["id"]);
|
1434
1696
|
};
|
1435
|
-
|
1697
|
+
result.getMetadata = function() {
|
1698
|
+
return xata;
|
1699
|
+
};
|
1700
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1436
1701
|
Object.defineProperty(result, prop, { enumerable: false });
|
1437
1702
|
}
|
1438
1703
|
Object.freeze(result);
|
@@ -1452,7 +1717,7 @@ var __accessCheck$3 = (obj, member, msg) => {
|
|
1452
1717
|
if (!member.has(obj))
|
1453
1718
|
throw TypeError("Cannot " + msg);
|
1454
1719
|
};
|
1455
|
-
var __privateGet$
|
1720
|
+
var __privateGet$3 = (obj, member, getter) => {
|
1456
1721
|
__accessCheck$3(obj, member, "read from private field");
|
1457
1722
|
return getter ? getter.call(obj) : member.get(obj);
|
1458
1723
|
};
|
@@ -1461,7 +1726,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1461
1726
|
throw TypeError("Cannot add the same private member more than once");
|
1462
1727
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1463
1728
|
};
|
1464
|
-
var __privateSet$
|
1729
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1465
1730
|
__accessCheck$3(obj, member, "write to private field");
|
1466
1731
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1467
1732
|
return value;
|
@@ -1470,30 +1735,30 @@ var _map;
|
|
1470
1735
|
class SimpleCache {
|
1471
1736
|
constructor(options = {}) {
|
1472
1737
|
__privateAdd$3(this, _map, void 0);
|
1473
|
-
__privateSet$
|
1738
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1474
1739
|
this.capacity = options.max ?? 500;
|
1475
1740
|
this.cacheRecords = options.cacheRecords ?? true;
|
1476
1741
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1477
1742
|
}
|
1478
1743
|
async getAll() {
|
1479
|
-
return Object.fromEntries(__privateGet$
|
1744
|
+
return Object.fromEntries(__privateGet$3(this, _map));
|
1480
1745
|
}
|
1481
1746
|
async get(key) {
|
1482
|
-
return __privateGet$
|
1747
|
+
return __privateGet$3(this, _map).get(key) ?? null;
|
1483
1748
|
}
|
1484
1749
|
async set(key, value) {
|
1485
1750
|
await this.delete(key);
|
1486
|
-
__privateGet$
|
1487
|
-
if (__privateGet$
|
1488
|
-
const leastRecentlyUsed = __privateGet$
|
1751
|
+
__privateGet$3(this, _map).set(key, value);
|
1752
|
+
if (__privateGet$3(this, _map).size > this.capacity) {
|
1753
|
+
const leastRecentlyUsed = __privateGet$3(this, _map).keys().next().value;
|
1489
1754
|
await this.delete(leastRecentlyUsed);
|
1490
1755
|
}
|
1491
1756
|
}
|
1492
1757
|
async delete(key) {
|
1493
|
-
__privateGet$
|
1758
|
+
__privateGet$3(this, _map).delete(key);
|
1494
1759
|
}
|
1495
1760
|
async clear() {
|
1496
|
-
return __privateGet$
|
1761
|
+
return __privateGet$3(this, _map).clear();
|
1497
1762
|
}
|
1498
1763
|
}
|
1499
1764
|
_map = new WeakMap();
|
@@ -1521,7 +1786,7 @@ var __accessCheck$2 = (obj, member, msg) => {
|
|
1521
1786
|
if (!member.has(obj))
|
1522
1787
|
throw TypeError("Cannot " + msg);
|
1523
1788
|
};
|
1524
|
-
var __privateGet$
|
1789
|
+
var __privateGet$2 = (obj, member, getter) => {
|
1525
1790
|
__accessCheck$2(obj, member, "read from private field");
|
1526
1791
|
return getter ? getter.call(obj) : member.get(obj);
|
1527
1792
|
};
|
@@ -1530,122 +1795,158 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1530
1795
|
throw TypeError("Cannot add the same private member more than once");
|
1531
1796
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1532
1797
|
};
|
1533
|
-
var
|
1798
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1799
|
+
__accessCheck$2(obj, member, "write to private field");
|
1800
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1801
|
+
return value;
|
1802
|
+
};
|
1803
|
+
var _tables, _schemaTables$1;
|
1534
1804
|
class SchemaPlugin extends XataPlugin {
|
1535
|
-
constructor(
|
1805
|
+
constructor(schemaTables) {
|
1536
1806
|
super();
|
1537
|
-
this.links = links;
|
1538
|
-
this.tableNames = tableNames;
|
1539
1807
|
__privateAdd$2(this, _tables, {});
|
1808
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1809
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1540
1810
|
}
|
1541
1811
|
build(pluginOptions) {
|
1542
|
-
const links = this.links;
|
1543
1812
|
const db = new Proxy({}, {
|
1544
1813
|
get: (_target, table) => {
|
1545
1814
|
if (!isString(table))
|
1546
1815
|
throw new Error("Invalid table name");
|
1547
|
-
if (
|
1548
|
-
__privateGet$
|
1816
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1817
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1549
1818
|
}
|
1550
|
-
return __privateGet$
|
1819
|
+
return __privateGet$2(this, _tables)[table];
|
1551
1820
|
}
|
1552
1821
|
});
|
1553
|
-
|
1554
|
-
|
1822
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1823
|
+
for (const table of tableNames) {
|
1824
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1555
1825
|
}
|
1556
1826
|
return db;
|
1557
1827
|
}
|
1558
1828
|
}
|
1559
1829
|
_tables = new WeakMap();
|
1830
|
+
_schemaTables$1 = new WeakMap();
|
1560
1831
|
|
1561
1832
|
var __accessCheck$1 = (obj, member, msg) => {
|
1562
1833
|
if (!member.has(obj))
|
1563
1834
|
throw TypeError("Cannot " + msg);
|
1564
1835
|
};
|
1836
|
+
var __privateGet$1 = (obj, member, getter) => {
|
1837
|
+
__accessCheck$1(obj, member, "read from private field");
|
1838
|
+
return getter ? getter.call(obj) : member.get(obj);
|
1839
|
+
};
|
1565
1840
|
var __privateAdd$1 = (obj, member, value) => {
|
1566
1841
|
if (member.has(obj))
|
1567
1842
|
throw TypeError("Cannot add the same private member more than once");
|
1568
1843
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1569
1844
|
};
|
1845
|
+
var __privateSet$1 = (obj, member, value, setter) => {
|
1846
|
+
__accessCheck$1(obj, member, "write to private field");
|
1847
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1848
|
+
return value;
|
1849
|
+
};
|
1570
1850
|
var __privateMethod$1 = (obj, member, method) => {
|
1571
1851
|
__accessCheck$1(obj, member, "access private method");
|
1572
1852
|
return method;
|
1573
1853
|
};
|
1574
|
-
var _search, search_fn;
|
1854
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1575
1855
|
class SearchPlugin extends XataPlugin {
|
1576
|
-
constructor(db,
|
1856
|
+
constructor(db, schemaTables) {
|
1577
1857
|
super();
|
1578
1858
|
this.db = db;
|
1579
|
-
this.links = links;
|
1580
1859
|
__privateAdd$1(this, _search);
|
1860
|
+
__privateAdd$1(this, _getSchemaTables);
|
1861
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1862
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1581
1863
|
}
|
1582
1864
|
build({ getFetchProps }) {
|
1583
1865
|
return {
|
1584
1866
|
all: async (query, options = {}) => {
|
1585
1867
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1868
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1586
1869
|
return records.map((record) => {
|
1587
1870
|
const { table = "orphan" } = record.xata;
|
1588
|
-
return { table, record: initObject(this.db,
|
1871
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1589
1872
|
});
|
1590
1873
|
},
|
1591
1874
|
byTable: async (query, options = {}) => {
|
1592
1875
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1876
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1593
1877
|
return records.reduce((acc, record) => {
|
1594
1878
|
const { table = "orphan" } = record.xata;
|
1595
1879
|
const items = acc[table] ?? [];
|
1596
|
-
const item = initObject(this.db,
|
1880
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1597
1881
|
return { ...acc, [table]: [...items, item] };
|
1598
1882
|
}, {});
|
1599
1883
|
}
|
1600
1884
|
};
|
1601
1885
|
}
|
1602
1886
|
}
|
1887
|
+
_schemaTables = new WeakMap();
|
1603
1888
|
_search = new WeakSet();
|
1604
1889
|
search_fn = async function(query, options, getFetchProps) {
|
1605
1890
|
const fetchProps = await getFetchProps();
|
1606
|
-
const { tables, fuzziness } = options ?? {};
|
1891
|
+
const { tables, fuzziness, highlight } = options ?? {};
|
1607
1892
|
const { records } = await searchBranch({
|
1608
1893
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1609
|
-
body: { tables, query, fuzziness },
|
1894
|
+
body: { tables, query, fuzziness, highlight },
|
1610
1895
|
...fetchProps
|
1611
1896
|
});
|
1612
1897
|
return records;
|
1613
1898
|
};
|
1899
|
+
_getSchemaTables = new WeakSet();
|
1900
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1901
|
+
if (__privateGet$1(this, _schemaTables))
|
1902
|
+
return __privateGet$1(this, _schemaTables);
|
1903
|
+
const fetchProps = await getFetchProps();
|
1904
|
+
const { schema } = await getBranchDetails({
|
1905
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1906
|
+
...fetchProps
|
1907
|
+
});
|
1908
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1909
|
+
return schema.tables;
|
1910
|
+
};
|
1614
1911
|
|
1615
1912
|
const isBranchStrategyBuilder = (strategy) => {
|
1616
1913
|
return typeof strategy === "function";
|
1617
1914
|
};
|
1618
1915
|
|
1619
|
-
const envBranchNames = [
|
1620
|
-
"XATA_BRANCH",
|
1621
|
-
"VERCEL_GIT_COMMIT_REF",
|
1622
|
-
"CF_PAGES_BRANCH",
|
1623
|
-
"BRANCH"
|
1624
|
-
];
|
1625
|
-
const defaultBranch = "main";
|
1626
1916
|
async function getCurrentBranchName(options) {
|
1627
|
-
const
|
1628
|
-
if (
|
1629
|
-
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
return defaultBranch;
|
1917
|
+
const { branch, envBranch } = getEnvironment();
|
1918
|
+
if (branch) {
|
1919
|
+
const details = await getDatabaseBranch(branch, options);
|
1920
|
+
if (details)
|
1921
|
+
return branch;
|
1922
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1923
|
+
}
|
1924
|
+
const gitBranch = envBranch || await getGitBranch();
|
1925
|
+
return resolveXataBranch(gitBranch, options);
|
1637
1926
|
}
|
1638
1927
|
async function getCurrentBranchDetails(options) {
|
1639
|
-
const
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
1645
|
-
|
1646
|
-
|
1647
|
-
|
1648
|
-
|
1928
|
+
const branch = await getCurrentBranchName(options);
|
1929
|
+
return getDatabaseBranch(branch, options);
|
1930
|
+
}
|
1931
|
+
async function resolveXataBranch(gitBranch, options) {
|
1932
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1933
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1934
|
+
if (!databaseURL)
|
1935
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1936
|
+
if (!apiKey)
|
1937
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1938
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1939
|
+
const [workspace] = host.split(".");
|
1940
|
+
const { fallbackBranch } = getEnvironment();
|
1941
|
+
const { branch } = await resolveBranch({
|
1942
|
+
apiKey,
|
1943
|
+
apiUrl: databaseURL,
|
1944
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1945
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1946
|
+
pathParams: { dbName, workspace },
|
1947
|
+
queryParams: { gitBranch, fallbackBranch }
|
1948
|
+
});
|
1949
|
+
return branch;
|
1649
1950
|
}
|
1650
1951
|
async function getDatabaseBranch(branch, options) {
|
1651
1952
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1663,10 +1964,7 @@ async function getDatabaseBranch(branch, options) {
|
|
1663
1964
|
apiUrl: databaseURL,
|
1664
1965
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1665
1966
|
workspacesApiUrl: `${protocol}//${host}`,
|
1666
|
-
pathParams: {
|
1667
|
-
dbBranchName,
|
1668
|
-
workspace
|
1669
|
-
}
|
1967
|
+
pathParams: { dbBranchName, workspace }
|
1670
1968
|
});
|
1671
1969
|
} catch (err) {
|
1672
1970
|
if (isObject(err) && err.status === 404)
|
@@ -1674,21 +1972,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1674
1972
|
throw err;
|
1675
1973
|
}
|
1676
1974
|
}
|
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
1975
|
function getDatabaseURL() {
|
1690
1976
|
try {
|
1691
|
-
|
1977
|
+
const { databaseURL } = getEnvironment();
|
1978
|
+
return databaseURL;
|
1692
1979
|
} catch (err) {
|
1693
1980
|
return void 0;
|
1694
1981
|
}
|
@@ -1719,7 +2006,7 @@ var __privateMethod = (obj, member, method) => {
|
|
1719
2006
|
const buildClient = (plugins) => {
|
1720
2007
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1721
2008
|
return _a = class {
|
1722
|
-
constructor(options = {},
|
2009
|
+
constructor(options = {}, schemaTables) {
|
1723
2010
|
__privateAdd(this, _parseOptions);
|
1724
2011
|
__privateAdd(this, _getFetchProps);
|
1725
2012
|
__privateAdd(this, _evaluateBranch);
|
@@ -1729,12 +2016,12 @@ const buildClient = (plugins) => {
|
|
1729
2016
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1730
2017
|
cache: safeOptions.cache
|
1731
2018
|
};
|
1732
|
-
const db = new SchemaPlugin(
|
1733
|
-
const search = new SearchPlugin(db,
|
2019
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2020
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1734
2021
|
this.db = db;
|
1735
2022
|
this.search = search;
|
1736
2023
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1737
|
-
if (
|
2024
|
+
if (namespace === void 0)
|
1738
2025
|
continue;
|
1739
2026
|
const result = namespace.build(pluginOptions);
|
1740
2027
|
if (result instanceof Promise) {
|
@@ -1751,7 +2038,7 @@ const buildClient = (plugins) => {
|
|
1751
2038
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1752
2039
|
const apiKey = options?.apiKey || getAPIKey();
|
1753
2040
|
const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
|
1754
|
-
const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
2041
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1755
2042
|
if (!databaseURL || !apiKey) {
|
1756
2043
|
throw new Error("Options databaseURL and apiKey are required");
|
1757
2044
|
}
|
@@ -1778,7 +2065,7 @@ const buildClient = (plugins) => {
|
|
1778
2065
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1779
2066
|
if (__privateGet(this, _branch))
|
1780
2067
|
return __privateGet(this, _branch);
|
1781
|
-
if (
|
2068
|
+
if (param === void 0)
|
1782
2069
|
return void 0;
|
1783
2070
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1784
2071
|
const evaluateBranch = async (strategy) => {
|
@@ -1796,6 +2083,88 @@ const buildClient = (plugins) => {
|
|
1796
2083
|
class BaseClient extends buildClient() {
|
1797
2084
|
}
|
1798
2085
|
|
2086
|
+
const META = "__";
|
2087
|
+
const VALUE = "___";
|
2088
|
+
class Serializer {
|
2089
|
+
constructor() {
|
2090
|
+
this.classes = {};
|
2091
|
+
}
|
2092
|
+
add(clazz) {
|
2093
|
+
this.classes[clazz.name] = clazz;
|
2094
|
+
}
|
2095
|
+
toJSON(data) {
|
2096
|
+
function visit(obj) {
|
2097
|
+
if (Array.isArray(obj))
|
2098
|
+
return obj.map(visit);
|
2099
|
+
const type = typeof obj;
|
2100
|
+
if (type === "undefined")
|
2101
|
+
return { [META]: "undefined" };
|
2102
|
+
if (type === "bigint")
|
2103
|
+
return { [META]: "bigint", [VALUE]: obj.toString() };
|
2104
|
+
if (obj === null || type !== "object")
|
2105
|
+
return obj;
|
2106
|
+
const constructor = obj.constructor;
|
2107
|
+
const o = { [META]: constructor.name };
|
2108
|
+
for (const [key, value] of Object.entries(obj)) {
|
2109
|
+
o[key] = visit(value);
|
2110
|
+
}
|
2111
|
+
if (constructor === Date)
|
2112
|
+
o[VALUE] = obj.toISOString();
|
2113
|
+
if (constructor === Map)
|
2114
|
+
o[VALUE] = Object.fromEntries(obj);
|
2115
|
+
if (constructor === Set)
|
2116
|
+
o[VALUE] = [...obj];
|
2117
|
+
return o;
|
2118
|
+
}
|
2119
|
+
return JSON.stringify(visit(data));
|
2120
|
+
}
|
2121
|
+
fromJSON(json) {
|
2122
|
+
return JSON.parse(json, (key, value) => {
|
2123
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
2124
|
+
const { [META]: clazz, [VALUE]: val, ...rest } = value;
|
2125
|
+
const constructor = this.classes[clazz];
|
2126
|
+
if (constructor) {
|
2127
|
+
return Object.assign(Object.create(constructor.prototype), rest);
|
2128
|
+
}
|
2129
|
+
if (clazz === "Date")
|
2130
|
+
return new Date(val);
|
2131
|
+
if (clazz === "Set")
|
2132
|
+
return new Set(val);
|
2133
|
+
if (clazz === "Map")
|
2134
|
+
return new Map(Object.entries(val));
|
2135
|
+
if (clazz === "bigint")
|
2136
|
+
return BigInt(val);
|
2137
|
+
if (clazz === "undefined")
|
2138
|
+
return void 0;
|
2139
|
+
return rest;
|
2140
|
+
}
|
2141
|
+
return value;
|
2142
|
+
});
|
2143
|
+
}
|
2144
|
+
}
|
2145
|
+
const serialize = () => {
|
2146
|
+
throw new Error("Not implemented");
|
2147
|
+
};
|
2148
|
+
const deserialize = () => {
|
2149
|
+
throw new Error("Not implemented");
|
2150
|
+
};
|
2151
|
+
|
2152
|
+
function buildWorkerRunner(config) {
|
2153
|
+
return function xataWorker(name, _worker) {
|
2154
|
+
return async (...args) => {
|
2155
|
+
const result = await fetch("http://localhost:64749", {
|
2156
|
+
method: "POST",
|
2157
|
+
headers: { "Content-Type": "application/json" },
|
2158
|
+
body: JSON.stringify({
|
2159
|
+
name,
|
2160
|
+
payload: args
|
2161
|
+
})
|
2162
|
+
});
|
2163
|
+
return result.json();
|
2164
|
+
};
|
2165
|
+
};
|
2166
|
+
}
|
2167
|
+
|
1799
2168
|
class XataError extends Error {
|
1800
2169
|
constructor(message, status) {
|
1801
2170
|
super(message);
|
@@ -1803,5 +2172,5 @@ class XataError extends Error {
|
|
1803
2172
|
}
|
1804
2173
|
}
|
1805
2174
|
|
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 };
|
2175
|
+
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, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, 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
2176
|
//# sourceMappingURL=index.mjs.map
|