/
DatabaseSchema.ts
51 lines (39 loc) · 1.63 KB
/
DatabaseSchema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { DatabaseTable } from './DatabaseTable';
import { SchemaHelper } from './SchemaHelper';
import { AbstractSqlConnection } from '../connections/AbstractSqlConnection';
import { Configuration } from '../utils';
export class DatabaseSchema {
private readonly tables: DatabaseTable[] = [];
addTable(name: string, schema: string | undefined): DatabaseTable {
const table = new DatabaseTable(name, schema);
this.tables.push(table);
return table;
}
getTables(): DatabaseTable[] {
return this.tables;
}
getTable(name: string): DatabaseTable | undefined {
return this.tables.find(t => t.name === name);
}
static async create(connection: AbstractSqlConnection, helper: SchemaHelper, config: Configuration) {
const schema = new DatabaseSchema();
const tables = await connection.execute<Table[]>(helper.getListTablesSQL());
for (const t of tables) {
if (t.table_name === config.get('migrations').tableName!) {
continue;
}
const table = schema.addTable(t.table_name, t.schema_name);
const cols = await helper.getColumns(connection, t.table_name, t.schema_name);
const indexes = await helper.getIndexes(connection, t.table_name, t.schema_name);
const pks = await helper.getPrimaryKeys(connection, indexes, t.table_name, t.schema_name);
const fks = await helper.getForeignKeys(connection, t.table_name, t.schema_name);
const enums = await helper.getEnumDefinitions(connection, t.table_name, t.schema_name);
table.init(cols, indexes, pks, fks, enums);
}
return schema;
}
}
export interface Table {
table_name: string;
schema_name?: string;
}