Home / Function/ createSQLStatementForUpdatePolicy() — supabase Function Reference

createSQLStatementForUpdatePolicy() — supabase Function Reference

Architecture documentation for the createSQLStatementForUpdatePolicy() function in Policies.utils.ts from the supabase codebase.

Entity Profile

Dependency Diagram

graph TD
  463c153b_efac_aae1_ca17_44b9918d55bd["createSQLStatementForUpdatePolicy()"]
  985f2d7f_95ed_bd41_d27b_4f31564dcf82["createSQLPolicy()"]
  985f2d7f_95ed_bd41_d27b_4f31564dcf82 -->|calls| 463c153b_efac_aae1_ca17_44b9918d55bd
  style 463c153b_efac_aae1_ca17_44b9918d55bd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

apps/studio/components/interfaces/Auth/Policies/Policies.utils.ts lines 81–114

const createSQLStatementForUpdatePolicy = (
  policyFormFields: PolicyFormField,
  fieldsToUpdate: Partial<PolicyFormField>
): PolicyForReview => {
  const { name, schema, table } = policyFormFields

  const definitionChanged = has(fieldsToUpdate, ['definition'])
  const checkChanged = has(fieldsToUpdate, ['check'])
  const nameChanged = has(fieldsToUpdate, ['name'])
  const rolesChanged = has(fieldsToUpdate, ['roles'])

  const parameters = Object.keys(fieldsToUpdate)
  const description = `Update policy's ${
    parameters.length === 1
      ? parameters[0]
      : `${parameters.slice(0, parameters.length - 1).join(', ')} and ${
          parameters[parameters.length - 1]
        }`
  } `
  const roles =
    (fieldsToUpdate?.roles ?? []).length === 0 ? ['public'] : (fieldsToUpdate.roles as string[])

  const alterStatement = `ALTER POLICY "${name}" ON "${schema}"."${table}"`
  const statement = [
    'BEGIN;',
    ...(definitionChanged ? [`  ${alterStatement} USING (${fieldsToUpdate.definition});`] : []),
    ...(checkChanged ? [`  ${alterStatement} WITH CHECK (${fieldsToUpdate.check});`] : []),
    ...(rolesChanged ? [`  ${alterStatement} TO ${roles.join(', ')};`] : []),
    ...(nameChanged ? [`  ${alterStatement} RENAME TO "${fieldsToUpdate.name}";`] : []),
    'COMMIT;',
  ].join('\n')

  return { description, statement }
}

Subdomains

Called By

Frequently Asked Questions

What does createSQLStatementForUpdatePolicy() do?
createSQLStatementForUpdatePolicy() is a function in the supabase codebase.
What calls createSQLStatementForUpdatePolicy()?
createSQLStatementForUpdatePolicy() is called by 1 function(s): createSQLPolicy.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free