Commit dbfb9d43 authored by Ennen Chris-Robin's avatar Ennen Chris-Robin
Browse files

Merge branch '2-db-connection' into 'main'

Resolve "DB connection"

Closes #2

See merge request !2
parents 6b57061d 7d7b9b5f
Pipeline #27764 passed with stages
in 1 minute and 46 seconds
This diff is collapsed.
......@@ -38,8 +38,13 @@
"typescript": "^4.4.4"
},
"dependencies": {
"json": "^11.0.0",
"knex": "^0.95.11"
"knex": "^0.95.11",
"knex-schema-inspector": "^1.6.4",
"mysql": "^2.18.1",
"oracledb": "^5.3.0",
"pg": "^8.7.1",
"sqlite3": "^5.0.2",
"tedious": "^14.0.0"
},
"husky": {
"hooks": {
......
import knex from 'knex'
import type { Knex } from 'knex'
import Client from './client'
import type { ClientParams } from './client'
jest.mock('knex')
jest.mock('knex-schema-inspector')
describe('The DB connection client', () => {
it('Initialize the knex client with correct parameters', () => {
const params: ClientParams = {
database: 'db',
dbtype: 'sqlite3',
host: 'localhost',
password: 'abc123',
port: 5432,
user: 'user',
searchPath: 'path',
}
new Client(params)
const { dbtype: client, ...connection } = params
const knexConfig: Knex.Config = { client, connection }
expect(knex).toHaveBeenCalledWith(knexConfig)
})
})
import knex from 'knex'
import schemaInspector from 'knex-schema-inspector'
export type SupportedDialects = 'pg' | 'mysql' | 'sqlite3' | 'oracledb' | 'mssql'
export interface ClientParams {
dbtype: SupportedDialects
host: string
port: number
user: string
password: string
database: string
searchPath?: string
}
/**
* The SQL client
*
*/
export default class Client {
protected dbClient
protected inspector
protected tables?: string[]
constructor(params: ClientParams) {
const { dbtype, ...connection } = params
this.dbClient = knex({
client: dbtype,
connection,
})
this.inspector = schemaInspector(this.dbClient)
}
async loadMetadata() {
this.loadTablesMetadata()
}
private async loadTablesMetadata() {
this.tables = await this.inspector.tables()
}
}
import { getCli } from "./cli/yargs"
import { getCli } from './cli/yargs'
import Client from './client/client'
import type { SupportedDialects } from './client/client'
const args = getCli()
const client = new Client({
database: args.dbname,
dbtype: args.dbtype as SupportedDialects,
host: args.hostname,
password: args.password,
port: args.port,
user: args.user,
searchPath: args.schemaname,
})
/**
*
*
* @param {string} string
*/
function test(string:string) {
console.log(string)
}
console.log(getCli())
console.log('test')
console.log('abc')
console.log(client)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment