Skip to content

Commit c8c9a29

Browse files
normalize types
1 parent 7c439c8 commit c8c9a29

File tree

1 file changed

+61
-2
lines changed

1 file changed

+61
-2
lines changed

machines/describe-table.js

+61-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module.exports = {
5151
example: [{
5252
fieldName: 'birthday',
5353
type: 'string', // number, string, boolean, dictionary, or array
54+
dbType: 'timezone with timestamp',
5455
indexed: true,
5556
unique: true,
5657
primaryKey: false,
@@ -76,7 +77,7 @@ module.exports = {
7677
var query = "SELECT x.nspname || '.' || x.relname as \"Table\", x.attnum as \"#\", x.attname as \"Column\", x.\"Type\"," +
7778
" case x.attnotnull when true then 'NOT NULL' else '' end as \"NULL\", r.conname as \"Constraint\", r.contype as \"C\", " +
7879
"r.consrc, fn.nspname || '.' || f.relname as \"F Key\", d.adsrc as \"Default\" FROM (" +
79-
"SELECT c.oid, a.attrelid, a.attnum, n.nspname, c.relname, a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod) as \"Type\", " +
80+
"SELECT c.oid, a.attrelid, a.attnum, n.nspname, c.relname, a.attname, pg_catalog.format_type(a.atttypid, null) as \"Type\", " +
8081
"a.attnotnull FROM pg_catalog.pg_attribute a, pg_namespace n, pg_class c WHERE a.attnum > 0 AND NOT a.attisdropped AND a.attrelid = c.oid " +
8182
"and c.relkind not in ('S','v') and c.relnamespace = n.oid and n.nspname not in ('pg_catalog','pg_toast','information_schema')) x " +
8283
"left join pg_attrdef d on d.adrelid = x.attrelid and d.adnum = x.attnum " +
@@ -173,7 +174,65 @@ module.exports = {
173174
obj.fieldName = column.Column;
174175

175176
// Set Type
176-
obj.type = column.Type;
177+
switch(column.Type) {
178+
179+
// Number types
180+
case 'smallint':
181+
case 'integer':
182+
case 'bigint':
183+
case 'decimal':
184+
case 'numeric':
185+
case 'real':
186+
case 'double precision':
187+
case 'smallserial':
188+
case 'bigserial':
189+
obj.type = 'number';
190+
break;
191+
192+
// String types
193+
case 'character':
194+
case 'char':
195+
case 'varchar':
196+
case 'character varying':
197+
case 'text':
198+
obj.type = 'string';
199+
break;
200+
201+
// Date types
202+
case 'timestamp':
203+
case 'timestamp without time zone':
204+
case 'timestamp with time zone':
205+
case 'time':
206+
case 'time without time zone':
207+
case 'time with time zone':
208+
case 'date':
209+
case 'interval':
210+
obj.type = 'string';
211+
break;
212+
213+
// Boolean type
214+
case 'boolean':
215+
obj.type = 'boolean';
216+
break;
217+
218+
// JSON type
219+
case 'json':
220+
obj.type = 'dictionary';
221+
break;
222+
223+
// Array types
224+
case 'array':
225+
obj.type = 'array';
226+
break;
227+
228+
// Everything else make a string
229+
default:
230+
obj.type = 'string';
231+
break;
232+
};
233+
234+
// Store the original db type as well
235+
obj.dbType = column.Type;
177236

178237
// Check for index
179238
if(column.indexed) {

0 commit comments

Comments
 (0)