assumptions.ts30 lines · main
| 1 | import { EmbeddedTarget, flattenTargets } from '@supabase/sql-to-rest' |
| 2 | |
| 3 | import type { ResultBundle } from './util' |
| 4 | |
| 5 | export type Assumption = { |
| 6 | id: string |
| 7 | condition: (result: ResultBundle) => boolean |
| 8 | assumptions: (result: ResultBundle) => string[] |
| 9 | } |
| 10 | |
| 11 | export const assumptions: Assumption[] = [ |
| 12 | { |
| 13 | id: 'join-assumptions', |
| 14 | condition: ({ statement }) => |
| 15 | // Show this if there is at least one resource embedding |
| 16 | statement.targets.some((target) => target.type === 'embedded-target'), |
| 17 | assumptions: ({ statement }) => { |
| 18 | const flattenedTargets = flattenTargets(statement.targets) |
| 19 | |
| 20 | const embeddedTargets = flattenedTargets.filter( |
| 21 | (target): target is EmbeddedTarget => target.type === 'embedded-target' |
| 22 | ) |
| 23 | |
| 24 | return embeddedTargets.map( |
| 25 | (t) => |
| 26 | `There is a [foreign key](https://postgrest.org/en/latest/references/api/resource_embedding.html#foreign-key-joins) relationship between \`${t.joinedColumns.left.relation}.${t.joinedColumns.left.column}\` and \`${t.joinedColumns.right.relation}.${t.joinedColumns.right.column}\`` |
| 27 | ) |
| 28 | }, |
| 29 | }, |
| 30 | ] |