|
| 1 | +import { literal } from 'pg-format' |
| 2 | +import { coalesceRowsToArray, filterByList } from './helpers.js' |
| 3 | +import { columnsSql, materializedViewsSql } from './sql/index.js' |
| 4 | +import { PostgresMetaResult, PostgresMaterializedView } from './types.js' |
| 5 | + |
| 6 | +export default class PostgresMetaMaterializedViews { |
| 7 | + query: (sql: string) => Promise<PostgresMetaResult<any>> |
| 8 | + |
| 9 | + constructor(query: (sql: string) => Promise<PostgresMetaResult<any>>) { |
| 10 | + this.query = query |
| 11 | + } |
| 12 | + |
| 13 | + async list(options: { |
| 14 | + includedSchemas?: string[] |
| 15 | + excludedSchemas?: string[] |
| 16 | + limit?: number |
| 17 | + offset?: number |
| 18 | + includeColumns: true |
| 19 | + }): Promise<PostgresMetaResult<(PostgresMaterializedView & { columns: unknown[] })[]>> |
| 20 | + async list(options?: { |
| 21 | + includedSchemas?: string[] |
| 22 | + excludedSchemas?: string[] |
| 23 | + limit?: number |
| 24 | + offset?: number |
| 25 | + includeColumns?: boolean |
| 26 | + }): Promise<PostgresMetaResult<(PostgresMaterializedView & { columns: never })[]>> |
| 27 | + async list({ |
| 28 | + includedSchemas, |
| 29 | + excludedSchemas, |
| 30 | + limit, |
| 31 | + offset, |
| 32 | + includeColumns = false, |
| 33 | + }: { |
| 34 | + includedSchemas?: string[] |
| 35 | + excludedSchemas?: string[] |
| 36 | + limit?: number |
| 37 | + offset?: number |
| 38 | + includeColumns?: boolean |
| 39 | + } = {}): Promise<PostgresMetaResult<PostgresMaterializedView[]>> { |
| 40 | + let sql = generateEnrichedMaterializedViewsSql({ includeColumns }) |
| 41 | + const filter = filterByList(includedSchemas, excludedSchemas, undefined) |
| 42 | + if (filter) { |
| 43 | + sql += ` where schema ${filter}` |
| 44 | + } |
| 45 | + if (limit) { |
| 46 | + sql += ` limit ${limit}` |
| 47 | + } |
| 48 | + if (offset) { |
| 49 | + sql += ` offset ${offset}` |
| 50 | + } |
| 51 | + return await this.query(sql) |
| 52 | + } |
| 53 | + |
| 54 | + async retrieve({ id }: { id: number }): Promise<PostgresMetaResult<PostgresMaterializedView>> |
| 55 | + async retrieve({ |
| 56 | + name, |
| 57 | + schema, |
| 58 | + }: { |
| 59 | + name: string |
| 60 | + schema: string |
| 61 | + }): Promise<PostgresMetaResult<PostgresMaterializedView>> |
| 62 | + async retrieve({ |
| 63 | + id, |
| 64 | + name, |
| 65 | + schema = 'public', |
| 66 | + }: { |
| 67 | + id?: number |
| 68 | + name?: string |
| 69 | + schema?: string |
| 70 | + }): Promise<PostgresMetaResult<PostgresMaterializedView>> { |
| 71 | + if (id) { |
| 72 | + const sql = `${generateEnrichedMaterializedViewsSql({ |
| 73 | + includeColumns: true, |
| 74 | + })} where materialized_views.id = ${literal(id)};` |
| 75 | + console.log(sql) |
| 76 | + const { data, error } = await this.query(sql) |
| 77 | + if (error) { |
| 78 | + return { data, error } |
| 79 | + } else if (data.length === 0) { |
| 80 | + return { data: null, error: { message: `Cannot find a materialized view with ID ${id}` } } |
| 81 | + } else { |
| 82 | + return { data: data[0], error } |
| 83 | + } |
| 84 | + } else if (name) { |
| 85 | + const sql = `${generateEnrichedMaterializedViewsSql({ |
| 86 | + includeColumns: true, |
| 87 | + })} where materialized_views.name = ${literal( |
| 88 | + name |
| 89 | + )} and materialized_views.schema = ${literal(schema)};` |
| 90 | + const { data, error } = await this.query(sql) |
| 91 | + if (error) { |
| 92 | + return { data, error } |
| 93 | + } else if (data.length === 0) { |
| 94 | + return { |
| 95 | + data: null, |
| 96 | + error: { message: `Cannot find a materialized view named ${name} in schema ${schema}` }, |
| 97 | + } |
| 98 | + } else { |
| 99 | + return { data: data[0], error } |
| 100 | + } |
| 101 | + } else { |
| 102 | + return { data: null, error: { message: 'Invalid parameters on materialized view retrieve' } } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +const generateEnrichedMaterializedViewsSql = ({ includeColumns }: { includeColumns: boolean }) => ` |
| 108 | +with materialized_views as (${materializedViewsSql}) |
| 109 | + ${includeColumns ? `, columns as (${columnsSql})` : ''} |
| 110 | +select |
| 111 | + * |
| 112 | + ${ |
| 113 | + includeColumns |
| 114 | + ? `, ${coalesceRowsToArray('columns', 'columns.table_id = materialized_views.id')}` |
| 115 | + : '' |
| 116 | + } |
| 117 | +from materialized_views` |
0 commit comments