Simple module that exposes all views
in information_schema
and pg_catalog
as TypeORM
entities (ViewEntity
).
Currently only Postgres is supported, but support for other
databases with information_schema
is possible.
I wanted to learn something new about Postgres internals, TypeORM internals and TypeScript.
Let's inspect columns of Photo
:
import { createConnection } from "typeorm";
// We import from /postgres/14
// Main module exports these under name Postgres14
import { entities, InformationSchema } from 'typeorm-information-schema/postgres/14'
@Entity()
export class Photo {
@PrimaryGeneratedColumn()
id: number;
@Column({
length: 100
})
name: string;
@Column()
isPublished: boolean;
}
const connection = await createConnection({
type: "postgres",
...
});
console.log(
await connection.getRepository(InformationSchema.Columns).find({
where: {
table_schema: 'public',
table_name: 'photo'
}
})
);
import { createConnection } from "typeorm";
// We import from /postgres/14
// Main module exports these under name Postgres14
import { entities, PgCatalog } from 'typeorm-information-schema/postgres/14'
const connection = await createConnection({
...
});
console.log(
await connection.getRepository(PgCatalog.PgStatActivity).find()
);
Clone this repository and run following commands (
requires docker-compose
) to enter REPL.
Only Postgres 14 is supported right now.
npm ci
npx tsc
npm run playground:postgres:14
Then just type:
await getRepository(InformationSchema.Columns).find({
where: {
table_schema: 'information_schema',
table_name: 'columns'
}
});
Following global variables are registered: connection
, InformationSchema
, PgCatalog
, entities
, getRepository
, pgQuery
(bound connection.query
), findMetadata
.
Relations in pg_catalog
follows following naming convention:
- If relation type is many-to-one and column field ends with
id
, relation will be named withoutid
(eg.srvid
will becomesrv
) - If relation type is many-to-one and column field doesn't end with
id
, relation will be named${fieldName}_rel
( eg.database
will becomedatabase_rel
) - If relation type is one-to-many, name corresponding to referenced entity will be chosen
Many entities in PgCatalog
for Postgres 14 has automatically generated comments.
Planned:
- Support relations between views (eg.
allow
getRepository(PgCatalog.PgStatActivity).find({relations: ['locks', 'locks.relation']})
to find locked tables)