Skip to content

Commit

Permalink
main api interface accept db as a string type
Browse files Browse the repository at this point in the history
  • Loading branch information
xiamx committed Aug 14, 2017
1 parent c588419 commit e5e0ef8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
4 changes: 1 addition & 3 deletions bin/schemats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ let argv: SchematsConfig = yargs
(async () => {

try {
let db = getDatabase(argv.conn)

if (!Array.isArray(argv.table)) {
if (!argv.table) {
argv.table = []
Expand All @@ -61,7 +59,7 @@ let argv: SchematsConfig = yargs
}

let formattedOutput = await typescriptOfSchema(
db, argv.table, argv.schema, { camelCase: argv.camelCase })
argv.conn, argv.table, argv.schema, { camelCase: argv.camelCase })
fs.writeFileSync(argv.output, formattedOutput)

} catch (e) {
Expand Down
20 changes: 14 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { generateEnumType, generateTableTypes, generateTableInterface } from './typescript'
import { Database } from './schema'
import { getDatabase, Database } from './schema'
import Options, { OptionValues } from './options'
import { processString } from 'typescript-formatter'
const pkgVersion = require('../package.json').version
Expand Down Expand Up @@ -45,21 +45,29 @@ function buildHeader (db: Database, tables: string[], schema: string|null, optio
`
}

export async function typescriptOfTable (db: Database,
export async function typescriptOfTable (db: Database|string,
table: string,
schema: string,
options = new Options()) {
if (typeof db === 'string') {
db = getDatabase(db)
}

let interfaces = ''
let tableTypes = await db.getTableTypes(table, schema, options)
interfaces += generateTableTypes(table, tableTypes, options)
interfaces += generateTableInterface(table, tableTypes, options)
return interfaces
}

export async function typescriptOfSchema (db: Database,
tables: string[],
schema: string|null,
options: OptionValues): Promise<string> {
export async function typescriptOfSchema (db: Database|string,
tables: string[] = [],
schema: string|null = null,
options: OptionValues = {}): Promise<string> {
if (typeof db === 'string') {
db = getDatabase(db)
}

if (!schema) {
schema = db.getDefaultSchema()
}
Expand Down

0 comments on commit e5e0ef8

Please sign in to comment.