Skip to content

Commit

Permalink
✨ tool management
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelvalley committed Dec 9, 2023
1 parent 1b60af7 commit 218770c
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 8 deletions.
17 changes: 17 additions & 0 deletions prisma/migrations/20231209142417_tool_update_date/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- RedefineTables
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Tool" (
"name" TEXT NOT NULL PRIMARY KEY,
"created" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"lastUsed" DATETIME,
"query" TEXT NOT NULL,
"description" TEXT,
"count" INTEGER NOT NULL DEFAULT 0
);
INSERT INTO "new_Tool" ("count", "created", "description", "lastUsed", "name", "query") SELECT "count", "created", "description", "lastUsed", "name", "query" FROM "Tool";
DROP TABLE "Tool";
ALTER TABLE "new_Tool" RENAME TO "Tool";
CREATE UNIQUE INDEX "Tool_name_key" ON "Tool"("name");
PRAGMA foreign_key_check;
PRAGMA foreign_keys=ON;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ model History {
model Tool {
name String @id @unique
created DateTime @default(now())
updated DateTime @default(now())
lastUsed DateTime?
query String
description String?
Expand Down
4 changes: 3 additions & 1 deletion src/commands/connection/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ export default class Create extends AppCommand {
connectionString: Args.string({description: 'Connection string for database', required: true}),
}

static description = 'Exec a query on the given database'
static description = 'Create a connection to a database'

static examples = []

static aliases = ['conn:create']

static flags = {
format,
description: Flags.string({description: 'Description of connection', required: false, aliases: ['d']}),
Expand Down
2 changes: 2 additions & 0 deletions src/commands/connection/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default class Delete extends AppCommand {

static examples = []

static aliases = ['conn:delete']

static flags = {
format,
}
Expand Down
2 changes: 2 additions & 0 deletions src/commands/connection/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default class Get extends AppCommand {
format,
}

static aliases = ['conn:get']

async run(): Promise<any> {
const {flags, args} = await this.parse(Get)
const {alias} = args
Expand Down
4 changes: 2 additions & 2 deletions src/commands/connection/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {AppCommand} from '../../AppCommand.js'
export default class List extends AppCommand {
static args = {
search: Args.string({
description: 'Search for a connection by alias, desciption, and conection string',
description: 'Search for a connection by alias, description, or conection string',
required: false,
}),
}

static aliases = ['ls']
static aliases = ['connection:ls', 'conn:ls']

static description = 'List all saved connections'

Expand Down
4 changes: 2 additions & 2 deletions src/commands/history/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {AppCommand} from '../../AppCommand.js'

export default class Get extends AppCommand {
static args = {
id: Args.integer({description: 'ID of item in history list', required: true}),
id: Args.integer({description: 'ID of history entry', required: true}),
}

static description = 'Get query from history'
static description = 'Get a history entry'

static examples = []

Expand Down
2 changes: 1 addition & 1 deletion src/commands/history/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class List extends AppCommand {
search: Args.string({description: 'Part of a query to search for', required: false}),
}

static aliases = ['ls']
static aliases = ['history:ls']

static description = 'Search query history'

Expand Down
2 changes: 1 addition & 1 deletion src/commands/query/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class File extends AppCommand {
file: Args.file({description: 'Path to file containing SQL query', required: true, exists: true}),
}

static description = 'Query data from a database'
static description = 'Query data from a database by file'

static examples = []

Expand Down
4 changes: 3 additions & 1 deletion src/commands/query/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {AppCommand} from '../../AppCommand.js'

export default class SQL extends AppCommand {
static args = {
id: Args.integer({description: 'ID of history command to execute', required: true}),
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 = {
Expand Down
14 changes: 14 additions & 0 deletions src/commands/query/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default class Tool extends AppCommand {

static examples = []

static aliases = ['history:query']

static flags = {
format,
confirm: this.confirmFlag,
Expand Down Expand Up @@ -48,6 +50,18 @@ export default class Tool extends AppCommand {
await this.confirmQuery(alias, query, confirm)

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

await this.db.tool.update({
where: {
name,
},
data: {
count: {
increment: 1,
},
lastUsed: new Date(),
},
})
}
}
function buildQuery(query: string, params: string[] = []) {
Expand Down
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions src/commands/tool/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {Args, Command, Flags, ux} from '@oclif/core'
import {PrismaClient} from '@prisma/client'
import {format, printFormatted} from '../../output.js'
import {sqlqdb} from '../../database.js'
import {AppCommand} from '../../AppCommand.js'

export default class Get extends AppCommand {
static args = {
name: Args.string({description: 'Name of tool', required: true}),
}

static description = 'Get a tool'

static examples = []

static flags = {
format,
}

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

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

if (!result) {
ux.error(`Tool with name '${name}' not found`)
}

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

export default class List extends AppCommand {
static args = {
search: Args.string({description: 'Part of a query to search for', required: false}),
}

static aliases = ['tool:ls']

static description = 'Search tools'

static examples = []

static flags = {
format,
count: Flags.integer({description: 'Maximum number of results to return', required: false, default: 20}),
}

async run(): Promise<any> {
const {flags, args} = await this.parse(List)
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,
},
},
{
description: {
contains: search,
},
},
],
},
}),
)

printFormatted(format, result)
}
}
47 changes: 47 additions & 0 deletions src/commands/tool/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {Args, Command, Flags, ux} from '@oclif/core'
import {PrismaClient} from '@prisma/client'
import {format, printFormatted} from '../../output.js'
import {sqlqdb} from '../../database.js'
import {AppCommand} from '../../AppCommand.js'

export default class Create extends AppCommand {
static args = {
name: Args.string({description: 'Name for the tool', required: true}),
query: Args.string({
description:
'A query that the tool will run. This may contain parameters in the format of $Index that will be evaluated when the query runs',
required: false,
}),
description: Args.string({
description: 'Description for the tool',
required: false,
}),
}

static description = 'Update a tool'

static examples = []

static flags = {
format,
}

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

const result = await this.db.tool.update({
where: {
name,
},
data: {
query,
description,
updated: new Date(),
},
})

printFormatted(format, result)
}
}

0 comments on commit 218770c

Please sign in to comment.