-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathschemaPostgres.ts
More file actions
173 lines (161 loc) · 6.41 KB
/
Copy pathschemaPostgres.ts
File metadata and controls
173 lines (161 loc) · 6.41 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import * as PgPromise from 'pg-promise'
import { mapValues } from 'lodash'
import { keys } from 'lodash'
import Options from './options'
import { TableDefinition, Database } from './schemaInterfaces'
const pgp = PgPromise()
export class PostgresDatabase implements Database {
private db: PgPromise.IDatabase<{}>
constructor (public connectionString: string) {
this.db = pgp(connectionString)
}
private static mapTableDefinitionToType (tableDefinition: TableDefinition, customTypes: string[], options: Options): TableDefinition {
return mapValues(tableDefinition, column => {
switch (column.udtName) {
case 'bpchar':
case 'char':
case 'varchar':
case 'text':
case 'citext':
case 'uuid':
case 'bytea':
case 'inet':
case 'time':
case 'timetz':
case 'interval':
case 'name':
column.tsType = 'string'
return column
case 'int2':
case 'int4':
case 'int8':
case 'float4':
case 'float8':
case 'numeric':
case 'money':
case 'oid':
column.tsType = 'number'
return column
case 'bool':
column.tsType = 'boolean'
return column
case 'json':
case 'jsonb':
column.tsType = 'Object'
return column
case 'date':
case 'timestamp':
case 'timestamptz':
column.tsType = 'Date'
return column
case '_int2':
case '_int4':
case '_int8':
case '_float4':
case '_float8':
case '_numeric':
case '_money':
column.tsType = 'Array<number>'
return column
case '_bool':
column.tsType = 'Array<boolean>'
return column
case '_varchar':
case '_text':
case '_citext':
case '_uuid':
case '_bytea':
column.tsType = 'Array<string>'
return column
case '_json':
case '_jsonb':
column.tsType = 'Array<Object>'
return column
case '_timestamptz':
column.tsType = 'Array<Date>'
return column
default:
if (customTypes.indexOf(column.udtName) !== -1) {
column.tsType = options.transformTypeName(column.udtName)
return column
} else {
console.log(`Type [${column.udtName} has been mapped to [any] because no specific type has been found.`)
column.tsType = 'any'
return column
}
}
})
}
public query (queryString: string) {
return this.db.query(queryString)
}
public async getEnumTypes (schema?: string) {
type T = {name: string, value: any}
let enums: any = {}
let enumSchemaWhereClause = schema ? pgp.as.format(`where n.nspname = $1`, schema) : ''
await this.db.each<T>(
'select n.nspname as schema, t.typname as name, e.enumlabel as value ' +
'from pg_type t ' +
'join pg_enum e on t.oid = e.enumtypid ' +
'join pg_catalog.pg_namespace n ON n.oid = t.typnamespace ' +
`${enumSchemaWhereClause} ` +
'order by t.typname asc, e.enumlabel asc;', [],
(item: T) => {
if (!enums[item.name]) {
enums[item.name] = []
}
enums[item.name].push(item.value)
}
)
return enums
}
public async getTableDefinition (tableName: string, tableSchema: string) {
let tableDefinition: TableDefinition = {}
type T = { column_name: string, udt_name: string, is_nullable: string, constraint_type: 'UNIQUE' | 'PRIMARY KEY' }
await this.db.each<T>(
'SELECT column_name, udt_name, is_nullable ' +
'FROM information_schema.columns ' +
'WHERE table_name = $1 and table_schema = $2',
[tableName, tableSchema],
(schemaItem: T) => {
tableDefinition[schemaItem.column_name] = {
udtName: schemaItem.udt_name,
nullable: schemaItem.is_nullable === 'YES',
primaryKey: false,
unique: false
}
})
// Fill in PRIMARY KEY and UNIQUE constraint details
await this.db.each<T>(
'SELECT kcu.column_name, tc.constraint_type ' +
'FROM information_schema.table_constraints AS tc ' +
'JOIN information_schema.key_column_usage AS kcu ' +
'ON tc.constraint_name = kcu.constraint_name ' +
'WHERE tc.table_name = $1 and tc.table_schema = $2',
[tableName, tableSchema],
(schemaItem: T) => {
tableDefinition[schemaItem.column_name].unique = schemaItem.constraint_type === 'UNIQUE'
tableDefinition[schemaItem.column_name].primaryKey = schemaItem.constraint_type === 'PRIMARY KEY'
}
)
return tableDefinition
}
public async getTableTypes (tableName: string, tableSchema: string, options: Options) {
let enumTypes = await this.getEnumTypes()
let customTypes = keys(enumTypes)
return PostgresDatabase.mapTableDefinitionToType(await this.getTableDefinition(tableName, tableSchema), customTypes, options)
}
public async getSchemaTables (schemaName: string): Promise<string[]> {
return await this.db.map<string>(
'SELECT table_name ' +
'FROM information_schema.columns ' +
'WHERE table_schema = $1 ' +
'GROUP BY table_name',
[schemaName],
(schemaItem: {table_name: string}) => schemaItem.table_name
)
}
getDefaultSchema (): string {
return 'public'
}
}