Skip to content

Commit af8fa3e

Browse files
w3b6x9soedirgo
authored andcommitted
refactor: convert FunctionInputs to PostgresFunctionCreate
1 parent c634704 commit af8fa3e

File tree

4 files changed

+59
-54
lines changed

4 files changed

+59
-54
lines changed

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"test": "run-s build:server && node -r esm ./node_modules/.bin/mocha 'test/**/*.js' --recursive"
2727
},
2828
"dependencies": {
29-
"@sinclair/typebox": "^0.16.7",
29+
"@sinclair/typebox": "^0.19.2",
3030
"pg": "^7.0.0",
3131
"pg-format": "^1.0.4"
3232
},

src/lib/PostgresMetaFunctions.ts

+35-45
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
import { ident, literal } from 'pg-format'
22
import { DEFAULT_SYSTEM_SCHEMAS } from './constants'
33
import { functionsSql } from './sql'
4-
import { PostgresMetaResult, PostgresFunction } from './types'
5-
6-
type FunctionInputs = {
7-
name: string
8-
definition: string
9-
args?: string[]
10-
behavior?: 'IMMUTABLE' | 'STABLE' | 'VOLATILE'
11-
config_params?: { [key: string]: string }
12-
schema?: string
13-
language?: string
14-
return_type?: string
15-
security_definer?: boolean
16-
}
4+
import { PostgresMetaResult, PostgresFunction, PostgresFunctionCreate } from './types'
175

186
export default class PostgresMetaFunctions {
197
query: (sql: string) => Promise<PostgresMetaResult<any>>
@@ -94,7 +82,7 @@ export default class PostgresMetaFunctions {
9482
behavior = 'VOLATILE',
9583
security_definer = false,
9684
config_params = {},
97-
}: FunctionInputs): Promise<PostgresMetaResult<PostgresFunction>> {
85+
}: PostgresFunctionCreate): Promise<PostgresMetaResult<PostgresFunction>> {
9886
const sql = this.generateCreateFunctionSql({
9987
name,
10088
schema,
@@ -130,19 +118,26 @@ export default class PostgresMetaFunctions {
130118
return { data: null, error }
131119
}
132120

133-
const updateDefinitionSql = typeof definition === 'string' ? this.generateCreateFunctionSql(
134-
{ ...currentFunc!, definition },
135-
{ replace: true }
136-
) : ''
121+
const args = currentFunc!.argument_types.split(', ')
137122

138-
const retrieveFunctionSql = this.generateRetrieveFunctionSql(
139-
{
140-
schema: currentFunc!.schema,
141-
name: currentFunc!.name,
142-
args: currentFunc!.argument_types.split(', '),
143-
},
144-
{ terminateCommand: false }
145-
)
123+
const updateDefinitionSql =
124+
typeof definition === 'string'
125+
? this.generateCreateFunctionSql(
126+
{
127+
...currentFunc!,
128+
definition,
129+
args,
130+
config_params: currentFunc!.config_params ?? {},
131+
},
132+
{ replace: true }
133+
)
134+
: ''
135+
136+
const retrieveFunctionSql = this.generateRetrieveFunctionSql({
137+
schema: currentFunc!.schema,
138+
name: currentFunc!.name,
139+
args,
140+
})
146141

147142
const updateNameSql =
148143
name && name !== currentFunc!.name
@@ -218,19 +213,18 @@ export default class PostgresMetaFunctions {
218213
name,
219214
schema,
220215
args,
221-
argument_types,
222216
definition,
223217
return_type,
224218
language,
225219
behavior,
226220
security_definer,
227221
config_params,
228-
}: Partial<Omit<FunctionInputs, 'config_params'> & PostgresFunction>,
229-
{ replace = false, terminateCommand = true } = {}
222+
}: PostgresFunctionCreate,
223+
{ replace = false } = {}
230224
): string {
231225
return `
232226
CREATE ${replace ? 'OR REPLACE' : ''} FUNCTION ${ident(schema!)}.${ident(name!)}(${
233-
argument_types || args?.join(', ') || ''
227+
args?.join(', ') || ''
234228
})
235229
RETURNS ${return_type}
236230
AS ${literal(definition)}
@@ -247,23 +241,19 @@ export default class PostgresMetaFunctions {
247241
)
248242
.join('\n')
249243
: ''
250-
}
251-
${terminateCommand ? ';' : ''}
244+
};
252245
`
253246
}
254247

255-
private generateRetrieveFunctionSql(
256-
{
257-
schema,
258-
name,
259-
args,
260-
}: {
261-
schema: string
262-
name: string
263-
args: string[]
264-
},
265-
{ terminateCommand = true } = {}
266-
): string {
248+
private generateRetrieveFunctionSql({
249+
schema,
250+
name,
251+
args,
252+
}: {
253+
schema: string
254+
name: string
255+
args: string[]
256+
}): string {
267257
return `${enrichedFunctionsSql} JOIN pg_proc AS p ON id = p.oid WHERE schema = ${literal(
268258
schema
269259
)} AND name = ${literal(name)} AND p.proargtypes::text = ${
@@ -286,7 +276,7 @@ export default class PostgresMetaFunctions {
286276
) AS arr
287277
) AS split_args
288278
) args
289-
) ${terminateCommand ? ';' : ''}`
279+
)`
290280
: literal('')
291281
}`
292282
}

src/lib/types.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,25 @@ const postgresFunctionSchema = Type.Object({
8585
Type.Literal('VOLATILE'),
8686
]),
8787
security_definer: Type.Boolean(),
88-
config_params: Type.Union([Type.Dict(Type.String()), Type.Null()]),
88+
config_params: Type.Union([Type.Record(Type.String(), Type.String()), Type.Null()]),
8989
})
9090
export type PostgresFunction = Static<typeof postgresFunctionSchema>
9191

92+
export const postgresFunctionCreateFunction = Type.Object({
93+
name: Type.String(),
94+
definition: Type.String(),
95+
args: Type.Optional(Type.Array(Type.String())),
96+
behavior: Type.Optional(
97+
Type.Union([Type.Literal('IMMUTABLE'), Type.Literal('STABLE'), Type.Literal('VOLATILE')])
98+
),
99+
config_params: Type.Optional(Type.Record(Type.String(), Type.String())),
100+
schema: Type.Optional(Type.String()),
101+
language: Type.Optional(Type.String()),
102+
return_type: Type.Optional(Type.String()),
103+
security_definer: Type.Optional(Type.Boolean()),
104+
})
105+
export type PostgresFunctionCreate = Static<typeof postgresFunctionCreateFunction>
106+
92107
export const postgresGrantSchema = Type.Object({
93108
table_id: Type.Integer(),
94109
grantor: Type.String(),

0 commit comments

Comments
 (0)