Skip to content

Commit af7b674

Browse files
authored
Merge pull request #474 from supabase/chore/use-typebox-type-provider
chore(deps): use @fastify/type-provider-typebox
2 parents db3895e + 48ff7c1 commit af7b674

File tree

8 files changed

+354
-230
lines changed

8 files changed

+354
-230
lines changed

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"devDependencies": {
6767
"@fastify/cors": "^8.2.0",
6868
"@fastify/swagger": "^8.2.1",
69+
"@fastify/type-provider-typebox": "^2.4.0",
6970
"@types/crypto-js": "^4.1.1",
7071
"@types/jest": "^29.2.4",
7172
"@types/node": "^16.18.3",

src/lib/PostgresMetaTables.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { ident, literal } from 'pg-format'
22
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
33
import { coalesceRowsToArray, filterByList } from './helpers.js'
44
import { columnsSql, primaryKeysSql, relationshipsSql, tablesSql } from './sql/index.js'
5-
import { PostgresMetaResult, PostgresTable } from './types.js'
5+
import {
6+
PostgresMetaResult,
7+
PostgresTable,
8+
PostgresTableCreate,
9+
PostgresTableUpdate,
10+
} from './types.js'
611

712
export default class PostgresMetaTables {
813
query: (sql: string) => Promise<PostgresMetaResult<any>>
@@ -93,11 +98,7 @@ export default class PostgresMetaTables {
9398
name,
9499
schema = 'public',
95100
comment,
96-
}: {
97-
name: string
98-
schema?: string
99-
comment?: string
100-
}): Promise<PostgresMetaResult<PostgresTable>> {
101+
}: PostgresTableCreate): Promise<PostgresMetaResult<PostgresTable>> {
101102
const tableSql = `CREATE TABLE ${ident(schema)}.${ident(name)} ();`
102103
const commentSql =
103104
comment === undefined
@@ -122,16 +123,7 @@ export default class PostgresMetaTables {
122123
replica_identity_index,
123124
primary_keys,
124125
comment,
125-
}: {
126-
name?: string
127-
schema?: string
128-
rls_enabled?: boolean
129-
rls_forced?: boolean
130-
replica_identity?: 'DEFAULT' | 'INDEX' | 'FULL' | 'NOTHING'
131-
replica_identity_index?: string
132-
primary_keys?: { name: string }[]
133-
comment?: string
134-
}
126+
}: PostgresTableUpdate
135127
): Promise<PostgresMetaResult<PostgresTable>> {
136128
const { data: old, error } = await this.retrieve({ id })
137129
if (error) {

src/lib/types.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,32 @@ export const postgresTableSchema = Type.Object({
295295
})
296296
export type PostgresTable = Static<typeof postgresTableSchema>
297297

298+
export const postgresTableCreateSchema = Type.Object({
299+
name: Type.String(),
300+
schema: Type.Optional(Type.String()),
301+
comment: Type.Optional(Type.String()),
302+
})
303+
export type PostgresTableCreate = Static<typeof postgresTableCreateSchema>
304+
305+
export const postgresTableUpdateSchema = Type.Object({
306+
name: Type.Optional(Type.String()),
307+
schema: Type.Optional(Type.String()),
308+
rls_enabled: Type.Optional(Type.Boolean()),
309+
rls_forced: Type.Optional(Type.Boolean()),
310+
replica_identity: Type.Optional(
311+
Type.Union([
312+
Type.Literal('DEFAULT'),
313+
Type.Literal('INDEX'),
314+
Type.Literal('FULL'),
315+
Type.Literal('NOTHING'),
316+
])
317+
),
318+
replica_identity_index: Type.Optional(Type.String()),
319+
primary_keys: Type.Optional(Type.Array(Type.Object({ name: Type.String() }))),
320+
comment: Type.Optional(Type.String()),
321+
})
322+
export type PostgresTableUpdate = Static<typeof postgresTableUpdateSchema>
323+
298324
export const postgresTriggerSchema = Type.Object({
299325
id: Type.Integer(),
300326
table_id: Type.Integer(),

src/server/routes/foreign-tables.ts

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
1-
import { FastifyInstance } from 'fastify'
1+
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
2+
import { Type } from '@sinclair/typebox'
23
import { PostgresMeta } from '../../lib/index.js'
4+
import { postgresForeignTableSchema } from '../../lib/types.js'
35
import { DEFAULT_POOL_CONFIG } from '../constants.js'
46
import { extractRequestForLogging } from '../utils.js'
57

6-
export default async (fastify: FastifyInstance) => {
7-
fastify.get<{
8-
Headers: { pg: string }
9-
Querystring: {
10-
limit?: number
11-
offset?: number
12-
}
13-
}>('/', async (request, reply) => {
14-
const connectionString = request.headers.pg
15-
const limit = request.query.limit
16-
const offset = request.query.offset
8+
const route: FastifyPluginAsyncTypebox = async (fastify) => {
9+
fastify.get(
10+
'/',
11+
{
12+
schema: {
13+
headers: Type.Object({
14+
pg: Type.String(),
15+
}),
16+
querystring: Type.Object({
17+
limit: Type.Optional(Type.Integer()),
18+
offset: Type.Optional(Type.Integer()),
19+
}),
20+
response: {
21+
200: Type.Array(postgresForeignTableSchema),
22+
500: Type.Object({
23+
error: Type.String(),
24+
}),
25+
},
26+
},
27+
},
28+
async (request, reply) => {
29+
const connectionString = request.headers.pg
30+
const limit = request.query.limit
31+
const offset = request.query.offset
1732

18-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
19-
const { data, error } = await pgMeta.foreignTables.list({ limit, offset })
20-
await pgMeta.end()
21-
if (error) {
22-
request.log.error({ error, request: extractRequestForLogging(request) })
23-
reply.code(500)
24-
return { error: error.message }
25-
}
33+
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
34+
const { data, error } = await pgMeta.foreignTables.list({ limit, offset })
35+
await pgMeta.end()
36+
if (error) {
37+
request.log.error({ error, request: extractRequestForLogging(request) })
38+
reply.code(500)
39+
return { error: error.message }
40+
}
2641

27-
return data
28-
})
42+
return data
43+
}
44+
)
2945
}
46+
export default route

src/server/routes/schemas.ts

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
1+
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
12
import { Type } from '@sinclair/typebox'
2-
import { FastifyInstance } from 'fastify'
33
import { PostgresMeta } from '../../lib/index.js'
44
import {
5-
PostgresSchemaCreate,
6-
PostgresSchemaUpdate,
75
postgresSchemaSchema,
86
postgresSchemaCreateSchema,
97
postgresSchemaUpdateSchema,
108
} from '../../lib/types.js'
119
import { DEFAULT_POOL_CONFIG } from '../constants.js'
1210
import { extractRequestForLogging } from '../utils.js'
1311

14-
export default async (fastify: FastifyInstance) => {
15-
fastify.get<{
16-
Headers: { pg: string }
17-
Querystring: {
18-
include_system_schemas?: string
19-
limit?: number
20-
offset?: number
21-
}
22-
}>(
12+
const route: FastifyPluginAsyncTypebox = async (fastify) => {
13+
fastify.get(
2314
'/',
2415
{
2516
schema: {
2617
headers: Type.Object({
2718
pg: Type.String(),
2819
}),
2920
querystring: Type.Object({
30-
include_system_schemas: Type.Optional(Type.String()),
31-
limit: Type.Optional(Type.String()),
32-
offset: Type.Optional(Type.String()),
21+
include_system_schemas: Type.Optional(Type.Boolean()),
22+
limit: Type.Optional(Type.Integer()),
23+
offset: Type.Optional(Type.Integer()),
3324
}),
3425
response: {
3526
200: Type.Array(postgresSchemaSchema),
@@ -41,7 +32,7 @@ export default async (fastify: FastifyInstance) => {
4132
},
4233
async (request, reply) => {
4334
const connectionString = request.headers.pg
44-
const includeSystemSchemas = request.query.include_system_schemas === 'true'
35+
const includeSystemSchemas = request.query.include_system_schemas
4536
const limit = request.query.limit
4637
const offset = request.query.offset
4738

@@ -58,20 +49,15 @@ export default async (fastify: FastifyInstance) => {
5849
}
5950
)
6051

61-
fastify.get<{
62-
Headers: { pg: string }
63-
Params: {
64-
id: string
65-
}
66-
}>(
52+
fastify.get(
6753
'/:id(\\d+)',
6854
{
6955
schema: {
7056
headers: Type.Object({
7157
pg: Type.String(),
7258
}),
7359
params: Type.Object({
74-
id: Type.RegEx(/\d+/),
60+
id: Type.Integer(),
7561
}),
7662
response: {
7763
200: postgresSchemaSchema,
@@ -83,7 +69,7 @@ export default async (fastify: FastifyInstance) => {
8369
},
8470
async (request, reply) => {
8571
const connectionString = request.headers.pg
86-
const id = Number(request.params.id)
72+
const id = request.params.id
8773

8874
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
8975
const { data, error } = await pgMeta.schemas.retrieve({ id })
@@ -98,10 +84,7 @@ export default async (fastify: FastifyInstance) => {
9884
}
9985
)
10086

101-
fastify.post<{
102-
Headers: { pg: string }
103-
Body: PostgresSchemaCreate
104-
}>(
87+
fastify.post(
10588
'/',
10689
{
10790
schema: {
@@ -133,21 +116,15 @@ export default async (fastify: FastifyInstance) => {
133116
}
134117
)
135118

136-
fastify.patch<{
137-
Headers: { pg: string }
138-
Params: {
139-
id: string
140-
}
141-
Body: PostgresSchemaUpdate
142-
}>(
119+
fastify.patch(
143120
'/:id(\\d+)',
144121
{
145122
schema: {
146123
headers: Type.Object({
147124
pg: Type.String(),
148125
}),
149126
params: Type.Object({
150-
id: Type.RegEx(/\d+/),
127+
id: Type.Integer(),
151128
}),
152129
body: postgresSchemaUpdateSchema,
153130
response: {
@@ -163,7 +140,7 @@ export default async (fastify: FastifyInstance) => {
163140
},
164141
async (request, reply) => {
165142
const connectionString = request.headers.pg
166-
const id = Number(request.params.id)
143+
const id = request.params.id
167144

168145
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
169146
const { data, error } = await pgMeta.schemas.update(id, request.body)
@@ -179,26 +156,18 @@ export default async (fastify: FastifyInstance) => {
179156
}
180157
)
181158

182-
fastify.delete<{
183-
Headers: { pg: string }
184-
Params: {
185-
id: string
186-
}
187-
Querystring: {
188-
cascade?: string
189-
}
190-
}>(
159+
fastify.delete(
191160
'/:id(\\d+)',
192161
{
193162
schema: {
194163
headers: Type.Object({
195164
pg: Type.String(),
196165
}),
197166
params: Type.Object({
198-
id: Type.RegEx(/\d+/),
167+
id: Type.Integer(),
199168
}),
200169
querystring: Type.Object({
201-
cascade: Type.Optional(Type.String()),
170+
cascade: Type.Optional(Type.Boolean()),
202171
}),
203172
response: {
204173
200: postgresSchemaSchema,
@@ -213,8 +182,8 @@ export default async (fastify: FastifyInstance) => {
213182
},
214183
async (request, reply) => {
215184
const connectionString = request.headers.pg
216-
const id = Number(request.params.id)
217-
const cascade = request.query.cascade === 'true'
185+
const id = request.params.id
186+
const cascade = request.query.cascade
218187

219188
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
220189
const { data, error } = await pgMeta.schemas.remove(id, { cascade })
@@ -230,3 +199,4 @@ export default async (fastify: FastifyInstance) => {
230199
}
231200
)
232201
}
202+
export default route

0 commit comments

Comments
 (0)