Skip to content

feat: add additional function create options #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/lib/PostgresMetaFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,37 @@ export default class PostgresMetaFunctions {
definition,
rettype = 'void',
language = 'sql',
behavior = 'VOLATILE',
security_definer = false,
config_params,
}: {
name: string
schema?: string
args?: string[]
definition: string
rettype?: string
language?: string
behavior?: 'IMMUTABLE' | 'STABLE' | 'VOLATILE'
security_definer?: boolean
config_params: { [key: string]: string[] }
}): Promise<PostgresMetaResult<PostgresFunction>> {
const sql = `
CREATE FUNCTION ${ident(schema)}.${ident(name)}(${args.join(', ')})
RETURNS ${rettype}
AS ${literal(definition)}
LANGUAGE ${language}
${behavior}
${security_definer ? 'SECURITY DEFINER' : 'SECURITY INVOKER'}
${Object.entries(config_params)
.map(
([param, values]) =>
`SET ${param} ${
values[0] === 'FROM CURRENT' ? 'FROM CURRENT' : 'TO ' + values.map(ident).join(',')
}`
)
.join('\n')}
RETURNS NULL ON NULL INPUT;
`
`
const { error } = await this.query(sql)
if (error) {
return { data: null, error }
Expand Down
27 changes: 26 additions & 1 deletion src/lib/sql/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,34 @@ SELECT
ELSE pg_get_functiondef(p.oid)
END AS definition,
pg_get_function_arguments(p.oid) AS argument_types,
t.typname AS return_type
t.typname AS return_type,
CASE
WHEN p.provolatile = 'i' THEN 'IMMUTABLE'
WHEN p.provolatile = 's' THEN 'STABLE'
WHEN p.provolatile = 'v' THEN 'VOLATILE'
END AS behavior,
p.prosecdef AS security_definer,
JSON_OBJECT_AGG(p_config.param, p_config.values)
FILTER (WHERE p_config.param IS NOT NULL) AS config_params
FROM
pg_proc p
LEFT JOIN pg_namespace n ON p.pronamespace = n.oid
LEFT JOIN pg_language l ON p.prolang = l.oid
LEFT JOIN pg_type t ON t.oid = p.prorettype
LEFT JOIN (
SELECT
oid as id,
(string_to_array(unnest(proconfig), '='))[1] AS param,
string_to_array((string_to_array(unnest(proconfig), '='))[2], ', ') AS values
FROM
pg_proc
) p_config ON p_config.id = p.oid
Comment on lines +26 to +32
Copy link
Member Author

@w3b6x9 w3b6x9 Jul 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@soedirgo not sure why the indentation here is so weird. Looks fine on my local:

Screen Shot 2021-07-20 at 3 14 52 AM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, looks like some tabs starting from line 27. Not a big deal.

GROUP BY
p.oid,
n.nspname,
p.proname,
l.lanname,
p.prosrc,
t.typname,
p.provolatile,
p.prosecdef
7 changes: 7 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ const postgresFunctionSchema = Type.Object({
definition: Type.String(),
argument_types: Type.String(),
return_type: Type.String(),
behavior: Type.Union([
Type.Literal('IMMUTABLE'),
Type.Literal('STABLE'),
Type.Literal('VOLATILE'),
]),
security_definer: Type.Boolean(),
config_params: Type.Union([Type.Dict(Type.Array(Type.String())), Type.Null()]),
})
export type PostgresFunction = Static<typeof postgresFunctionSchema>

Expand Down
6 changes: 6 additions & 0 deletions test/integration/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ describe('/functions', () => {
definition: 'select $1 + $2',
rettype: 'integer',
language: 'sql',
behavior: 'STABLE',
security_definer: true,
config_params: { search_path: ['hooks', 'auth'], role: ['postgres'] },
}
before(async () => {
await axios.post(`${URL}/query`, {
Expand Down Expand Up @@ -212,6 +215,9 @@ describe('/functions', () => {
assert.strictEqual(newFunc.schema, 'public')
assert.strictEqual(newFunc.language, 'sql')
assert.strictEqual(newFunc.return_type, 'int4')
assert.strictEqual(newFunc.behavior, 'STABLE')
assert.strictEqual(newFunc.security_definer, true)
assert.deepStrictEqual(newFunc.config_params, { search_path: ['hooks', 'auth'], role: ['postgres'] })
func.id = newFunc.id
})
it('PATCH', async () => {
Expand Down