diff --git a/cjs/src/postgres.js b/cjs/src/postgres.js index 1f4d92c..ff97303 100644 --- a/cjs/src/postgres.js +++ b/cjs/src/postgres.js @@ -20,6 +20,7 @@ function getTableDefinitions (sql, schema) { SELECT c.table_name AS name, obj_description(('"' || c.table_name || '"')::regclass) AS comment, t.table_type = 'VIEW' AS "isView", + FALSE AS "isMaterializedView", jsonb_agg( DISTINCT jsonb_build_object( 'name', column_name, @@ -75,6 +76,7 @@ async function getMaterializedViewDefinitions (sql, schema) { ) SELECT pc.relname AS name, TRUE AS "isView", + TRUE AS "isMaterializedView", jsonb_agg( DISTINCT jsonb_build_object( 'name', pa.attname, diff --git a/cjs/src/typescript.js b/cjs/src/typescript.js index 2101f2c..44b9e71 100644 --- a/cjs/src/typescript.js +++ b/cjs/src/typescript.js @@ -211,11 +211,17 @@ function typescript (opts, schema) { } if (opts.viewNames) { - const views = tables.filter(table => table.isView) + const views = tables.filter(table => table.isView && !table.isMaterializedView) if (views.length > 0) { result += `export type Views = ${views.sort(sortByField('name')).map(table => `'${table.name}'`).join(' | ')}${semicolon(opts)}` result += '\n\n' } + + const materializedViews = tables.filter(table => table.isMaterializedView) + if (materializedViews.length > 0) { + result += `export type MaterializedViews = ${materializedViews.sort(sortByField('name')).map(table => `'${table.name}'`).join(' | ')}${semicolon(opts)}` + result += '\n\n' + } } const enumTypes = generateEnumTypes(opts, enums) diff --git a/src/postgres.js b/src/postgres.js index 00dc28d..d87de6d 100644 --- a/src/postgres.js +++ b/src/postgres.js @@ -20,6 +20,7 @@ function getTableDefinitions (sql, schema) { SELECT c.table_name AS name, obj_description(('"' || c.table_name || '"')::regclass) AS comment, t.table_type = 'VIEW' AS "isView", + FALSE AS "isMaterializedView", jsonb_agg( DISTINCT jsonb_build_object( 'name', column_name, @@ -75,6 +76,7 @@ async function getMaterializedViewDefinitions (sql, schema) { ) SELECT pc.relname AS name, TRUE AS "isView", + TRUE AS "isMaterializedView", jsonb_agg( DISTINCT jsonb_build_object( 'name', pa.attname, diff --git a/src/typescript.js b/src/typescript.js index 27bc403..cfed929 100644 --- a/src/typescript.js +++ b/src/typescript.js @@ -211,11 +211,17 @@ function typescript (opts, schema) { } if (opts.viewNames) { - const views = tables.filter(table => table.isView) + const views = tables.filter(table => table.isView && !table.isMaterializedView) if (views.length > 0) { result += `export type Views = ${views.sort(sortByField('name')).map(table => `'${table.name}'`).join(' | ')}${semicolon(opts)}` result += '\n\n' } + + const materializedViews = tables.filter(table => table.isMaterializedView) + if (materializedViews.length > 0) { + result += `export type MaterializedViews = ${materializedViews.sort(sortByField('name')).map(table => `'${table.name}'`).join(' | ')}${semicolon(opts)}` + result += '\n\n' + } } const enumTypes = generateEnumTypes(opts, enums) diff --git a/tap-snapshots/cjs/test/cli.js.test.cjs b/tap-snapshots/cjs/test/cli.js.test.cjs index 73e1cf7..6354ebc 100644 --- a/tap-snapshots/cjs/test/cli.js.test.cjs +++ b/tap-snapshots/cjs/test/cli.js.test.cjs @@ -2231,7 +2231,9 @@ export type UserEntity = { ` exports['cjs/test/cli.js > TAP > generates view names > must match snapshot 1'] = ` -export type Views = 'materialized_items' | 'materialized_other_items' | 'some_view' +export type Views = 'some_view' + +export type MaterializedViews = 'materialized_items' | 'materialized_other_items' export enum DeliciousKebab { 'big-mix' = 'big-mix', diff --git a/tap-snapshots/cjs/test/pg-typegen.js.test.cjs b/tap-snapshots/cjs/test/pg-typegen.js.test.cjs index f9d2821..f057000 100644 --- a/tap-snapshots/cjs/test/pg-typegen.js.test.cjs +++ b/tap-snapshots/cjs/test/pg-typegen.js.test.cjs @@ -53,6 +53,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "address", }, @@ -69,6 +70,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "histories", }, @@ -85,6 +87,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "kebab-test", }, @@ -123,6 +126,7 @@ Object { "type": "timestamptz", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_items", }, @@ -145,6 +149,7 @@ Object { "type": "text", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_other_items", }, @@ -170,6 +175,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "PascalTableName", }, @@ -191,6 +197,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "snake_test", }, @@ -216,6 +223,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": true, "name": "some_view", }, @@ -1186,6 +1194,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "types", }, @@ -1252,6 +1261,7 @@ Object { }, ], "comment": "this is the users table", + "isMaterializedView": false, "isView": false, "name": "users", }, diff --git a/tap-snapshots/cjs/test/postgres.js.test.cjs b/tap-snapshots/cjs/test/postgres.js.test.cjs index d4a7b39..07501d6 100644 --- a/tap-snapshots/cjs/test/postgres.js.test.cjs +++ b/tap-snapshots/cjs/test/postgres.js.test.cjs @@ -62,6 +62,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "PascalTableName", }, @@ -78,6 +79,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "address", }, @@ -94,6 +96,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "histories", }, @@ -110,6 +113,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "kebab-test", }, @@ -131,6 +135,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "snake_test", }, @@ -156,6 +161,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": true, "name": "some_view", }, @@ -1126,6 +1132,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "types", }, @@ -1192,6 +1199,7 @@ Object { }, ], "comment": "this is the users table", + "isMaterializedView": false, "isView": false, "name": "users", }, @@ -1230,6 +1238,7 @@ Object { "type": "timestamptz", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_items", }, @@ -1252,6 +1261,7 @@ Object { "type": "text", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_other_items", }, @@ -1390,6 +1400,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "PascalTableName", }, @@ -1406,6 +1417,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "address", }, @@ -1422,6 +1434,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "histories", }, @@ -1438,6 +1451,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "kebab-test", }, @@ -1459,6 +1473,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "snake_test", }, @@ -1484,6 +1499,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": true, "name": "some_view", }, @@ -2454,6 +2470,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "types", }, @@ -2520,6 +2537,7 @@ Object { }, ], "comment": "this is the users table", + "isMaterializedView": false, "isView": false, "name": "users", }, @@ -2558,6 +2576,7 @@ Object { "type": "timestamptz", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_items", }, @@ -2580,6 +2599,7 @@ Object { "type": "text", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_other_items", }, @@ -2732,6 +2752,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "PascalTableName", }, @@ -2748,6 +2769,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "address", }, @@ -2764,6 +2786,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "histories", }, @@ -2780,6 +2803,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "kebab-test", }, @@ -2801,6 +2825,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "snake_test", }, @@ -2826,6 +2851,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": true, "name": "some_view", }, @@ -3796,6 +3822,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "types", }, @@ -3862,6 +3889,7 @@ Object { }, ], "comment": "this is the users table", + "isMaterializedView": false, "isView": false, "name": "users", }, @@ -3900,6 +3928,7 @@ Object { "type": "timestamptz", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_items", }, @@ -3922,6 +3951,7 @@ Object { "type": "text", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_other_items", }, @@ -4078,6 +4108,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "PascalTableName", }, @@ -4094,6 +4125,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "address", }, @@ -4110,6 +4142,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "histories", }, @@ -4126,6 +4159,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "kebab-test", }, @@ -4147,6 +4181,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "snake_test", }, @@ -4172,6 +4207,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": true, "name": "some_view", }, @@ -5142,6 +5178,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "types", }, @@ -5208,6 +5245,7 @@ Object { }, ], "comment": "this is the users table", + "isMaterializedView": false, "isView": false, "name": "users", }, @@ -5246,6 +5284,7 @@ Object { "type": "timestamptz", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_items", }, @@ -5268,6 +5307,7 @@ Object { "type": "text", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_other_items", }, @@ -5416,6 +5456,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "PascalTableName", }, @@ -5432,6 +5473,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "address", }, @@ -5448,6 +5490,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "histories", }, @@ -5464,6 +5507,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "kebab-test", }, @@ -5485,6 +5529,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "snake_test", }, @@ -5510,6 +5555,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": true, "name": "some_view", }, @@ -6480,6 +6526,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "types", }, @@ -6546,6 +6593,7 @@ Object { }, ], "comment": "this is the users table", + "isMaterializedView": false, "isView": false, "name": "users", }, @@ -6584,6 +6632,7 @@ Object { "type": "timestamptz", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_items", }, @@ -6606,6 +6655,7 @@ Object { "type": "text", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_other_items", }, diff --git a/tap-snapshots/test/cli.js.test.cjs b/tap-snapshots/test/cli.js.test.cjs index faeda5c..3967df8 100644 --- a/tap-snapshots/test/cli.js.test.cjs +++ b/tap-snapshots/test/cli.js.test.cjs @@ -2231,7 +2231,9 @@ export type UserEntity = { ` exports['test/cli.js > TAP > generates view names > must match snapshot 1'] = ` -export type Views = 'materialized_items' | 'materialized_other_items' | 'some_view' +export type Views = 'some_view' + +export type MaterializedViews = 'materialized_items' | 'materialized_other_items' export enum DeliciousKebab { 'big-mix' = 'big-mix', diff --git a/tap-snapshots/test/pg-typegen.js.test.cjs b/tap-snapshots/test/pg-typegen.js.test.cjs index 21adf5a..5138a65 100644 --- a/tap-snapshots/test/pg-typegen.js.test.cjs +++ b/tap-snapshots/test/pg-typegen.js.test.cjs @@ -53,6 +53,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "address", }, @@ -69,6 +70,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "histories", }, @@ -85,6 +87,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "kebab-test", }, @@ -123,6 +126,7 @@ Object { "type": "timestamptz", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_items", }, @@ -145,6 +149,7 @@ Object { "type": "text", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_other_items", }, @@ -170,6 +175,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "PascalTableName", }, @@ -191,6 +197,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "snake_test", }, @@ -216,6 +223,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": true, "name": "some_view", }, @@ -1186,6 +1194,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "types", }, @@ -1252,6 +1261,7 @@ Object { }, ], "comment": "this is the users table", + "isMaterializedView": false, "isView": false, "name": "users", }, diff --git a/tap-snapshots/test/postgres.js.test.cjs b/tap-snapshots/test/postgres.js.test.cjs index f7ae3a9..95b4144 100644 --- a/tap-snapshots/test/postgres.js.test.cjs +++ b/tap-snapshots/test/postgres.js.test.cjs @@ -62,6 +62,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "PascalTableName", }, @@ -78,6 +79,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "address", }, @@ -94,6 +96,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "histories", }, @@ -110,6 +113,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "kebab-test", }, @@ -131,6 +135,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "snake_test", }, @@ -156,6 +161,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": true, "name": "some_view", }, @@ -1126,6 +1132,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "types", }, @@ -1192,6 +1199,7 @@ Object { }, ], "comment": "this is the users table", + "isMaterializedView": false, "isView": false, "name": "users", }, @@ -1230,6 +1238,7 @@ Object { "type": "timestamptz", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_items", }, @@ -1252,6 +1261,7 @@ Object { "type": "text", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_other_items", }, @@ -1390,6 +1400,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "PascalTableName", }, @@ -1406,6 +1417,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "address", }, @@ -1422,6 +1434,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "histories", }, @@ -1438,6 +1451,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "kebab-test", }, @@ -1459,6 +1473,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "snake_test", }, @@ -1484,6 +1499,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": true, "name": "some_view", }, @@ -2454,6 +2470,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "types", }, @@ -2520,6 +2537,7 @@ Object { }, ], "comment": "this is the users table", + "isMaterializedView": false, "isView": false, "name": "users", }, @@ -2558,6 +2576,7 @@ Object { "type": "timestamptz", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_items", }, @@ -2580,6 +2599,7 @@ Object { "type": "text", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_other_items", }, @@ -2732,6 +2752,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "PascalTableName", }, @@ -2748,6 +2769,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "address", }, @@ -2764,6 +2786,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "histories", }, @@ -2780,6 +2803,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "kebab-test", }, @@ -2801,6 +2825,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "snake_test", }, @@ -2826,6 +2851,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": true, "name": "some_view", }, @@ -3796,6 +3822,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "types", }, @@ -3862,6 +3889,7 @@ Object { }, ], "comment": "this is the users table", + "isMaterializedView": false, "isView": false, "name": "users", }, @@ -3900,6 +3928,7 @@ Object { "type": "timestamptz", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_items", }, @@ -3922,6 +3951,7 @@ Object { "type": "text", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_other_items", }, @@ -4078,6 +4108,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "PascalTableName", }, @@ -4094,6 +4125,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "address", }, @@ -4110,6 +4142,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "histories", }, @@ -4126,6 +4159,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "kebab-test", }, @@ -4147,6 +4181,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "snake_test", }, @@ -4172,6 +4207,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": true, "name": "some_view", }, @@ -5142,6 +5178,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "types", }, @@ -5208,6 +5245,7 @@ Object { }, ], "comment": "this is the users table", + "isMaterializedView": false, "isView": false, "name": "users", }, @@ -5246,6 +5284,7 @@ Object { "type": "timestamptz", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_items", }, @@ -5268,6 +5307,7 @@ Object { "type": "text", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_other_items", }, @@ -5416,6 +5456,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "PascalTableName", }, @@ -5432,6 +5473,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "address", }, @@ -5448,6 +5490,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "histories", }, @@ -5464,6 +5507,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "kebab-test", }, @@ -5485,6 +5529,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "snake_test", }, @@ -5510,6 +5555,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": true, "name": "some_view", }, @@ -6480,6 +6526,7 @@ Object { }, ], "comment": null, + "isMaterializedView": false, "isView": false, "name": "types", }, @@ -6546,6 +6593,7 @@ Object { }, ], "comment": "this is the users table", + "isMaterializedView": false, "isView": false, "name": "users", }, @@ -6584,6 +6632,7 @@ Object { "type": "timestamptz", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_items", }, @@ -6606,6 +6655,7 @@ Object { "type": "text", }, ], + "isMaterializedView": true, "isView": true, "name": "materialized_other_items", },