@xata.io/client 0.0.0-alpha.vfb4479d → 0.0.0-alpha.vfbac5b5
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 +60 -0
- package/README.md +271 -1
- package/Usage.md +395 -0
- package/dist/index.cjs +256 -125
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +167 -55
- package/dist/index.mjs +238 -126
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -3
package/dist/index.mjs
CHANGED
@@ -17,39 +17,86 @@ function toBase64(value) {
|
|
17
17
|
try {
|
18
18
|
return btoa(value);
|
19
19
|
} catch (err) {
|
20
|
-
|
20
|
+
const buf = Buffer;
|
21
|
+
return buf.from(value).toString("base64");
|
21
22
|
}
|
22
23
|
}
|
23
24
|
|
24
|
-
function
|
25
|
+
function getEnvironment() {
|
25
26
|
try {
|
26
|
-
if (isObject(process) &&
|
27
|
-
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
|
+
};
|
28
35
|
}
|
29
36
|
} catch (err) {
|
30
37
|
}
|
31
38
|
try {
|
32
|
-
if (isObject(Deno) &&
|
33
|
-
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
|
+
};
|
34
47
|
}
|
35
48
|
} catch (err) {
|
36
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
|
+
}
|
37
85
|
}
|
38
86
|
async function getGitBranch() {
|
87
|
+
const cmd = ["git", "branch", "--show-current"];
|
88
|
+
const nodeModule = ["child", "process"].join("_");
|
39
89
|
try {
|
40
90
|
if (typeof require === "function") {
|
41
|
-
|
42
|
-
return req("child_process").execSync("git branch --show-current", { encoding: "utf-8" }).trim();
|
91
|
+
return require(nodeModule).execSync(cmd.join(" "), { encoding: "utf-8" }).trim();
|
43
92
|
}
|
93
|
+
const { execSync } = await import(nodeModule);
|
94
|
+
return execSync(cmd.join(" "), { encoding: "utf-8" }).toString().trim();
|
44
95
|
} catch (err) {
|
45
96
|
}
|
46
97
|
try {
|
47
98
|
if (isObject(Deno)) {
|
48
|
-
const process2 = Deno.run({
|
49
|
-
cmd: ["git", "branch", "--show-current"],
|
50
|
-
stdout: "piped",
|
51
|
-
stderr: "piped"
|
52
|
-
});
|
99
|
+
const process2 = Deno.run({ cmd, stdout: "piped", stderr: "piped" });
|
53
100
|
return new TextDecoder().decode(await process2.output()).trim();
|
54
101
|
}
|
55
102
|
} catch (err) {
|
@@ -58,7 +105,8 @@ async function getGitBranch() {
|
|
58
105
|
|
59
106
|
function getAPIKey() {
|
60
107
|
try {
|
61
|
-
|
108
|
+
const { apiKey } = getEnvironment();
|
109
|
+
return apiKey;
|
62
110
|
} catch (err) {
|
63
111
|
return void 0;
|
64
112
|
}
|
@@ -73,21 +121,28 @@ function getFetchImplementation(userFetch) {
|
|
73
121
|
return fetchImpl;
|
74
122
|
}
|
75
123
|
|
124
|
+
const VERSION = "0.0.0-alpha.vfbac5b5";
|
125
|
+
|
76
126
|
class ErrorWithCause extends Error {
|
77
127
|
constructor(message, options) {
|
78
128
|
super(message, options);
|
79
129
|
}
|
80
130
|
}
|
81
131
|
class FetcherError extends ErrorWithCause {
|
82
|
-
constructor(status, data) {
|
132
|
+
constructor(status, data, requestId) {
|
83
133
|
super(getMessage(data));
|
84
134
|
this.status = status;
|
85
135
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
136
|
+
this.requestId = requestId;
|
86
137
|
if (data instanceof Error) {
|
87
138
|
this.stack = data.stack;
|
88
139
|
this.cause = data.cause;
|
89
140
|
}
|
90
141
|
}
|
142
|
+
toString() {
|
143
|
+
const error = super.toString();
|
144
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
145
|
+
}
|
91
146
|
}
|
92
147
|
function isBulkError(error) {
|
93
148
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -110,7 +165,12 @@ function getMessage(data) {
|
|
110
165
|
}
|
111
166
|
|
112
167
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
113
|
-
const
|
168
|
+
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
169
|
+
if (value === void 0 || value === null)
|
170
|
+
return acc;
|
171
|
+
return { ...acc, [key]: value };
|
172
|
+
}, {});
|
173
|
+
const query = new URLSearchParams(cleanQueryParams).toString();
|
114
174
|
const queryString = query.length > 0 ? `?${query}` : "";
|
115
175
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
116
176
|
};
|
@@ -150,6 +210,7 @@ async function fetch$1({
|
|
150
210
|
body: body ? JSON.stringify(body) : void 0,
|
151
211
|
headers: {
|
152
212
|
"Content-Type": "application/json",
|
213
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
153
214
|
...headers,
|
154
215
|
...hostHeader(fullUrl),
|
155
216
|
Authorization: `Bearer ${apiKey}`
|
@@ -158,14 +219,15 @@ async function fetch$1({
|
|
158
219
|
if (response.status === 204) {
|
159
220
|
return {};
|
160
221
|
}
|
222
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
161
223
|
try {
|
162
224
|
const jsonResponse = await response.json();
|
163
225
|
if (response.ok) {
|
164
226
|
return jsonResponse;
|
165
227
|
}
|
166
|
-
throw new FetcherError(response.status, jsonResponse);
|
228
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
167
229
|
} catch (error) {
|
168
|
-
throw new FetcherError(response.status, error);
|
230
|
+
throw new FetcherError(response.status, error, requestId);
|
169
231
|
}
|
170
232
|
}
|
171
233
|
|
@@ -485,7 +547,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
485
547
|
throw TypeError("Cannot add the same private member more than once");
|
486
548
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
487
549
|
};
|
488
|
-
var __privateSet$
|
550
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
489
551
|
__accessCheck$7(obj, member, "write to private field");
|
490
552
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
491
553
|
return value;
|
@@ -500,7 +562,7 @@ class XataApiClient {
|
|
500
562
|
if (!apiKey) {
|
501
563
|
throw new Error("Could not resolve a valid apiKey");
|
502
564
|
}
|
503
|
-
__privateSet$
|
565
|
+
__privateSet$7(this, _extraProps, {
|
504
566
|
apiUrl: getHostUrl(provider, "main"),
|
505
567
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
506
568
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -689,10 +751,10 @@ class DatabaseApi {
|
|
689
751
|
...this.extraProps
|
690
752
|
});
|
691
753
|
}
|
692
|
-
resolveBranch(workspace, dbName, gitBranch) {
|
754
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
693
755
|
return operationsByTag.database.resolveBranch({
|
694
756
|
pathParams: { workspace, dbName },
|
695
|
-
queryParams: { gitBranch },
|
757
|
+
queryParams: { gitBranch, fallbackBranch },
|
696
758
|
...this.extraProps
|
697
759
|
});
|
698
760
|
}
|
@@ -937,18 +999,18 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
937
999
|
throw TypeError("Cannot add the same private member more than once");
|
938
1000
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
939
1001
|
};
|
940
|
-
var __privateSet$
|
1002
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
941
1003
|
__accessCheck$6(obj, member, "write to private field");
|
942
1004
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
943
1005
|
return value;
|
944
1006
|
};
|
945
|
-
var _query;
|
1007
|
+
var _query, _page;
|
946
1008
|
class Page {
|
947
1009
|
constructor(query, meta, records = []) {
|
948
1010
|
__privateAdd$6(this, _query, void 0);
|
949
|
-
__privateSet$
|
1011
|
+
__privateSet$6(this, _query, query);
|
950
1012
|
this.meta = meta;
|
951
|
-
this.records = records;
|
1013
|
+
this.records = new RecordArray(this, records);
|
952
1014
|
}
|
953
1015
|
async nextPage(size, offset) {
|
954
1016
|
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
@@ -968,12 +1030,56 @@ class Page {
|
|
968
1030
|
}
|
969
1031
|
_query = new WeakMap();
|
970
1032
|
const PAGINATION_MAX_SIZE = 200;
|
971
|
-
const PAGINATION_DEFAULT_SIZE =
|
1033
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
972
1034
|
const PAGINATION_MAX_OFFSET = 800;
|
973
1035
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
974
1036
|
function isCursorPaginationOptions(options) {
|
975
1037
|
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
976
1038
|
}
|
1039
|
+
const _RecordArray = class extends Array {
|
1040
|
+
constructor(...args) {
|
1041
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
1042
|
+
__privateAdd$6(this, _page, void 0);
|
1043
|
+
__privateSet$6(this, _page, isObject(args[0]?.meta) ? args[0] : { meta: { page: { cursor: "", more: false } }, records: [] });
|
1044
|
+
}
|
1045
|
+
static parseConstructorParams(...args) {
|
1046
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1047
|
+
return new Array(args[0]);
|
1048
|
+
}
|
1049
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1050
|
+
const result = args[1] ?? args[0].records ?? [];
|
1051
|
+
return new Array(...result);
|
1052
|
+
}
|
1053
|
+
return new Array(...args);
|
1054
|
+
}
|
1055
|
+
toArray() {
|
1056
|
+
return new Array(...this);
|
1057
|
+
}
|
1058
|
+
map(callbackfn, thisArg) {
|
1059
|
+
return this.toArray().map(callbackfn, thisArg);
|
1060
|
+
}
|
1061
|
+
async nextPage(size, offset) {
|
1062
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1063
|
+
return new _RecordArray(newPage);
|
1064
|
+
}
|
1065
|
+
async previousPage(size, offset) {
|
1066
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1067
|
+
return new _RecordArray(newPage);
|
1068
|
+
}
|
1069
|
+
async firstPage(size, offset) {
|
1070
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1071
|
+
return new _RecordArray(newPage);
|
1072
|
+
}
|
1073
|
+
async lastPage(size, offset) {
|
1074
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1075
|
+
return new _RecordArray(newPage);
|
1076
|
+
}
|
1077
|
+
hasNextPage() {
|
1078
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1079
|
+
}
|
1080
|
+
};
|
1081
|
+
let RecordArray = _RecordArray;
|
1082
|
+
_page = new WeakMap();
|
977
1083
|
|
978
1084
|
var __accessCheck$5 = (obj, member, msg) => {
|
979
1085
|
if (!member.has(obj))
|
@@ -988,7 +1094,7 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
988
1094
|
throw TypeError("Cannot add the same private member more than once");
|
989
1095
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
990
1096
|
};
|
991
|
-
var __privateSet$
|
1097
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
992
1098
|
__accessCheck$5(obj, member, "write to private field");
|
993
1099
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
994
1100
|
return value;
|
@@ -1000,12 +1106,12 @@ const _Query = class {
|
|
1000
1106
|
__privateAdd$5(this, _repository, void 0);
|
1001
1107
|
__privateAdd$5(this, _data, { filter: {} });
|
1002
1108
|
this.meta = { page: { cursor: "start", more: true } };
|
1003
|
-
this.records = [];
|
1004
|
-
__privateSet$
|
1109
|
+
this.records = new RecordArray(this, []);
|
1110
|
+
__privateSet$5(this, _table$1, table);
|
1005
1111
|
if (repository) {
|
1006
|
-
__privateSet$
|
1112
|
+
__privateSet$5(this, _repository, repository);
|
1007
1113
|
} else {
|
1008
|
-
__privateSet$
|
1114
|
+
__privateSet$5(this, _repository, this);
|
1009
1115
|
}
|
1010
1116
|
const parent = cleanParent(data, rawParent);
|
1011
1117
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
@@ -1089,8 +1195,11 @@ const _Query = class {
|
|
1089
1195
|
}
|
1090
1196
|
}
|
1091
1197
|
async getMany(options = {}) {
|
1092
|
-
const
|
1093
|
-
|
1198
|
+
const page = await this.getPaginated(options);
|
1199
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1200
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1201
|
+
}
|
1202
|
+
return page.records;
|
1094
1203
|
}
|
1095
1204
|
async getAll(options = {}) {
|
1096
1205
|
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
@@ -1138,7 +1247,9 @@ function isIdentifiable(x) {
|
|
1138
1247
|
return isObject(x) && isString(x?.id);
|
1139
1248
|
}
|
1140
1249
|
function isXataRecord(x) {
|
1141
|
-
|
1250
|
+
const record = x;
|
1251
|
+
const metadata = record?.getMetadata();
|
1252
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1142
1253
|
}
|
1143
1254
|
|
1144
1255
|
function isSortFilterString(value) {
|
@@ -1177,7 +1288,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1177
1288
|
throw TypeError("Cannot add the same private member more than once");
|
1178
1289
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1179
1290
|
};
|
1180
|
-
var __privateSet$
|
1291
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1181
1292
|
__accessCheck$4(obj, member, "write to private field");
|
1182
1293
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1183
1294
|
return value;
|
@@ -1186,7 +1297,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1186
1297
|
__accessCheck$4(obj, member, "access private method");
|
1187
1298
|
return method;
|
1188
1299
|
};
|
1189
|
-
var _table, _getFetchProps, _cache,
|
1300
|
+
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;
|
1190
1301
|
class Repository extends Query {
|
1191
1302
|
}
|
1192
1303
|
class RestRepository extends Query {
|
@@ -1203,15 +1314,16 @@ class RestRepository extends Query {
|
|
1203
1314
|
__privateAdd$4(this, _getCacheRecord);
|
1204
1315
|
__privateAdd$4(this, _setCacheQuery);
|
1205
1316
|
__privateAdd$4(this, _getCacheQuery);
|
1206
|
-
__privateAdd$4(this,
|
1317
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1207
1318
|
__privateAdd$4(this, _table, void 0);
|
1208
1319
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1209
1320
|
__privateAdd$4(this, _cache, void 0);
|
1210
|
-
__privateAdd$4(this,
|
1211
|
-
__privateSet$
|
1212
|
-
__privateSet$
|
1321
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1322
|
+
__privateSet$4(this, _table, options.table);
|
1323
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1213
1324
|
this.db = options.db;
|
1214
|
-
__privateSet$
|
1325
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1326
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1215
1327
|
}
|
1216
1328
|
async create(a, b) {
|
1217
1329
|
if (Array.isArray(a)) {
|
@@ -1246,20 +1358,22 @@ class RestRepository extends Query {
|
|
1246
1358
|
if (Array.isArray(a)) {
|
1247
1359
|
if (a.length === 0)
|
1248
1360
|
return [];
|
1249
|
-
|
1361
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1362
|
+
return this.getAll({ filter: { id: { $any: ids } } });
|
1250
1363
|
}
|
1251
|
-
|
1252
|
-
|
1364
|
+
const id = isString(a) ? a : a.id;
|
1365
|
+
if (isString(id)) {
|
1366
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
|
1253
1367
|
if (cacheRecord)
|
1254
1368
|
return cacheRecord;
|
1255
1369
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1256
1370
|
try {
|
1257
1371
|
const response = await getRecord({
|
1258
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId:
|
1372
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1259
1373
|
...fetchProps
|
1260
1374
|
});
|
1261
|
-
const
|
1262
|
-
return initObject(this.db,
|
1375
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1376
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1263
1377
|
} catch (e) {
|
1264
1378
|
if (isObject(e) && e.status === 404) {
|
1265
1379
|
return null;
|
@@ -1348,8 +1462,8 @@ class RestRepository extends Query {
|
|
1348
1462
|
},
|
1349
1463
|
...fetchProps
|
1350
1464
|
});
|
1351
|
-
const
|
1352
|
-
return records.map((item) => initObject(this.db,
|
1465
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1466
|
+
return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
|
1353
1467
|
}
|
1354
1468
|
async query(query) {
|
1355
1469
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
@@ -1368,8 +1482,8 @@ class RestRepository extends Query {
|
|
1368
1482
|
body,
|
1369
1483
|
...fetchProps
|
1370
1484
|
});
|
1371
|
-
const
|
1372
|
-
const records = objects.map((record) => initObject(this.db,
|
1485
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1486
|
+
const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
|
1373
1487
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1374
1488
|
return new Page(query, meta, records);
|
1375
1489
|
}
|
@@ -1377,7 +1491,7 @@ class RestRepository extends Query {
|
|
1377
1491
|
_table = new WeakMap();
|
1378
1492
|
_getFetchProps = new WeakMap();
|
1379
1493
|
_cache = new WeakMap();
|
1380
|
-
|
1494
|
+
_schemaTables$2 = new WeakMap();
|
1381
1495
|
_insertRecordWithoutId = new WeakSet();
|
1382
1496
|
insertRecordWithoutId_fn = async function(object) {
|
1383
1497
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
@@ -1422,16 +1536,20 @@ _bulkInsertTableRecords = new WeakSet();
|
|
1422
1536
|
bulkInsertTableRecords_fn = async function(objects) {
|
1423
1537
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1424
1538
|
const records = objects.map((object) => transformObjectLinks(object));
|
1425
|
-
const
|
1539
|
+
const { recordIDs } = await bulkInsertTableRecords({
|
1426
1540
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1427
1541
|
body: { records },
|
1428
1542
|
...fetchProps
|
1429
1543
|
});
|
1430
|
-
const finalObjects = await this.
|
1544
|
+
const finalObjects = await this.read(recordIDs);
|
1431
1545
|
if (finalObjects.length !== objects.length) {
|
1432
1546
|
throw new Error("The server failed to save some records");
|
1433
1547
|
}
|
1434
|
-
|
1548
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1549
|
+
acc[object.id] = object;
|
1550
|
+
return acc;
|
1551
|
+
}, {});
|
1552
|
+
return recordIDs.map((id) => dictionary[id]);
|
1435
1553
|
};
|
1436
1554
|
_updateRecordWithID = new WeakSet();
|
1437
1555
|
updateRecordWithID_fn = async function(recordId, object) {
|
@@ -1507,17 +1625,17 @@ getCacheQuery_fn = async function(query) {
|
|
1507
1625
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1508
1626
|
return hasExpired ? null : result;
|
1509
1627
|
};
|
1510
|
-
|
1511
|
-
|
1512
|
-
if (__privateGet$4(this,
|
1513
|
-
return __privateGet$4(this,
|
1628
|
+
_getSchemaTables$1 = new WeakSet();
|
1629
|
+
getSchemaTables_fn$1 = async function() {
|
1630
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1631
|
+
return __privateGet$4(this, _schemaTables$2);
|
1514
1632
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1515
1633
|
const { schema } = await getBranchDetails({
|
1516
1634
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1517
1635
|
...fetchProps
|
1518
1636
|
});
|
1519
|
-
__privateSet$
|
1520
|
-
return schema;
|
1637
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1638
|
+
return schema.tables;
|
1521
1639
|
};
|
1522
1640
|
const transformObjectLinks = (object) => {
|
1523
1641
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
@@ -1526,20 +1644,21 @@ const transformObjectLinks = (object) => {
|
|
1526
1644
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1527
1645
|
}, {});
|
1528
1646
|
};
|
1529
|
-
const initObject = (db,
|
1647
|
+
const initObject = (db, schemaTables, table, object) => {
|
1530
1648
|
const result = {};
|
1531
|
-
|
1532
|
-
|
1649
|
+
const { xata, ...rest } = object ?? {};
|
1650
|
+
Object.assign(result, rest);
|
1651
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1533
1652
|
if (!columns)
|
1534
1653
|
console.error(`Table ${table} not found in schema`);
|
1535
1654
|
for (const column of columns ?? []) {
|
1536
1655
|
const value = result[column.name];
|
1537
1656
|
switch (column.type) {
|
1538
1657
|
case "datetime": {
|
1539
|
-
const date = new Date(value);
|
1540
|
-
if (isNaN(date.getTime())) {
|
1658
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1659
|
+
if (date && isNaN(date.getTime())) {
|
1541
1660
|
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1542
|
-
} else {
|
1661
|
+
} else if (date) {
|
1543
1662
|
result[column.name] = date;
|
1544
1663
|
}
|
1545
1664
|
break;
|
@@ -1549,7 +1668,7 @@ const initObject = (db, schema, table, object) => {
|
|
1549
1668
|
if (!linkTable) {
|
1550
1669
|
console.error(`Failed to parse link for field ${column.name}`);
|
1551
1670
|
} else if (isObject(value)) {
|
1552
|
-
result[column.name] = initObject(db,
|
1671
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1553
1672
|
}
|
1554
1673
|
break;
|
1555
1674
|
}
|
@@ -1564,7 +1683,10 @@ const initObject = (db, schema, table, object) => {
|
|
1564
1683
|
result.delete = function() {
|
1565
1684
|
return db[table].delete(result["id"]);
|
1566
1685
|
};
|
1567
|
-
|
1686
|
+
result.getMetadata = function() {
|
1687
|
+
return xata;
|
1688
|
+
};
|
1689
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1568
1690
|
Object.defineProperty(result, prop, { enumerable: false });
|
1569
1691
|
}
|
1570
1692
|
Object.freeze(result);
|
@@ -1593,7 +1715,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1593
1715
|
throw TypeError("Cannot add the same private member more than once");
|
1594
1716
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1595
1717
|
};
|
1596
|
-
var __privateSet$
|
1718
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1597
1719
|
__accessCheck$3(obj, member, "write to private field");
|
1598
1720
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1599
1721
|
return value;
|
@@ -1602,7 +1724,7 @@ var _map;
|
|
1602
1724
|
class SimpleCache {
|
1603
1725
|
constructor(options = {}) {
|
1604
1726
|
__privateAdd$3(this, _map, void 0);
|
1605
|
-
__privateSet$
|
1727
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1606
1728
|
this.capacity = options.max ?? 500;
|
1607
1729
|
this.cacheRecords = options.cacheRecords ?? true;
|
1608
1730
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
@@ -1662,12 +1784,18 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1662
1784
|
throw TypeError("Cannot add the same private member more than once");
|
1663
1785
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1664
1786
|
};
|
1665
|
-
var
|
1787
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1788
|
+
__accessCheck$2(obj, member, "write to private field");
|
1789
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1790
|
+
return value;
|
1791
|
+
};
|
1792
|
+
var _tables, _schemaTables$1;
|
1666
1793
|
class SchemaPlugin extends XataPlugin {
|
1667
|
-
constructor(
|
1794
|
+
constructor(schemaTables) {
|
1668
1795
|
super();
|
1669
|
-
this.tableNames = tableNames;
|
1670
1796
|
__privateAdd$2(this, _tables, {});
|
1797
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1798
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1671
1799
|
}
|
1672
1800
|
build(pluginOptions) {
|
1673
1801
|
const db = new Proxy({}, {
|
@@ -1680,13 +1808,15 @@ class SchemaPlugin extends XataPlugin {
|
|
1680
1808
|
return __privateGet$2(this, _tables)[table];
|
1681
1809
|
}
|
1682
1810
|
});
|
1683
|
-
|
1684
|
-
|
1811
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1812
|
+
for (const table of tableNames) {
|
1813
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1685
1814
|
}
|
1686
1815
|
return db;
|
1687
1816
|
}
|
1688
1817
|
}
|
1689
1818
|
_tables = new WeakMap();
|
1819
|
+
_schemaTables$1 = new WeakMap();
|
1690
1820
|
|
1691
1821
|
var __accessCheck$1 = (obj, member, msg) => {
|
1692
1822
|
if (!member.has(obj))
|
@@ -1710,39 +1840,40 @@ var __privateMethod$1 = (obj, member, method) => {
|
|
1710
1840
|
__accessCheck$1(obj, member, "access private method");
|
1711
1841
|
return method;
|
1712
1842
|
};
|
1713
|
-
var
|
1843
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1714
1844
|
class SearchPlugin extends XataPlugin {
|
1715
|
-
constructor(db) {
|
1845
|
+
constructor(db, schemaTables) {
|
1716
1846
|
super();
|
1717
1847
|
this.db = db;
|
1718
1848
|
__privateAdd$1(this, _search);
|
1719
|
-
__privateAdd$1(this,
|
1720
|
-
__privateAdd$1(this,
|
1849
|
+
__privateAdd$1(this, _getSchemaTables);
|
1850
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1851
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1721
1852
|
}
|
1722
1853
|
build({ getFetchProps }) {
|
1723
1854
|
return {
|
1724
1855
|
all: async (query, options = {}) => {
|
1725
1856
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1726
|
-
const
|
1857
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1727
1858
|
return records.map((record) => {
|
1728
1859
|
const { table = "orphan" } = record.xata;
|
1729
|
-
return { table, record: initObject(this.db,
|
1860
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1730
1861
|
});
|
1731
1862
|
},
|
1732
1863
|
byTable: async (query, options = {}) => {
|
1733
1864
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1734
|
-
const
|
1865
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1735
1866
|
return records.reduce((acc, record) => {
|
1736
1867
|
const { table = "orphan" } = record.xata;
|
1737
1868
|
const items = acc[table] ?? [];
|
1738
|
-
const item = initObject(this.db,
|
1869
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1739
1870
|
return { ...acc, [table]: [...items, item] };
|
1740
1871
|
}, {});
|
1741
1872
|
}
|
1742
1873
|
};
|
1743
1874
|
}
|
1744
1875
|
}
|
1745
|
-
|
1876
|
+
_schemaTables = new WeakMap();
|
1746
1877
|
_search = new WeakSet();
|
1747
1878
|
search_fn = async function(query, options, getFetchProps) {
|
1748
1879
|
const fetchProps = await getFetchProps();
|
@@ -1754,38 +1885,32 @@ search_fn = async function(query, options, getFetchProps) {
|
|
1754
1885
|
});
|
1755
1886
|
return records;
|
1756
1887
|
};
|
1757
|
-
|
1758
|
-
|
1759
|
-
if (__privateGet$1(this,
|
1760
|
-
return __privateGet$1(this,
|
1888
|
+
_getSchemaTables = new WeakSet();
|
1889
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1890
|
+
if (__privateGet$1(this, _schemaTables))
|
1891
|
+
return __privateGet$1(this, _schemaTables);
|
1761
1892
|
const fetchProps = await getFetchProps();
|
1762
1893
|
const { schema } = await getBranchDetails({
|
1763
1894
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1764
1895
|
...fetchProps
|
1765
1896
|
});
|
1766
|
-
__privateSet$1(this,
|
1767
|
-
return schema;
|
1897
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1898
|
+
return schema.tables;
|
1768
1899
|
};
|
1769
1900
|
|
1770
1901
|
const isBranchStrategyBuilder = (strategy) => {
|
1771
1902
|
return typeof strategy === "function";
|
1772
1903
|
};
|
1773
1904
|
|
1774
|
-
const envBranchNames = [
|
1775
|
-
"XATA_BRANCH",
|
1776
|
-
"VERCEL_GIT_COMMIT_REF",
|
1777
|
-
"CF_PAGES_BRANCH",
|
1778
|
-
"BRANCH"
|
1779
|
-
];
|
1780
1905
|
async function getCurrentBranchName(options) {
|
1781
|
-
const
|
1782
|
-
if (
|
1783
|
-
const details = await getDatabaseBranch(
|
1906
|
+
const { branch, envBranch } = getEnvironment();
|
1907
|
+
if (branch) {
|
1908
|
+
const details = await getDatabaseBranch(branch, options);
|
1784
1909
|
if (details)
|
1785
|
-
return
|
1786
|
-
console.warn(`Branch ${
|
1910
|
+
return branch;
|
1911
|
+
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
1787
1912
|
}
|
1788
|
-
const gitBranch = await getGitBranch();
|
1913
|
+
const gitBranch = envBranch || await getGitBranch();
|
1789
1914
|
return resolveXataBranch(gitBranch, options);
|
1790
1915
|
}
|
1791
1916
|
async function getCurrentBranchDetails(options) {
|
@@ -1801,13 +1926,14 @@ async function resolveXataBranch(gitBranch, options) {
|
|
1801
1926
|
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1802
1927
|
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1803
1928
|
const [workspace] = host.split(".");
|
1929
|
+
const { fallbackBranch } = getEnvironment();
|
1804
1930
|
const { branch } = await resolveBranch({
|
1805
1931
|
apiKey,
|
1806
1932
|
apiUrl: databaseURL,
|
1807
1933
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1808
1934
|
workspacesApiUrl: `${protocol}//${host}`,
|
1809
1935
|
pathParams: { dbName, workspace },
|
1810
|
-
queryParams: { gitBranch, fallbackBranch
|
1936
|
+
queryParams: { gitBranch, fallbackBranch }
|
1811
1937
|
});
|
1812
1938
|
return branch;
|
1813
1939
|
}
|
@@ -1827,10 +1953,7 @@ async function getDatabaseBranch(branch, options) {
|
|
1827
1953
|
apiUrl: databaseURL,
|
1828
1954
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1829
1955
|
workspacesApiUrl: `${protocol}//${host}`,
|
1830
|
-
pathParams: {
|
1831
|
-
dbBranchName,
|
1832
|
-
workspace
|
1833
|
-
}
|
1956
|
+
pathParams: { dbBranchName, workspace }
|
1834
1957
|
});
|
1835
1958
|
} catch (err) {
|
1836
1959
|
if (isObject(err) && err.status === 404)
|
@@ -1838,21 +1961,10 @@ async function getDatabaseBranch(branch, options) {
|
|
1838
1961
|
throw err;
|
1839
1962
|
}
|
1840
1963
|
}
|
1841
|
-
function getBranchByEnvVariable() {
|
1842
|
-
for (const name of envBranchNames) {
|
1843
|
-
const value = getEnvVariable(name);
|
1844
|
-
if (value) {
|
1845
|
-
return value;
|
1846
|
-
}
|
1847
|
-
}
|
1848
|
-
try {
|
1849
|
-
return XATA_BRANCH;
|
1850
|
-
} catch (err) {
|
1851
|
-
}
|
1852
|
-
}
|
1853
1964
|
function getDatabaseURL() {
|
1854
1965
|
try {
|
1855
|
-
|
1966
|
+
const { databaseURL } = getEnvironment();
|
1967
|
+
return databaseURL;
|
1856
1968
|
} catch (err) {
|
1857
1969
|
return void 0;
|
1858
1970
|
}
|
@@ -1883,7 +1995,7 @@ var __privateMethod = (obj, member, method) => {
|
|
1883
1995
|
const buildClient = (plugins) => {
|
1884
1996
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1885
1997
|
return _a = class {
|
1886
|
-
constructor(options = {},
|
1998
|
+
constructor(options = {}, schemaTables) {
|
1887
1999
|
__privateAdd(this, _parseOptions);
|
1888
2000
|
__privateAdd(this, _getFetchProps);
|
1889
2001
|
__privateAdd(this, _evaluateBranch);
|
@@ -1893,8 +2005,8 @@ const buildClient = (plugins) => {
|
|
1893
2005
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1894
2006
|
cache: safeOptions.cache
|
1895
2007
|
};
|
1896
|
-
const db = new SchemaPlugin(
|
1897
|
-
const search = new SearchPlugin(db).build(pluginOptions);
|
2008
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2009
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1898
2010
|
this.db = db;
|
1899
2011
|
this.search = search;
|
1900
2012
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
@@ -1967,5 +2079,5 @@ class XataError extends Error {
|
|
1967
2079
|
}
|
1968
2080
|
}
|
1969
2081
|
|
1970
|
-
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, addGitBranchesEntry, 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, 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, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
2082
|
+
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, RecordArray, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, 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, 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, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
1971
2083
|
//# sourceMappingURL=index.mjs.map
|