-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
198 lines (171 loc) · 4.68 KB
/
index.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import pgStructure from 'pg-structure';
import userConfig from './config.js';
function getEntity(entity, config) {
const baseUrl = (config && config.baseUrl) || '';
const version =
(config && config.schemaVersion) ||
'http://json-schema.org/draft-07/schema#';
const jsonSchema = {
$schema: version,
$id: `${baseUrl}/${entity.schema.name}/${entity.name}/schema.json`,
title: entity.name,
description: entity.comment,
properties: {},
required: [],
type: 'object'
};
const columns = entity.columns;
for (const column of columns) {
jsonSchema.properties[column.name] = _getColumnData(column);
if (column.notNull && column.defaultWithTypeCast === null) {
jsonSchema.required.push(column.name);
}
}
return jsonSchema;
}
async function getSchemas(override = {}) {
const config = Object.assign({}, userConfig.config, override);
const schemas = config.schemas.map((i) => i.name);
const db = await pgStructure.default(
{
database: config.db.database,
user: config.db.user,
password: config.db.password
},
{ includeSchemas: schemas }
);
const collection = {};
for (const dbSchema of schemas) {
const schema = db.get(dbSchema);
collection[schema.name] = {};
const schemaConfig = config.schemas.find((s) => s.name === schema.name);
const tables = schema.tables.filter(
(table) => !schemaConfig.ignores.includes(table.name)
);
for (const table of tables) {
collection[schema.name][table.name] = getEntity(table, config);
}
}
return collection;
}
function _getColumnData(column) {
const columnType = column.type.name;
const isArray = column.arrayDimension > 0;
switch (columnType) {
case 'bit':
case 'bit varying':
case 'varbit':
case 'character':
case 'character varying':
case 'geography':
case 'text': {
const typeDef = { type: 'string' };
if (isArray) {
return { type: 'array', items: typeDef };
}
return typeDef;
}
case 'uuid': {
const typeDef = { type: 'string', format: 'uuid' };
if (isArray) {
return { type: 'array', items: typeDef };
}
return typeDef;
}
case 'date': {
const typeDef = { type: 'string', format: 'date' };
if (isArray) {
return { type: 'array', items: typeDef };
}
return typeDef;
}
case 'time with time zone':
case 'time without time zone': {
const typeDef = { type: 'string', format: 'time' };
if (isArray) {
return { type: 'array', items: typeDef };
}
return typeDef;
}
case 'timestamp with time zone':
case 'timestamp without time zone':
case 'timestamp': {
const typeDef = { type: 'string', format: 'date-time' };
if (isArray) {
return { type: 'array', items: typeDef };
}
return typeDef;
}
case 'boolean': {
const typeDef = { type: 'boolean' };
if (isArray) {
return { type: 'array', items: typeDef };
}
return typeDef;
}
case 'bigint':
case 'decimal':
case 'double precision':
case 'float8':
case 'int':
case 'integer':
case 'smallint':
case 'numeric':
case 'real': {
const typeDef = { type: 'number' };
if (isArray) {
return { type: 'array', items: typeDef };
}
return typeDef;
}
case 'json':
case 'jsonb': {
const typeDef = { type: 'object', properties: {} };
if (isArray) {
return { type: 'array', items: typeDef };
}
return typeDef;
}
case 'interval': {
const typeDef = {
oneOf: [
{
type: 'number',
description: 'Duration in seconds'
},
{
type: 'string',
description: 'Descriptive duration i.e. 8 hours'
},
{
type: 'object',
description: 'Duration object',
properties: {
years: { type: 'number' },
months: { type: 'number' },
days: { type: 'number' },
hours: { type: 'number' },
minutes: { type: 'number' },
seconds: { type: 'number' },
milliseconds: { type: 'number' }
}
}
]
};
if (isArray) {
return { type: 'array', items: typeDef };
}
return typeDef;
}
default: {
if (column.type.category === 'E' && Array.isArray(column.type.values)) {
return { enum: column.type.values };
}
console.warn(
`Unsupported column type: ${columnType}. Defaulting to null`
);
return { type: 'null' };
}
}
}
export { getEntity, getSchemas };