| 48 | } |
| 49 | |
| 50 | export default class Postgres implements SchemaInspector { |
| 51 | knex: Knex; |
| 52 | schema: string; |
| 53 | explodedSchema: string[]; |
| 54 | |
| 55 | constructor(knex: Knex) { |
| 56 | this.knex = knex; |
| 57 | const config = knex.client.config; |
| 58 | |
| 59 | if (!config.searchPath) { |
| 60 | this.schema = 'public'; |
| 61 | this.explodedSchema = [this.schema]; |
| 62 | } else if (typeof config.searchPath === 'string') { |
| 63 | this.schema = config.searchPath; |
| 64 | this.explodedSchema = [config.searchPath]; |
| 65 | } else { |
| 66 | this.schema = config.searchPath[0]; |
| 67 | this.explodedSchema = config.searchPath; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Postgres specific |
| 72 | // =============================================================================================== |
| 73 | |
| 74 | /** |
| 75 | * Set the schema to be used in other methods |
| 76 | */ |
| 77 | withSchema(schema: string): this { |
| 78 | this.schema = schema; |
| 79 | this.explodedSchema = [this.schema]; |
| 80 | return this; |
| 81 | } |
| 82 | |
| 83 | // Overview |
| 84 | // =============================================================================================== |
| 85 | |
| 86 | async overview(): Promise<SchemaOverview> { |
| 87 | type RawColumn = { |
| 88 | table_name: string; |
| 89 | column_name: string; |
| 90 | default_value: string | null; |
| 91 | data_type: string; |
| 92 | max_length: number | null; |
| 93 | is_identity: boolean; |
| 94 | is_nullable: boolean; |
| 95 | is_generated: boolean; |
| 96 | }; |
| 97 | |
| 98 | type RawGeometryColumn = { |
| 99 | table_name: string; |
| 100 | column_name: string; |
| 101 | data_type: string; |
| 102 | }; |
| 103 | |
| 104 | const bindings = this.explodedSchema.map(() => '?').join(','); |
| 105 | |
| 106 | const [columnsResult, primaryKeysResult] = await Promise.all([ |
| 107 | // Only select columns from BASE TABLEs to exclude views (Postgres views |
nothing calls this directly
no outgoing calls
no test coverage detected