@xata.io/client 0.0.0-alpha.vff52a72 → 0.0.0-alpha.vff9649a
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 +3 -2
- package/.turbo/turbo-add-version.log +4 -0
- package/.turbo/turbo-build.log +13 -0
- package/CHANGELOG.md +110 -0
- package/README.md +2 -2
- package/Usage.md +5 -5
- package/dist/index.cjs +439 -183
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1136 -812
- package/dist/index.mjs +438 -160
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -8
- package/rollup.config.mjs +44 -0
- package/rollup.config.js +0 -29
package/dist/index.cjs
CHANGED
@@ -1,27 +1,8 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
function _interopNamespace(e) {
|
6
|
-
if (e && e.__esModule) return e;
|
7
|
-
var n = Object.create(null);
|
8
|
-
if (e) {
|
9
|
-
Object.keys(e).forEach(function (k) {
|
10
|
-
if (k !== 'default') {
|
11
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
12
|
-
Object.defineProperty(n, k, d.get ? d : {
|
13
|
-
enumerable: true,
|
14
|
-
get: function () { return e[k]; }
|
15
|
-
});
|
16
|
-
}
|
17
|
-
});
|
18
|
-
}
|
19
|
-
n["default"] = e;
|
20
|
-
return Object.freeze(n);
|
21
|
-
}
|
22
|
-
|
23
|
-
const defaultTrace = async (_name, fn, _options) => {
|
3
|
+
const defaultTrace = async (name, fn, _options) => {
|
24
4
|
return await fn({
|
5
|
+
name,
|
25
6
|
setAttributes: () => {
|
26
7
|
return;
|
27
8
|
}
|
@@ -63,6 +44,18 @@ function isStringArray(value) {
|
|
63
44
|
function isNumber(value) {
|
64
45
|
return isDefined(value) && typeof value === "number";
|
65
46
|
}
|
47
|
+
function parseNumber(value) {
|
48
|
+
if (isNumber(value)) {
|
49
|
+
return value;
|
50
|
+
}
|
51
|
+
if (isString(value)) {
|
52
|
+
const parsed = Number(value);
|
53
|
+
if (!Number.isNaN(parsed)) {
|
54
|
+
return parsed;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
return void 0;
|
58
|
+
}
|
66
59
|
function toBase64(value) {
|
67
60
|
try {
|
68
61
|
return btoa(value);
|
@@ -82,10 +75,20 @@ function deepMerge(a, b) {
|
|
82
75
|
}
|
83
76
|
return result;
|
84
77
|
}
|
78
|
+
function chunk(array, chunkSize) {
|
79
|
+
const result = [];
|
80
|
+
for (let i = 0; i < array.length; i += chunkSize) {
|
81
|
+
result.push(array.slice(i, i + chunkSize));
|
82
|
+
}
|
83
|
+
return result;
|
84
|
+
}
|
85
|
+
async function timeout(ms) {
|
86
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
87
|
+
}
|
85
88
|
|
86
89
|
function getEnvironment() {
|
87
90
|
try {
|
88
|
-
if (
|
91
|
+
if (isDefined(process) && isDefined(process.env)) {
|
89
92
|
return {
|
90
93
|
apiKey: process.env.XATA_API_KEY ?? getGlobalApiKey(),
|
91
94
|
databaseURL: process.env.XATA_DATABASE_URL ?? getGlobalDatabaseURL(),
|
@@ -116,6 +119,25 @@ function getEnvironment() {
|
|
116
119
|
fallbackBranch: getGlobalFallbackBranch()
|
117
120
|
};
|
118
121
|
}
|
122
|
+
function getEnableBrowserVariable() {
|
123
|
+
try {
|
124
|
+
if (isObject(process) && isObject(process.env) && process.env.XATA_ENABLE_BROWSER !== void 0) {
|
125
|
+
return process.env.XATA_ENABLE_BROWSER === "true";
|
126
|
+
}
|
127
|
+
} catch (err) {
|
128
|
+
}
|
129
|
+
try {
|
130
|
+
if (isObject(Deno) && isObject(Deno.env) && Deno.env.get("XATA_ENABLE_BROWSER") !== void 0) {
|
131
|
+
return Deno.env.get("XATA_ENABLE_BROWSER") === "true";
|
132
|
+
}
|
133
|
+
} catch (err) {
|
134
|
+
}
|
135
|
+
try {
|
136
|
+
return XATA_ENABLE_BROWSER === true || XATA_ENABLE_BROWSER === "true";
|
137
|
+
} catch (err) {
|
138
|
+
return void 0;
|
139
|
+
}
|
140
|
+
}
|
119
141
|
function getGlobalApiKey() {
|
120
142
|
try {
|
121
143
|
return XATA_API_KEY;
|
@@ -153,8 +175,6 @@ async function getGitBranch() {
|
|
153
175
|
if (typeof require === "function") {
|
154
176
|
return require(nodeModule).execSync(fullCmd, execOptions).trim();
|
155
177
|
}
|
156
|
-
const { execSync } = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(nodeModule);
|
157
|
-
return execSync(fullCmd, execOptions).toString().trim();
|
158
178
|
} catch (err) {
|
159
179
|
}
|
160
180
|
try {
|
@@ -175,6 +195,29 @@ function getAPIKey() {
|
|
175
195
|
}
|
176
196
|
}
|
177
197
|
|
198
|
+
var __accessCheck$8 = (obj, member, msg) => {
|
199
|
+
if (!member.has(obj))
|
200
|
+
throw TypeError("Cannot " + msg);
|
201
|
+
};
|
202
|
+
var __privateGet$8 = (obj, member, getter) => {
|
203
|
+
__accessCheck$8(obj, member, "read from private field");
|
204
|
+
return getter ? getter.call(obj) : member.get(obj);
|
205
|
+
};
|
206
|
+
var __privateAdd$8 = (obj, member, value) => {
|
207
|
+
if (member.has(obj))
|
208
|
+
throw TypeError("Cannot add the same private member more than once");
|
209
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
210
|
+
};
|
211
|
+
var __privateSet$8 = (obj, member, value, setter) => {
|
212
|
+
__accessCheck$8(obj, member, "write to private field");
|
213
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
214
|
+
return value;
|
215
|
+
};
|
216
|
+
var __privateMethod$4 = (obj, member, method) => {
|
217
|
+
__accessCheck$8(obj, member, "access private method");
|
218
|
+
return method;
|
219
|
+
};
|
220
|
+
var _fetch, _queue, _concurrency, _enqueue, enqueue_fn;
|
178
221
|
function getFetchImplementation(userFetch) {
|
179
222
|
const globalFetch = typeof fetch !== "undefined" ? fetch : void 0;
|
180
223
|
const fetchImpl = userFetch ?? globalFetch;
|
@@ -185,8 +228,81 @@ function getFetchImplementation(userFetch) {
|
|
185
228
|
}
|
186
229
|
return fetchImpl;
|
187
230
|
}
|
231
|
+
class ApiRequestPool {
|
232
|
+
constructor(concurrency = 10) {
|
233
|
+
__privateAdd$8(this, _enqueue);
|
234
|
+
__privateAdd$8(this, _fetch, void 0);
|
235
|
+
__privateAdd$8(this, _queue, void 0);
|
236
|
+
__privateAdd$8(this, _concurrency, void 0);
|
237
|
+
__privateSet$8(this, _queue, []);
|
238
|
+
__privateSet$8(this, _concurrency, concurrency);
|
239
|
+
this.running = 0;
|
240
|
+
this.started = 0;
|
241
|
+
}
|
242
|
+
setFetch(fetch2) {
|
243
|
+
__privateSet$8(this, _fetch, fetch2);
|
244
|
+
}
|
245
|
+
getFetch() {
|
246
|
+
if (!__privateGet$8(this, _fetch)) {
|
247
|
+
throw new Error("Fetch not set");
|
248
|
+
}
|
249
|
+
return __privateGet$8(this, _fetch);
|
250
|
+
}
|
251
|
+
request(url, options) {
|
252
|
+
const start = new Date();
|
253
|
+
const fetch2 = this.getFetch();
|
254
|
+
const runRequest = async (stalled = false) => {
|
255
|
+
const response = await fetch2(url, options);
|
256
|
+
if (response.status === 429) {
|
257
|
+
const rateLimitReset = parseNumber(response.headers?.get("x-ratelimit-reset")) ?? 1;
|
258
|
+
await timeout(rateLimitReset * 1e3);
|
259
|
+
return await runRequest(true);
|
260
|
+
}
|
261
|
+
if (stalled) {
|
262
|
+
const stalledTime = new Date().getTime() - start.getTime();
|
263
|
+
console.warn(`A request to Xata hit your workspace limits, was retried and stalled for ${stalledTime}ms`);
|
264
|
+
}
|
265
|
+
return response;
|
266
|
+
};
|
267
|
+
return __privateMethod$4(this, _enqueue, enqueue_fn).call(this, async () => {
|
268
|
+
return await runRequest();
|
269
|
+
});
|
270
|
+
}
|
271
|
+
}
|
272
|
+
_fetch = new WeakMap();
|
273
|
+
_queue = new WeakMap();
|
274
|
+
_concurrency = new WeakMap();
|
275
|
+
_enqueue = new WeakSet();
|
276
|
+
enqueue_fn = function(task) {
|
277
|
+
const promise = new Promise((resolve) => __privateGet$8(this, _queue).push(resolve)).finally(() => {
|
278
|
+
this.started--;
|
279
|
+
this.running++;
|
280
|
+
}).then(() => task()).finally(() => {
|
281
|
+
this.running--;
|
282
|
+
const next = __privateGet$8(this, _queue).shift();
|
283
|
+
if (next !== void 0) {
|
284
|
+
this.started++;
|
285
|
+
next();
|
286
|
+
}
|
287
|
+
});
|
288
|
+
if (this.running + this.started < __privateGet$8(this, _concurrency)) {
|
289
|
+
const next = __privateGet$8(this, _queue).shift();
|
290
|
+
if (next !== void 0) {
|
291
|
+
this.started++;
|
292
|
+
next();
|
293
|
+
}
|
294
|
+
}
|
295
|
+
return promise;
|
296
|
+
};
|
297
|
+
|
298
|
+
function generateUUID() {
|
299
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
300
|
+
const r = Math.random() * 16 | 0, v = c == "x" ? r : r & 3 | 8;
|
301
|
+
return v.toString(16);
|
302
|
+
});
|
303
|
+
}
|
188
304
|
|
189
|
-
const VERSION = "0.
|
305
|
+
const VERSION = "0.21.6";
|
190
306
|
|
191
307
|
class ErrorWithCause extends Error {
|
192
308
|
constructor(message, options) {
|
@@ -197,7 +313,7 @@ class FetcherError extends ErrorWithCause {
|
|
197
313
|
constructor(status, data, requestId) {
|
198
314
|
super(getMessage(data));
|
199
315
|
this.status = status;
|
200
|
-
this.errors = isBulkError(data) ? data.errors :
|
316
|
+
this.errors = isBulkError(data) ? data.errors : [{ message: getMessage(data), status }];
|
201
317
|
this.requestId = requestId;
|
202
318
|
if (data instanceof Error) {
|
203
319
|
this.stack = data.stack;
|
@@ -229,6 +345,7 @@ function getMessage(data) {
|
|
229
345
|
}
|
230
346
|
}
|
231
347
|
|
348
|
+
const pool = new ApiRequestPool();
|
232
349
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
233
350
|
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
234
351
|
if (value === void 0 || value === null)
|
@@ -261,11 +378,12 @@ function hostHeader(url) {
|
|
261
378
|
const { groups } = pattern.exec(url) ?? {};
|
262
379
|
return groups?.host ? { Host: groups.host } : {};
|
263
380
|
}
|
381
|
+
const defaultClientID = generateUUID();
|
264
382
|
async function fetch$1({
|
265
383
|
url: path,
|
266
384
|
method,
|
267
385
|
body,
|
268
|
-
headers,
|
386
|
+
headers: customHeaders,
|
269
387
|
pathParams,
|
270
388
|
queryParams,
|
271
389
|
fetchImpl,
|
@@ -276,9 +394,12 @@ async function fetch$1({
|
|
276
394
|
trace,
|
277
395
|
signal,
|
278
396
|
clientID,
|
279
|
-
sessionID
|
397
|
+
sessionID,
|
398
|
+
clientName,
|
399
|
+
fetchOptions = {}
|
280
400
|
}) {
|
281
|
-
|
401
|
+
pool.setFetch(fetchImpl);
|
402
|
+
return await trace(
|
282
403
|
`${method.toUpperCase()} ${path}`,
|
283
404
|
async ({ setAttributes }) => {
|
284
405
|
const baseUrl = buildBaseUrl({ endpoint, path, workspacesApiUrl, pathParams, apiUrl });
|
@@ -288,23 +409,28 @@ async function fetch$1({
|
|
288
409
|
[TraceAttributes.HTTP_URL]: url,
|
289
410
|
[TraceAttributes.HTTP_TARGET]: resolveUrl(path, queryParams, pathParams)
|
290
411
|
});
|
291
|
-
const
|
412
|
+
const xataAgent = compact([
|
413
|
+
["client", "TS_SDK"],
|
414
|
+
["version", VERSION],
|
415
|
+
isDefined(clientName) ? ["service", clientName] : void 0
|
416
|
+
]).map(([key, value]) => `${key}=${value}`).join("; ");
|
417
|
+
const headers = {
|
418
|
+
"Accept-Encoding": "identity",
|
419
|
+
"Content-Type": "application/json",
|
420
|
+
"X-Xata-Client-ID": clientID ?? defaultClientID,
|
421
|
+
"X-Xata-Session-ID": sessionID ?? generateUUID(),
|
422
|
+
"X-Xata-Agent": xataAgent,
|
423
|
+
...customHeaders,
|
424
|
+
...hostHeader(fullUrl),
|
425
|
+
Authorization: `Bearer ${apiKey}`
|
426
|
+
};
|
427
|
+
const response = await pool.request(url, {
|
428
|
+
...fetchOptions,
|
292
429
|
method: method.toUpperCase(),
|
293
430
|
body: body ? JSON.stringify(body) : void 0,
|
294
|
-
headers
|
295
|
-
"Content-Type": "application/json",
|
296
|
-
"User-Agent": `Xata client-ts/${VERSION}`,
|
297
|
-
"X-Xata-Client-ID": clientID ?? "",
|
298
|
-
"X-Xata-Session-ID": sessionID ?? "",
|
299
|
-
...headers,
|
300
|
-
...hostHeader(fullUrl),
|
301
|
-
Authorization: `Bearer ${apiKey}`
|
302
|
-
},
|
431
|
+
headers,
|
303
432
|
signal
|
304
433
|
});
|
305
|
-
if (response.status === 204) {
|
306
|
-
return {};
|
307
|
-
}
|
308
434
|
const { host, protocol } = parseUrl(response.url);
|
309
435
|
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
310
436
|
setAttributes({
|
@@ -314,6 +440,12 @@ async function fetch$1({
|
|
314
440
|
[TraceAttributes.HTTP_HOST]: host,
|
315
441
|
[TraceAttributes.HTTP_SCHEME]: protocol?.replace(":", "")
|
316
442
|
});
|
443
|
+
if (response.status === 204) {
|
444
|
+
return {};
|
445
|
+
}
|
446
|
+
if (response.status === 429) {
|
447
|
+
throw new FetcherError(response.status, "Rate limit exceeded", requestId);
|
448
|
+
}
|
317
449
|
try {
|
318
450
|
const jsonResponse = await response.json();
|
319
451
|
if (response.ok) {
|
@@ -338,17 +470,12 @@ function parseUrl(url) {
|
|
338
470
|
|
339
471
|
const dataPlaneFetch = async (options) => fetch$1({ ...options, endpoint: "dataPlane" });
|
340
472
|
|
341
|
-
const dEPRECATEDgetDatabaseList = (variables, signal) => dataPlaneFetch({ url: "/dbs", method: "get", ...variables, signal });
|
342
473
|
const getBranchList = (variables, signal) => dataPlaneFetch({
|
343
474
|
url: "/dbs/{dbName}",
|
344
475
|
method: "get",
|
345
476
|
...variables,
|
346
477
|
signal
|
347
478
|
});
|
348
|
-
const dEPRECATEDcreateDatabase = (variables, signal) => dataPlaneFetch({ url: "/dbs/{dbName}", method: "put", ...variables, signal });
|
349
|
-
const dEPRECATEDdeleteDatabase = (variables, signal) => dataPlaneFetch({ url: "/dbs/{dbName}", method: "delete", ...variables, signal });
|
350
|
-
const dEPRECATEDgetDatabaseMetadata = (variables, signal) => dataPlaneFetch({ url: "/dbs/{dbName}/metadata", method: "get", ...variables, signal });
|
351
|
-
const dEPRECATEDupdateDatabaseMetadata = (variables, signal) => dataPlaneFetch({ url: "/dbs/{dbName}/metadata", method: "patch", ...variables, signal });
|
352
479
|
const getBranchDetails = (variables, signal) => dataPlaneFetch({
|
353
480
|
url: "/db/{dbBranchName}",
|
354
481
|
method: "get",
|
@@ -387,6 +514,7 @@ const resolveBranch = (variables, signal) => dataPlaneFetch({ url: "/dbs/{dbName
|
|
387
514
|
const getBranchMigrationHistory = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/migrations", method: "get", ...variables, signal });
|
388
515
|
const getBranchMigrationPlan = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/migrations/plan", method: "post", ...variables, signal });
|
389
516
|
const executeBranchMigrationPlan = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/migrations/execute", method: "post", ...variables, signal });
|
517
|
+
const branchTransaction = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/transaction", method: "post", ...variables, signal });
|
390
518
|
const queryMigrationRequests = (variables, signal) => dataPlaneFetch({ url: "/dbs/{dbName}/migrations/query", method: "post", ...variables, signal });
|
391
519
|
const createMigrationRequest = (variables, signal) => dataPlaneFetch({ url: "/dbs/{dbName}/migrations", method: "post", ...variables, signal });
|
392
520
|
const getMigrationRequest = (variables, signal) => dataPlaneFetch({
|
@@ -486,13 +614,6 @@ const searchTable = (variables, signal) => dataPlaneFetch({
|
|
486
614
|
const summarizeTable = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/tables/{tableName}/summarize", method: "post", ...variables, signal });
|
487
615
|
const aggregateTable = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/tables/{tableName}/aggregate", method: "post", ...variables, signal });
|
488
616
|
const operationsByTag$2 = {
|
489
|
-
database: {
|
490
|
-
dEPRECATEDgetDatabaseList,
|
491
|
-
dEPRECATEDcreateDatabase,
|
492
|
-
dEPRECATEDdeleteDatabase,
|
493
|
-
dEPRECATEDgetDatabaseMetadata,
|
494
|
-
dEPRECATEDupdateDatabaseMetadata
|
495
|
-
},
|
496
617
|
branch: {
|
497
618
|
getBranchList,
|
498
619
|
getBranchDetails,
|
@@ -517,6 +638,16 @@ const operationsByTag$2 = {
|
|
517
638
|
previewBranchSchemaEdit,
|
518
639
|
applyBranchSchemaEdit
|
519
640
|
},
|
641
|
+
records: {
|
642
|
+
branchTransaction,
|
643
|
+
insertRecord,
|
644
|
+
getRecord,
|
645
|
+
insertRecordWithID,
|
646
|
+
updateRecordWithID,
|
647
|
+
upsertRecordWithID,
|
648
|
+
deleteRecord,
|
649
|
+
bulkInsertTableRecords
|
650
|
+
},
|
520
651
|
migrationRequests: {
|
521
652
|
queryMigrationRequests,
|
522
653
|
createMigrationRequest,
|
@@ -539,15 +670,6 @@ const operationsByTag$2 = {
|
|
539
670
|
updateColumn,
|
540
671
|
deleteColumn
|
541
672
|
},
|
542
|
-
records: {
|
543
|
-
insertRecord,
|
544
|
-
getRecord,
|
545
|
-
insertRecordWithID,
|
546
|
-
updateRecordWithID,
|
547
|
-
upsertRecordWithID,
|
548
|
-
deleteRecord,
|
549
|
-
bulkInsertTableRecords
|
550
|
-
},
|
551
673
|
searchAndFilter: { queryTable, searchBranch, searchTable, summarizeTable, aggregateTable }
|
552
674
|
};
|
553
675
|
|
@@ -721,12 +843,12 @@ function parseProviderString(provider = "production") {
|
|
721
843
|
function parseWorkspacesUrlParts(url) {
|
722
844
|
if (!isString(url))
|
723
845
|
return null;
|
724
|
-
const regex = /(?:https:\/\/)?([^.]+)(?:\.([^.]+))
|
725
|
-
const regexStaging = /(?:https:\/\/)?([^.]+)\.staging(?:\.([^.]+))
|
846
|
+
const regex = /(?:https:\/\/)?([^.]+)(?:\.([^.]+))\.xata\.sh.*/;
|
847
|
+
const regexStaging = /(?:https:\/\/)?([^.]+)\.staging(?:\.([^.]+))\.xatabase\.co.*/;
|
726
848
|
const match = url.match(regex) || url.match(regexStaging);
|
727
849
|
if (!match)
|
728
850
|
return null;
|
729
|
-
return { workspace: match[1], region: match[2]
|
851
|
+
return { workspace: match[1], region: match[2] };
|
730
852
|
}
|
731
853
|
|
732
854
|
var __accessCheck$7 = (obj, member, msg) => {
|
@@ -755,6 +877,7 @@ class XataApiClient {
|
|
755
877
|
const provider = options.host ?? "production";
|
756
878
|
const apiKey = options.apiKey ?? getAPIKey();
|
757
879
|
const trace = options.trace ?? defaultTrace;
|
880
|
+
const clientID = generateUUID();
|
758
881
|
if (!apiKey) {
|
759
882
|
throw new Error("Could not resolve a valid apiKey");
|
760
883
|
}
|
@@ -763,7 +886,9 @@ class XataApiClient {
|
|
763
886
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
764
887
|
fetchImpl: getFetchImplementation(options.fetch),
|
765
888
|
apiKey,
|
766
|
-
trace
|
889
|
+
trace,
|
890
|
+
clientName: options.clientName,
|
891
|
+
clientID
|
767
892
|
});
|
768
893
|
}
|
769
894
|
get user() {
|
@@ -1365,6 +1490,19 @@ class RecordsApi {
|
|
1365
1490
|
...this.extraProps
|
1366
1491
|
});
|
1367
1492
|
}
|
1493
|
+
branchTransaction({
|
1494
|
+
workspace,
|
1495
|
+
region,
|
1496
|
+
database,
|
1497
|
+
branch,
|
1498
|
+
operations
|
1499
|
+
}) {
|
1500
|
+
return operationsByTag.records.branchTransaction({
|
1501
|
+
pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
|
1502
|
+
body: { operations },
|
1503
|
+
...this.extraProps
|
1504
|
+
});
|
1505
|
+
}
|
1368
1506
|
}
|
1369
1507
|
class SearchAndFilterApi {
|
1370
1508
|
constructor(extraProps) {
|
@@ -1379,11 +1517,12 @@ class SearchAndFilterApi {
|
|
1379
1517
|
filter,
|
1380
1518
|
sort,
|
1381
1519
|
page,
|
1382
|
-
columns
|
1520
|
+
columns,
|
1521
|
+
consistency
|
1383
1522
|
}) {
|
1384
1523
|
return operationsByTag.searchAndFilter.queryTable({
|
1385
1524
|
pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table },
|
1386
|
-
body: { filter, sort, page, columns },
|
1525
|
+
body: { filter, sort, page, columns, consistency },
|
1387
1526
|
...this.extraProps
|
1388
1527
|
});
|
1389
1528
|
}
|
@@ -1435,11 +1574,12 @@ class SearchAndFilterApi {
|
|
1435
1574
|
summaries,
|
1436
1575
|
sort,
|
1437
1576
|
summariesFilter,
|
1438
|
-
page
|
1577
|
+
page,
|
1578
|
+
consistency
|
1439
1579
|
}) {
|
1440
1580
|
return operationsByTag.searchAndFilter.summarizeTable({
|
1441
1581
|
pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table },
|
1442
|
-
body: { filter, columns, summaries, sort, summariesFilter, page },
|
1582
|
+
body: { filter, columns, summaries, sort, summariesFilter, page, consistency },
|
1443
1583
|
...this.extraProps
|
1444
1584
|
});
|
1445
1585
|
}
|
@@ -1753,13 +1893,6 @@ class XataApiPlugin {
|
|
1753
1893
|
class XataPlugin {
|
1754
1894
|
}
|
1755
1895
|
|
1756
|
-
function generateUUID() {
|
1757
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
|
1758
|
-
const r = Math.random() * 16 | 0, v = c == "x" ? r : r & 3 | 8;
|
1759
|
-
return v.toString(16);
|
1760
|
-
});
|
1761
|
-
}
|
1762
|
-
|
1763
1896
|
function cleanFilter(filter) {
|
1764
1897
|
if (!filter)
|
1765
1898
|
return void 0;
|
@@ -1799,11 +1932,11 @@ class Page {
|
|
1799
1932
|
async previousPage(size, offset) {
|
1800
1933
|
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
1801
1934
|
}
|
1802
|
-
async
|
1803
|
-
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset,
|
1935
|
+
async startPage(size, offset) {
|
1936
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, start: this.meta.page.cursor } });
|
1804
1937
|
}
|
1805
|
-
async
|
1806
|
-
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset,
|
1938
|
+
async endPage(size, offset) {
|
1939
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, end: this.meta.page.cursor } });
|
1807
1940
|
}
|
1808
1941
|
hasNextPage() {
|
1809
1942
|
return this.meta.page.more;
|
@@ -1815,7 +1948,7 @@ const PAGINATION_DEFAULT_SIZE = 20;
|
|
1815
1948
|
const PAGINATION_MAX_OFFSET = 800;
|
1816
1949
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
1817
1950
|
function isCursorPaginationOptions(options) {
|
1818
|
-
return isDefined(options) && (isDefined(options.
|
1951
|
+
return isDefined(options) && (isDefined(options.start) || isDefined(options.end) || isDefined(options.after) || isDefined(options.before));
|
1819
1952
|
}
|
1820
1953
|
const _RecordArray = class extends Array {
|
1821
1954
|
constructor(...args) {
|
@@ -1847,12 +1980,12 @@ const _RecordArray = class extends Array {
|
|
1847
1980
|
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1848
1981
|
return new _RecordArray(newPage);
|
1849
1982
|
}
|
1850
|
-
async
|
1851
|
-
const newPage = await __privateGet$6(this, _page).
|
1983
|
+
async startPage(size, offset) {
|
1984
|
+
const newPage = await __privateGet$6(this, _page).startPage(size, offset);
|
1852
1985
|
return new _RecordArray(newPage);
|
1853
1986
|
}
|
1854
|
-
async
|
1855
|
-
const newPage = await __privateGet$6(this, _page).
|
1987
|
+
async endPage(size, offset) {
|
1988
|
+
const newPage = await __privateGet$6(this, _page).endPage(size, offset);
|
1856
1989
|
return new _RecordArray(newPage);
|
1857
1990
|
}
|
1858
1991
|
hasNextPage() {
|
@@ -1907,8 +2040,10 @@ const _Query = class {
|
|
1907
2040
|
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
1908
2041
|
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
1909
2042
|
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns;
|
2043
|
+
__privateGet$5(this, _data).consistency = data.consistency ?? parent?.consistency;
|
1910
2044
|
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
1911
2045
|
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
2046
|
+
__privateGet$5(this, _data).fetchOptions = data.fetchOptions ?? parent?.fetchOptions;
|
1912
2047
|
this.any = this.any.bind(this);
|
1913
2048
|
this.all = this.all.bind(this);
|
1914
2049
|
this.not = this.not.bind(this);
|
@@ -2036,15 +2171,15 @@ const _Query = class {
|
|
2036
2171
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
2037
2172
|
}
|
2038
2173
|
nextPage(size, offset) {
|
2039
|
-
return this.
|
2174
|
+
return this.startPage(size, offset);
|
2040
2175
|
}
|
2041
2176
|
previousPage(size, offset) {
|
2042
|
-
return this.
|
2177
|
+
return this.startPage(size, offset);
|
2043
2178
|
}
|
2044
|
-
|
2179
|
+
startPage(size, offset) {
|
2045
2180
|
return this.getPaginated({ pagination: { size, offset } });
|
2046
2181
|
}
|
2047
|
-
|
2182
|
+
endPage(size, offset) {
|
2048
2183
|
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
2049
2184
|
}
|
2050
2185
|
hasNextPage() {
|
@@ -2127,7 +2262,8 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
2127
2262
|
__accessCheck$4(obj, member, "access private method");
|
2128
2263
|
return method;
|
2129
2264
|
};
|
2130
|
-
var _table, _getFetchProps, _db, _cache, _schemaTables$2, _trace, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn,
|
2265
|
+
var _table, _getFetchProps, _db, _cache, _schemaTables$2, _trace, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _insertRecords, insertRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _updateRecords, updateRecords_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _deleteRecords, deleteRecords_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn, _getSchemaTables$1, getSchemaTables_fn$1;
|
2266
|
+
const BULK_OPERATION_MAX_SIZE = 1e3;
|
2131
2267
|
class Repository extends Query {
|
2132
2268
|
}
|
2133
2269
|
class RestRepository extends Query {
|
@@ -2139,10 +2275,12 @@ class RestRepository extends Query {
|
|
2139
2275
|
);
|
2140
2276
|
__privateAdd$4(this, _insertRecordWithoutId);
|
2141
2277
|
__privateAdd$4(this, _insertRecordWithId);
|
2142
|
-
__privateAdd$4(this,
|
2278
|
+
__privateAdd$4(this, _insertRecords);
|
2143
2279
|
__privateAdd$4(this, _updateRecordWithID);
|
2280
|
+
__privateAdd$4(this, _updateRecords);
|
2144
2281
|
__privateAdd$4(this, _upsertRecordWithID);
|
2145
2282
|
__privateAdd$4(this, _deleteRecord);
|
2283
|
+
__privateAdd$4(this, _deleteRecords);
|
2146
2284
|
__privateAdd$4(this, _setCacheQuery);
|
2147
2285
|
__privateAdd$4(this, _getCacheQuery);
|
2148
2286
|
__privateAdd$4(this, _getSchemaTables$1);
|
@@ -2176,20 +2314,22 @@ class RestRepository extends Query {
|
|
2176
2314
|
if (Array.isArray(a)) {
|
2177
2315
|
if (a.length === 0)
|
2178
2316
|
return [];
|
2179
|
-
const
|
2180
|
-
|
2317
|
+
const ids = await __privateMethod$2(this, _insertRecords, insertRecords_fn).call(this, a, { ifVersion, createOnly: true });
|
2318
|
+
const columns = isStringArray(b) ? b : ["*"];
|
2319
|
+
const result = await this.read(ids, columns);
|
2320
|
+
return result;
|
2181
2321
|
}
|
2182
2322
|
if (isString(a) && isObject(b)) {
|
2183
2323
|
if (a === "")
|
2184
2324
|
throw new Error("The id can't be empty");
|
2185
2325
|
const columns = isStringArray(c) ? c : void 0;
|
2186
|
-
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns, { createOnly: true, ifVersion });
|
2326
|
+
return await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a, b, columns, { createOnly: true, ifVersion });
|
2187
2327
|
}
|
2188
2328
|
if (isObject(a) && isString(a.id)) {
|
2189
2329
|
if (a.id === "")
|
2190
2330
|
throw new Error("The id can't be empty");
|
2191
2331
|
const columns = isStringArray(b) ? b : void 0;
|
2192
|
-
return __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns, { createOnly: true, ifVersion });
|
2332
|
+
return await __privateMethod$2(this, _insertRecordWithId, insertRecordWithId_fn).call(this, a.id, { ...a, id: void 0 }, columns, { createOnly: true, ifVersion });
|
2193
2333
|
}
|
2194
2334
|
if (isObject(a)) {
|
2195
2335
|
const columns = isStringArray(b) ? b : void 0;
|
@@ -2264,19 +2404,29 @@ class RestRepository extends Query {
|
|
2264
2404
|
if (Array.isArray(a)) {
|
2265
2405
|
if (a.length === 0)
|
2266
2406
|
return [];
|
2267
|
-
|
2268
|
-
|
2269
|
-
|
2407
|
+
const existing = await this.read(a, ["id"]);
|
2408
|
+
const updates = a.filter((_item, index) => existing[index] !== null);
|
2409
|
+
await __privateMethod$2(this, _updateRecords, updateRecords_fn).call(this, updates, {
|
2410
|
+
ifVersion,
|
2411
|
+
upsert: false
|
2412
|
+
});
|
2270
2413
|
const columns = isStringArray(b) ? b : ["*"];
|
2271
|
-
|
2272
|
-
|
2273
|
-
if (isString(a) && isObject(b)) {
|
2274
|
-
const columns = isStringArray(c) ? c : void 0;
|
2275
|
-
return __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns, { ifVersion });
|
2414
|
+
const result = await this.read(a, columns);
|
2415
|
+
return result;
|
2276
2416
|
}
|
2277
|
-
|
2278
|
-
|
2279
|
-
|
2417
|
+
try {
|
2418
|
+
if (isString(a) && isObject(b)) {
|
2419
|
+
const columns = isStringArray(c) ? c : void 0;
|
2420
|
+
return await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a, b, columns, { ifVersion });
|
2421
|
+
}
|
2422
|
+
if (isObject(a) && isString(a.id)) {
|
2423
|
+
const columns = isStringArray(b) ? b : void 0;
|
2424
|
+
return await __privateMethod$2(this, _updateRecordWithID, updateRecordWithID_fn).call(this, a.id, { ...a, id: void 0 }, columns, { ifVersion });
|
2425
|
+
}
|
2426
|
+
} catch (error) {
|
2427
|
+
if (error.status === 422)
|
2428
|
+
return null;
|
2429
|
+
throw error;
|
2280
2430
|
}
|
2281
2431
|
throw new Error("Invalid arguments for update method");
|
2282
2432
|
});
|
@@ -2306,11 +2456,13 @@ class RestRepository extends Query {
|
|
2306
2456
|
if (Array.isArray(a)) {
|
2307
2457
|
if (a.length === 0)
|
2308
2458
|
return [];
|
2309
|
-
|
2310
|
-
|
2311
|
-
|
2459
|
+
await __privateMethod$2(this, _updateRecords, updateRecords_fn).call(this, a, {
|
2460
|
+
ifVersion,
|
2461
|
+
upsert: true
|
2462
|
+
});
|
2312
2463
|
const columns = isStringArray(b) ? b : ["*"];
|
2313
|
-
|
2464
|
+
const result = await this.read(a, columns);
|
2465
|
+
return result;
|
2314
2466
|
}
|
2315
2467
|
if (isString(a) && isObject(b)) {
|
2316
2468
|
const columns = isStringArray(c) ? c : void 0;
|
@@ -2329,8 +2481,10 @@ class RestRepository extends Query {
|
|
2329
2481
|
if (Array.isArray(a)) {
|
2330
2482
|
if (a.length === 0)
|
2331
2483
|
return [];
|
2484
|
+
const ids = await __privateMethod$2(this, _insertRecords, insertRecords_fn).call(this, a, { ifVersion, createOnly: false });
|
2332
2485
|
const columns = isStringArray(b) ? b : ["*"];
|
2333
|
-
|
2486
|
+
const result = await this.read(ids, columns);
|
2487
|
+
return result;
|
2334
2488
|
}
|
2335
2489
|
if (isString(a) && isObject(b)) {
|
2336
2490
|
const columns = isStringArray(c) ? c : void 0;
|
@@ -2348,10 +2502,17 @@ class RestRepository extends Query {
|
|
2348
2502
|
if (Array.isArray(a)) {
|
2349
2503
|
if (a.length === 0)
|
2350
2504
|
return [];
|
2351
|
-
|
2352
|
-
|
2353
|
-
|
2354
|
-
|
2505
|
+
const ids = a.map((o) => {
|
2506
|
+
if (isString(o))
|
2507
|
+
return o;
|
2508
|
+
if (isString(o.id))
|
2509
|
+
return o.id;
|
2510
|
+
throw new Error("Invalid arguments for delete method");
|
2511
|
+
});
|
2512
|
+
const columns = isStringArray(b) ? b : ["*"];
|
2513
|
+
const result = await this.read(a, columns);
|
2514
|
+
await __privateMethod$2(this, _deleteRecords, deleteRecords_fn).call(this, ids);
|
2515
|
+
return result;
|
2355
2516
|
}
|
2356
2517
|
if (isString(a)) {
|
2357
2518
|
return __privateMethod$2(this, _deleteRecord, deleteRecord_fn).call(this, a, b);
|
@@ -2396,7 +2557,9 @@ class RestRepository extends Query {
|
|
2396
2557
|
prefix: options.prefix,
|
2397
2558
|
highlight: options.highlight,
|
2398
2559
|
filter: options.filter,
|
2399
|
-
boosters: options.boosters
|
2560
|
+
boosters: options.boosters,
|
2561
|
+
page: options.page,
|
2562
|
+
target: options.target
|
2400
2563
|
},
|
2401
2564
|
...fetchProps
|
2402
2565
|
});
|
@@ -2438,8 +2601,10 @@ class RestRepository extends Query {
|
|
2438
2601
|
filter: cleanFilter(data.filter),
|
2439
2602
|
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
2440
2603
|
page: data.pagination,
|
2441
|
-
columns: data.columns ?? ["*"]
|
2604
|
+
columns: data.columns ?? ["*"],
|
2605
|
+
consistency: data.consistency
|
2442
2606
|
},
|
2607
|
+
fetchOptions: data.fetchOptions,
|
2443
2608
|
...fetchProps
|
2444
2609
|
});
|
2445
2610
|
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
@@ -2465,6 +2630,7 @@ class RestRepository extends Query {
|
|
2465
2630
|
filter: cleanFilter(data.filter),
|
2466
2631
|
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
2467
2632
|
columns: data.columns,
|
2633
|
+
consistency: data.consistency,
|
2468
2634
|
page: data.pagination?.size !== void 0 ? { size: data.pagination?.size } : void 0,
|
2469
2635
|
summaries,
|
2470
2636
|
summariesFilter
|
@@ -2518,31 +2684,40 @@ insertRecordWithId_fn = async function(recordId, object, columns = ["*"], { crea
|
|
2518
2684
|
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
2519
2685
|
return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response, columns);
|
2520
2686
|
};
|
2521
|
-
|
2522
|
-
|
2687
|
+
_insertRecords = new WeakSet();
|
2688
|
+
insertRecords_fn = async function(objects, { createOnly, ifVersion }) {
|
2523
2689
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
2524
|
-
const
|
2525
|
-
|
2526
|
-
|
2527
|
-
|
2528
|
-
|
2529
|
-
|
2530
|
-
|
2531
|
-
|
2532
|
-
|
2533
|
-
|
2534
|
-
|
2535
|
-
|
2536
|
-
|
2537
|
-
|
2690
|
+
const chunkedOperations = chunk(
|
2691
|
+
objects.map((object) => ({
|
2692
|
+
insert: { table: __privateGet$4(this, _table), record: transformObjectLinks(object), createOnly, ifVersion }
|
2693
|
+
})),
|
2694
|
+
BULK_OPERATION_MAX_SIZE
|
2695
|
+
);
|
2696
|
+
const ids = [];
|
2697
|
+
for (const operations of chunkedOperations) {
|
2698
|
+
const { results } = await branchTransaction({
|
2699
|
+
pathParams: {
|
2700
|
+
workspace: "{workspaceId}",
|
2701
|
+
dbBranchName: "{dbBranch}",
|
2702
|
+
region: "{region}"
|
2703
|
+
},
|
2704
|
+
body: { operations },
|
2705
|
+
...fetchProps
|
2706
|
+
});
|
2707
|
+
for (const result of results) {
|
2708
|
+
if (result.operation === "insert") {
|
2709
|
+
ids.push(result.id);
|
2710
|
+
} else {
|
2711
|
+
ids.push(null);
|
2712
|
+
}
|
2713
|
+
}
|
2538
2714
|
}
|
2539
|
-
|
2540
|
-
return response.records?.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item, columns));
|
2715
|
+
return ids;
|
2541
2716
|
};
|
2542
2717
|
_updateRecordWithID = new WeakSet();
|
2543
2718
|
updateRecordWithID_fn = async function(recordId, object, columns = ["*"], { ifVersion }) {
|
2544
2719
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
2545
|
-
const record = transformObjectLinks(object);
|
2720
|
+
const { id: _id, ...record } = transformObjectLinks(object);
|
2546
2721
|
try {
|
2547
2722
|
const response = await updateRecordWithID({
|
2548
2723
|
pathParams: {
|
@@ -2565,6 +2740,36 @@ updateRecordWithID_fn = async function(recordId, object, columns = ["*"], { ifVe
|
|
2565
2740
|
throw e;
|
2566
2741
|
}
|
2567
2742
|
};
|
2743
|
+
_updateRecords = new WeakSet();
|
2744
|
+
updateRecords_fn = async function(objects, { ifVersion, upsert }) {
|
2745
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
2746
|
+
const chunkedOperations = chunk(
|
2747
|
+
objects.map(({ id, ...object }) => ({
|
2748
|
+
update: { table: __privateGet$4(this, _table), id, ifVersion, upsert, fields: transformObjectLinks(object) }
|
2749
|
+
})),
|
2750
|
+
BULK_OPERATION_MAX_SIZE
|
2751
|
+
);
|
2752
|
+
const ids = [];
|
2753
|
+
for (const operations of chunkedOperations) {
|
2754
|
+
const { results } = await branchTransaction({
|
2755
|
+
pathParams: {
|
2756
|
+
workspace: "{workspaceId}",
|
2757
|
+
dbBranchName: "{dbBranch}",
|
2758
|
+
region: "{region}"
|
2759
|
+
},
|
2760
|
+
body: { operations },
|
2761
|
+
...fetchProps
|
2762
|
+
});
|
2763
|
+
for (const result of results) {
|
2764
|
+
if (result.operation === "update") {
|
2765
|
+
ids.push(result.id);
|
2766
|
+
} else {
|
2767
|
+
ids.push(null);
|
2768
|
+
}
|
2769
|
+
}
|
2770
|
+
}
|
2771
|
+
return ids;
|
2772
|
+
};
|
2568
2773
|
_upsertRecordWithID = new WeakSet();
|
2569
2774
|
upsertRecordWithID_fn = async function(recordId, object, columns = ["*"], { ifVersion }) {
|
2570
2775
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
@@ -2607,6 +2812,25 @@ deleteRecord_fn = async function(recordId, columns = ["*"]) {
|
|
2607
2812
|
throw e;
|
2608
2813
|
}
|
2609
2814
|
};
|
2815
|
+
_deleteRecords = new WeakSet();
|
2816
|
+
deleteRecords_fn = async function(recordIds) {
|
2817
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
2818
|
+
const chunkedOperations = chunk(
|
2819
|
+
recordIds.map((id) => ({ delete: { table: __privateGet$4(this, _table), id } })),
|
2820
|
+
BULK_OPERATION_MAX_SIZE
|
2821
|
+
);
|
2822
|
+
for (const operations of chunkedOperations) {
|
2823
|
+
await branchTransaction({
|
2824
|
+
pathParams: {
|
2825
|
+
workspace: "{workspaceId}",
|
2826
|
+
dbBranchName: "{dbBranch}",
|
2827
|
+
region: "{region}"
|
2828
|
+
},
|
2829
|
+
body: { operations },
|
2830
|
+
...fetchProps
|
2831
|
+
});
|
2832
|
+
}
|
2833
|
+
};
|
2610
2834
|
_setCacheQuery = new WeakSet();
|
2611
2835
|
setCacheQuery_fn = async function(query, meta, records) {
|
2612
2836
|
await __privateGet$4(this, _cache).set(`query_${__privateGet$4(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
|
@@ -2643,23 +2867,23 @@ const transformObjectLinks = (object) => {
|
|
2643
2867
|
}, {});
|
2644
2868
|
};
|
2645
2869
|
const initObject = (db, schemaTables, table, object, selectedColumns) => {
|
2646
|
-
const
|
2870
|
+
const data = {};
|
2647
2871
|
const { xata, ...rest } = object ?? {};
|
2648
|
-
Object.assign(
|
2872
|
+
Object.assign(data, rest);
|
2649
2873
|
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
2650
2874
|
if (!columns)
|
2651
2875
|
console.error(`Table ${table} not found in schema`);
|
2652
2876
|
for (const column of columns ?? []) {
|
2653
2877
|
if (!isValidColumn(selectedColumns, column))
|
2654
2878
|
continue;
|
2655
|
-
const value =
|
2879
|
+
const value = data[column.name];
|
2656
2880
|
switch (column.type) {
|
2657
2881
|
case "datetime": {
|
2658
|
-
const date = value !== void 0 ? new Date(value) :
|
2659
|
-
if (date && isNaN(date.getTime())) {
|
2882
|
+
const date = value !== void 0 ? new Date(value) : null;
|
2883
|
+
if (date !== null && isNaN(date.getTime())) {
|
2660
2884
|
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
2661
|
-
} else
|
2662
|
-
|
2885
|
+
} else {
|
2886
|
+
data[column.name] = date;
|
2663
2887
|
}
|
2664
2888
|
break;
|
2665
2889
|
}
|
@@ -2678,48 +2902,46 @@ const initObject = (db, schemaTables, table, object, selectedColumns) => {
|
|
2678
2902
|
}
|
2679
2903
|
return acc;
|
2680
2904
|
}, []);
|
2681
|
-
|
2905
|
+
data[column.name] = initObject(db, schemaTables, linkTable, value, selectedLinkColumns);
|
2682
2906
|
} else {
|
2683
|
-
|
2907
|
+
data[column.name] = null;
|
2684
2908
|
}
|
2685
2909
|
break;
|
2686
2910
|
}
|
2687
2911
|
default:
|
2688
|
-
|
2912
|
+
data[column.name] = value ?? null;
|
2689
2913
|
if (column.notNull === true && value === null) {
|
2690
2914
|
console.error(`Parse error, column ${column.name} is non nullable and value resolves null`);
|
2691
2915
|
}
|
2692
2916
|
break;
|
2693
2917
|
}
|
2694
2918
|
}
|
2695
|
-
|
2696
|
-
|
2919
|
+
const record = { ...data };
|
2920
|
+
record.read = function(columns2) {
|
2921
|
+
return db[table].read(record["id"], columns2);
|
2697
2922
|
};
|
2698
|
-
|
2923
|
+
record.update = function(data2, b, c) {
|
2699
2924
|
const columns2 = isStringArray(b) ? b : ["*"];
|
2700
2925
|
const ifVersion = parseIfVersion(b, c);
|
2701
|
-
return db[table].update(
|
2926
|
+
return db[table].update(record["id"], data2, columns2, { ifVersion });
|
2702
2927
|
};
|
2703
|
-
|
2928
|
+
record.replace = function(data2, b, c) {
|
2704
2929
|
const columns2 = isStringArray(b) ? b : ["*"];
|
2705
2930
|
const ifVersion = parseIfVersion(b, c);
|
2706
|
-
return db[table].createOrReplace(
|
2931
|
+
return db[table].createOrReplace(record["id"], data2, columns2, { ifVersion });
|
2707
2932
|
};
|
2708
|
-
|
2709
|
-
return db[table].delete(
|
2933
|
+
record.delete = function() {
|
2934
|
+
return db[table].delete(record["id"]);
|
2710
2935
|
};
|
2711
|
-
|
2936
|
+
record.getMetadata = function() {
|
2712
2937
|
return xata;
|
2713
2938
|
};
|
2714
|
-
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
2715
|
-
Object.defineProperty(
|
2939
|
+
for (const prop of ["read", "update", "replace", "delete", "getMetadata"]) {
|
2940
|
+
Object.defineProperty(record, prop, { enumerable: false });
|
2716
2941
|
}
|
2717
|
-
Object.freeze(
|
2718
|
-
return
|
2942
|
+
Object.freeze(record);
|
2943
|
+
return record;
|
2719
2944
|
};
|
2720
|
-
function isResponseWithRecords(value) {
|
2721
|
-
return isObject(value) && Array.isArray(value.records);
|
2722
|
-
}
|
2723
2945
|
function extractId(value) {
|
2724
2946
|
if (isString(value))
|
2725
2947
|
return value;
|
@@ -2929,10 +3151,10 @@ _schemaTables = new WeakMap();
|
|
2929
3151
|
_search = new WeakSet();
|
2930
3152
|
search_fn = async function(query, options, getFetchProps) {
|
2931
3153
|
const fetchProps = await getFetchProps();
|
2932
|
-
const { tables, fuzziness, highlight, prefix } = options ?? {};
|
3154
|
+
const { tables, fuzziness, highlight, prefix, page } = options ?? {};
|
2933
3155
|
const { records } = await searchBranch({
|
2934
3156
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", region: "{region}" },
|
2935
|
-
body: { tables, query, fuzziness, prefix, highlight },
|
3157
|
+
body: { tables, query, fuzziness, prefix, highlight, page },
|
2936
3158
|
...fetchProps
|
2937
3159
|
});
|
2938
3160
|
return records;
|
@@ -2950,18 +3172,30 @@ getSchemaTables_fn = async function(getFetchProps) {
|
|
2950
3172
|
return schema.tables;
|
2951
3173
|
};
|
2952
3174
|
|
3175
|
+
class TransactionPlugin extends XataPlugin {
|
3176
|
+
build({ getFetchProps }) {
|
3177
|
+
return {
|
3178
|
+
run: async (operations) => {
|
3179
|
+
const fetchProps = await getFetchProps();
|
3180
|
+
const response = await branchTransaction({
|
3181
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", region: "{region}" },
|
3182
|
+
body: { operations },
|
3183
|
+
...fetchProps
|
3184
|
+
});
|
3185
|
+
return response;
|
3186
|
+
}
|
3187
|
+
};
|
3188
|
+
}
|
3189
|
+
}
|
3190
|
+
|
2953
3191
|
const isBranchStrategyBuilder = (strategy) => {
|
2954
3192
|
return typeof strategy === "function";
|
2955
3193
|
};
|
2956
3194
|
|
2957
3195
|
async function getCurrentBranchName(options) {
|
2958
3196
|
const { branch, envBranch } = getEnvironment();
|
2959
|
-
if (branch)
|
2960
|
-
|
2961
|
-
if (details)
|
2962
|
-
return branch;
|
2963
|
-
console.warn(`Branch ${branch} not found in Xata. Ignoring...`);
|
2964
|
-
}
|
3197
|
+
if (branch)
|
3198
|
+
return branch;
|
2965
3199
|
const gitBranch = envBranch || await getGitBranch();
|
2966
3200
|
return resolveXataBranch(gitBranch, options);
|
2967
3201
|
}
|
@@ -2993,7 +3227,8 @@ async function resolveXataBranch(gitBranch, options) {
|
|
2993
3227
|
workspacesApiUrl: `${protocol}//${host}`,
|
2994
3228
|
pathParams: { dbName, workspace, region },
|
2995
3229
|
queryParams: { gitBranch, fallbackBranch },
|
2996
|
-
trace: defaultTrace
|
3230
|
+
trace: defaultTrace,
|
3231
|
+
clientName: options?.clientName
|
2997
3232
|
});
|
2998
3233
|
return branch;
|
2999
3234
|
}
|
@@ -3077,8 +3312,10 @@ const buildClient = (plugins) => {
|
|
3077
3312
|
};
|
3078
3313
|
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
3079
3314
|
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
3315
|
+
const transactions = new TransactionPlugin().build(pluginOptions);
|
3080
3316
|
this.db = db;
|
3081
3317
|
this.search = search;
|
3318
|
+
this.transactions = transactions;
|
3082
3319
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
3083
3320
|
if (namespace === void 0)
|
3084
3321
|
continue;
|
@@ -3098,20 +3335,41 @@ const buildClient = (plugins) => {
|
|
3098
3335
|
return { databaseURL, branch };
|
3099
3336
|
}
|
3100
3337
|
}, _branch = new WeakMap(), _options = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
|
3338
|
+
const enableBrowser = options?.enableBrowser ?? getEnableBrowserVariable() ?? false;
|
3339
|
+
const isBrowser = typeof window !== "undefined" && typeof Deno === "undefined";
|
3340
|
+
if (isBrowser && !enableBrowser) {
|
3341
|
+
throw new Error(
|
3342
|
+
"You are trying to use Xata from the browser, which is potentially a non-secure environment. If you understand the security concerns, such as leaking your credentials, pass `enableBrowser: true` to the client options to remove this error."
|
3343
|
+
);
|
3344
|
+
}
|
3101
3345
|
const fetch = getFetchImplementation(options?.fetch);
|
3102
3346
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
3103
3347
|
const apiKey = options?.apiKey || getAPIKey();
|
3104
3348
|
const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
|
3105
3349
|
const trace = options?.trace ?? defaultTrace;
|
3106
|
-
const
|
3350
|
+
const clientName = options?.clientName;
|
3351
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({
|
3352
|
+
apiKey,
|
3353
|
+
databaseURL,
|
3354
|
+
fetchImpl: options?.fetch,
|
3355
|
+
clientName: options?.clientName
|
3356
|
+
});
|
3107
3357
|
if (!apiKey) {
|
3108
3358
|
throw new Error("Option apiKey is required");
|
3109
3359
|
}
|
3110
3360
|
if (!databaseURL) {
|
3111
3361
|
throw new Error("Option databaseURL is required");
|
3112
3362
|
}
|
3113
|
-
return { fetch, databaseURL, apiKey, branch, cache, trace, clientID: generateUUID() };
|
3114
|
-
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
|
3363
|
+
return { fetch, databaseURL, apiKey, branch, cache, trace, clientID: generateUUID(), enableBrowser, clientName };
|
3364
|
+
}, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
|
3365
|
+
fetch,
|
3366
|
+
apiKey,
|
3367
|
+
databaseURL,
|
3368
|
+
branch,
|
3369
|
+
trace,
|
3370
|
+
clientID,
|
3371
|
+
clientName
|
3372
|
+
}) {
|
3115
3373
|
const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
|
3116
3374
|
if (!branchValue)
|
3117
3375
|
throw new Error("Unable to resolve branch value");
|
@@ -3125,7 +3383,8 @@ const buildClient = (plugins) => {
|
|
3125
3383
|
return databaseURL + newPath;
|
3126
3384
|
},
|
3127
3385
|
trace,
|
3128
|
-
clientID
|
3386
|
+
clientID,
|
3387
|
+
clientName
|
3129
3388
|
};
|
3130
3389
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
3131
3390
|
if (__privateGet(this, _branch))
|
@@ -3216,7 +3475,7 @@ const deserialize = (json) => {
|
|
3216
3475
|
};
|
3217
3476
|
|
3218
3477
|
function buildWorkerRunner(config) {
|
3219
|
-
return function xataWorker(name,
|
3478
|
+
return function xataWorker(name, worker) {
|
3220
3479
|
return async (...args) => {
|
3221
3480
|
const url = process.env.NODE_ENV === "development" ? `http://localhost:64749/${name}` : `https://dispatcher.xata.workers.dev/${config.workspace}/${config.worker}/${name}`;
|
3222
3481
|
const result = await fetch(url, {
|
@@ -3238,6 +3497,7 @@ class XataError extends Error {
|
|
3238
3497
|
}
|
3239
3498
|
|
3240
3499
|
exports.BaseClient = BaseClient;
|
3500
|
+
exports.FetcherError = FetcherError;
|
3241
3501
|
exports.Operations = operationsByTag;
|
3242
3502
|
exports.PAGINATION_DEFAULT_OFFSET = PAGINATION_DEFAULT_OFFSET;
|
3243
3503
|
exports.PAGINATION_DEFAULT_SIZE = PAGINATION_DEFAULT_SIZE;
|
@@ -3261,6 +3521,7 @@ exports.addGitBranchesEntry = addGitBranchesEntry;
|
|
3261
3521
|
exports.addTableColumn = addTableColumn;
|
3262
3522
|
exports.aggregateTable = aggregateTable;
|
3263
3523
|
exports.applyBranchSchemaEdit = applyBranchSchemaEdit;
|
3524
|
+
exports.branchTransaction = branchTransaction;
|
3264
3525
|
exports.buildClient = buildClient;
|
3265
3526
|
exports.buildWorkerRunner = buildWorkerRunner;
|
3266
3527
|
exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
@@ -3275,11 +3536,6 @@ exports.createMigrationRequest = createMigrationRequest;
|
|
3275
3536
|
exports.createTable = createTable;
|
3276
3537
|
exports.createUserAPIKey = createUserAPIKey;
|
3277
3538
|
exports.createWorkspace = createWorkspace;
|
3278
|
-
exports.dEPRECATEDcreateDatabase = dEPRECATEDcreateDatabase;
|
3279
|
-
exports.dEPRECATEDdeleteDatabase = dEPRECATEDdeleteDatabase;
|
3280
|
-
exports.dEPRECATEDgetDatabaseList = dEPRECATEDgetDatabaseList;
|
3281
|
-
exports.dEPRECATEDgetDatabaseMetadata = dEPRECATEDgetDatabaseMetadata;
|
3282
|
-
exports.dEPRECATEDupdateDatabaseMetadata = dEPRECATEDupdateDatabaseMetadata;
|
3283
3539
|
exports.deleteBranch = deleteBranch;
|
3284
3540
|
exports.deleteColumn = deleteColumn;
|
3285
3541
|
exports.deleteDatabase = deleteDatabase;
|