Skip to content

Commit d20e386

Browse files
committed
feat: add embeded functions introspections
2 parents e55441c + a142d73 commit d20e386

File tree

9 files changed

+1443
-1051
lines changed

9 files changed

+1443
-1051
lines changed

src/server/app.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ import * as Sentry from '@sentry/node'
33
import cors from '@fastify/cors'
44
import swagger from '@fastify/swagger'
55
import { fastify, FastifyInstance, FastifyServerOptions } from 'fastify'
6-
import { PG_META_REQ_HEADER } from './constants.js'
6+
import { PG_META_REQ_HEADER, MAX_BODY_LIMIT } from './constants.js'
77
import routes from './routes/index.js'
88
import { extractRequestForLogging } from './utils.js'
99
// Pseudo package declared only for this module
1010
import pkg from '#package.json' with { type: 'json' }
1111

1212
export const build = (opts: FastifyServerOptions = {}): FastifyInstance => {
13-
const app = fastify({ disableRequestLogging: true, requestIdHeader: PG_META_REQ_HEADER, ...opts })
13+
const app = fastify({
14+
disableRequestLogging: true,
15+
requestIdHeader: PG_META_REQ_HEADER,
16+
bodyLimit: MAX_BODY_LIMIT,
17+
...opts,
18+
})
1419
Sentry.setupFastifyErrorHandler(app)
1520

1621
app.setErrorHandler((error, request, reply) => {

src/server/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ export const PG_META_MAX_RESULT_SIZE = process.env.PG_META_MAX_RESULT_SIZE_MB
6161
parseInt(process.env.PG_META_MAX_RESULT_SIZE_MB, 10) * 1024 * 1024
6262
: 2 * 1024 * 1024 * 1024 // default to 2GB max query size result
6363

64+
export const MAX_BODY_LIMIT = process.env.PG_META_MAX_BODY_LIMIT_MB
65+
? // Fastify server max body size allowed, is in bytes, convert from MB to Bytes
66+
parseInt(process.env.PG_META_MAX_BODY_LIMIT_MB, 10) * 1024 * 1024
67+
: 3 * 1024 * 1024
68+
6469
export const DEFAULT_POOL_CONFIG: PoolConfig = {
6570
max: 1,
6671
connectionTimeoutMillis: PG_CONN_TIMEOUT_SECS * 1000,

src/server/templates/swift.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ const pgTypeToSwiftType = (
309309
swiftType = 'Float'
310310
} else if (pgType === 'float8') {
311311
swiftType = 'Double'
312+
} else if (['numeric', 'decimal'].includes(pgType)) {
313+
swiftType = 'Decimal'
312314
} else if (pgType === 'uuid') {
313315
swiftType = 'UUID'
314316
} else if (

src/server/templates/typescript.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export const apply = async ({
137137
}
138138

139139
const internal_supabase_schema = postgrestVersion
140-
? `// Allows to automatically instanciate createClient with right options
140+
? `// Allows to automatically instantiate createClient with right options
141141
// instead of createClient<Database, { PostgrestVersion: 'XX' }>(URL, KEY)
142142
__InternalSupabase: {
143143
PostgrestVersion: '${postgrestVersion}'
@@ -464,6 +464,7 @@ export type Database = {
464464
const fns = _fns.toSorted((a, b) => b.definition.localeCompare(a.definition))
465465
466466
const functionSignatures = fns.map((fn) => {
467+
fn.args.sort((a, b) => a.name.localeCompare(b.name))
467468
const inArgs = fn.args.filter(({ mode }) => VALID_FUNCTION_ARGS_MODE.has(mode))
468469
469470
// Special error case for functions that take table row but don't qualify as embedded functions

test/db/00-init.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ CREATE TYPE composite_type_with_array_attribute AS (my_text_array text[]);
88
CREATE TABLE public.users (
99
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
1010
name text,
11-
status user_status DEFAULT 'ACTIVE'
11+
status user_status DEFAULT 'ACTIVE',
12+
decimal numeric
1213
);
1314
INSERT INTO
1415
public.users (name)
@@ -404,13 +405,13 @@ CREATE OR REPLACE FUNCTION public.function_using_table_returns(user_row users)
404405
RETURNS todos
405406
LANGUAGE SQL STABLE
406407
AS $$
407-
SELECT * FROM public.todos WHERE user_id = user_row.id LIMIT 1;
408+
SELECT * FROM public.todos WHERE todos."user-id" = user_row.id LIMIT 1;
408409
$$;
409410

410411
CREATE OR REPLACE FUNCTION public.function_using_setof_rows_one(user_row users)
411412
RETURNS SETOF todos
412413
LANGUAGE SQL STABLE
413414
ROWS 1
414415
AS $$
415-
SELECT * FROM public.todos WHERE user_id = user_row.idLIMIT 1;
416+
SELECT * FROM public.todos WHERE todos."user-id" = user_row.id LIMIT 1;
416417
$$;

test/lib/functions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ test('list set-returning function with single object limit', async () => {
8080
"definition": "
8181
SELECT * FROM public.users_audit WHERE user_id = user_row.id;
8282
",
83-
"id": 16504,
83+
"id": 16506,
8484
"identity_argument_types": "user_row users",
8585
"is_set_returning_function": true,
8686
"language": "sql",
@@ -126,7 +126,7 @@ test('list set-returning function with multiples definitions', async () => {
126126
"definition": "
127127
SELECT * FROM public.todos WHERE "user-id" = user_row.id;
128128
",
129-
"id": 16505,
129+
"id": 16507,
130130
"identity_argument_types": "user_row users",
131131
"is_set_returning_function": true,
132132
"language": "sql",
@@ -164,7 +164,7 @@ test('list set-returning function with multiples definitions', async () => {
164164
"definition": "
165165
SELECT * FROM public.todos WHERE "user-id" = todo_row."user-id";
166166
",
167-
"id": 16506,
167+
"id": 16508,
168168
"identity_argument_types": "todo_row todos",
169169
"is_set_returning_function": true,
170170
"language": "sql",

test/lib/tables.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ test('list', async () => {
7878
"schema": "public",
7979
"table": "users",
8080
},
81+
{
82+
"check": null,
83+
"comment": null,
84+
"data_type": "numeric",
85+
"default_value": null,
86+
"enums": [],
87+
"format": "numeric",
88+
"identity_generation": null,
89+
"is_generated": false,
90+
"is_identity": false,
91+
"is_nullable": true,
92+
"is_unique": false,
93+
"is_updatable": true,
94+
"name": "decimal",
95+
"ordinal_position": 4,
96+
"schema": "public",
97+
"table": "users",
98+
},
8199
{
82100
"check": null,
83101
"comment": null,

test/server/query.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ test('query', async () => {
1010
expect(res.json()).toMatchInlineSnapshot(`
1111
[
1212
{
13+
"decimal": null,
1314
"id": 1,
1415
"name": "Joe Bloggs",
1516
"status": "ACTIVE",
1617
},
1718
{
19+
"decimal": null,
1820
"id": 2,
1921
"name": "Jane Doe",
2022
"status": "ACTIVE",

0 commit comments

Comments
 (0)