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

Handle upper case columns #103

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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"main": "./src/index.js",
"types": "./src/index.d.ts",
"scripts": {
"lint": "tslint --project tsconfig.json --type-check 'src/**/*.ts' 'test/**/*.test.ts' 'bin/**/*.ts' --exclude '**/*.d.ts'",
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"dependency-check": "dependency-check . --entry bin/schemats.js --missing --no-dev",
"test": "npm run lint && npm run build && npm run dependency-check && mocha",
Expand Down
9 changes: 8 additions & 1 deletion src/schemaMysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,19 @@ export class MysqlDatabase implements Database {
if (error) {
return reject(error)
}
return resolve(results)
return resolve(this.toLowerCaseColumnName(results))
})
})
}

public getDefaultSchema (): string {
return this.defaultSchema
}

private toLowerCaseColumnName (results: Object[]): Object[] {
return results.map((row: any) => Object.keys(row).reduce((newRow, key) => {
newRow[key.toLowerCase()] = row[key]
return newRow
}, {} as any))
}
}
4 changes: 2 additions & 2 deletions test/integration/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('schemats cli tool integration testing', () => {
it('should run without error', () => {
let {status, stdout, stderr} = spawnSync('node', [
'bin/schemats', 'generate',
'-c', process.env.POSTGRES_URL,
'-c', process.env.POSTGRES_URL as string,
'-o', '/tmp/schemats_cli_postgres.ts'
], { encoding: 'utf-8' })
console.log('opopopopop', stdout, stderr)
Expand All @@ -27,7 +27,7 @@ describe('schemats cli tool integration testing', () => {
it('should run without error', () => {
let {status} = spawnSync('node', [
'bin/schemats', 'generate',
'-c', process.env.MYSQL_URL,
'-c', process.env.MYSQL_URL as string,
'-s', 'test',
'-o', '/tmp/schemats_cli_postgres.ts'
])
Expand Down
13 changes: 6 additions & 7 deletions test/testUtility.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as fs from 'mz/fs'
import { typescriptOfSchema, Database } from '../src/index'
import Options from '../src/options'
import * as ts from 'typescript';
import * as ts from 'typescript'

const diff = require('diff')
interface IDiffResult {
Expand All @@ -11,13 +10,14 @@ interface IDiffResult {
removed?: boolean
}

export function compile(fileNames: string[], options: ts.CompilerOptions): boolean {
export function compile (fileNames: string[], options: ts.CompilerOptions): boolean {
let program = ts.createProgram(fileNames, options)
let emitResult = program.emit()
let exitCode = emitResult.emitSkipped ? 1 : 0
return exitCode === 0
}
export async function compare(goldStandardFile: string, outputFile: string): Promise<boolean> {

export async function compare (goldStandardFile: string, outputFile: string): Promise<boolean> {

let gold = await fs.readFile(goldStandardFile, {encoding: 'utf8'})
let actual = await fs.readFile(outputFile, {encoding: 'utf8'})
Expand All @@ -38,15 +38,14 @@ export async function compare(goldStandardFile: string, outputFile: string): Pro
}
}


export async function loadSchema(db: Database, file: string) {
export async function loadSchema (db: Database, file: string) {
let query = await fs.readFile(file, {
encoding: 'utf8'
})
return await db.query(query)
}

export async function writeTsFile(inputSQLFile: string, inputConfigFile: string, outputFile: string, db: Database) {
export async function writeTsFile (inputSQLFile: string, inputConfigFile: string, outputFile: string, db: Database) {
await loadSchema(db, inputSQLFile)
const config: any = require(inputConfigFile)
let formattedOutput = await typescriptOfSchema(
Expand Down
10 changes: 10 additions & 0 deletions test/unit/schemaMysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ describe('MysqlDatabase', () => {
const results = await testDb.query('SELECT * FROM test_table')
assert.deepEqual(results, [])
})
it('query returns with results with columns as lower-case', async () => {
(mysql.createConnection as any).returns({
query: function query (queryString: string, params: Array<any>, cb: Function) {
cb(null, [{COLUMN_1: 'val1', COLUMN_2: 'val1'}, {COLUMN_1: 'val2', COLUMN_2: 'val2'}])
}
})
const testDb: any = new MysqlDatabase('mysql://user:password@localhost/test')
const results = await testDb.query('SELECT * FROM test_table')
assert.deepEqual(results, [{column_1: 'val1', column_2: 'val1'}, {column_1: 'val2', column_2: 'val2'}])
})
})
describe('getEnumTypes', () => {
it('writes correct query with schema name', async () => {
Expand Down
8 changes: 7 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"dts",
"test/actual",
"test/expected",
"test/fixture"
"test/fixture",
"**/*.d.ts"
],
"include": [
"src/**/*.ts",
"test/**/*.test.ts",
"bin/**/*.ts"
]
}