@xata.io/client 0.22.2 → 0.22.4

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/dist/index.cjs CHANGED
@@ -166,23 +166,20 @@ function getGlobalFallbackBranch() {
166
166
  return void 0;
167
167
  }
168
168
  }
169
- async function getGitBranch() {
170
- const cmd = ["git", "branch", "--show-current"];
171
- const fullCmd = cmd.join(" ");
172
- const nodeModule = ["child", "process"].join("_");
173
- const execOptions = { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] };
169
+ function getDatabaseURL() {
174
170
  try {
175
- if (typeof require === "function") {
176
- return require(nodeModule).execSync(fullCmd, execOptions).trim();
177
- }
171
+ const { databaseURL } = getEnvironment();
172
+ return databaseURL;
178
173
  } catch (err) {
174
+ return void 0;
179
175
  }
176
+ }
177
+ function getBranch() {
180
178
  try {
181
- if (isObject(Deno)) {
182
- const process2 = Deno.run({ cmd, stdout: "piped", stderr: "null" });
183
- return new TextDecoder().decode(await process2.output()).trim();
184
- }
179
+ const { branch, envBranch } = getEnvironment();
180
+ return branch ?? envBranch;
185
181
  } catch (err) {
182
+ return void 0;
186
183
  }
187
184
  }
188
185
 
@@ -302,7 +299,180 @@ function generateUUID() {
302
299
  });
303
300
  }
304
301
 
305
- const VERSION = "0.22.2";
302
+ async function getBytes(stream, onChunk) {
303
+ const reader = stream.getReader();
304
+ let result;
305
+ while (!(result = await reader.read()).done) {
306
+ onChunk(result.value);
307
+ }
308
+ }
309
+ function getLines(onLine) {
310
+ let buffer;
311
+ let position;
312
+ let fieldLength;
313
+ let discardTrailingNewline = false;
314
+ return function onChunk(arr) {
315
+ if (buffer === void 0) {
316
+ buffer = arr;
317
+ position = 0;
318
+ fieldLength = -1;
319
+ } else {
320
+ buffer = concat(buffer, arr);
321
+ }
322
+ const bufLength = buffer.length;
323
+ let lineStart = 0;
324
+ while (position < bufLength) {
325
+ if (discardTrailingNewline) {
326
+ if (buffer[position] === 10 /* NewLine */) {
327
+ lineStart = ++position;
328
+ }
329
+ discardTrailingNewline = false;
330
+ }
331
+ let lineEnd = -1;
332
+ for (; position < bufLength && lineEnd === -1; ++position) {
333
+ switch (buffer[position]) {
334
+ case 58 /* Colon */:
335
+ if (fieldLength === -1) {
336
+ fieldLength = position - lineStart;
337
+ }
338
+ break;
339
+ case 13 /* CarriageReturn */:
340
+ discardTrailingNewline = true;
341
+ case 10 /* NewLine */:
342
+ lineEnd = position;
343
+ break;
344
+ }
345
+ }
346
+ if (lineEnd === -1) {
347
+ break;
348
+ }
349
+ onLine(buffer.subarray(lineStart, lineEnd), fieldLength);
350
+ lineStart = position;
351
+ fieldLength = -1;
352
+ }
353
+ if (lineStart === bufLength) {
354
+ buffer = void 0;
355
+ } else if (lineStart !== 0) {
356
+ buffer = buffer.subarray(lineStart);
357
+ position -= lineStart;
358
+ }
359
+ };
360
+ }
361
+ function getMessages(onId, onRetry, onMessage) {
362
+ let message = newMessage();
363
+ const decoder = new TextDecoder();
364
+ return function onLine(line, fieldLength) {
365
+ if (line.length === 0) {
366
+ onMessage?.(message);
367
+ message = newMessage();
368
+ } else if (fieldLength > 0) {
369
+ const field = decoder.decode(line.subarray(0, fieldLength));
370
+ const valueOffset = fieldLength + (line[fieldLength + 1] === 32 /* Space */ ? 2 : 1);
371
+ const value = decoder.decode(line.subarray(valueOffset));
372
+ switch (field) {
373
+ case "data":
374
+ message.data = message.data ? message.data + "\n" + value : value;
375
+ break;
376
+ case "event":
377
+ message.event = value;
378
+ break;
379
+ case "id":
380
+ onId(message.id = value);
381
+ break;
382
+ case "retry":
383
+ const retry = parseInt(value, 10);
384
+ if (!isNaN(retry)) {
385
+ onRetry(message.retry = retry);
386
+ }
387
+ break;
388
+ }
389
+ }
390
+ };
391
+ }
392
+ function concat(a, b) {
393
+ const res = new Uint8Array(a.length + b.length);
394
+ res.set(a);
395
+ res.set(b, a.length);
396
+ return res;
397
+ }
398
+ function newMessage() {
399
+ return {
400
+ data: "",
401
+ event: "",
402
+ id: "",
403
+ retry: void 0
404
+ };
405
+ }
406
+ const EventStreamContentType = "text/event-stream";
407
+ const LastEventId = "last-event-id";
408
+ function fetchEventSource(input, {
409
+ signal: inputSignal,
410
+ headers: inputHeaders,
411
+ onopen: inputOnOpen,
412
+ onmessage,
413
+ onclose,
414
+ onerror,
415
+ fetch: inputFetch,
416
+ ...rest
417
+ }) {
418
+ return new Promise((resolve, reject) => {
419
+ const headers = { ...inputHeaders };
420
+ if (!headers.accept) {
421
+ headers.accept = EventStreamContentType;
422
+ }
423
+ let curRequestController;
424
+ function dispose() {
425
+ curRequestController.abort();
426
+ }
427
+ inputSignal?.addEventListener("abort", () => {
428
+ dispose();
429
+ resolve();
430
+ });
431
+ const fetchImpl = inputFetch ?? fetch;
432
+ const onopen = inputOnOpen ?? defaultOnOpen;
433
+ async function create() {
434
+ curRequestController = new AbortController();
435
+ try {
436
+ const response = await fetchImpl(input, {
437
+ ...rest,
438
+ headers,
439
+ signal: curRequestController.signal
440
+ });
441
+ await onopen(response);
442
+ await getBytes(
443
+ response.body,
444
+ getLines(
445
+ getMessages(
446
+ (id) => {
447
+ if (id) {
448
+ headers[LastEventId] = id;
449
+ } else {
450
+ delete headers[LastEventId];
451
+ }
452
+ },
453
+ (_retry) => {
454
+ },
455
+ onmessage
456
+ )
457
+ )
458
+ );
459
+ onclose?.();
460
+ dispose();
461
+ resolve();
462
+ } catch (err) {
463
+ }
464
+ }
465
+ create();
466
+ });
467
+ }
468
+ function defaultOnOpen(response) {
469
+ const contentType = response.headers?.get("content-type");
470
+ if (!contentType?.startsWith(EventStreamContentType)) {
471
+ throw new Error(`Expected content-type to be ${EventStreamContentType}, Actual: ${contentType}`);
472
+ }
473
+ }
474
+
475
+ const VERSION = "0.22.4";
306
476
 
307
477
  class ErrorWithCause extends Error {
308
478
  constructor(message, options) {
@@ -386,7 +556,7 @@ async function fetch$1({
386
556
  headers: customHeaders,
387
557
  pathParams,
388
558
  queryParams,
389
- fetchImpl,
559
+ fetch: fetch2,
390
560
  apiKey,
391
561
  endpoint,
392
562
  apiUrl,
@@ -399,7 +569,7 @@ async function fetch$1({
399
569
  xataAgentExtra,
400
570
  fetchOptions = {}
401
571
  }) {
402
- pool.setFetch(fetchImpl);
572
+ pool.setFetch(fetch2);
403
573
  return await trace(
404
574
  `${method.toUpperCase()} ${path}`,
405
575
  async ({ setAttributes }) => {
@@ -461,6 +631,59 @@ async function fetch$1({
461
631
  { [TraceAttributes.HTTP_METHOD]: method.toUpperCase(), [TraceAttributes.HTTP_ROUTE]: path }
462
632
  );
463
633
  }
634
+ function fetchSSERequest({
635
+ url: path,
636
+ method,
637
+ body,
638
+ headers: customHeaders,
639
+ pathParams,
640
+ queryParams,
641
+ fetch: fetch2,
642
+ apiKey,
643
+ endpoint,
644
+ apiUrl,
645
+ workspacesApiUrl,
646
+ onMessage,
647
+ onError,
648
+ onClose,
649
+ signal,
650
+ clientID,
651
+ sessionID,
652
+ clientName,
653
+ xataAgentExtra
654
+ }) {
655
+ const baseUrl = buildBaseUrl({ endpoint, path, workspacesApiUrl, pathParams, apiUrl });
656
+ const fullUrl = resolveUrl(baseUrl, queryParams, pathParams);
657
+ const url = fullUrl.includes("localhost") ? fullUrl.replace(/^[^.]+\./, "http://") : fullUrl;
658
+ void fetchEventSource(url, {
659
+ method,
660
+ body: JSON.stringify(body),
661
+ fetch: fetch2,
662
+ signal,
663
+ headers: {
664
+ "X-Xata-Client-ID": clientID ?? defaultClientID,
665
+ "X-Xata-Session-ID": sessionID ?? generateUUID(),
666
+ "X-Xata-Agent": compact([
667
+ ["client", "TS_SDK"],
668
+ ["version", VERSION],
669
+ isDefined(clientName) ? ["service", clientName] : void 0,
670
+ ...Object.entries(xataAgentExtra ?? {})
671
+ ]).map(([key, value]) => `${key}=${value}`).join("; "),
672
+ ...customHeaders,
673
+ Authorization: `Bearer ${apiKey}`,
674
+ "Content-Type": "application/json"
675
+ },
676
+ onmessage(ev) {
677
+ onMessage?.(JSON.parse(ev.data));
678
+ },
679
+ onerror(ev) {
680
+ onError?.(JSON.parse(ev.data));
681
+ },
682
+ onclose() {
683
+ onClose?.();
684
+ }
685
+ });
686
+ }
464
687
  function parseUrl(url) {
465
688
  try {
466
689
  const { host, protocol } = new URL(url);
@@ -491,6 +714,12 @@ const deleteBranch = (variables, signal) => dataPlaneFetch({
491
714
  ...variables,
492
715
  signal
493
716
  });
717
+ const copyBranch = (variables, signal) => dataPlaneFetch({
718
+ url: "/db/{dbBranchName}/copy",
719
+ method: "post",
720
+ ...variables,
721
+ signal
722
+ });
494
723
  const updateBranchMetadata = (variables, signal) => dataPlaneFetch({
495
724
  url: "/db/{dbBranchName}/metadata",
496
725
  method: "put",
@@ -614,6 +843,12 @@ const searchTable = (variables, signal) => dataPlaneFetch({
614
843
  signal
615
844
  });
616
845
  const vectorSearchTable = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/tables/{tableName}/vectorSearch", method: "post", ...variables, signal });
846
+ const askTable = (variables, signal) => dataPlaneFetch({
847
+ url: "/db/{dbBranchName}/tables/{tableName}/ask",
848
+ method: "post",
849
+ ...variables,
850
+ signal
851
+ });
617
852
  const summarizeTable = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/tables/{tableName}/summarize", method: "post", ...variables, signal });
618
853
  const aggregateTable = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/tables/{tableName}/aggregate", method: "post", ...variables, signal });
619
854
  const operationsByTag$2 = {
@@ -622,6 +857,7 @@ const operationsByTag$2 = {
622
857
  getBranchDetails,
623
858
  createBranch,
624
859
  deleteBranch,
860
+ copyBranch,
625
861
  updateBranchMetadata,
626
862
  getBranchMetadata,
627
863
  getBranchStats,
@@ -673,7 +909,15 @@ const operationsByTag$2 = {
673
909
  deleteRecord,
674
910
  bulkInsertTableRecords
675
911
  },
676
- searchAndFilter: { queryTable, searchBranch, searchTable, vectorSearchTable, summarizeTable, aggregateTable }
912
+ searchAndFilter: {
913
+ queryTable,
914
+ searchBranch,
915
+ searchTable,
916
+ vectorSearchTable,
917
+ askTable,
918
+ summarizeTable,
919
+ aggregateTable
920
+ }
677
921
  };
678
922
 
679
923
  const controlPlaneFetch = async (options) => fetch$1({ ...options, endpoint: "controlPlane" });
@@ -895,7 +1139,7 @@ class XataApiClient {
895
1139
  __privateSet$7(this, _extraProps, {
896
1140
  apiUrl: getHostUrl(provider, "main"),
897
1141
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
898
- fetchImpl: getFetchImplementation(options.fetch),
1142
+ fetch: getFetchImplementation(options.fetch),
899
1143
  apiKey,
900
1144
  trace,
901
1145
  clientName: options.clientName,
@@ -1161,6 +1405,20 @@ class BranchApi {
1161
1405
  ...this.extraProps
1162
1406
  });
1163
1407
  }
1408
+ copyBranch({
1409
+ workspace,
1410
+ region,
1411
+ database,
1412
+ branch,
1413
+ destinationBranch,
1414
+ limit
1415
+ }) {
1416
+ return operationsByTag.branch.copyBranch({
1417
+ pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
1418
+ body: { destinationBranch, limit },
1419
+ ...this.extraProps
1420
+ });
1421
+ }
1164
1422
  updateBranchMetadata({
1165
1423
  workspace,
1166
1424
  region,
@@ -1575,6 +1833,38 @@ class SearchAndFilterApi {
1575
1833
  ...this.extraProps
1576
1834
  });
1577
1835
  }
1836
+ vectorSearchTable({
1837
+ workspace,
1838
+ region,
1839
+ database,
1840
+ branch,
1841
+ table,
1842
+ queryVector,
1843
+ column,
1844
+ similarityFunction,
1845
+ size,
1846
+ filter
1847
+ }) {
1848
+ return operationsByTag.searchAndFilter.vectorSearchTable({
1849
+ pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table },
1850
+ body: { queryVector, column, similarityFunction, size, filter },
1851
+ ...this.extraProps
1852
+ });
1853
+ }
1854
+ askTable({
1855
+ workspace,
1856
+ region,
1857
+ database,
1858
+ branch,
1859
+ table,
1860
+ options
1861
+ }) {
1862
+ return operationsByTag.searchAndFilter.askTable({
1863
+ pathParams: { workspace, region, dbBranchName: `${database}:${branch}`, tableName: table },
1864
+ body: { ...options },
1865
+ ...this.extraProps
1866
+ });
1867
+ }
1578
1868
  summarizeTable({
1579
1869
  workspace,
1580
1870
  region,
@@ -1928,9 +2218,8 @@ class DatabaseApi {
1928
2218
  }
1929
2219
 
1930
2220
  class XataApiPlugin {
1931
- async build(options) {
1932
- const { fetchImpl, apiKey } = await options.getFetchProps();
1933
- return new XataApiClient({ fetch: fetchImpl, apiKey });
2221
+ build(options) {
2222
+ return new XataApiClient(options);
1934
2223
  }
1935
2224
  }
1936
2225
 
@@ -2344,10 +2633,7 @@ class RestRepository extends Query {
2344
2633
  __privateSet$4(this, _db, options.db);
2345
2634
  __privateSet$4(this, _cache, options.pluginOptions.cache);
2346
2635
  __privateSet$4(this, _schemaTables$2, options.schemaTables);
2347
- __privateSet$4(this, _getFetchProps, async () => {
2348
- const props = await options.pluginOptions.getFetchProps();
2349
- return { ...props, sessionID: generateUUID() };
2350
- });
2636
+ __privateSet$4(this, _getFetchProps, () => ({ ...options.pluginOptions, sessionID: generateUUID() }));
2351
2637
  const trace = options.pluginOptions.trace ?? defaultTrace;
2352
2638
  __privateSet$4(this, _trace, async (name, fn, options2 = {}) => {
2353
2639
  return trace(name, fn, {
@@ -2404,7 +2690,6 @@ class RestRepository extends Query {
2404
2690
  }
2405
2691
  const id = extractId(a);
2406
2692
  if (id) {
2407
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2408
2693
  try {
2409
2694
  const response = await getRecord({
2410
2695
  pathParams: {
@@ -2415,7 +2700,7 @@ class RestRepository extends Query {
2415
2700
  recordId: id
2416
2701
  },
2417
2702
  queryParams: { columns },
2418
- ...fetchProps
2703
+ ...__privateGet$4(this, _getFetchProps).call(this)
2419
2704
  });
2420
2705
  const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
2421
2706
  return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response, columns);
@@ -2593,7 +2878,6 @@ class RestRepository extends Query {
2593
2878
  }
2594
2879
  async search(query, options = {}) {
2595
2880
  return __privateGet$4(this, _trace).call(this, "search", async () => {
2596
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2597
2881
  const { records } = await searchTable({
2598
2882
  pathParams: {
2599
2883
  workspace: "{workspaceId}",
@@ -2611,7 +2895,7 @@ class RestRepository extends Query {
2611
2895
  page: options.page,
2612
2896
  target: options.target
2613
2897
  },
2614
- ...fetchProps
2898
+ ...__privateGet$4(this, _getFetchProps).call(this)
2615
2899
  });
2616
2900
  const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
2617
2901
  return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item, ["*"]));
@@ -2619,7 +2903,6 @@ class RestRepository extends Query {
2619
2903
  }
2620
2904
  async vectorSearch(column, query, options) {
2621
2905
  return __privateGet$4(this, _trace).call(this, "vectorSearch", async () => {
2622
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2623
2906
  const { records } = await vectorSearchTable({
2624
2907
  pathParams: {
2625
2908
  workspace: "{workspaceId}",
@@ -2634,7 +2917,7 @@ class RestRepository extends Query {
2634
2917
  size: options?.size,
2635
2918
  filter: options?.filter
2636
2919
  },
2637
- ...fetchProps
2920
+ ...__privateGet$4(this, _getFetchProps).call(this)
2638
2921
  });
2639
2922
  const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
2640
2923
  return records.map((item) => initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), item, ["*"]));
@@ -2642,7 +2925,6 @@ class RestRepository extends Query {
2642
2925
  }
2643
2926
  async aggregate(aggs, filter) {
2644
2927
  return __privateGet$4(this, _trace).call(this, "aggregate", async () => {
2645
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2646
2928
  const result = await aggregateTable({
2647
2929
  pathParams: {
2648
2930
  workspace: "{workspaceId}",
@@ -2651,7 +2933,7 @@ class RestRepository extends Query {
2651
2933
  tableName: __privateGet$4(this, _table)
2652
2934
  },
2653
2935
  body: { aggs, filter },
2654
- ...fetchProps
2936
+ ...__privateGet$4(this, _getFetchProps).call(this)
2655
2937
  });
2656
2938
  return result;
2657
2939
  });
@@ -2662,7 +2944,6 @@ class RestRepository extends Query {
2662
2944
  if (cacheQuery)
2663
2945
  return new Page(query, cacheQuery.meta, cacheQuery.records);
2664
2946
  const data = query.getQueryOptions();
2665
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2666
2947
  const { meta, records: objects } = await queryTable({
2667
2948
  pathParams: {
2668
2949
  workspace: "{workspaceId}",
@@ -2678,7 +2959,7 @@ class RestRepository extends Query {
2678
2959
  consistency: data.consistency
2679
2960
  },
2680
2961
  fetchOptions: data.fetchOptions,
2681
- ...fetchProps
2962
+ ...__privateGet$4(this, _getFetchProps).call(this)
2682
2963
  });
2683
2964
  const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
2684
2965
  const records = objects.map(
@@ -2691,7 +2972,6 @@ class RestRepository extends Query {
2691
2972
  async summarizeTable(query, summaries, summariesFilter) {
2692
2973
  return __privateGet$4(this, _trace).call(this, "summarize", async () => {
2693
2974
  const data = query.getQueryOptions();
2694
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2695
2975
  const result = await summarizeTable({
2696
2976
  pathParams: {
2697
2977
  workspace: "{workspaceId}",
@@ -2708,11 +2988,39 @@ class RestRepository extends Query {
2708
2988
  summaries,
2709
2989
  summariesFilter
2710
2990
  },
2711
- ...fetchProps
2991
+ ...__privateGet$4(this, _getFetchProps).call(this)
2712
2992
  });
2713
2993
  return result;
2714
2994
  });
2715
2995
  }
2996
+ ask(question, options) {
2997
+ const params = {
2998
+ pathParams: {
2999
+ workspace: "{workspaceId}",
3000
+ dbBranchName: "{dbBranch}",
3001
+ region: "{region}",
3002
+ tableName: __privateGet$4(this, _table)
3003
+ },
3004
+ body: {
3005
+ question,
3006
+ ...options
3007
+ },
3008
+ ...__privateGet$4(this, _getFetchProps).call(this)
3009
+ };
3010
+ if (options?.onMessage) {
3011
+ fetchSSERequest({
3012
+ endpoint: "dataPlane",
3013
+ url: "/db/{dbBranchName}/tables/{tableName}/ask",
3014
+ method: "POST",
3015
+ onMessage: (message) => {
3016
+ options.onMessage?.({ answer: message.text });
3017
+ },
3018
+ ...params
3019
+ });
3020
+ } else {
3021
+ return askTable(params);
3022
+ }
3023
+ }
2716
3024
  }
2717
3025
  _table = new WeakMap();
2718
3026
  _getFetchProps = new WeakMap();
@@ -2722,7 +3030,6 @@ _schemaTables$2 = new WeakMap();
2722
3030
  _trace = new WeakMap();
2723
3031
  _insertRecordWithoutId = new WeakSet();
2724
3032
  insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
2725
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2726
3033
  const record = transformObjectLinks(object);
2727
3034
  const response = await insertRecord({
2728
3035
  pathParams: {
@@ -2733,14 +3040,13 @@ insertRecordWithoutId_fn = async function(object, columns = ["*"]) {
2733
3040
  },
2734
3041
  queryParams: { columns },
2735
3042
  body: record,
2736
- ...fetchProps
3043
+ ...__privateGet$4(this, _getFetchProps).call(this)
2737
3044
  });
2738
3045
  const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
2739
3046
  return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response, columns);
2740
3047
  };
2741
3048
  _insertRecordWithId = new WeakSet();
2742
3049
  insertRecordWithId_fn = async function(recordId, object, columns = ["*"], { createOnly, ifVersion }) {
2743
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2744
3050
  const record = transformObjectLinks(object);
2745
3051
  const response = await insertRecordWithID({
2746
3052
  pathParams: {
@@ -2752,14 +3058,13 @@ insertRecordWithId_fn = async function(recordId, object, columns = ["*"], { crea
2752
3058
  },
2753
3059
  body: record,
2754
3060
  queryParams: { createOnly, columns, ifVersion },
2755
- ...fetchProps
3061
+ ...__privateGet$4(this, _getFetchProps).call(this)
2756
3062
  });
2757
3063
  const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
2758
3064
  return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response, columns);
2759
3065
  };
2760
3066
  _insertRecords = new WeakSet();
2761
3067
  insertRecords_fn = async function(objects, { createOnly, ifVersion }) {
2762
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2763
3068
  const chunkedOperations = chunk(
2764
3069
  objects.map((object) => ({
2765
3070
  insert: { table: __privateGet$4(this, _table), record: transformObjectLinks(object), createOnly, ifVersion }
@@ -2775,7 +3080,7 @@ insertRecords_fn = async function(objects, { createOnly, ifVersion }) {
2775
3080
  region: "{region}"
2776
3081
  },
2777
3082
  body: { operations },
2778
- ...fetchProps
3083
+ ...__privateGet$4(this, _getFetchProps).call(this)
2779
3084
  });
2780
3085
  for (const result of results) {
2781
3086
  if (result.operation === "insert") {
@@ -2789,7 +3094,6 @@ insertRecords_fn = async function(objects, { createOnly, ifVersion }) {
2789
3094
  };
2790
3095
  _updateRecordWithID = new WeakSet();
2791
3096
  updateRecordWithID_fn = async function(recordId, object, columns = ["*"], { ifVersion }) {
2792
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2793
3097
  const { id: _id, ...record } = transformObjectLinks(object);
2794
3098
  try {
2795
3099
  const response = await updateRecordWithID({
@@ -2802,7 +3106,7 @@ updateRecordWithID_fn = async function(recordId, object, columns = ["*"], { ifVe
2802
3106
  },
2803
3107
  queryParams: { columns, ifVersion },
2804
3108
  body: record,
2805
- ...fetchProps
3109
+ ...__privateGet$4(this, _getFetchProps).call(this)
2806
3110
  });
2807
3111
  const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
2808
3112
  return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response, columns);
@@ -2815,7 +3119,6 @@ updateRecordWithID_fn = async function(recordId, object, columns = ["*"], { ifVe
2815
3119
  };
2816
3120
  _updateRecords = new WeakSet();
2817
3121
  updateRecords_fn = async function(objects, { ifVersion, upsert }) {
2818
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2819
3122
  const chunkedOperations = chunk(
2820
3123
  objects.map(({ id, ...object }) => ({
2821
3124
  update: { table: __privateGet$4(this, _table), id, ifVersion, upsert, fields: transformObjectLinks(object) }
@@ -2831,7 +3134,7 @@ updateRecords_fn = async function(objects, { ifVersion, upsert }) {
2831
3134
  region: "{region}"
2832
3135
  },
2833
3136
  body: { operations },
2834
- ...fetchProps
3137
+ ...__privateGet$4(this, _getFetchProps).call(this)
2835
3138
  });
2836
3139
  for (const result of results) {
2837
3140
  if (result.operation === "update") {
@@ -2845,7 +3148,6 @@ updateRecords_fn = async function(objects, { ifVersion, upsert }) {
2845
3148
  };
2846
3149
  _upsertRecordWithID = new WeakSet();
2847
3150
  upsertRecordWithID_fn = async function(recordId, object, columns = ["*"], { ifVersion }) {
2848
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2849
3151
  const response = await upsertRecordWithID({
2850
3152
  pathParams: {
2851
3153
  workspace: "{workspaceId}",
@@ -2856,14 +3158,13 @@ upsertRecordWithID_fn = async function(recordId, object, columns = ["*"], { ifVe
2856
3158
  },
2857
3159
  queryParams: { columns, ifVersion },
2858
3160
  body: object,
2859
- ...fetchProps
3161
+ ...__privateGet$4(this, _getFetchProps).call(this)
2860
3162
  });
2861
3163
  const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
2862
3164
  return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response, columns);
2863
3165
  };
2864
3166
  _deleteRecord = new WeakSet();
2865
3167
  deleteRecord_fn = async function(recordId, columns = ["*"]) {
2866
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2867
3168
  try {
2868
3169
  const response = await deleteRecord({
2869
3170
  pathParams: {
@@ -2874,7 +3175,7 @@ deleteRecord_fn = async function(recordId, columns = ["*"]) {
2874
3175
  recordId
2875
3176
  },
2876
3177
  queryParams: { columns },
2877
- ...fetchProps
3178
+ ...__privateGet$4(this, _getFetchProps).call(this)
2878
3179
  });
2879
3180
  const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
2880
3181
  return initObject(__privateGet$4(this, _db), schemaTables, __privateGet$4(this, _table), response, columns);
@@ -2887,7 +3188,6 @@ deleteRecord_fn = async function(recordId, columns = ["*"]) {
2887
3188
  };
2888
3189
  _deleteRecords = new WeakSet();
2889
3190
  deleteRecords_fn = async function(recordIds) {
2890
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2891
3191
  const chunkedOperations = chunk(
2892
3192
  recordIds.map((id) => ({ delete: { table: __privateGet$4(this, _table), id } })),
2893
3193
  BULK_OPERATION_MAX_SIZE
@@ -2900,7 +3200,7 @@ deleteRecords_fn = async function(recordIds) {
2900
3200
  region: "{region}"
2901
3201
  },
2902
3202
  body: { operations },
2903
- ...fetchProps
3203
+ ...__privateGet$4(this, _getFetchProps).call(this)
2904
3204
  });
2905
3205
  }
2906
3206
  };
@@ -2924,10 +3224,9 @@ _getSchemaTables$1 = new WeakSet();
2924
3224
  getSchemaTables_fn$1 = async function() {
2925
3225
  if (__privateGet$4(this, _schemaTables$2))
2926
3226
  return __privateGet$4(this, _schemaTables$2);
2927
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
2928
3227
  const { schema } = await getBranchDetails({
2929
3228
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", region: "{region}" },
2930
- ...fetchProps
3229
+ ...__privateGet$4(this, _getFetchProps).call(this)
2931
3230
  });
2932
3231
  __privateSet$4(this, _schemaTables$2, schema.tables);
2933
3232
  return schema.tables;
@@ -3203,19 +3502,19 @@ class SearchPlugin extends XataPlugin {
3203
3502
  __privateAdd$1(this, _schemaTables, void 0);
3204
3503
  __privateSet$1(this, _schemaTables, schemaTables);
3205
3504
  }
3206
- build({ getFetchProps }) {
3505
+ build(pluginOptions) {
3207
3506
  return {
3208
3507
  all: async (query, options = {}) => {
3209
- const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
3210
- const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
3508
+ const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, pluginOptions);
3509
+ const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, pluginOptions);
3211
3510
  return records.map((record) => {
3212
3511
  const { table = "orphan" } = record.xata;
3213
3512
  return { table, record: initObject(this.db, schemaTables, table, record, ["*"]) };
3214
3513
  });
3215
3514
  },
3216
3515
  byTable: async (query, options = {}) => {
3217
- const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
3218
- const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
3516
+ const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, pluginOptions);
3517
+ const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, pluginOptions);
3219
3518
  return records.reduce((acc, record) => {
3220
3519
  const { table = "orphan" } = record.xata;
3221
3520
  const items = acc[table] ?? [];
@@ -3228,38 +3527,35 @@ class SearchPlugin extends XataPlugin {
3228
3527
  }
3229
3528
  _schemaTables = new WeakMap();
3230
3529
  _search = new WeakSet();
3231
- search_fn = async function(query, options, getFetchProps) {
3232
- const fetchProps = await getFetchProps();
3530
+ search_fn = async function(query, options, pluginOptions) {
3233
3531
  const { tables, fuzziness, highlight, prefix, page } = options ?? {};
3234
3532
  const { records } = await searchBranch({
3235
3533
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", region: "{region}" },
3236
3534
  body: { tables, query, fuzziness, prefix, highlight, page },
3237
- ...fetchProps
3535
+ ...pluginOptions
3238
3536
  });
3239
3537
  return records;
3240
3538
  };
3241
3539
  _getSchemaTables = new WeakSet();
3242
- getSchemaTables_fn = async function(getFetchProps) {
3540
+ getSchemaTables_fn = async function(pluginOptions) {
3243
3541
  if (__privateGet$1(this, _schemaTables))
3244
3542
  return __privateGet$1(this, _schemaTables);
3245
- const fetchProps = await getFetchProps();
3246
3543
  const { schema } = await getBranchDetails({
3247
3544
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", region: "{region}" },
3248
- ...fetchProps
3545
+ ...pluginOptions
3249
3546
  });
3250
3547
  __privateSet$1(this, _schemaTables, schema.tables);
3251
3548
  return schema.tables;
3252
3549
  };
3253
3550
 
3254
3551
  class TransactionPlugin extends XataPlugin {
3255
- build({ getFetchProps }) {
3552
+ build(pluginOptions) {
3256
3553
  return {
3257
3554
  run: async (operations) => {
3258
- const fetchProps = await getFetchProps();
3259
3555
  const response = await branchTransaction({
3260
3556
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", region: "{region}" },
3261
3557
  body: { operations },
3262
- ...fetchProps
3558
+ ...pluginOptions
3263
3559
  });
3264
3560
  return response;
3265
3561
  }
@@ -3267,91 +3563,6 @@ class TransactionPlugin extends XataPlugin {
3267
3563
  }
3268
3564
  }
3269
3565
 
3270
- const isBranchStrategyBuilder = (strategy) => {
3271
- return typeof strategy === "function";
3272
- };
3273
-
3274
- async function getCurrentBranchName(options) {
3275
- const { branch, envBranch } = getEnvironment();
3276
- if (branch)
3277
- return branch;
3278
- const gitBranch = envBranch || await getGitBranch();
3279
- return resolveXataBranch(gitBranch, options);
3280
- }
3281
- async function getCurrentBranchDetails(options) {
3282
- const branch = await getCurrentBranchName(options);
3283
- return getDatabaseBranch(branch, options);
3284
- }
3285
- async function resolveXataBranch(gitBranch, options) {
3286
- const databaseURL = options?.databaseURL || getDatabaseURL();
3287
- const apiKey = options?.apiKey || getAPIKey();
3288
- if (!databaseURL)
3289
- throw new Error(
3290
- "A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
3291
- );
3292
- if (!apiKey)
3293
- throw new Error(
3294
- "An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
3295
- );
3296
- const [protocol, , host, , dbName] = databaseURL.split("/");
3297
- const urlParts = parseWorkspacesUrlParts(host);
3298
- if (!urlParts)
3299
- throw new Error(`Unable to parse workspace and region: ${databaseURL}`);
3300
- const { workspace, region } = urlParts;
3301
- const { fallbackBranch } = getEnvironment();
3302
- const { branch } = await resolveBranch({
3303
- apiKey,
3304
- apiUrl: databaseURL,
3305
- fetchImpl: getFetchImplementation(options?.fetchImpl),
3306
- workspacesApiUrl: `${protocol}//${host}`,
3307
- pathParams: { dbName, workspace, region },
3308
- queryParams: { gitBranch, fallbackBranch },
3309
- trace: defaultTrace,
3310
- clientName: options?.clientName,
3311
- xataAgentExtra: options?.xataAgentExtra
3312
- });
3313
- return branch;
3314
- }
3315
- async function getDatabaseBranch(branch, options) {
3316
- const databaseURL = options?.databaseURL || getDatabaseURL();
3317
- const apiKey = options?.apiKey || getAPIKey();
3318
- if (!databaseURL)
3319
- throw new Error(
3320
- "A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely"
3321
- );
3322
- if (!apiKey)
3323
- throw new Error(
3324
- "An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely"
3325
- );
3326
- const [protocol, , host, , database] = databaseURL.split("/");
3327
- const urlParts = parseWorkspacesUrlParts(host);
3328
- if (!urlParts)
3329
- throw new Error(`Unable to parse workspace and region: ${databaseURL}`);
3330
- const { workspace, region } = urlParts;
3331
- try {
3332
- return await getBranchDetails({
3333
- apiKey,
3334
- apiUrl: databaseURL,
3335
- fetchImpl: getFetchImplementation(options?.fetchImpl),
3336
- workspacesApiUrl: `${protocol}//${host}`,
3337
- pathParams: { dbBranchName: `${database}:${branch}`, workspace, region },
3338
- trace: defaultTrace
3339
- });
3340
- } catch (err) {
3341
- if (isObject(err) && err.status === 404)
3342
- return null;
3343
- throw err;
3344
- }
3345
- }
3346
- function getDatabaseURL() {
3347
- try {
3348
- const { databaseURL } = getEnvironment();
3349
- return databaseURL;
3350
- } catch (err) {
3351
- return void 0;
3352
- }
3353
- }
3354
-
3355
3566
  var __accessCheck = (obj, member, msg) => {
3356
3567
  if (!member.has(obj))
3357
3568
  throw TypeError("Cannot " + msg);
@@ -3375,20 +3586,17 @@ var __privateMethod = (obj, member, method) => {
3375
3586
  return method;
3376
3587
  };
3377
3588
  const buildClient = (plugins) => {
3378
- var _branch, _options, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
3589
+ var _options, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _a;
3379
3590
  return _a = class {
3380
3591
  constructor(options = {}, schemaTables) {
3381
3592
  __privateAdd(this, _parseOptions);
3382
3593
  __privateAdd(this, _getFetchProps);
3383
- __privateAdd(this, _evaluateBranch);
3384
- __privateAdd(this, _branch, void 0);
3385
3594
  __privateAdd(this, _options, void 0);
3386
3595
  const safeOptions = __privateMethod(this, _parseOptions, parseOptions_fn).call(this, options);
3387
3596
  __privateSet(this, _options, safeOptions);
3388
3597
  const pluginOptions = {
3389
- getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
3390
- cache: safeOptions.cache,
3391
- trace: safeOptions.trace
3598
+ ...__privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
3599
+ cache: safeOptions.cache
3392
3600
  };
3393
3601
  const db = new SchemaPlugin(schemaTables).build(pluginOptions);
3394
3602
  const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
@@ -3411,10 +3619,10 @@ const buildClient = (plugins) => {
3411
3619
  }
3412
3620
  async getConfig() {
3413
3621
  const databaseURL = __privateGet(this, _options).databaseURL;
3414
- const branch = await __privateGet(this, _options).branch();
3622
+ const branch = __privateGet(this, _options).branch;
3415
3623
  return { databaseURL, branch };
3416
3624
  }
3417
- }, _branch = new WeakMap(), _options = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
3625
+ }, _options = new WeakMap(), _parseOptions = new WeakSet(), parseOptions_fn = function(options) {
3418
3626
  const enableBrowser = options?.enableBrowser ?? getEnableBrowserVariable() ?? false;
3419
3627
  const isBrowser = typeof window !== "undefined" && typeof Deno === "undefined";
3420
3628
  if (isBrowser && !enableBrowser) {
@@ -3424,18 +3632,13 @@ const buildClient = (plugins) => {
3424
3632
  }
3425
3633
  const fetch = getFetchImplementation(options?.fetch);
3426
3634
  const databaseURL = options?.databaseURL || getDatabaseURL();
3635
+ const branch = options?.branch || getBranch() || "main";
3427
3636
  const apiKey = options?.apiKey || getAPIKey();
3428
3637
  const cache = options?.cache ?? new SimpleCache({ defaultQueryTTL: 0 });
3429
3638
  const trace = options?.trace ?? defaultTrace;
3430
3639
  const clientName = options?.clientName;
3640
+ const host = options?.host ?? "production";
3431
3641
  const xataAgentExtra = options?.xataAgentExtra;
3432
- const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({
3433
- apiKey,
3434
- databaseURL,
3435
- fetchImpl: options?.fetch,
3436
- clientName,
3437
- xataAgentExtra
3438
- });
3439
3642
  if (!apiKey) {
3440
3643
  throw new Error("Option apiKey is required");
3441
3644
  }
@@ -3449,12 +3652,13 @@ const buildClient = (plugins) => {
3449
3652
  branch,
3450
3653
  cache,
3451
3654
  trace,
3655
+ host,
3452
3656
  clientID: generateUUID(),
3453
3657
  enableBrowser,
3454
3658
  clientName,
3455
3659
  xataAgentExtra
3456
3660
  };
3457
- }, _getFetchProps = new WeakSet(), getFetchProps_fn = async function({
3661
+ }, _getFetchProps = new WeakSet(), getFetchProps_fn = function({
3458
3662
  fetch,
3459
3663
  apiKey,
3460
3664
  databaseURL,
@@ -3464,16 +3668,13 @@ const buildClient = (plugins) => {
3464
3668
  clientName,
3465
3669
  xataAgentExtra
3466
3670
  }) {
3467
- const branchValue = await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, branch);
3468
- if (!branchValue)
3469
- throw new Error("Unable to resolve branch value");
3470
3671
  return {
3471
- fetchImpl: fetch,
3672
+ fetch,
3472
3673
  apiKey,
3473
3674
  apiUrl: "",
3474
3675
  workspacesApiUrl: (path, params) => {
3475
3676
  const hasBranch = params.dbBranchName ?? params.branch;
3476
- const newPath = path.replace(/^\/db\/[^/]+/, hasBranch !== void 0 ? `:${branchValue}` : "");
3677
+ const newPath = path.replace(/^\/db\/[^/]+/, hasBranch !== void 0 ? `:${branch}` : "");
3477
3678
  return databaseURL + newPath;
3478
3679
  },
3479
3680
  trace,
@@ -3481,22 +3682,6 @@ const buildClient = (plugins) => {
3481
3682
  clientName,
3482
3683
  xataAgentExtra
3483
3684
  };
3484
- }, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
3485
- if (__privateGet(this, _branch))
3486
- return __privateGet(this, _branch);
3487
- if (param === void 0)
3488
- return void 0;
3489
- const strategies = Array.isArray(param) ? [...param] : [param];
3490
- const evaluateBranch = async (strategy) => {
3491
- return isBranchStrategyBuilder(strategy) ? await strategy() : strategy;
3492
- };
3493
- for await (const strategy of strategies) {
3494
- const branch = await evaluateBranch(strategy);
3495
- if (branch) {
3496
- __privateSet(this, _branch, branch);
3497
- return branch;
3498
- }
3499
- }
3500
3685
  }, _a;
3501
3686
  };
3502
3687
  class BaseClient extends buildClient() {
@@ -3616,6 +3801,7 @@ exports.addGitBranchesEntry = addGitBranchesEntry;
3616
3801
  exports.addTableColumn = addTableColumn;
3617
3802
  exports.aggregateTable = aggregateTable;
3618
3803
  exports.applyBranchSchemaEdit = applyBranchSchemaEdit;
3804
+ exports.askTable = askTable;
3619
3805
  exports.branchTransaction = branchTransaction;
3620
3806
  exports.buildClient = buildClient;
3621
3807
  exports.buildWorkerRunner = buildWorkerRunner;
@@ -3625,6 +3811,7 @@ exports.compareBranchSchemas = compareBranchSchemas;
3625
3811
  exports.compareBranchWithUserSchema = compareBranchWithUserSchema;
3626
3812
  exports.compareMigrationRequest = compareMigrationRequest;
3627
3813
  exports.contains = contains;
3814
+ exports.copyBranch = copyBranch;
3628
3815
  exports.createBranch = createBranch;
3629
3816
  exports.createDatabase = createDatabase;
3630
3817
  exports.createMigrationRequest = createMigrationRequest;
@@ -3647,6 +3834,7 @@ exports.executeBranchMigrationPlan = executeBranchMigrationPlan;
3647
3834
  exports.exists = exists;
3648
3835
  exports.ge = ge;
3649
3836
  exports.getAPIKey = getAPIKey;
3837
+ exports.getBranch = getBranch;
3650
3838
  exports.getBranchDetails = getBranchDetails;
3651
3839
  exports.getBranchList = getBranchList;
3652
3840
  exports.getBranchMetadata = getBranchMetadata;
@@ -3655,8 +3843,6 @@ exports.getBranchMigrationPlan = getBranchMigrationPlan;
3655
3843
  exports.getBranchSchemaHistory = getBranchSchemaHistory;
3656
3844
  exports.getBranchStats = getBranchStats;
3657
3845
  exports.getColumn = getColumn;
3658
- exports.getCurrentBranchDetails = getCurrentBranchDetails;
3659
- exports.getCurrentBranchName = getCurrentBranchName;
3660
3846
  exports.getDatabaseGithubSettings = getDatabaseGithubSettings;
3661
3847
  exports.getDatabaseList = getDatabaseList;
3662
3848
  exports.getDatabaseMetadata = getDatabaseMetadata;