Skip to content

Commit

Permalink
✨ pg driver works (maybe mssql)
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelvalley committed Dec 9, 2023
1 parent 77f0d10 commit d8163cf
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 110 deletions.
24 changes: 3 additions & 21 deletions src/AppCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,15 @@ export abstract class AppCommand extends Command {
},
})

static confirmFlag = Flags.boolean({
description: 'Bypass query confirmation',
aliases: ['y'],
})

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

const message = `Running query on ${alias} ${connectionString}\n ${query}`

try {
const result = await runQuery(driver, connectionString, query)
console.log('here', result)
// const result = await this.load('Executing query', task)
const result = await this.load(message, runQuery(driver, connectionString, query))
printFormatted(format, result)
await this.saveHistory(alias, query, true)
} catch (err) {
Expand All @@ -65,19 +60,6 @@ export abstract class AppCommand extends Command {
}
}

async confirmQuery(alias: string, query: string, skip: boolean) {
const message = `Execute on ${alias}: \n ${query}`
if (skip) {
console.log(message)
return
}

const isConfirmed = await ux.confirm(message)
if (!isConfirmed) {
ux.error('Execution cancelled')
}
}

getConnection(alias: string) {
return this.load(
`Getting connection details for '${alias}'`,
Expand Down
6 changes: 3 additions & 3 deletions src/commands/connection/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class Create extends AppCommand {
options: drivers,
}),
connectionString: Args.string({description: 'Connection string for database', required: true}),
description: Args.string({description: 'Description of connection', required: false}),
}

static description = 'Create a connection to a database'
Expand All @@ -23,13 +24,12 @@ export default class Create extends AppCommand {

static flags = {
format,
description: Flags.string({description: 'Description of connection', required: false, aliases: ['d']}),
}

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

const connection = await this.db.connection.findFirst({
where: {
Expand Down
13 changes: 5 additions & 8 deletions src/commands/connection/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ export default class Get extends AppCommand {
const {alias} = args
const {format} = flags

const result = await this.load(
'Searching',
this.db.connection.findFirst({
where: {
alias,
},
}),
)
const result = await this.db.connection.findFirst({
where: {
alias,
},
})

this.assertConnectionExists(alias, result)

Expand Down
13 changes: 5 additions & 8 deletions src/commands/history/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ export default class Get extends AppCommand {
const {id} = args
const {format} = flags

const result = await this.load(
'Searching',
this.db.history.findFirst({
where: {
id,
},
}),
)
const result = await this.db.history.findFirst({
where: {
id,
},
})

if (!result) {
ux.error(`History with id '${id}' not found`)
Expand Down
43 changes: 20 additions & 23 deletions src/commands/history/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,28 @@ export default class List extends AppCommand {
const {search = ''} = args
const {alias, count, aliasExact, format} = flags

const result = await this.load(
'Searching',
this.db.history.findMany({
take: count,
orderBy: {
lastUsed: 'desc',
},

where: {
OR: [
{
query: {
contains: search,
},
const result = await this.db.history.findMany({
take: count,
orderBy: {
lastUsed: 'desc',
},

where: {
OR: [
{
query: {
contains: search,
},
{
connectionAlias: {
equals: aliasExact ? alias : undefined,
contains: aliasExact ? undefined : alias,
},
},
{
connectionAlias: {
equals: aliasExact ? alias : undefined,
contains: aliasExact ? undefined : alias,
},
],
},
}),
)
},
],
},
})

printFormatted(format, result)
}
Expand Down
3 changes: 3 additions & 0 deletions src/commands/history/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import History from '../query/history.js'

export default History
11 changes: 3 additions & 8 deletions src/commands/query/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,24 @@ import {PrismaClient} from '@prisma/client'
import {format, printFormatted} from '../../output.js'
import {AppCommand} from '../../AppCommand.js'

export default class SQL extends AppCommand {
export default class History extends AppCommand {
static args = {
id: Args.integer({description: 'ID of history entry to execute', required: true}),
}

static description = 'Re-run a previous database query'

static aliases = ['history:query']

static examples = []

static flags = {
format,
confirm: this.confirmFlag,
withAlias: Flags.string({description: 'Override the initial alias used to run the command', required: false}),
}

async run(): Promise<any> {
const {args, flags} = await this.parse(SQL)
const {args, flags} = await this.parse(History)
const {id} = args
const {format, withAlias, confirm} = flags
const {format, withAlias} = flags

const history = await this.db.history.findFirst({
where: {
Expand All @@ -40,8 +37,6 @@ export default class SQL extends AppCommand {

this.assertConnectionExists(alias, connection)

await this.confirmQuery(alias, history.query, confirm)

await this.executeQuery(connection.driver, alias, connection.connectionString, history.query, format)
}
}
8 changes: 2 additions & 6 deletions src/commands/query/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ export default class Tool extends AppCommand {

static examples = []

static aliases = ['history:query']

static flags = {
format,
confirm: this.confirmFlag,
params: Flags.string({
multiple: true,
aliases: ['p'],
Expand All @@ -28,7 +25,7 @@ export default class Tool extends AppCommand {
async run(): Promise<any> {
const {args, flags} = await this.parse(Tool)
const {alias, name} = args
const {format, confirm, params} = flags
const {format, params} = flags

const [connection, tool] = await Promise.all([
this.getConnection(alias),
Expand All @@ -47,8 +44,6 @@ export default class Tool extends AppCommand {

const query = buildQuery(tool.query, params)

await this.confirmQuery(alias, query, confirm)

await this.executeQuery(connection.driver, alias, connection.connectionString, query, format)

await this.db.tool.update({
Expand All @@ -64,6 +59,7 @@ export default class Tool extends AppCommand {
})
}
}

function buildQuery(query: string, params: string[] = []) {
return params.reduce((acc, param, i) => acc.replaceAll('$' + i, param), query)
}
13 changes: 5 additions & 8 deletions src/commands/tool/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ export default class Get extends AppCommand {
const {name} = args
const {format} = flags

const result = await this.load(
'Searching',
this.db.tool.findFirst({
where: {
name,
},
}),
)
const result = await this.db.tool.findFirst({
where: {
name,
},
})

if (!result) {
ux.error(`Tool with name '${name}' not found`)
Expand Down
39 changes: 18 additions & 21 deletions src/commands/tool/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,26 @@ export default class List extends AppCommand {
const {search = ''} = args
const {count, format} = flags

const result = await this.load(
'Searching',
this.db.tool.findMany({
take: count,
orderBy: {
lastUsed: 'desc',
},
where: {
OR: [
{
query: {
contains: search,
},
const result = await this.db.tool.findMany({
take: count,
orderBy: {
lastUsed: 'desc',
},
where: {
OR: [
{
query: {
contains: search,
},
{
description: {
contains: search,
},
},
{
description: {
contains: search,
},
],
},
}),
)
},
],
},
})

printFormatted(format, result)
}
Expand Down
3 changes: 3 additions & 0 deletions src/commands/tool/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Tool from '../query/tool.js'

export default Tool
8 changes: 4 additions & 4 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const mssql: QueryHandler = async (connectionString, query) => {
const client = await MSSQL.connect(connectionString)
try {
const result = await MSSQL.query(query)
await client.close()
client.close()
return result.output
} catch (err) {
await client.close()
client.close()
throw err
}
}
Expand All @@ -31,10 +31,10 @@ const pg: QueryHandler = async (connectionString, query) => {
try {
await client.connect()
const result = await client.query(query)
await client.end()
client.end()
return result.rows
} catch (err) {
await client.end()
client.end()
throw err
}
}
Expand Down

0 comments on commit d8163cf

Please sign in to comment.