-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathPostgresMetaTypes.ts
70 lines (67 loc) · 1.72 KB
/
PostgresMetaTypes.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
import { filterByList } from './helpers.js'
import { typesSql } from './sql/index.js'
import { PostgresMetaResult, PostgresType } from './types.js'
export default class PostgresMetaTypes {
query: (sql: string) => Promise<PostgresMetaResult<any>>
constructor(query: (sql: string) => Promise<PostgresMetaResult<any>>) {
this.query = query
}
async list({
includeTableTypes = false,
includeArrayTypes = false,
includeSystemSchemas = false,
includedSchemas,
excludedSchemas,
limit,
offset,
}: {
includeTableTypes?: boolean
includeArrayTypes?: boolean
includeSystemSchemas?: boolean
includedSchemas?: string[]
excludedSchemas?: string[]
limit?: number
offset?: number
} = {}): Promise<PostgresMetaResult<PostgresType[]>> {
let sql = `${typesSql}
where
(
t.typrelid = 0
or (
select
c.relkind ${includeTableTypes ? `in ('c', 'r', 'v')` : `= 'c'`}
from
pg_class c
where
c.oid = t.typrelid
)
)
`
if (!includeArrayTypes) {
sql += ` and not exists (
select
from
pg_type el
where
el.oid = t.typelem
and el.typarray = t.oid
)`
}
const filter = filterByList(
includedSchemas,
excludedSchemas,
!includeSystemSchemas ? DEFAULT_SYSTEM_SCHEMAS : undefined
)
if (filter) {
sql += ` and n.nspname ${filter}`
}
if (limit) {
sql += ` limit ${limit}`
}
if (offset) {
sql += ` offset ${offset}`
}
return await this.query(sql)
}
}