@xata.io/client 0.29.1 → 0.29.3

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.mjs CHANGED
@@ -526,7 +526,7 @@ function defaultOnOpen(response) {
526
526
  }
527
527
  }
528
528
 
529
- const VERSION = "0.29.1";
529
+ const VERSION = "0.29.3";
530
530
 
531
531
  class ErrorWithCause extends Error {
532
532
  constructor(message, options) {
@@ -619,15 +619,15 @@ function parseWorkspacesUrlParts(url) {
619
619
  if (!isString(url))
620
620
  return null;
621
621
  const matches = {
622
- production: url.match(/(?:https:\/\/)?([^.]+)(?:\.([^.]+))\.xata\.sh.*/),
623
- staging: url.match(/(?:https:\/\/)?([^.]+)(?:\.([^.]+))\.staging-xata\.dev.*/),
624
- dev: url.match(/(?:https:\/\/)?([^.]+)(?:\.([^.]+))\.dev-xata\.dev.*/),
625
- local: url.match(/(?:https?:\/\/)?([^.]+)(?:\.([^.]+))\.localhost:(\d+)/)
622
+ production: url.match(/(?:https:\/\/)?([^.]+)(?:\.([^.]+))\.xata\.sh\/db\/([^:]+):?(.*)?/),
623
+ staging: url.match(/(?:https:\/\/)?([^.]+)(?:\.([^.]+))\.staging-xata\.dev\/db\/([^:]+):?(.*)?/),
624
+ dev: url.match(/(?:https:\/\/)?([^.]+)(?:\.([^.]+))\.dev-xata\.dev\/db\/([^:]+):?(.*)?/),
625
+ local: url.match(/(?:https?:\/\/)?([^.]+)(?:\.([^.]+))\.localhost:([^:]+):?(.*)?/)
626
626
  };
627
627
  const [host, match] = Object.entries(matches).find(([, match2]) => match2 !== null) ?? [];
628
628
  if (!isHostProviderAlias(host) || !match)
629
629
  return null;
630
- return { workspace: match[1], region: match[2], host };
630
+ return { workspace: match[1], region: match[2], database: match[3], branch: match[4], host };
631
631
  }
632
632
 
633
633
  const pool = new ApiRequestPool();
@@ -845,26 +845,29 @@ function parseUrl(url) {
845
845
 
846
846
  const dataPlaneFetch = async (options) => fetch$1({ ...options, endpoint: "dataPlane" });
847
847
 
848
- const applyMigration = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/pgroll/apply", method: "post", ...variables, signal });
849
- const pgRollStatus = (variables, signal) => dataPlaneFetch({
850
- url: "/db/{dbBranchName}/pgroll/status",
851
- method: "get",
848
+ const applyMigration = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/migrations/apply", method: "post", ...variables, signal });
849
+ const adaptTable = (variables, signal) => dataPlaneFetch({
850
+ url: "/db/{dbBranchName}/migrations/adapt/{tableName}",
851
+ method: "post",
852
852
  ...variables,
853
853
  signal
854
854
  });
855
- const pgRollJobStatus = (variables, signal) => dataPlaneFetch({
856
- url: "/db/{dbBranchName}/pgroll/jobs/{jobId}",
855
+ const getBranchMigrationJobStatus = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/migrations/status", method: "get", ...variables, signal });
856
+ const getMigrationJobStatus = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/migrations/jobs/{jobId}", method: "get", ...variables, signal });
857
+ const getMigrationHistory = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/migrations/history", method: "get", ...variables, signal });
858
+ const getBranchList = (variables, signal) => dataPlaneFetch({
859
+ url: "/dbs/{dbName}",
857
860
  method: "get",
858
861
  ...variables,
859
862
  signal
860
863
  });
861
- const pgRollMigrationHistory = (variables, signal) => dataPlaneFetch({ url: "/db/{dbBranchName}/pgroll/migrations", method: "get", ...variables, signal });
862
- const getBranchList = (variables, signal) => dataPlaneFetch({
863
- url: "/dbs/{dbName}",
864
+ const getDatabaseSettings = (variables, signal) => dataPlaneFetch({
865
+ url: "/dbs/{dbName}/settings",
864
866
  method: "get",
865
867
  ...variables,
866
868
  signal
867
869
  });
870
+ const updateDatabaseSettings = (variables, signal) => dataPlaneFetch({ url: "/dbs/{dbName}/settings", method: "patch", ...variables, signal });
868
871
  const getBranchDetails = (variables, signal) => dataPlaneFetch({
869
872
  url: "/db/{dbBranchName}",
870
873
  method: "get",
@@ -1078,11 +1081,25 @@ const sqlQuery = (variables, signal) => dataPlaneFetch({
1078
1081
  signal
1079
1082
  });
1080
1083
  const operationsByTag$2 = {
1081
- branch: {
1084
+ migrations: {
1082
1085
  applyMigration,
1083
- pgRollStatus,
1084
- pgRollJobStatus,
1085
- pgRollMigrationHistory,
1086
+ adaptTable,
1087
+ getBranchMigrationJobStatus,
1088
+ getMigrationJobStatus,
1089
+ getMigrationHistory,
1090
+ getSchema,
1091
+ getBranchMigrationHistory,
1092
+ getBranchMigrationPlan,
1093
+ executeBranchMigrationPlan,
1094
+ getBranchSchemaHistory,
1095
+ compareBranchWithUserSchema,
1096
+ compareBranchSchemas,
1097
+ updateBranchSchema,
1098
+ previewBranchSchemaEdit,
1099
+ applyBranchSchemaEdit,
1100
+ pushBranchMigrations
1101
+ },
1102
+ branch: {
1086
1103
  getBranchList,
1087
1104
  getBranchDetails,
1088
1105
  createBranch,
@@ -1096,19 +1113,7 @@ const operationsByTag$2 = {
1096
1113
  removeGitBranchesEntry,
1097
1114
  resolveBranch
1098
1115
  },
1099
- migrations: {
1100
- getSchema,
1101
- getBranchMigrationHistory,
1102
- getBranchMigrationPlan,
1103
- executeBranchMigrationPlan,
1104
- getBranchSchemaHistory,
1105
- compareBranchWithUserSchema,
1106
- compareBranchSchemas,
1107
- updateBranchSchema,
1108
- previewBranchSchemaEdit,
1109
- applyBranchSchemaEdit,
1110
- pushBranchMigrations
1111
- },
1116
+ database: { getDatabaseSettings, updateDatabaseSettings },
1112
1117
  migrationRequests: {
1113
1118
  queryMigrationRequests,
1114
1119
  createMigrationRequest,
@@ -1250,6 +1255,8 @@ const deleteWorkspace = (variables, signal) => controlPlaneFetch({
1250
1255
  ...variables,
1251
1256
  signal
1252
1257
  });
1258
+ const getWorkspaceSettings = (variables, signal) => controlPlaneFetch({ url: "/workspaces/{workspaceId}/settings", method: "get", ...variables, signal });
1259
+ const updateWorkspaceSettings = (variables, signal) => controlPlaneFetch({ url: "/workspaces/{workspaceId}/settings", method: "patch", ...variables, signal });
1253
1260
  const getWorkspaceMembersList = (variables, signal) => controlPlaneFetch({ url: "/workspaces/{workspaceId}/members", method: "get", ...variables, signal });
1254
1261
  const updateWorkspaceMemberRole = (variables, signal) => controlPlaneFetch({ url: "/workspaces/{workspaceId}/members/{userId}", method: "put", ...variables, signal });
1255
1262
  const removeWorkspaceMember = (variables, signal) => controlPlaneFetch({
@@ -1315,6 +1322,8 @@ const operationsByTag$1 = {
1315
1322
  getWorkspace,
1316
1323
  updateWorkspace,
1317
1324
  deleteWorkspace,
1325
+ getWorkspaceSettings,
1326
+ updateWorkspaceSettings,
1318
1327
  getWorkspaceMembersList,
1319
1328
  updateWorkspaceMemberRole,
1320
1329
  removeWorkspaceMember
@@ -1750,7 +1759,7 @@ class BranchApi {
1750
1759
  database,
1751
1760
  branch
1752
1761
  }) {
1753
- return operationsByTag.branch.pgRollMigrationHistory({
1762
+ return operationsByTag.migrations.getMigrationHistory({
1754
1763
  pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
1755
1764
  ...this.extraProps
1756
1765
  });
@@ -1762,7 +1771,7 @@ class BranchApi {
1762
1771
  branch,
1763
1772
  migration
1764
1773
  }) {
1765
- return operationsByTag.branch.applyMigration({
1774
+ return operationsByTag.migrations.applyMigration({
1766
1775
  pathParams: { workspace, region, dbBranchName: `${database}:${branch}` },
1767
1776
  body: migration,
1768
1777
  ...this.extraProps
@@ -4582,26 +4591,33 @@ function prepareParams(param1, param2) {
4582
4591
  return { statement, params: param2?.map((value) => prepareValue(value)) };
4583
4592
  }
4584
4593
  if (isObject(param1)) {
4585
- const { statement, params, consistency } = param1;
4586
- return { statement, params: params?.map((value) => prepareValue(value)), consistency };
4594
+ const { statement, params, consistency, responseType } = param1;
4595
+ return { statement, params: params?.map((value) => prepareValue(value)), consistency, responseType };
4587
4596
  }
4588
4597
  throw new Error("Invalid query");
4589
4598
  }
4590
4599
 
4591
4600
  class SQLPlugin extends XataPlugin {
4592
4601
  build(pluginOptions) {
4593
- return async (query, ...parameters) => {
4602
+ const sqlFunction = async (query, ...parameters) => {
4594
4603
  if (!isParamsObject(query) && (!isTemplateStringsArray(query) || !Array.isArray(parameters))) {
4595
4604
  throw new Error("Invalid usage of `xata.sql`. Please use it as a tagged template or with an object.");
4596
4605
  }
4597
- const { statement, params, consistency } = prepareParams(query, parameters);
4598
- const { records, warning, columns } = await sqlQuery({
4606
+ const { statement, params, consistency, responseType } = prepareParams(query, parameters);
4607
+ const {
4608
+ records,
4609
+ rows,
4610
+ warning,
4611
+ columns = []
4612
+ } = await sqlQuery({
4599
4613
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", region: "{region}" },
4600
- body: { statement, params, consistency },
4614
+ body: { statement, params, consistency, responseType },
4601
4615
  ...pluginOptions
4602
4616
  });
4603
- return { records, warning, columns };
4617
+ return { records, rows, warning, columns };
4604
4618
  };
4619
+ sqlFunction.connectionString = buildConnectionString(pluginOptions);
4620
+ return sqlFunction;
4605
4621
  }
4606
4622
  }
4607
4623
  function isTemplateStringsArray(strings) {
@@ -4610,6 +4626,33 @@ function isTemplateStringsArray(strings) {
4610
4626
  function isParamsObject(params) {
4611
4627
  return isObject(params) && "statement" in params;
4612
4628
  }
4629
+ function buildDomain(host, region) {
4630
+ switch (host) {
4631
+ case "production":
4632
+ return `${region}.sql.xata.sh`;
4633
+ case "staging":
4634
+ return `${region}.sql.staging-xata.dev`;
4635
+ case "dev":
4636
+ return `${region}.sql.dev-xata.dev`;
4637
+ case "local":
4638
+ return "localhost:7654";
4639
+ default:
4640
+ throw new Error("Invalid host provider");
4641
+ }
4642
+ }
4643
+ function buildConnectionString({ apiKey, workspacesApiUrl, branch }) {
4644
+ const url = isString(workspacesApiUrl) ? workspacesApiUrl : workspacesApiUrl("", {});
4645
+ const parts = parseWorkspacesUrlParts(url);
4646
+ if (!parts)
4647
+ throw new Error("Invalid workspaces URL");
4648
+ const { workspace: workspaceSlug, region, database, host } = parts;
4649
+ const domain = buildDomain(host, region);
4650
+ const workspace = workspaceSlug.split("-").pop();
4651
+ if (!workspace || !region || !database || !apiKey || !branch) {
4652
+ throw new Error("Unable to build xata connection string");
4653
+ }
4654
+ return `postgresql://${workspace}:${apiKey}@${domain}/${database}:${branch}?sslmode=require`;
4655
+ }
4613
4656
 
4614
4657
  class TransactionPlugin extends XataPlugin {
4615
4658
  build(pluginOptions) {
@@ -4661,7 +4704,8 @@ const buildClient = (plugins) => {
4661
4704
  ...__privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
4662
4705
  cache: safeOptions.cache,
4663
4706
  host: safeOptions.host,
4664
- tables
4707
+ tables,
4708
+ branch: safeOptions.branch
4665
4709
  };
4666
4710
  const db = new SchemaPlugin().build(pluginOptions);
4667
4711
  const search = new SearchPlugin(db).build(pluginOptions);
@@ -4690,7 +4734,7 @@ const buildClient = (plugins) => {
4690
4734
  const isBrowser = typeof window !== "undefined" && typeof Deno === "undefined";
4691
4735
  if (isBrowser && !enableBrowser) {
4692
4736
  throw new Error(
4693
- "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."
4737
+ "You are trying to use Xata from the browser, which is potentially a non-secure environment. How to fix: https://xata.io/docs/messages/api-key-browser-error"
4694
4738
  );
4695
4739
  }
4696
4740
  const fetch = getFetchImplementation(options?.fetch);
@@ -4844,5 +4888,5 @@ class XataError extends Error {
4844
4888
  }
4845
4889
  }
4846
4890
 
4847
- export { BaseClient, FetcherError, FilesPlugin, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, PageRecordArray, Query, RecordArray, RecordColumnTypes, Repository, RestRepository, SQLPlugin, SchemaPlugin, SearchPlugin, Serializer, SimpleCache, TransactionPlugin, XataApiClient, XataApiPlugin, XataError, XataFile, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, aggregateTable, applyBranchSchemaEdit, applyMigration, askTable, askTableSession, branchTransaction, buildClient, buildPreviewBranchName, buildProviderString, bulkInsertTableRecords, cancelWorkspaceMemberInvite, compareBranchSchemas, compareBranchWithUserSchema, compareMigrationRequest, contains, copyBranch, createBranch, createCluster, createDatabase, createMigrationRequest, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteDatabaseGithubSettings, deleteFile, deleteFileItem, deleteOAuthAccessToken, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteUserOAuthClient, deleteWorkspace, deserialize, endsWith, equals, executeBranchMigrationPlan, exists, fileAccess, fileUpload, ge, getAPIKey, getAuthorizationCode, getBranch, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchSchemaHistory, getBranchStats, getCluster, getColumn, getDatabaseGithubSettings, getDatabaseList, getDatabaseMetadata, getDatabaseURL, getFile, getFileItem, getGitBranchesMapping, getHostUrl, getMigrationRequest, getMigrationRequestIsMerged, getPreviewBranch, getRecord, getSchema, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getUserOAuthAccessTokens, getUserOAuthClients, getWorkspace, getWorkspaceMembersList, getWorkspacesList, grantAuthorizationCode, greaterEquals, greaterThan, greaterThanEquals, gt, gte, iContains, iPattern, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isHostProviderAlias, isHostProviderBuilder, isIdentifiable, isNot, isValidExpandedColumn, isValidSelectableColumns, isXataRecord, le, lessEquals, lessThan, lessThanEquals, listClusters, listMigrationRequestsCommits, listRegions, lt, lte, mergeMigrationRequest, notExists, operationsByTag, parseProviderString, parseWorkspacesUrlParts, pattern, pgRollJobStatus, pgRollMigrationHistory, pgRollStatus, previewBranchSchemaEdit, pushBranchMigrations, putFile, putFileItem, queryMigrationRequests, queryTable, removeGitBranchesEntry, removeWorkspaceMember, renameDatabase, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, serialize, setTableSchema, sqlQuery, startsWith, summarizeTable, transformImage, updateBranchMetadata, updateBranchSchema, updateCluster, updateColumn, updateDatabaseGithubSettings, updateDatabaseMetadata, updateMigrationRequest, updateOAuthAccessToken, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberInvite, updateWorkspaceMemberRole, upsertRecordWithID, vectorSearchTable };
4891
+ export { BaseClient, FetcherError, FilesPlugin, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, PageRecordArray, Query, RecordArray, RecordColumnTypes, Repository, RestRepository, SQLPlugin, SchemaPlugin, SearchPlugin, Serializer, SimpleCache, TransactionPlugin, XataApiClient, XataApiPlugin, XataError, XataFile, XataPlugin, acceptWorkspaceMemberInvite, adaptTable, addGitBranchesEntry, addTableColumn, aggregateTable, applyBranchSchemaEdit, applyMigration, askTable, askTableSession, branchTransaction, buildClient, buildPreviewBranchName, buildProviderString, bulkInsertTableRecords, cancelWorkspaceMemberInvite, compareBranchSchemas, compareBranchWithUserSchema, compareMigrationRequest, contains, copyBranch, createBranch, createCluster, createDatabase, createMigrationRequest, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteDatabaseGithubSettings, deleteFile, deleteFileItem, deleteOAuthAccessToken, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteUserOAuthClient, deleteWorkspace, deserialize, endsWith, equals, executeBranchMigrationPlan, exists, fileAccess, fileUpload, ge, getAPIKey, getAuthorizationCode, getBranch, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationJobStatus, getBranchMigrationPlan, getBranchSchemaHistory, getBranchStats, getCluster, getColumn, getDatabaseGithubSettings, getDatabaseList, getDatabaseMetadata, getDatabaseSettings, getDatabaseURL, getFile, getFileItem, getGitBranchesMapping, getHostUrl, getMigrationHistory, getMigrationJobStatus, getMigrationRequest, getMigrationRequestIsMerged, getPreviewBranch, getRecord, getSchema, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getUserOAuthAccessTokens, getUserOAuthClients, getWorkspace, getWorkspaceMembersList, getWorkspaceSettings, getWorkspacesList, grantAuthorizationCode, greaterEquals, greaterThan, greaterThanEquals, gt, gte, iContains, iPattern, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isHostProviderAlias, isHostProviderBuilder, isIdentifiable, isNot, isValidExpandedColumn, isValidSelectableColumns, isXataRecord, le, lessEquals, lessThan, lessThanEquals, listClusters, listMigrationRequestsCommits, listRegions, lt, lte, mergeMigrationRequest, notExists, operationsByTag, parseProviderString, parseWorkspacesUrlParts, pattern, previewBranchSchemaEdit, pushBranchMigrations, putFile, putFileItem, queryMigrationRequests, queryTable, removeGitBranchesEntry, removeWorkspaceMember, renameDatabase, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, serialize, setTableSchema, sqlQuery, startsWith, summarizeTable, transformImage, updateBranchMetadata, updateBranchSchema, updateCluster, updateColumn, updateDatabaseGithubSettings, updateDatabaseMetadata, updateDatabaseSettings, updateMigrationRequest, updateOAuthAccessToken, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberInvite, updateWorkspaceMemberRole, updateWorkspaceSettings, upsertRecordWithID, vectorSearchTable };
4848
4892
  //# sourceMappingURL=index.mjs.map