Skip to content

Commit

Permalink
✨ output data to file
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelvalley committed Dec 9, 2023
1 parent 42882e8 commit 2e1c26b
Show file tree
Hide file tree
Showing 22 changed files with 165 additions and 112 deletions.
2 changes: 2 additions & 0 deletions data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ID,NAME,AGE,ADDRESS,SALARY
1,Paul,32,California,20000
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"@types/inquirer": "^9.0.7",
"@types/mssql": "^9.1.4",
"@types/pg": "^8.10.9",
"csv": "^6.3.6",
"inquirer": "^9.2.12",
"json-2-csv": "^5.0.1",
"mssql": "^10.0.1",
"pg": "^8.11.3",
"prisma": "^5.7.0",
Expand Down
48 changes: 20 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 18 additions & 25 deletions src/AppCommand.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import {Command, Flags, ux} from '@oclif/core'
import {Driver, drivers, isDriver, runQuery, sqlqdb} from './database.js'
import {Connection, PrismaClient} from '@prisma/client'
import {Format, printFormatted} from './output.js'
import {Format, outputData} from './output.js'

export abstract class AppCommand extends Command {
sqlqdb = sqlqdb

async load<T>(start: string, task: Promise<T>, stop?: string) {
ux.action.start(start)
const result = await task
ux.action.stop(stop)

return result
}

assertConnectionExists(alias: string, connection: Connection | null): asserts connection is Connection {
if (!connection) {
ux.error(`Connection with alias '${alias}' not found`)
Expand Down Expand Up @@ -43,23 +35,27 @@ export abstract class AppCommand extends Command {
},
})

async printQuery(driver: Driver, connectionString: string, query: string, format: Format) {
console.log({query})
const message = `Executing query ${query.slice(0, 100)}`

const result = await this.load(message, runQuery(driver, connectionString, query))
printFormatted(format, result)
async printQuery(driver: Driver, connectionString: string, query: string, format: Format, file: string | undefined) {
const result = await runQuery(driver, connectionString, query)
outputData(format, file, result)

return result
}

async printQueryWithHistory(driver: string, alias: string, connectionString: string, query: string, format: Format) {
async printQueryWithHistory(
driver: string,
alias: string,
connectionString: string,
query: string,
format: Format,
file: string | undefined,
) {
if (!isDriver(driver)) {
ux.error(`Connection has driver '${driver}' which is not supported`)
}

try {
await this.printQuery(driver, connectionString, query, format)
await this.printQuery(driver, connectionString, query, format, file)
await this.saveHistory(alias, query, true)
} catch (err) {
await this.saveHistory(alias, query, false)
Expand All @@ -68,13 +64,10 @@ export abstract class AppCommand extends Command {
}

getConnection(alias: string) {
return this.load(
`Getting connection details for '${alias}'`,
this.sqlqdb.connection.findFirst({
where: {
alias,
},
}),
)
return this.sqlqdb.connection.findFirst({
where: {
alias,
},
})
}
}
7 changes: 4 additions & 3 deletions src/commands/connection/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Args, Command, Flags, ux} from '@oclif/core'
import {PrismaClient} from '@prisma/client'
import {format, printFormatted} from '../../output.js'
import {format, outfile, outputData} from '../../output.js'
import {drivers, sqlqdb} from '../../database.js'
import {AppCommand} from '../../AppCommand.js'

Expand All @@ -24,12 +24,13 @@ export default class Create extends AppCommand {

static flags = {
format,
outfile,
}

async run(): Promise<any> {
const {args, flags} = await this.parse(Create)
const {alias, driver, connectionString, description} = args
const {format} = flags
const {format, outfile} = flags

const connection = await this.sqlqdb.connection.findFirst({
where: {
Expand All @@ -50,6 +51,6 @@ export default class Create extends AppCommand {
},
})

printFormatted(format, result)
outputData(format, outfile, result)
}
}
7 changes: 4 additions & 3 deletions src/commands/connection/delete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Args, Command, Flags, ux} from '@oclif/core'
import {PrismaClient} from '@prisma/client'
import {format, printFormatted} from '../../output.js'
import {format, outfile, outputData} from '../../output.js'
import {sqlqdb} from '../../database.js'
import {AppCommand} from '../../AppCommand.js'

Expand All @@ -17,12 +17,13 @@ export default class Delete extends AppCommand {

static flags = {
format,
outfile,
}

async run(): Promise<any> {
const {args, flags} = await this.parse(Delete)
const {alias} = args
const {format} = flags
const {format, outfile} = flags

const result = await this.sqlqdb.connection.delete({
where: {
Expand All @@ -31,6 +32,6 @@ export default class Delete extends AppCommand {
},
})

printFormatted(format, result)
outputData(format, outfile, result)
}
}
7 changes: 4 additions & 3 deletions src/commands/connection/get.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Args, Command, Flags, ux} from '@oclif/core'
import {PrismaClient} from '@prisma/client'
import {format, printFormatted} from '../../output.js'
import {format, outfile, outputData} from '../../output.js'
import {sqlqdb} from '../../database.js'
import {AppCommand} from '../../AppCommand.js'

Expand All @@ -15,14 +15,15 @@ export default class Get extends AppCommand {

static flags = {
format,
outfile,
}

static aliases = ['conn:get']

async run(): Promise<any> {
const {flags, args} = await this.parse(Get)
const {alias} = args
const {format} = flags
const {format, outfile} = flags

const result = await this.sqlqdb.connection.findFirst({
where: {
Expand All @@ -32,6 +33,6 @@ export default class Get extends AppCommand {

this.assertConnectionExists(alias, result)

printFormatted(format, result)
outputData(format, outfile, result)
}
}
7 changes: 4 additions & 3 deletions src/commands/connection/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Args, Command, Flags} from '@oclif/core'
import {PrismaClient} from '@prisma/client'
import {format, printFormatted} from '../../output.js'
import {format, outfile, outputData} from '../../output.js'
import {sqlqdb} from '../../database.js'
import {AppCommand} from '../../AppCommand.js'

Expand All @@ -20,12 +20,13 @@ export default class List extends AppCommand {

static flags = {
format,
outfile,
}

async run(): Promise<any> {
const {args, flags} = await this.parse(List)
const {search = ''} = args
const {format} = flags
const {format, outfile} = flags

const result = await this.sqlqdb.connection.findMany({
where: {
Expand All @@ -49,6 +50,6 @@ export default class List extends AppCommand {
},
})

printFormatted(format, result)
outputData(format, outfile, result)
}
}
7 changes: 4 additions & 3 deletions src/commands/history/delete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Args, Command, Flags, ux} from '@oclif/core'
import {PrismaClient} from '@prisma/client'
import {format, printFormatted} from '../../output.js'
import {format, outfile, outputData} from '../../output.js'
import {sqlqdb} from '../../database.js'
import {AppCommand} from '../../AppCommand.js'

Expand All @@ -15,19 +15,20 @@ export default class Delete extends AppCommand {

static flags = {
format,
outfile,
}

async run(): Promise<any> {
const {args, flags} = await this.parse(Delete)
const {id} = args
const {format} = flags
const {format, outfile} = flags

const result = await this.sqlqdb.history.delete({
where: {
id,
},
})

printFormatted(format, result)
outputData(format, outfile, result)
}
}
7 changes: 4 additions & 3 deletions src/commands/history/get.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Args, Command, Flags, ux} from '@oclif/core'
import {PrismaClient} from '@prisma/client'
import {format, printFormatted} from '../../output.js'
import {format, outfile, outputData} from '../../output.js'
import {sqlqdb} from '../../database.js'
import {AppCommand} from '../../AppCommand.js'

Expand All @@ -15,12 +15,13 @@ export default class Get extends AppCommand {

static flags = {
format,
outfile,
}

async run(): Promise<any> {
const {flags, args} = await this.parse(Get)
const {id} = args
const {format} = flags
const {format, outfile} = flags

const result = await this.sqlqdb.history.findFirst({
where: {
Expand All @@ -32,6 +33,6 @@ export default class Get extends AppCommand {
ux.error(`History with id '${id}' not found`)
}

printFormatted(format, result)
outputData(format, outfile, result)
}
}
Loading

0 comments on commit 2e1c26b

Please sign in to comment.