diff --git a/src/index.ts b/src/index.ts index 1266bb3..00b0a01 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,10 +45,23 @@ function buildHeader (db: Database, tables: string[], schema: string|null, optio ` } -export async function typescriptOfTable (db: Database|string, - table: string, - schema: string, - options = new Options()) { +/** + * Define common interfaces used for db column types (eg : Json) + */ +function getCommonInterfaces(): string { + + return ` + interface JsonParsed { + [k: string]: string | number | boolean | Date | JsonParsed | JsonParsedArray; + } + interface JsonParsedArray extends Array { } + ` +} + +export async function typescriptOfTable(db: Database | string, + table: string, + schema: string, + options = new Options()) { if (typeof db === 'string') { db = getDatabase(db) } @@ -87,6 +100,7 @@ export async function typescriptOfSchema (db: Database|string, if (optionsObject.options.writeHeader) { output += buildHeader(db, tables, schema, options) } + output += getCommonInterfaces() output += enumTypes output += interfaces diff --git a/src/schemaMysql.ts b/src/schemaMysql.ts index f5beff1..cff874f 100644 --- a/src/schemaMysql.ts +++ b/src/schemaMysql.ts @@ -53,7 +53,7 @@ export class MysqlDatabase implements Database { column.tsType = 'boolean' return column case 'json': - column.tsType = 'Object' + column.tsType = 'JsonParsed' return column case 'date': case 'datetime': diff --git a/src/schemaPostgres.ts b/src/schemaPostgres.ts index 2acceb9..79a67fb 100644 --- a/src/schemaPostgres.ts +++ b/src/schemaPostgres.ts @@ -46,7 +46,7 @@ export class PostgresDatabase implements Database { return column case 'json': case 'jsonb': - column.tsType = 'Object' + column.tsType = 'JsonParsed' return column case 'date': case 'timestamp': @@ -74,7 +74,7 @@ export class PostgresDatabase implements Database { return column case '_json': case '_jsonb': - column.tsType = 'Array' + column.tsType = 'Array' return column case '_timestamptz': column.tsType = 'Array' diff --git a/test/expected/postgres/osm.ts b/test/expected/postgres/osm.ts index 8c59518..75acd88 100644 --- a/test/expected/postgres/osm.ts +++ b/test/expected/postgres/osm.ts @@ -42,7 +42,7 @@ export namespace usersFields { export type char_col = string | null; export type time_col = string | null; export type inet_col = string | null; - export type jsonb_col = Object | null; + export type jsonb_col = JsonParsed | null; export type numeric_col = number | null; export type bytea_col = string | null; export type bool_array_col = Array | null; @@ -58,12 +58,12 @@ export namespace usersFields { export type time_with_tz = string | null; export type oid_col = number | null; export type interval_col = string | null; - export type json_col = Object | null; + export type json_col = JsonParsed | null; export type date_col = Date | null; export type unspported_path_type = any | null; export type name_type_col = string | null; - export type json_array_col = Array | null; - export type jsonb_array_col = Array | null; + export type json_array_col = Array | null; + export type jsonb_array_col = Array | null; export type timestamptz_array_col = Array | null; } diff --git a/test/unit/schemaMysql.test.ts b/test/unit/schemaMysql.test.ts index 2b33878..4aa5dee 100644 --- a/test/unit/schemaMysql.test.ts +++ b/test/unit/schemaMysql.test.ts @@ -398,7 +398,7 @@ describe('MysqlDatabase', () => { nullable: false } } - assert.equal(MysqlDBReflection.mapTableDefinitionToType(td,[],options).column.tsType, 'Object') + assert.equal(MysqlDBReflection.mapTableDefinitionToType(td, [], options).column.tsType, 'JsonParsed') }) }) describe('maps to Date', () => { diff --git a/test/unit/schemaPostgres.test.ts b/test/unit/schemaPostgres.test.ts index f8faa16..098b6fc 100644 --- a/test/unit/schemaPostgres.test.ts +++ b/test/unit/schemaPostgres.test.ts @@ -350,7 +350,7 @@ describe('PostgresDatabase', () => { nullable: false } } - assert.equal(PostgresDBReflection.mapTableDefinitionToType(td,[],options).column.tsType, 'Object') + assert.equal(PostgresDBReflection.mapTableDefinitionToType(td, [], options).column.tsType, 'JsonParsed') }) it('jsonb', () => { const td: TableDefinition = { @@ -359,7 +359,7 @@ describe('PostgresDatabase', () => { nullable: false } } - assert.equal(PostgresDBReflection.mapTableDefinitionToType(td,[],options).column.tsType, 'Object') + assert.equal(PostgresDBReflection.mapTableDefinitionToType(td, [], options).column.tsType, 'JsonParsed') }) }) describe('maps to Date', () => { @@ -523,7 +523,7 @@ describe('PostgresDatabase', () => { nullable: false } } - assert.equal(PostgresDBReflection.mapTableDefinitionToType(td,[],options).column.tsType, 'Array') + assert.equal(PostgresDBReflection.mapTableDefinitionToType(td, [], options).column.tsType, 'Array') }) it('_jsonb', () => { const td: TableDefinition = { @@ -532,7 +532,7 @@ describe('PostgresDatabase', () => { nullable: false } } - assert.equal(PostgresDBReflection.mapTableDefinitionToType(td,[],options).column.tsType, 'Array') + assert.equal(PostgresDBReflection.mapTableDefinitionToType(td, [], options).column.tsType, 'Array') }) })