@tinycloud/sdk-services 2.4.0-beta.2 → 2.4.0-beta.8

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.js CHANGED
@@ -1714,6 +1714,7 @@ var DatabaseHandle = class {
1714
1714
  var SQLAction = {
1715
1715
  READ: "tinycloud.sql/read",
1716
1716
  WRITE: "tinycloud.sql/write",
1717
+ DDL: "tinycloud.sql/ddl",
1717
1718
  ADMIN: "tinycloud.sql/admin",
1718
1719
  SELECT: "tinycloud.sql/select",
1719
1720
  INSERT: "tinycloud.sql/insert",
@@ -1725,6 +1726,7 @@ var SQLAction = {
1725
1726
  };
1726
1727
 
1727
1728
  // src/sql/SQLService.ts
1729
+ var DDL_TOKENS = /* @__PURE__ */ new Set(["alter", "create", "drop"]);
1728
1730
  var SQLService = class extends BaseService {
1729
1731
  constructor(config = {}) {
1730
1732
  super();
@@ -1800,9 +1802,15 @@ var SQLService = class extends BaseService {
1800
1802
  if (options?.schema) {
1801
1803
  body.schema = options.schema;
1802
1804
  }
1805
+ const actions = [
1806
+ this.actionForSql(sql, SQLAction.WRITE),
1807
+ ...(options?.schema ?? []).map(
1808
+ (statement) => this.actionForSql(statement, SQLAction.DDL)
1809
+ )
1810
+ ];
1803
1811
  const response = await this.invokeSQL(
1804
1812
  dbName,
1805
- this.actionForSql(sql, SQLAction.WRITE),
1813
+ this.dedupeActions(actions),
1806
1814
  body,
1807
1815
  options?.signal
1808
1816
  );
@@ -1824,7 +1832,7 @@ var SQLService = class extends BaseService {
1824
1832
  try {
1825
1833
  const response = await this.invokeSQL(
1826
1834
  dbName,
1827
- this.actionForSqlBatch(statements),
1835
+ this.actionsForSqlBatch(statements),
1828
1836
  { action: "batch", statements },
1829
1837
  options?.signal
1830
1838
  );
@@ -1888,9 +1896,10 @@ var SQLService = class extends BaseService {
1888
1896
  });
1889
1897
  }
1890
1898
  // === Private helpers ===
1891
- async invokeSQL(dbName, action, body, signal) {
1899
+ async invokeSQL(dbName, actions, body, signal) {
1892
1900
  const session = this.context.session;
1893
- const headers = this.context.invoke(session, "sql", dbName, action);
1901
+ const actionList = Array.isArray(actions) ? actions : [actions];
1902
+ const headers = actionList.length === 1 ? this.context.invoke(session, "sql", dbName, actionList[0]) : this.invokeSQLAny(session, dbName, actionList);
1894
1903
  return this.context.fetch(`${this.host}/invoke`, {
1895
1904
  method: "POST",
1896
1905
  headers: {
@@ -1902,12 +1911,34 @@ var SQLService = class extends BaseService {
1902
1911
  });
1903
1912
  }
1904
1913
  actionForSql(sql, fallback) {
1905
- return firstSqlToken(sql) === "pragma" ? SQLAction.ADMIN : fallback;
1914
+ const token = firstSqlToken(sql);
1915
+ if (token === "pragma") return SQLAction.ADMIN;
1916
+ if (token !== void 0 && DDL_TOKENS.has(token)) return SQLAction.DDL;
1917
+ return fallback;
1918
+ }
1919
+ actionsForSqlBatch(statements) {
1920
+ return this.dedupeActions(
1921
+ statements.map((statement) => this.actionForSql(statement.sql, SQLAction.WRITE))
1922
+ );
1923
+ }
1924
+ dedupeActions(actions) {
1925
+ return [...new Set(actions)];
1906
1926
  }
1907
- actionForSqlBatch(statements) {
1908
- return statements.some(
1909
- (statement) => this.actionForSql(statement.sql, SQLAction.WRITE) === SQLAction.ADMIN
1910
- ) ? SQLAction.ADMIN : SQLAction.WRITE;
1927
+ invokeSQLAny(session, dbName, actions) {
1928
+ if (!this.context.invokeAny) {
1929
+ throw new Error(
1930
+ `SQL operation requires multiple permissions (${actions.join(", ")}) but this SDK runtime does not support multi-resource invocations`
1931
+ );
1932
+ }
1933
+ return this.context.invokeAny(
1934
+ session,
1935
+ actions.map((action) => ({
1936
+ spaceId: session.spaceId,
1937
+ service: "sql",
1938
+ path: dbName,
1939
+ action
1940
+ }))
1941
+ );
1911
1942
  }
1912
1943
  async handleErrorResponse(response, operation) {
1913
1944
  const errorText = await response.text();