Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a more convenient type for json columns (fix #80) #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | number | boolean | Date | JsonParsed | JsonParsedArray> { }
`
}

export async function typescriptOfTable(db: Database | string,
table: string,
schema: string,
options = new Options()) {
if (typeof db === 'string') {
db = getDatabase(db)
}
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/schemaMysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
4 changes: 2 additions & 2 deletions src/schemaPostgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -74,7 +74,7 @@ export class PostgresDatabase implements Database {
return column
case '_json':
case '_jsonb':
column.tsType = 'Array<Object>'
column.tsType = 'Array<JsonParsed>'
return column
case '_timestamptz':
column.tsType = 'Array<Date>'
Expand Down
8 changes: 4 additions & 4 deletions test/expected/postgres/osm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> | null;
Expand All @@ -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<Object> | null;
export type jsonb_array_col = Array<Object> | null;
export type json_array_col = Array<JsonParsed> | null;
export type jsonb_array_col = Array<JsonParsed> | null;
export type timestamptz_array_col = Array<Date> | null;

}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/schemaMysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
8 changes: 4 additions & 4 deletions test/unit/schemaPostgres.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -523,7 +523,7 @@ describe('PostgresDatabase', () => {
nullable: false
}
}
assert.equal(PostgresDBReflection.mapTableDefinitionToType(td,[],options).column.tsType, 'Array<Object>')
assert.equal(PostgresDBReflection.mapTableDefinitionToType(td, [], options).column.tsType, 'Array<JsonParsed>')
})
it('_jsonb', () => {
const td: TableDefinition = {
Expand All @@ -532,7 +532,7 @@ describe('PostgresDatabase', () => {
nullable: false
}
}
assert.equal(PostgresDBReflection.mapTableDefinitionToType(td,[],options).column.tsType, 'Array<Object>')
assert.equal(PostgresDBReflection.mapTableDefinitionToType(td, [], options).column.tsType, 'Array<JsonParsed>')
})
})

Expand Down