Skip to content

Commit

Permalink
feat: generate types for views and materialized views
Browse files Browse the repository at this point in the history
  • Loading branch information
aldis-ameriks committed Aug 23, 2024
1 parent e34d23e commit 9710d2d
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cjs/src/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion cjs/src/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion src/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion tap-snapshots/test/cli.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 10 additions & 0 deletions tap-snapshots/test/pg-typegen.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Object {
},
],
"comment": null,
"isMaterializedView": false,
"isView": false,
"name": "address",
},
Expand All @@ -69,6 +70,7 @@ Object {
},
],
"comment": null,
"isMaterializedView": false,
"isView": false,
"name": "histories",
},
Expand All @@ -85,6 +87,7 @@ Object {
},
],
"comment": null,
"isMaterializedView": false,
"isView": false,
"name": "kebab-test",
},
Expand Down Expand Up @@ -123,6 +126,7 @@ Object {
"type": "timestamptz",
},
],
"isMaterializedView": true,
"isView": true,
"name": "materialized_items",
},
Expand All @@ -145,6 +149,7 @@ Object {
"type": "text",
},
],
"isMaterializedView": true,
"isView": true,
"name": "materialized_other_items",
},
Expand All @@ -170,6 +175,7 @@ Object {
},
],
"comment": null,
"isMaterializedView": false,
"isView": false,
"name": "PascalTableName",
},
Expand All @@ -191,6 +197,7 @@ Object {
},
],
"comment": null,
"isMaterializedView": false,
"isView": false,
"name": "snake_test",
},
Expand All @@ -216,6 +223,7 @@ Object {
},
],
"comment": null,
"isMaterializedView": false,
"isView": true,
"name": "some_view",
},
Expand Down Expand Up @@ -1186,6 +1194,7 @@ Object {
},
],
"comment": null,
"isMaterializedView": false,
"isView": false,
"name": "types",
},
Expand Down Expand Up @@ -1252,6 +1261,7 @@ Object {
},
],
"comment": "this is the users table",
"isMaterializedView": false,
"isView": false,
"name": "users",
},
Expand Down
Loading

0 comments on commit 9710d2d

Please sign in to comment.