@xata.io/client 0.15.0 → 0.16.2
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 +38 -0
- package/README.md +2 -0
- package/Usage.md +60 -6
- package/dist/index.cjs +477 -300
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +533 -136
- package/dist/index.mjs +466 -301
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
@@ -1,3 +1,27 @@
|
|
1
|
+
const defaultTrace = async (_name, fn, _options) => {
|
2
|
+
return await fn({
|
3
|
+
setAttributes: () => {
|
4
|
+
return;
|
5
|
+
},
|
6
|
+
onError: () => {
|
7
|
+
return;
|
8
|
+
}
|
9
|
+
});
|
10
|
+
};
|
11
|
+
const TraceAttributes = {
|
12
|
+
VERSION: "xata.sdk.version",
|
13
|
+
TABLE: "xata.table",
|
14
|
+
HTTP_REQUEST_ID: "http.request_id",
|
15
|
+
HTTP_STATUS_CODE: "http.status_code",
|
16
|
+
HTTP_HOST: "http.host",
|
17
|
+
HTTP_SCHEME: "http.scheme",
|
18
|
+
HTTP_USER_AGENT: "http.user_agent",
|
19
|
+
HTTP_METHOD: "http.method",
|
20
|
+
HTTP_URL: "http.url",
|
21
|
+
HTTP_ROUTE: "http.route",
|
22
|
+
HTTP_TARGET: "http.target"
|
23
|
+
};
|
24
|
+
|
1
25
|
function notEmpty(value) {
|
2
26
|
return value !== null && value !== void 0;
|
3
27
|
}
|
@@ -13,6 +37,9 @@ function isDefined(value) {
|
|
13
37
|
function isString(value) {
|
14
38
|
return isDefined(value) && typeof value === "string";
|
15
39
|
}
|
40
|
+
function isStringArray(value) {
|
41
|
+
return isDefined(value) && Array.isArray(value) && value.every(isString);
|
42
|
+
}
|
16
43
|
function toBase64(value) {
|
17
44
|
try {
|
18
45
|
return btoa(value);
|
@@ -118,12 +145,14 @@ function getFetchImplementation(userFetch) {
|
|
118
145
|
const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
|
119
146
|
const fetchImpl = userFetch ?? globalFetch;
|
120
147
|
if (!fetchImpl) {
|
121
|
-
throw new Error(
|
148
|
+
throw new Error(
|
149
|
+
`Couldn't find \`fetch\`. Install a fetch implementation such as \`node-fetch\` and pass it explicitly.`
|
150
|
+
);
|
122
151
|
}
|
123
152
|
return fetchImpl;
|
124
153
|
}
|
125
154
|
|
126
|
-
const VERSION = "0.
|
155
|
+
const VERSION = "0.16.2";
|
127
156
|
|
128
157
|
class ErrorWithCause extends Error {
|
129
158
|
constructor(message, options) {
|
@@ -202,34 +231,62 @@ async function fetch$1({
|
|
202
231
|
fetchImpl,
|
203
232
|
apiKey,
|
204
233
|
apiUrl,
|
205
|
-
workspacesApiUrl
|
234
|
+
workspacesApiUrl,
|
235
|
+
trace
|
206
236
|
}) {
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
237
|
+
return trace(
|
238
|
+
`${method.toUpperCase()} ${path}`,
|
239
|
+
async ({ setAttributes, onError }) => {
|
240
|
+
const baseUrl = buildBaseUrl({ path, workspacesApiUrl, pathParams, apiUrl });
|
241
|
+
const fullUrl = resolveUrl(baseUrl, queryParams, pathParams);
|
242
|
+
const url = fullUrl.includes("localhost") ? fullUrl.replace(/^[^.]+\./, "http://") : fullUrl;
|
243
|
+
setAttributes({
|
244
|
+
[TraceAttributes.HTTP_URL]: url,
|
245
|
+
[TraceAttributes.HTTP_TARGET]: resolveUrl(path, queryParams, pathParams)
|
246
|
+
});
|
247
|
+
const response = await fetchImpl(url, {
|
248
|
+
method: method.toUpperCase(),
|
249
|
+
body: body ? JSON.stringify(body) : void 0,
|
250
|
+
headers: {
|
251
|
+
"Content-Type": "application/json",
|
252
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
253
|
+
...headers,
|
254
|
+
...hostHeader(fullUrl),
|
255
|
+
Authorization: `Bearer ${apiKey}`
|
256
|
+
}
|
257
|
+
});
|
258
|
+
if (response.status === 204) {
|
259
|
+
return {};
|
260
|
+
}
|
261
|
+
const { host, protocol } = parseUrl(response.url);
|
262
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
263
|
+
setAttributes({
|
264
|
+
[TraceAttributes.HTTP_REQUEST_ID]: requestId,
|
265
|
+
[TraceAttributes.HTTP_STATUS_CODE]: response.status,
|
266
|
+
[TraceAttributes.HTTP_HOST]: host,
|
267
|
+
[TraceAttributes.HTTP_SCHEME]: protocol?.replace(":", "")
|
268
|
+
});
|
269
|
+
try {
|
270
|
+
const jsonResponse = await response.json();
|
271
|
+
if (response.ok) {
|
272
|
+
return jsonResponse;
|
273
|
+
}
|
274
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
275
|
+
} catch (error) {
|
276
|
+
const fetcherError = new FetcherError(response.status, error, requestId);
|
277
|
+
onError(fetcherError.message);
|
278
|
+
throw fetcherError;
|
279
|
+
}
|
280
|
+
},
|
281
|
+
{ [TraceAttributes.HTTP_METHOD]: method.toUpperCase(), [TraceAttributes.HTTP_ROUTE]: path }
|
282
|
+
);
|
283
|
+
}
|
284
|
+
function parseUrl(url) {
|
225
285
|
try {
|
226
|
-
const
|
227
|
-
|
228
|
-
return jsonResponse;
|
229
|
-
}
|
230
|
-
throw new FetcherError(response.status, jsonResponse, requestId);
|
286
|
+
const { host, protocol } = new URL(url);
|
287
|
+
return { host, protocol };
|
231
288
|
} catch (error) {
|
232
|
-
|
289
|
+
return {};
|
233
290
|
}
|
234
291
|
}
|
235
292
|
|
@@ -324,6 +381,11 @@ const deleteDatabase = (variables) => fetch$1({
|
|
324
381
|
method: "delete",
|
325
382
|
...variables
|
326
383
|
});
|
384
|
+
const getDatabaseMetadata = (variables) => fetch$1({
|
385
|
+
url: "/dbs/{dbName}/metadata",
|
386
|
+
method: "get",
|
387
|
+
...variables
|
388
|
+
});
|
327
389
|
const getGitBranchesMapping = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "get", ...variables });
|
328
390
|
const addGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "post", ...variables });
|
329
391
|
const removeGitBranchesEntry = (variables) => fetch$1({ url: "/dbs/{dbName}/gitBranches", method: "delete", ...variables });
|
@@ -337,11 +399,7 @@ const getBranchDetails = (variables) => fetch$1({
|
|
337
399
|
method: "get",
|
338
400
|
...variables
|
339
401
|
});
|
340
|
-
const createBranch = (variables) => fetch$1({
|
341
|
-
url: "/db/{dbBranchName}",
|
342
|
-
method: "put",
|
343
|
-
...variables
|
344
|
-
});
|
402
|
+
const createBranch = (variables) => fetch$1({ url: "/db/{dbBranchName}", method: "put", ...variables });
|
345
403
|
const deleteBranch = (variables) => fetch$1({
|
346
404
|
url: "/db/{dbBranchName}",
|
347
405
|
method: "delete",
|
@@ -415,11 +473,7 @@ const updateColumn = (variables) => fetch$1({
|
|
415
473
|
method: "patch",
|
416
474
|
...variables
|
417
475
|
});
|
418
|
-
const insertRecord = (variables) => fetch$1({
|
419
|
-
url: "/db/{dbBranchName}/tables/{tableName}/data",
|
420
|
-
method: "post",
|
421
|
-
...variables
|
422
|
-
});
|
476
|
+
const insertRecord = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data", method: "post", ...variables });
|
423
477
|
const insertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "put", ...variables });
|
424
478
|
const updateRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "patch", ...variables });
|
425
479
|
const upsertRecordWithID = (variables) => fetch$1({ url: "/db/{dbBranchName}/tables/{tableName}/data/{recordId}", method: "post", ...variables });
|
@@ -470,6 +524,7 @@ const operationsByTag = {
|
|
470
524
|
getDatabaseList,
|
471
525
|
createDatabase,
|
472
526
|
deleteDatabase,
|
527
|
+
getDatabaseMetadata,
|
473
528
|
getGitBranchesMapping,
|
474
529
|
addGitBranchesEntry,
|
475
530
|
removeGitBranchesEntry,
|
@@ -562,7 +617,8 @@ class XataApiClient {
|
|
562
617
|
__privateAdd$7(this, _extraProps, void 0);
|
563
618
|
__privateAdd$7(this, _namespaces, {});
|
564
619
|
const provider = options.host ?? "production";
|
565
|
-
const apiKey = options
|
620
|
+
const apiKey = options.apiKey ?? getAPIKey();
|
621
|
+
const trace = options.trace ?? defaultTrace;
|
566
622
|
if (!apiKey) {
|
567
623
|
throw new Error("Could not resolve a valid apiKey");
|
568
624
|
}
|
@@ -570,7 +626,8 @@ class XataApiClient {
|
|
570
626
|
apiUrl: getHostUrl(provider, "main"),
|
571
627
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
572
628
|
fetchImpl: getFetchImplementation(options.fetch),
|
573
|
-
apiKey
|
629
|
+
apiKey,
|
630
|
+
trace
|
574
631
|
});
|
575
632
|
}
|
576
633
|
get user() {
|
@@ -742,6 +799,12 @@ class DatabaseApi {
|
|
742
799
|
...this.extraProps
|
743
800
|
});
|
744
801
|
}
|
802
|
+
getDatabaseMetadata(workspace, dbName) {
|
803
|
+
return operationsByTag.database.getDatabaseMetadata({
|
804
|
+
pathParams: { workspace, dbName },
|
805
|
+
...this.extraProps
|
806
|
+
});
|
807
|
+
}
|
745
808
|
getGitBranchesMapping(workspace, dbName) {
|
746
809
|
return operationsByTag.database.getGitBranchesMapping({
|
747
810
|
pathParams: { workspace, dbName },
|
@@ -914,9 +977,10 @@ class RecordsApi {
|
|
914
977
|
constructor(extraProps) {
|
915
978
|
this.extraProps = extraProps;
|
916
979
|
}
|
917
|
-
insertRecord(workspace, database, branch, tableName, record) {
|
980
|
+
insertRecord(workspace, database, branch, tableName, record, options = {}) {
|
918
981
|
return operationsByTag.records.insertRecord({
|
919
982
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
983
|
+
queryParams: options,
|
920
984
|
body: record,
|
921
985
|
...this.extraProps
|
922
986
|
});
|
@@ -945,21 +1009,24 @@ class RecordsApi {
|
|
945
1009
|
...this.extraProps
|
946
1010
|
});
|
947
1011
|
}
|
948
|
-
deleteRecord(workspace, database, branch, tableName, recordId) {
|
1012
|
+
deleteRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
949
1013
|
return operationsByTag.records.deleteRecord({
|
950
1014
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
1015
|
+
queryParams: options,
|
951
1016
|
...this.extraProps
|
952
1017
|
});
|
953
1018
|
}
|
954
1019
|
getRecord(workspace, database, branch, tableName, recordId, options = {}) {
|
955
1020
|
return operationsByTag.records.getRecord({
|
956
1021
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName, recordId },
|
1022
|
+
queryParams: options,
|
957
1023
|
...this.extraProps
|
958
1024
|
});
|
959
1025
|
}
|
960
|
-
bulkInsertTableRecords(workspace, database, branch, tableName, records) {
|
1026
|
+
bulkInsertTableRecords(workspace, database, branch, tableName, records, options = {}) {
|
961
1027
|
return operationsByTag.records.bulkInsertTableRecords({
|
962
1028
|
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
1029
|
+
queryParams: options,
|
963
1030
|
body: { records },
|
964
1031
|
...this.extraProps
|
965
1032
|
});
|
@@ -1177,13 +1244,18 @@ const _Query = class {
|
|
1177
1244
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { filter: { $all } }, __privateGet$5(this, _data));
|
1178
1245
|
}
|
1179
1246
|
}
|
1180
|
-
sort(column, direction) {
|
1247
|
+
sort(column, direction = "asc") {
|
1181
1248
|
const originalSort = [__privateGet$5(this, _data).sort ?? []].flat();
|
1182
1249
|
const sort = [...originalSort, { column, direction }];
|
1183
1250
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { sort }, __privateGet$5(this, _data));
|
1184
1251
|
}
|
1185
1252
|
select(columns) {
|
1186
|
-
return new _Query(
|
1253
|
+
return new _Query(
|
1254
|
+
__privateGet$5(this, _repository),
|
1255
|
+
__privateGet$5(this, _table$1),
|
1256
|
+
{ columns },
|
1257
|
+
__privateGet$5(this, _data)
|
1258
|
+
);
|
1187
1259
|
}
|
1188
1260
|
getPaginated(options = {}) {
|
1189
1261
|
const query = new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), options, __privateGet$5(this, _data));
|
@@ -1308,7 +1380,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1308
1380
|
__accessCheck$4(obj, member, "access private method");
|
1309
1381
|
return method;
|
1310
1382
|
};
|
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,
|
1383
|
+
var _table, _getFetchProps, _db, _cache, _schemaTables$2, _trace, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn, _getSchemaTables$1, getSchemaTables_fn$1;
|
1312
1384
|
class Repository extends Query {
|
1313
1385
|
}
|
1314
1386
|
class RestRepository extends Query {
|
@@ -1320,191 +1392,214 @@ class RestRepository extends Query {
|
|
1320
1392
|
__privateAdd$4(this, _updateRecordWithID);
|
1321
1393
|
__privateAdd$4(this, _upsertRecordWithID);
|
1322
1394
|
__privateAdd$4(this, _deleteRecord);
|
1323
|
-
__privateAdd$4(this, _invalidateCache);
|
1324
|
-
__privateAdd$4(this, _setCacheRecord);
|
1325
|
-
__privateAdd$4(this, _getCacheRecord);
|
1326
1395
|
__privateAdd$4(this, _setCacheQuery);
|
1327
1396
|
__privateAdd$4(this, _getCacheQuery);
|
1328
1397
|
__privateAdd$4(this, _getSchemaTables$1);
|
1329
1398
|
__privateAdd$4(this, _table, void 0);
|
1330
1399
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1400
|
+
__privateAdd$4(this, _db, void 0);
|
1331
1401
|
__privateAdd$4(this, _cache, void 0);
|
1332
1402
|
__privateAdd$4(this, _schemaTables$2, void 0);
|
1403
|
+
__privateAdd$4(this, _trace, void 0);
|
1333
1404
|
__privateSet$4(this, _table, options.table);
|
1334
1405
|
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1335
|
-
this
|
1406
|
+
__privateSet$4(this, _db, options.db);
|
1336
1407
|
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1337
1408
|
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1409
|
+
const trace = options.pluginOptions.trace ?? defaultTrace;
|
1410
|
+
__privateSet$4(this, _trace, async (name, fn, options2 = {}) => {
|
1411
|
+
return trace(name, fn, {
|
1412
|
+
...options2,
|
1413
|
+
[TraceAttributes.TABLE]: __privateGet$4(this, _table),
|
1414
|
+
[TraceAttributes.VERSION]: VERSION
|
1415
|
+
});
|
1416
|
+
});
|
1338
1417
|
}
|
1339
|
-
async create(a, b) {
|
1340
|
-
|
1341
|
-
if (a
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
|
1348
|
-
|
1349
|
-
|
1350
|
-
|
1351
|
-
|
1352
|
-
|
1353
|
-
|
1354
|
-
|
1355
|
-
|
1356
|
-
|
1357
|
-
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
1363
|
-
|
1364
|
-
|
1365
|
-
|
1366
|
-
|
1367
|
-
|
1368
|
-
|
1369
|
-
|
1370
|
-
|
1371
|
-
|
1372
|
-
|
1373
|
-
|
1374
|
-
|
1375
|
-
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
const
|
1381
|
-
|
1382
|
-
const
|
1383
|
-
|
1384
|
-
|
1385
|
-
|
1386
|
-
|
1387
|
-
|
1388
|
-
|
1389
|
-
|
1390
|
-
|
1418
|
+
async create(a, b, c) {
|
1419
|
+
return __privateGet$4(this, _trace).call(this, "create", async () => {
|
1420
|
+
if (Array.isArray(a)) {
|
1421
|
+
if (a.length === 0)
|
1422
|
+
return [];
|
1423
|
+
const columns = isStringArray(b) ? b : void 0;
|
1424
|
+
return __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a, columns);
|
1425
|
+
}
|
1426
|
+
if (isString(a) && isObject(b)) {
|
1427
|
+
if (a === "")
|
1428
|
+
throw new Error("The id can't be empty");
|
1429
|
+
const columns = isStringArray(c) ? c : void 0;
|
1430
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns);
|
1431
|
+
}
|
1432
|
+
if (isObject(a) && isString(a.id)) {
|
1433
|
+
if (a.id === "")
|
1434
|
+
throw new Error("The id can't be empty");
|
1435
|
+
const columns = isStringArray(b) ? b : void 0;
|
1436
|
+
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1437
|
+
}
|
1438
|
+
if (isObject(a)) {
|
1439
|
+
const columns = isStringArray(b) ? b : void 0;
|
1440
|
+
return __privateMethod$2(this, _insertRecordWithoutId, insertRecordWithoutId_fn).call(this, a, columns);
|
1441
|
+
}
|
1442
|
+
throw new Error("Invalid arguments for create method");
|
1443
|
+
});
|
1444
|
+
}
|
1445
|
+
async read(a, b) {
|
1446
|
+
return __privateGet$4(this, _trace).call(this, "read", async () => {
|
1447
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1448
|
+
if (Array.isArray(a)) {
|
1449
|
+
if (a.length === 0)
|
1450
|
+
return [];
|
1451
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1452
|
+
const finalObjects = await this.getAll({ filter: { id: { $any: ids } }, columns });
|
1453
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1454
|
+
acc[object.id] = object;
|
1455
|
+
return acc;
|
1456
|
+
}, {});
|
1457
|
+
return ids.map((id2) => dictionary[id2] ?? null);
|
1458
|
+
}
|
1459
|
+
const id = isString(a) ? a : a.id;
|
1460
|
+
if (isString(id)) {
|
1461
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1462
|
+
try {
|
1463
|
+
const response = await getRecord({
|
1464
|
+
pathParams: {
|
1465
|
+
workspace: "{workspaceId}",
|
1466
|
+
dbBranchName: "{dbBranch}",
|
1467
|
+
tableName: __privateGet$4(this, _table),
|
1468
|
+
recordId: id
|
1469
|
+
},
|
1470
|
+
queryParams: { columns },
|
1471
|
+
...fetchProps
|
1472
|
+
});
|
1473
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1474
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1475
|
+
} catch (e) {
|
1476
|
+
if (isObject(e) && e.status === 404) {
|
1477
|
+
return null;
|
1478
|
+
}
|
1479
|
+
throw e;
|
1391
1480
|
}
|
1392
|
-
throw e;
|
1393
1481
|
}
|
1394
|
-
|
1482
|
+
return null;
|
1483
|
+
});
|
1395
1484
|
}
|
1396
|
-
async update(a, b) {
|
1397
|
-
|
1398
|
-
if (a
|
1399
|
-
|
1400
|
-
|
1401
|
-
|
1485
|
+
async update(a, b, c) {
|
1486
|
+
return __privateGet$4(this, _trace).call(this, "update", async () => {
|
1487
|
+
if (Array.isArray(a)) {
|
1488
|
+
if (a.length === 0)
|
1489
|
+
return [];
|
1490
|
+
if (a.length > 100) {
|
1491
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1492
|
+
}
|
1493
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1494
|
+
return Promise.all(a.map((object) => this.update(object, columns)));
|
1402
1495
|
}
|
1403
|
-
|
1404
|
-
|
1405
|
-
|
1406
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a);
|
1407
|
-
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b);
|
1408
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1409
|
-
return record;
|
1410
|
-
}
|
1411
|
-
if (isObject(a) && isString(a.id)) {
|
1412
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1413
|
-
const record = await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 });
|
1414
|
-
await __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record);
|
1415
|
-
return record;
|
1416
|
-
}
|
1417
|
-
throw new Error("Invalid arguments for update method");
|
1418
|
-
}
|
1419
|
-
async createOrUpdate(a, b) {
|
1420
|
-
if (Array.isArray(a)) {
|
1421
|
-
if (a.length === 0)
|
1422
|
-
return [];
|
1423
|
-
if (a.length > 100) {
|
1424
|
-
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1496
|
+
if (isString(a) && isObject(b)) {
|
1497
|
+
const columns = isStringArray(c) ? c : void 0;
|
1498
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns);
|
1425
1499
|
}
|
1426
|
-
|
1427
|
-
|
1428
|
-
|
1429
|
-
|
1430
|
-
|
1431
|
-
|
1432
|
-
|
1433
|
-
|
1434
|
-
|
1435
|
-
|
1436
|
-
|
1437
|
-
|
1438
|
-
|
1439
|
-
|
1440
|
-
|
1500
|
+
if (isObject(a) && isString(a.id)) {
|
1501
|
+
const columns = isStringArray(b) ? b : void 0;
|
1502
|
+
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1503
|
+
}
|
1504
|
+
throw new Error("Invalid arguments for update method");
|
1505
|
+
});
|
1506
|
+
}
|
1507
|
+
async createOrUpdate(a, b, c) {
|
1508
|
+
return __privateGet$4(this, _trace).call(this, "createOrUpdate", async () => {
|
1509
|
+
if (Array.isArray(a)) {
|
1510
|
+
if (a.length === 0)
|
1511
|
+
return [];
|
1512
|
+
if (a.length > 100) {
|
1513
|
+
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1514
|
+
}
|
1515
|
+
const columns = isStringArray(b) ? b : ["*"];
|
1516
|
+
return Promise.all(a.map((object) => this.createOrUpdate(object, columns)));
|
1517
|
+
}
|
1518
|
+
if (isString(a) && isObject(b)) {
|
1519
|
+
const columns = isStringArray(c) ? c : void 0;
|
1520
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a, b, columns);
|
1521
|
+
}
|
1522
|
+
if (isObject(a) && isString(a.id)) {
|
1523
|
+
const columns = isStringArray(c) ? c : void 0;
|
1524
|
+
return __privateMethod$2(this, _upsertRecordWithID, upsertRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns);
|
1525
|
+
}
|
1526
|
+
throw new Error("Invalid arguments for createOrUpdate method");
|
1527
|
+
});
|
1441
1528
|
}
|
1442
1529
|
async delete(a) {
|
1443
|
-
|
1444
|
-
if (a
|
1530
|
+
return __privateGet$4(this, _trace).call(this, "delete", async () => {
|
1531
|
+
if (Array.isArray(a)) {
|
1532
|
+
if (a.length === 0)
|
1533
|
+
return;
|
1534
|
+
if (a.length > 100) {
|
1535
|
+
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1536
|
+
}
|
1537
|
+
await Promise.all(a.map((id) => this.delete(id)));
|
1445
1538
|
return;
|
1446
|
-
if (a.length > 100) {
|
1447
|
-
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1448
1539
|
}
|
1449
|
-
|
1450
|
-
|
1451
|
-
|
1452
|
-
|
1453
|
-
|
1454
|
-
|
1455
|
-
|
1456
|
-
|
1457
|
-
|
1458
|
-
|
1459
|
-
await __privateMethod$2(this, _invalidateCache, invalidateCache_fn).call(this, a.id);
|
1460
|
-
return;
|
1461
|
-
}
|
1462
|
-
throw new Error("Invalid arguments for delete method");
|
1540
|
+
if (isString(a)) {
|
1541
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a);
|
1542
|
+
return;
|
1543
|
+
}
|
1544
|
+
if (isObject(a) && isString(a.id)) {
|
1545
|
+
await __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a.id);
|
1546
|
+
return;
|
1547
|
+
}
|
1548
|
+
throw new Error("Invalid arguments for delete method");
|
1549
|
+
});
|
1463
1550
|
}
|
1464
1551
|
async search(query, options = {}) {
|
1465
|
-
|
1466
|
-
|
1467
|
-
|
1468
|
-
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1552
|
+
return __privateGet$4(this, _trace).call(this, "search", async () => {
|
1553
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1554
|
+
const { records } = await searchTable({
|
1555
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1556
|
+
body: {
|
1557
|
+
query,
|
1558
|
+
fuzziness: options.fuzziness,
|
1559
|
+
prefix: options.prefix,
|
1560
|
+
highlight: options.highlight,
|
1561
|
+
filter: options.filter,
|
1562
|
+
boosters: options.boosters
|
1563
|
+
},
|
1564
|
+
...fetchProps
|
1565
|
+
});
|
1566
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1567
|
+
return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1475
1568
|
});
|
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));
|
1478
1569
|
}
|
1479
1570
|
async query(query) {
|
1480
|
-
|
1481
|
-
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
|
1494
|
-
|
1571
|
+
return __privateGet$4(this, _trace).call(this, "query", async () => {
|
1572
|
+
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
1573
|
+
if (cacheQuery)
|
1574
|
+
return new Page(query, cacheQuery.meta, cacheQuery.records);
|
1575
|
+
const data = query.getQueryOptions();
|
1576
|
+
const body = {
|
1577
|
+
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1578
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1579
|
+
page: data.pagination,
|
1580
|
+
columns: data.columns
|
1581
|
+
};
|
1582
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1583
|
+
const { meta, records: objects } = await queryTable({
|
1584
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1585
|
+
body,
|
1586
|
+
...fetchProps
|
1587
|
+
});
|
1588
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1589
|
+
const records = objects.map((record) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), record));
|
1590
|
+
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1591
|
+
return new Page(query, meta, records);
|
1495
1592
|
});
|
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));
|
1498
|
-
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1499
|
-
return new Page(query, meta, records);
|
1500
1593
|
}
|
1501
1594
|
}
|
1502
1595
|
_table = new WeakMap();
|
1503
1596
|
_getFetchProps = new WeakMap();
|
1597
|
+
_db = new WeakMap();
|
1504
1598
|
_cache = new WeakMap();
|
1505
1599
|
_schemaTables$2 = new WeakMap();
|
1600
|
+
_trace = new WeakMap();
|
1506
1601
|
_insertRecordWithoutId = new WeakSet();
|
1507
|
-
insertRecordWithoutId_fn = async function(object) {
|
1602
|
+
insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
|
1508
1603
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1509
1604
|
const record = transformObjectLinks(object);
|
1510
1605
|
const response = await insertRecord({
|
@@ -1513,17 +1608,15 @@ insertRecordWithoutId_fn = async function(object) {
|
|
1513
1608
|
dbBranchName: "{dbBranch}",
|
1514
1609
|
tableName: __privateGet$4(this, _table)
|
1515
1610
|
},
|
1611
|
+
queryParams: { columns },
|
1516
1612
|
body: record,
|
1517
1613
|
...fetchProps
|
1518
1614
|
});
|
1519
|
-
const
|
1520
|
-
|
1521
|
-
throw new Error("The server failed to save the record");
|
1522
|
-
}
|
1523
|
-
return finalObject;
|
1615
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1616
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1524
1617
|
};
|
1525
1618
|
_insertRecordWithId = new WeakSet();
|
1526
|
-
insertRecordWithId_fn = async function(recordId, object) {
|
1619
|
+
insertRecordWithId_fn = async function(recordId, object, columns = ["*"]) {
|
1527
1620
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1528
1621
|
const record = transformObjectLinks(object);
|
1529
1622
|
const response = await insertRecordWithID({
|
@@ -1534,60 +1627,52 @@ insertRecordWithId_fn = async function(recordId, object) {
|
|
1534
1627
|
recordId
|
1535
1628
|
},
|
1536
1629
|
body: record,
|
1537
|
-
queryParams: { createOnly: true },
|
1630
|
+
queryParams: { createOnly: true, columns },
|
1538
1631
|
...fetchProps
|
1539
1632
|
});
|
1540
|
-
const
|
1541
|
-
|
1542
|
-
throw new Error("The server failed to save the record");
|
1543
|
-
}
|
1544
|
-
return finalObject;
|
1633
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1634
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1545
1635
|
};
|
1546
1636
|
_bulkInsertTableRecords = new WeakSet();
|
1547
|
-
bulkInsertTableRecords_fn = async function(objects) {
|
1637
|
+
bulkInsertTableRecords_fn = async function(objects, columns = ["*"]) {
|
1548
1638
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1549
1639
|
const records = objects.map((object) => transformObjectLinks(object));
|
1550
|
-
const
|
1640
|
+
const response = await bulkInsertTableRecords({
|
1551
1641
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1642
|
+
queryParams: { columns },
|
1552
1643
|
body: { records },
|
1553
1644
|
...fetchProps
|
1554
1645
|
});
|
1555
|
-
|
1556
|
-
|
1557
|
-
throw new Error("The server failed to save some records");
|
1646
|
+
if (!isResponseWithRecords(response)) {
|
1647
|
+
throw new Error("Request included columns but server didn't include them");
|
1558
1648
|
}
|
1559
|
-
const
|
1560
|
-
|
1561
|
-
return acc;
|
1562
|
-
}, {});
|
1563
|
-
return recordIDs.map((id) => dictionary[id]);
|
1649
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1650
|
+
return response.records?.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item));
|
1564
1651
|
};
|
1565
1652
|
_updateRecordWithID = new WeakSet();
|
1566
|
-
updateRecordWithID_fn = async function(recordId, object) {
|
1653
|
+
updateRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1567
1654
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1568
1655
|
const record = transformObjectLinks(object);
|
1569
1656
|
const response = await updateRecordWithID({
|
1570
1657
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1658
|
+
queryParams: { columns },
|
1571
1659
|
body: record,
|
1572
1660
|
...fetchProps
|
1573
1661
|
});
|
1574
|
-
const
|
1575
|
-
|
1576
|
-
throw new Error("The server failed to save the record");
|
1577
|
-
return item;
|
1662
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1663
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1578
1664
|
};
|
1579
1665
|
_upsertRecordWithID = new WeakSet();
|
1580
|
-
upsertRecordWithID_fn = async function(recordId, object) {
|
1666
|
+
upsertRecordWithID_fn = async function(recordId, object, columns = ["*"]) {
|
1581
1667
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1582
1668
|
const response = await upsertRecordWithID({
|
1583
1669
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
|
1670
|
+
queryParams: { columns },
|
1584
1671
|
body: object,
|
1585
1672
|
...fetchProps
|
1586
1673
|
});
|
1587
|
-
const
|
1588
|
-
|
1589
|
-
throw new Error("The server failed to save the record");
|
1590
|
-
return item;
|
1674
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1675
|
+
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response);
|
1591
1676
|
};
|
1592
1677
|
_deleteRecord = new WeakSet();
|
1593
1678
|
deleteRecord_fn = async function(recordId) {
|
@@ -1597,29 +1682,6 @@ deleteRecord_fn = async function(recordId) {
|
|
1597
1682
|
...fetchProps
|
1598
1683
|
});
|
1599
1684
|
};
|
1600
|
-
_invalidateCache = new WeakSet();
|
1601
|
-
invalidateCache_fn = async function(recordId) {
|
1602
|
-
await __privateGet$4(this, _cache).delete(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1603
|
-
const cacheItems = await __privateGet$4(this, _cache).getAll();
|
1604
|
-
const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
|
1605
|
-
for (const [key, value] of queries) {
|
1606
|
-
const ids = getIds(value);
|
1607
|
-
if (ids.includes(recordId))
|
1608
|
-
await __privateGet$4(this, _cache).delete(key);
|
1609
|
-
}
|
1610
|
-
};
|
1611
|
-
_setCacheRecord = new WeakSet();
|
1612
|
-
setCacheRecord_fn = async function(record) {
|
1613
|
-
if (!__privateGet$4(this, _cache).cacheRecords)
|
1614
|
-
return;
|
1615
|
-
await __privateGet$4(this, _cache).set(`rec_${__privateGet$4(this, _table)}:${record.id}`, record);
|
1616
|
-
};
|
1617
|
-
_getCacheRecord = new WeakSet();
|
1618
|
-
getCacheRecord_fn = async function(recordId) {
|
1619
|
-
if (!__privateGet$4(this, _cache).cacheRecords)
|
1620
|
-
return null;
|
1621
|
-
return __privateGet$4(this, _cache).get(`rec_${__privateGet$4(this, _table)}:${recordId}`);
|
1622
|
-
};
|
1623
1685
|
_setCacheQuery = new WeakSet();
|
1624
1686
|
setCacheQuery_fn = async function(query, meta, records) {
|
1625
1687
|
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
@@ -1685,11 +1747,11 @@ const initObject = (db, schemaTables, table, object) => {
|
|
1685
1747
|
}
|
1686
1748
|
}
|
1687
1749
|
}
|
1688
|
-
result.read = function() {
|
1689
|
-
return db[table].read(result["id"]);
|
1750
|
+
result.read = function(columns2) {
|
1751
|
+
return db[table].read(result["id"], columns2);
|
1690
1752
|
};
|
1691
|
-
result.update = function(data) {
|
1692
|
-
return db[table].update(result["id"], data);
|
1753
|
+
result.update = function(data, columns2) {
|
1754
|
+
return db[table].update(result["id"], data, columns2);
|
1693
1755
|
};
|
1694
1756
|
result.delete = function() {
|
1695
1757
|
return db[table].delete(result["id"]);
|
@@ -1703,14 +1765,8 @@ const initObject = (db, schemaTables, table, object) => {
|
|
1703
1765
|
Object.freeze(result);
|
1704
1766
|
return result;
|
1705
1767
|
};
|
1706
|
-
function
|
1707
|
-
|
1708
|
-
return value.map((item) => getIds(item)).flat();
|
1709
|
-
}
|
1710
|
-
if (!isObject(value))
|
1711
|
-
return [];
|
1712
|
-
const nestedIds = Object.values(value).map((item) => getIds(item)).flat();
|
1713
|
-
return isString(value.id) ? [value.id, ...nestedIds] : nestedIds;
|
1768
|
+
function isResponseWithRecords(value) {
|
1769
|
+
return isObject(value) && Array.isArray(value.records);
|
1714
1770
|
}
|
1715
1771
|
|
1716
1772
|
var __accessCheck$3 = (obj, member, msg) => {
|
@@ -1737,7 +1793,6 @@ class SimpleCache {
|
|
1737
1793
|
__privateAdd$3(this, _map, void 0);
|
1738
1794
|
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1739
1795
|
this.capacity = options.max ?? 500;
|
1740
|
-
this.cacheRecords = options.cacheRecords ?? true;
|
1741
1796
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
1742
1797
|
}
|
1743
1798
|
async getAll() {
|
@@ -1763,18 +1818,25 @@ class SimpleCache {
|
|
1763
1818
|
}
|
1764
1819
|
_map = new WeakMap();
|
1765
1820
|
|
1766
|
-
const
|
1767
|
-
const
|
1768
|
-
const
|
1769
|
-
const
|
1770
|
-
const
|
1771
|
-
const
|
1821
|
+
const greaterThan = (value) => ({ $gt: value });
|
1822
|
+
const gt = greaterThan;
|
1823
|
+
const greaterThanEquals = (value) => ({ $ge: value });
|
1824
|
+
const greaterEquals = greaterThanEquals;
|
1825
|
+
const gte = greaterThanEquals;
|
1826
|
+
const ge = greaterThanEquals;
|
1827
|
+
const lessThan = (value) => ({ $lt: value });
|
1828
|
+
const lt = lessThan;
|
1829
|
+
const lessThanEquals = (value) => ({ $le: value });
|
1830
|
+
const lessEquals = lessThanEquals;
|
1831
|
+
const lte = lessThanEquals;
|
1832
|
+
const le = lessThanEquals;
|
1772
1833
|
const exists = (column) => ({ $exists: column });
|
1773
1834
|
const notExists = (column) => ({ $notExists: column });
|
1774
1835
|
const startsWith = (value) => ({ $startsWith: value });
|
1775
1836
|
const endsWith = (value) => ({ $endsWith: value });
|
1776
1837
|
const pattern = (value) => ({ $pattern: value });
|
1777
1838
|
const is = (value) => ({ $is: value });
|
1839
|
+
const equals = is;
|
1778
1840
|
const isNot = (value) => ({ $isNot: value });
|
1779
1841
|
const contains = (value) => ({ $contains: value });
|
1780
1842
|
const includes = (value) => ({ $includes: value });
|
@@ -1809,16 +1871,19 @@ class SchemaPlugin extends XataPlugin {
|
|
1809
1871
|
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1810
1872
|
}
|
1811
1873
|
build(pluginOptions) {
|
1812
|
-
const db = new Proxy(
|
1813
|
-
|
1814
|
-
|
1815
|
-
|
1816
|
-
|
1817
|
-
|
1874
|
+
const db = new Proxy(
|
1875
|
+
{},
|
1876
|
+
{
|
1877
|
+
get: (_target, table) => {
|
1878
|
+
if (!isString(table))
|
1879
|
+
throw new Error("Invalid table name");
|
1880
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1881
|
+
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1882
|
+
}
|
1883
|
+
return __privateGet$2(this, _tables)[table];
|
1818
1884
|
}
|
1819
|
-
return __privateGet$2(this, _tables)[table];
|
1820
1885
|
}
|
1821
|
-
|
1886
|
+
);
|
1822
1887
|
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1823
1888
|
for (const table of tableNames) {
|
1824
1889
|
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
@@ -1888,10 +1953,10 @@ _schemaTables = new WeakMap();
|
|
1888
1953
|
_search = new WeakSet();
|
1889
1954
|
search_fn = async function(query, options, getFetchProps) {
|
1890
1955
|
const fetchProps = await getFetchProps();
|
1891
|
-
const { tables, fuzziness, highlight } = options ?? {};
|
1956
|
+
const { tables, fuzziness, highlight, prefix } = options ?? {};
|
1892
1957
|
const { records } = await searchBranch({
|
1893
1958
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1894
|
-
body: { tables, query, fuzziness, highlight },
|
1959
|
+
body: { tables, query, fuzziness, prefix, highlight },
|
1895
1960
|
...fetchProps
|
1896
1961
|
});
|
1897
1962
|
return records;
|
@@ -1932,9 +1997,13 @@ async function resolveXataBranch(gitBranch, options) {
|
|
1932
1997
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1933
1998
|
const apiKey = options?.apiKey || getAPIKey();
|
1934
1999
|
if (!databaseURL)
|
1935
|
-
throw new Error(
|
2000
|
+
throw new Error(
|
2001
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
2002
|
+
);
|
1936
2003
|
if (!apiKey)
|
1937
|
-
throw new Error(
|
2004
|
+
throw new Error(
|
2005
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
2006
|
+
);
|
1938
2007
|
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1939
2008
|
const [workspace] = host.split(".");
|
1940
2009
|
const { fallbackBranch } = getEnvironment();
|
@@ -1944,7 +2013,8 @@ async function resolveXataBranch(gitBranch, options) {
|
|
1944
2013
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1945
2014
|
workspacesApiUrl: `${protocol}//${host}`,
|
1946
2015
|
pathParams: { dbName, workspace },
|
1947
|
-
queryParams: { gitBranch, fallbackBranch }
|
2016
|
+
queryParams: { gitBranch, fallbackBranch },
|
2017
|
+
trace: defaultTrace
|
1948
2018
|
});
|
1949
2019
|
return branch;
|
1950
2020
|
}
|
@@ -1952,9 +2022,13 @@ async function getDatabaseBranch(branch, options) {
|
|
1952
2022
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1953
2023
|
const apiKey = options?.apiKey || getAPIKey();
|
1954
2024
|
if (!databaseURL)
|
1955
|
-
throw new Error(
|
2025
|
+
throw new Error(
|
2026
|
+
"A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
|
2027
|
+
);
|
1956
2028
|
if (!apiKey)
|
1957
|
-
throw new Error(
|
2029
|
+
throw new Error(
|
2030
|
+
"An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
|
2031
|
+
);
|
1958
2032
|
const [protocol, , host, , database] = databaseURL.split("/");
|
1959
2033
|
const [workspace] = host.split(".");
|
1960
2034
|
const dbBranchName = `${database}:${branch}`;
|
@@ -1964,7 +2038,8 @@ async function getDatabaseBranch(branch, options) {
|
|
1964
2038
|
apiUrl: databaseURL,
|
1965
2039
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1966
2040
|
workspacesApiUrl: `${protocol}//${host}`,
|
1967
|
-
pathParams: { dbBranchName, workspace }
|
2041
|
+
pathParams: { dbBranchName, workspace },
|
2042
|
+
trace: defaultTrace
|
1968
2043
|
});
|
1969
2044
|
} catch (err) {
|
1970
2045
|
if (isObject(err) && err.status === 404)
|
@@ -2004,17 +2079,20 @@ var __privateMethod = (obj, member, method) => {
|
|
2004
2079
|
return method;
|
2005
2080
|
};
|
2006
2081
|
const buildClient = (plugins) => {
|
2007
|
-
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
2082
|
+
var _branch, _options, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
2008
2083
|
return _a = class {
|
2009
2084
|
constructor(options = {}, schemaTables) {
|
2010
2085
|
__privateAdd(this, _parseOptions);
|
2011
2086
|
__privateAdd(this, _getFetchProps);
|
2012
2087
|
__privateAdd(this, _evaluateBranch);
|
2013
2088
|
__privateAdd(this, _branch, void 0);
|
2089
|
+
__privateAdd(this, _options, void 0);
|
2014
2090
|
const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
|
2091
|
+
__privateSet(this, _options, safeOptions);
|
2015
2092
|
const pluginOptions = {
|
2016
2093
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
2017
|
-
cache: safeOptions.cache
|
2094
|
+
cache: safeOptions.cache,
|
2095
|
+
trace: safeOptions.trace
|
2018
2096
|
};
|
2019
2097
|
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
2020
2098
|
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
@@ -2033,22 +2111,26 @@ const buildClient = (plugins) => {
|
|
2033
2111
|
}
|
2034
2112
|
}
|
2035
2113
|
}
|
2036
|
-
|
2114
|
+
async getConfig() {
|
2115
|
+
const databaseURL = __privateGet(this, _options).databaseURL;
|
2116
|
+
const branch = await __privateGet(this, _options).branch();
|
2117
|
+
return { databaseURL, branch };
|
2118
|
+
}
|
2119
|
+
}, _branch = new WeakMap(), _options = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
|
2037
2120
|
const fetch = getFetchImplementation(options?.fetch);
|
2038
2121
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
2039
2122
|
const apiKey = options?.apiKey || getAPIKey();
|
2040
|
-
const cache = options?.cache ?? new SimpleCache({
|
2123
|
+
const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
|
2124
|
+
const trace = options?.trace ?? defaultTrace;
|
2041
2125
|
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
2042
|
-
if (!
|
2043
|
-
throw new Error("
|
2126
|
+
if (!apiKey) {
|
2127
|
+
throw new Error("Option apiKey is required");
|
2044
2128
|
}
|
2045
|
-
|
2046
|
-
|
2047
|
-
|
2048
|
-
apiKey,
|
2049
|
-
|
2050
|
-
branch
|
2051
|
-
}) {
|
2129
|
+
if (!databaseURL) {
|
2130
|
+
throw new Error("Option databaseURL is required");
|
2131
|
+
}
|
2132
|
+
return { fetch, databaseURL, apiKey, branch, cache, trace };
|
2133
|
+
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({ fetch, apiKey, databaseURL, branch, trace }) {
|
2052
2134
|
const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
|
2053
2135
|
if (!branchValue)
|
2054
2136
|
throw new Error("Unable to resolve branch value");
|
@@ -2060,7 +2142,8 @@ const buildClient = (plugins) => {
|
|
2060
2142
|
const hasBranch = params.dbBranchName ?? params.branch;
|
2061
2143
|
const newPath = path.replace(/^\/db\/[^/]+/, hasBranch ? `:${branchValue}` : "");
|
2062
2144
|
return databaseURL + newPath;
|
2063
|
-
}
|
2145
|
+
},
|
2146
|
+
trace
|
2064
2147
|
};
|
2065
2148
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
2066
2149
|
if (__privateGet(this, _branch))
|
@@ -2083,6 +2166,88 @@ const buildClient = (plugins) => {
|
|
2083
2166
|
class BaseClient extends buildClient() {
|
2084
2167
|
}
|
2085
2168
|
|
2169
|
+
const META = "__";
|
2170
|
+
const VALUE = "___";
|
2171
|
+
class Serializer {
|
2172
|
+
constructor() {
|
2173
|
+
this.classes = {};
|
2174
|
+
}
|
2175
|
+
add(clazz) {
|
2176
|
+
this.classes[clazz.name] = clazz;
|
2177
|
+
}
|
2178
|
+
toJSON(data) {
|
2179
|
+
function visit(obj) {
|
2180
|
+
if (Array.isArray(obj))
|
2181
|
+
return obj.map(visit);
|
2182
|
+
const type = typeof obj;
|
2183
|
+
if (type === "undefined")
|
2184
|
+
return { [META]: "undefined" };
|
2185
|
+
if (type === "bigint")
|
2186
|
+
return { [META]: "bigint", [VALUE]: obj.toString() };
|
2187
|
+
if (obj === null || type !== "object")
|
2188
|
+
return obj;
|
2189
|
+
const constructor = obj.constructor;
|
2190
|
+
const o = { [META]: constructor.name };
|
2191
|
+
for (const [key, value] of Object.entries(obj)) {
|
2192
|
+
o[key] = visit(value);
|
2193
|
+
}
|
2194
|
+
if (constructor === Date)
|
2195
|
+
o[VALUE] = obj.toISOString();
|
2196
|
+
if (constructor === Map)
|
2197
|
+
o[VALUE] = Object.fromEntries(obj);
|
2198
|
+
if (constructor === Set)
|
2199
|
+
o[VALUE] = [...obj];
|
2200
|
+
return o;
|
2201
|
+
}
|
2202
|
+
return JSON.stringify(visit(data));
|
2203
|
+
}
|
2204
|
+
fromJSON(json) {
|
2205
|
+
return JSON.parse(json, (key, value) => {
|
2206
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
2207
|
+
const { [META]: clazz, [VALUE]: val, ...rest } = value;
|
2208
|
+
const constructor = this.classes[clazz];
|
2209
|
+
if (constructor) {
|
2210
|
+
return Object.assign(Object.create(constructor.prototype), rest);
|
2211
|
+
}
|
2212
|
+
if (clazz === "Date")
|
2213
|
+
return new Date(val);
|
2214
|
+
if (clazz === "Set")
|
2215
|
+
return new Set(val);
|
2216
|
+
if (clazz === "Map")
|
2217
|
+
return new Map(Object.entries(val));
|
2218
|
+
if (clazz === "bigint")
|
2219
|
+
return BigInt(val);
|
2220
|
+
if (clazz === "undefined")
|
2221
|
+
return void 0;
|
2222
|
+
return rest;
|
2223
|
+
}
|
2224
|
+
return value;
|
2225
|
+
});
|
2226
|
+
}
|
2227
|
+
}
|
2228
|
+
const defaultSerializer = new Serializer();
|
2229
|
+
const serialize = (data) => {
|
2230
|
+
return defaultSerializer.toJSON(data);
|
2231
|
+
};
|
2232
|
+
const deserialize = (json) => {
|
2233
|
+
return defaultSerializer.fromJSON(json);
|
2234
|
+
};
|
2235
|
+
|
2236
|
+
function buildWorkerRunner(config) {
|
2237
|
+
return function xataWorker(name, _worker) {
|
2238
|
+
return async (...args) => {
|
2239
|
+
const url = process.env.NODE_ENV === "development" ? `http://localhost:64749/${name}` : `https://dispatcher.xata.workers.dev/${config.workspace}/${config.worker}/${name}`;
|
2240
|
+
const result = await fetch(url, {
|
2241
|
+
method: "POST",
|
2242
|
+
headers: { "Content-Type": "application/json" },
|
2243
|
+
body: serialize({ args })
|
2244
|
+
});
|
2245
|
+
const text = await result.text();
|
2246
|
+
return deserialize(text);
|
2247
|
+
};
|
2248
|
+
};
|
2249
|
+
}
|
2250
|
+
|
2086
2251
|
class XataError extends Error {
|
2087
2252
|
constructor(message, status) {
|
2088
2253
|
super(message);
|
@@ -2090,5 +2255,5 @@ class XataError extends Error {
|
|
2090
2255
|
}
|
2091
2256
|
}
|
2092
2257
|
|
2093
|
-
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, updateWorkspaceMemberInvite, updateWorkspaceMemberRole, upsertRecordWithID };
|
2258
|
+
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, RecordArray, Repository, RestRepository, SchemaPlugin, SearchPlugin, Serializer, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, buildWorkerRunner, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, deserialize, endsWith, equals, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseMetadata, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, greaterEquals, greaterThan, greaterThanEquals, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lessEquals, lessThan, lessThanEquals, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, serialize, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberInvite, updateWorkspaceMemberRole, upsertRecordWithID };
|
2094
2259
|
//# sourceMappingURL=index.mjs.map
|