Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generacion de interfaces typescript de todos los schemas de una base de datos postgresql
-- generacion de interfaces agrupadas por schema+tabla
WITH INTERFACES AS (SELECT
CHR(9)|| 'interface ' || table_name || ' {' || CHR(10) ||
string_agg(CHR(9) ||CHR(9)|| column_name || ': ' ||
CASE WHEN udt_name IN
('jsonb', 'json')
THEN 'any'
WHEN udt_name = 'varchar'
THEN 'string'
WHEN udt_name = 'numeric'
THEN 'number'
WHEN udt_name = 'timestamp'
THEN 'Date'
ELSE 'any' END || ';'
, CHR(10) ORDER BY ordinal_position) || CHR(10) || CHR(9)||'}' interfaz,
table_name,
table_schema
FROM information_schema.columns
WHERE table_schema NOT IN ('information_schema', 'pg_catalog')
GROUP BY table_name, table_schema),
-- agrupacion de interfaces a un namespace por schema
namespaces as (
SELECT 'declare namespace ' || table_schema || ' { ' ||CHR(10)||
string_agg(interfaz, CHR(10)) || CHR(10)|| '}' namespace
FROM INTERFACES
GROUP BY table_schema)
-- concatenacion de todos los namespaces
SELECT string_agg(namespace,CHR(10)) from namespaces;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.