Skip to content

Commit 923e2cb

Browse files
authored
Merge pull request #641 from supabase/feat/optional-one-to-one-rels-detection
feat(typegen): make 1-to-1 relationships detection optional
2 parents a9d3cf6 + a099c6c commit 923e2cb

File tree

6 files changed

+376
-14
lines changed

6 files changed

+376
-14
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"clean": "rimraf dist tsconfig.tsbuildinfo",
2222
"format": "prettier --write \"{src,test}/**/*.ts\"",
2323
"build": "tsc -p tsconfig.json && cpy 'src/lib/sql/*.sql' dist/lib/sql",
24-
"docs:export": "node --loader ts-node/esm src/server/server.ts docs export > openapi.json",
25-
"gen:types:typescript": "node --loader ts-node/esm src/server/server.ts gen types typescript",
24+
"docs:export": "PG_META_EXPORT_DOCS=true node --loader ts-node/esm src/server/server.ts > openapi.json",
25+
"gen:types:typescript": "PG_META_GENERATE_TYPES=typescript node --loader ts-node/esm src/server/server.ts",
2626
"start": "node dist/server/server.js",
2727
"dev": "trap 'npm run db:clean' INT && run-s db:clean db:run && nodemon --exec node --loader ts-node/esm src/server/server.ts | pino-pretty --colorize",
2828
"pkg": "run-s clean build && pkg .pkg.config.json",

src/server/constants.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ if (PG_META_DB_SSL_ROOT_CERT) {
3333
new crypto.X509Certificate(PG_META_DB_SSL_ROOT_CERT)
3434
}
3535

36-
export const EXPORT_DOCS = process.argv[2] === 'docs' && process.argv[3] === 'export'
37-
export const GENERATE_TYPES =
38-
process.argv[2] === 'gen' && process.argv[3] === 'types' ? process.argv[4] : undefined
39-
export const GENERATE_TYPES_INCLUDED_SCHEMAS =
40-
GENERATE_TYPES && process.argv[5] === '--include-schemas' ? process.argv[6]?.split(',') ?? [] : []
36+
export const EXPORT_DOCS = process.env.PG_META_EXPORT_DOCS === 'true'
37+
export const GENERATE_TYPES = process.env.PG_META_GENERATE_TYPES
38+
export const GENERATE_TYPES_INCLUDED_SCHEMAS = GENERATE_TYPES
39+
? process.env.PG_META_GENERATE_TYPES_INCLUDED_SCHEMAS?.split(',') ?? []
40+
: []
41+
export const GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS =
42+
process.env.PG_META_GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS === 'true'
4143

4244
export const DEFAULT_POOL_CONFIG: PoolConfig = {
4345
max: 1,

src/server/routes/generators/typescript.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ export default async (fastify: FastifyInstance) => {
1010
Querystring: {
1111
excluded_schemas?: string
1212
included_schemas?: string
13+
detect_one_to_one_relationships?: string
1314
}
1415
}>('/', async (request, reply) => {
1516
const connectionString = request.headers.pg
1617
const excludedSchemas =
1718
request.query.excluded_schemas?.split(',').map((schema) => schema.trim()) ?? []
1819
const includedSchemas =
1920
request.query.included_schemas?.split(',').map((schema) => schema.trim()) ?? []
21+
const detectOneToOneRelationships = request.query.detect_one_to_one_relationships === 'true'
2022

2123
const pgMeta: PostgresMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
2224
const { data: schemas, error: schemasError } = await pgMeta.schemas.list()
@@ -111,6 +113,7 @@ export default async (fastify: FastifyInstance) => {
111113
),
112114
types: types.filter(({ name }) => name[0] !== '_'),
113115
arrayTypes: types.filter(({ name }) => name[0] === '_'),
116+
detectOneToOneRelationships,
114117
})
115118
})
116119
}

src/server/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
DEFAULT_POOL_CONFIG,
77
EXPORT_DOCS,
88
GENERATE_TYPES,
9+
GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
910
GENERATE_TYPES_INCLUDED_SCHEMAS,
1011
PG_CONNECTION,
1112
PG_META_HOST,
@@ -110,6 +111,7 @@ if (EXPORT_DOCS) {
110111
),
111112
types: types.filter(({ name }) => name[0] !== '_'),
112113
arrayTypes: types.filter(({ name }) => name[0] === '_'),
114+
detectOneToOneRelationships: GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
113115
})
114116
)
115117
} else {

src/server/templates/typescript.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const apply = ({
2020
functions,
2121
types,
2222
arrayTypes,
23+
detectOneToOneRelationships,
2324
}: {
2425
schemas: PostgresSchema[]
2526
tables: Omit<PostgresTable, 'columns'>[]
@@ -30,6 +31,7 @@ export const apply = ({
3031
functions: PostgresFunction[]
3132
types: PostgresType[]
3233
arrayTypes: PostgresType[]
34+
detectOneToOneRelationships: boolean
3335
}): string => {
3436
const columnsByTableId = columns
3537
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
@@ -169,8 +171,11 @@ export interface Database {
169171
(relationship) => `{
170172
foreignKeyName: ${JSON.stringify(relationship.foreign_key_name)}
171173
columns: ${JSON.stringify(relationship.columns)}
172-
isOneToOne: ${relationship.is_one_to_one}
173-
referencedRelation: ${JSON.stringify(relationship.referenced_relation)}
174+
${
175+
detectOneToOneRelationships
176+
? `isOneToOne: ${relationship.is_one_to_one};`
177+
: ''
178+
}referencedRelation: ${JSON.stringify(relationship.referenced_relation)}
174179
referencedColumns: ${JSON.stringify(relationship.referenced_columns)}
175180
}`
176181
)}
@@ -238,8 +243,11 @@ export interface Database {
238243
(relationship) => `{
239244
foreignKeyName: ${JSON.stringify(relationship.foreign_key_name)}
240245
columns: ${JSON.stringify(relationship.columns)}
241-
isOneToOne: ${relationship.is_one_to_one}
242-
referencedRelation: ${JSON.stringify(relationship.referenced_relation)}
246+
${
247+
detectOneToOneRelationships
248+
? `isOneToOne: ${relationship.is_one_to_one};`
249+
: ''
250+
}referencedRelation: ${JSON.stringify(relationship.referenced_relation)}
243251
referencedColumns: ${JSON.stringify(relationship.referenced_columns)}
244252
}`
245253
)}

0 commit comments

Comments
 (0)