@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.cjs CHANGED
@@ -1844,6 +1844,7 @@ var DatabaseHandle = class {
1844
1844
  var SQLAction = {
1845
1845
  READ: "tinycloud.sql/read",
1846
1846
  WRITE: "tinycloud.sql/write",
1847
+ DDL: "tinycloud.sql/ddl",
1847
1848
  ADMIN: "tinycloud.sql/admin",
1848
1849
  SELECT: "tinycloud.sql/select",
1849
1850
  INSERT: "tinycloud.sql/insert",
@@ -1855,6 +1856,7 @@ var SQLAction = {
1855
1856
  };
1856
1857
 
1857
1858
  // src/sql/SQLService.ts
1859
+ var DDL_TOKENS = /* @__PURE__ */ new Set(["alter", "create", "drop"]);
1858
1860
  var SQLService = class extends BaseService {
1859
1861
  constructor(config = {}) {
1860
1862
  super();
@@ -1930,9 +1932,15 @@ var SQLService = class extends BaseService {
1930
1932
  if (options?.schema) {
1931
1933
  body.schema = options.schema;
1932
1934
  }
1935
+ const actions = [
1936
+ this.actionForSql(sql, SQLAction.WRITE),
1937
+ ...(options?.schema ?? []).map(
1938
+ (statement) => this.actionForSql(statement, SQLAction.DDL)
1939
+ )
1940
+ ];
1933
1941
  const response = await this.invokeSQL(
1934
1942
  dbName,
1935
- this.actionForSql(sql, SQLAction.WRITE),
1943
+ this.dedupeActions(actions),
1936
1944
  body,
1937
1945
  options?.signal
1938
1946
  );
@@ -1954,7 +1962,7 @@ var SQLService = class extends BaseService {
1954
1962
  try {
1955
1963
  const response = await this.invokeSQL(
1956
1964
  dbName,
1957
- this.actionForSqlBatch(statements),
1965
+ this.actionsForSqlBatch(statements),
1958
1966
  { action: "batch", statements },
1959
1967
  options?.signal
1960
1968
  );
@@ -2018,9 +2026,10 @@ var SQLService = class extends BaseService {
2018
2026
  });
2019
2027
  }
2020
2028
  // === Private helpers ===
2021
- async invokeSQL(dbName, action, body, signal) {
2029
+ async invokeSQL(dbName, actions, body, signal) {
2022
2030
  const session = this.context.session;
2023
- const headers = this.context.invoke(session, "sql", dbName, action);
2031
+ const actionList = Array.isArray(actions) ? actions : [actions];
2032
+ const headers = actionList.length === 1 ? this.context.invoke(session, "sql", dbName, actionList[0]) : this.invokeSQLAny(session, dbName, actionList);
2024
2033
  return this.context.fetch(`${this.host}/invoke`, {
2025
2034
  method: "POST",
2026
2035
  headers: {
@@ -2032,12 +2041,34 @@ var SQLService = class extends BaseService {
2032
2041
  });
2033
2042
  }
2034
2043
  actionForSql(sql, fallback) {
2035
- return firstSqlToken(sql) === "pragma" ? SQLAction.ADMIN : fallback;
2044
+ const token = firstSqlToken(sql);
2045
+ if (token === "pragma") return SQLAction.ADMIN;
2046
+ if (token !== void 0 && DDL_TOKENS.has(token)) return SQLAction.DDL;
2047
+ return fallback;
2048
+ }
2049
+ actionsForSqlBatch(statements) {
2050
+ return this.dedupeActions(
2051
+ statements.map((statement) => this.actionForSql(statement.sql, SQLAction.WRITE))
2052
+ );
2053
+ }
2054
+ dedupeActions(actions) {
2055
+ return [...new Set(actions)];
2036
2056
  }
2037
- actionForSqlBatch(statements) {
2038
- return statements.some(
2039
- (statement) => this.actionForSql(statement.sql, SQLAction.WRITE) === SQLAction.ADMIN
2040
- ) ? SQLAction.ADMIN : SQLAction.WRITE;
2057
+ invokeSQLAny(session, dbName, actions) {
2058
+ if (!this.context.invokeAny) {
2059
+ throw new Error(
2060
+ `SQL operation requires multiple permissions (${actions.join(", ")}) but this SDK runtime does not support multi-resource invocations`
2061
+ );
2062
+ }
2063
+ return this.context.invokeAny(
2064
+ session,
2065
+ actions.map((action) => ({
2066
+ spaceId: session.spaceId,
2067
+ service: "sql",
2068
+ path: dbName,
2069
+ action
2070
+ }))
2071
+ );
2041
2072
  }
2042
2073
  async handleErrorResponse(response, operation) {
2043
2074
  const errorText = await response.text();