Skip to content

Commit

Permalink
Merge pull request #641 from supabase/feat/optional-one-to-one-rels-d…
Browse files Browse the repository at this point in the history
…etection

feat(typegen): make 1-to-1 relationships detection optional
  • Loading branch information
soedirgo authored Nov 2, 2023
2 parents 1b1b5c7 + f89e330 commit 893af73
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"clean": "rimraf dist tsconfig.tsbuildinfo",
"format": "prettier --write \"{src,test}/**/*.ts\"",
"build": "tsc -p tsconfig.json && cpy 'src/lib/sql/*.sql' dist/lib/sql",
"docs:export": "node --loader ts-node/esm src/server/server.ts docs export > openapi.json",
"gen:types:typescript": "node --loader ts-node/esm src/server/server.ts gen types typescript",
"docs:export": "PG_META_EXPORT_DOCS=true node --loader ts-node/esm src/server/server.ts > openapi.json",
"gen:types:typescript": "PG_META_GENERATE_TYPES=typescript node --loader ts-node/esm src/server/server.ts",
"start": "node dist/server/server.js",
"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",
"pkg": "run-s clean build && pkg .pkg.config.json",
Expand Down
12 changes: 7 additions & 5 deletions src/server/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ if (PG_META_DB_SSL_ROOT_CERT) {
new crypto.X509Certificate(PG_META_DB_SSL_ROOT_CERT)
}

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

export const DEFAULT_POOL_CONFIG: PoolConfig = {
max: 1,
Expand Down
3 changes: 3 additions & 0 deletions src/server/routes/generators/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ export default async (fastify: FastifyInstance) => {
Querystring: {
excluded_schemas?: string
included_schemas?: string
detect_one_to_one_relationships?: string
}
}>('/', async (request, reply) => {
const connectionString = request.headers.pg
const excludedSchemas =
request.query.excluded_schemas?.split(',').map((schema) => schema.trim()) ?? []
const includedSchemas =
request.query.included_schemas?.split(',').map((schema) => schema.trim()) ?? []
const detectOneToOneRelationships = request.query.detect_one_to_one_relationships === 'true'

const pgMeta: PostgresMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
const { data: schemas, error: schemasError } = await pgMeta.schemas.list()
Expand Down Expand Up @@ -111,6 +113,7 @@ export default async (fastify: FastifyInstance) => {
),
types: types.filter(({ name }) => name[0] !== '_'),
arrayTypes: types.filter(({ name }) => name[0] === '_'),
detectOneToOneRelationships,
})
})
}
2 changes: 2 additions & 0 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DEFAULT_POOL_CONFIG,
EXPORT_DOCS,
GENERATE_TYPES,
GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
GENERATE_TYPES_INCLUDED_SCHEMAS,
PG_CONNECTION,
PG_META_HOST,
Expand Down Expand Up @@ -110,6 +111,7 @@ if (EXPORT_DOCS) {
),
types: types.filter(({ name }) => name[0] !== '_'),
arrayTypes: types.filter(({ name }) => name[0] === '_'),
detectOneToOneRelationships: GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
})
)
} else {
Expand Down
16 changes: 12 additions & 4 deletions src/server/templates/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const apply = ({
functions,
types,
arrayTypes,
detectOneToOneRelationships,
}: {
schemas: PostgresSchema[]
tables: Omit<PostgresTable, 'columns'>[]
Expand All @@ -30,6 +31,7 @@ export const apply = ({
functions: PostgresFunction[]
types: PostgresType[]
arrayTypes: PostgresType[]
detectOneToOneRelationships: boolean
}): string => {
const columnsByTableId = columns
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
Expand Down Expand Up @@ -169,8 +171,11 @@ export interface Database {
(relationship) => `{
foreignKeyName: ${JSON.stringify(relationship.foreign_key_name)}
columns: ${JSON.stringify(relationship.columns)}
isOneToOne: ${relationship.is_one_to_one}
referencedRelation: ${JSON.stringify(relationship.referenced_relation)}
${
detectOneToOneRelationships
? `isOneToOne: ${relationship.is_one_to_one};`
: ''
}referencedRelation: ${JSON.stringify(relationship.referenced_relation)}
referencedColumns: ${JSON.stringify(relationship.referenced_columns)}
}`
)}
Expand Down Expand Up @@ -238,8 +243,11 @@ export interface Database {
(relationship) => `{
foreignKeyName: ${JSON.stringify(relationship.foreign_key_name)}
columns: ${JSON.stringify(relationship.columns)}
isOneToOne: ${relationship.is_one_to_one}
referencedRelation: ${JSON.stringify(relationship.referenced_relation)}
${
detectOneToOneRelationships
? `isOneToOne: ${relationship.is_one_to_one};`
: ''
}referencedRelation: ${JSON.stringify(relationship.referenced_relation)}
referencedColumns: ${JSON.stringify(relationship.referenced_columns)}
}`
)}
Expand Down
Loading

0 comments on commit 893af73

Please sign in to comment.