Adding interfacing for sqlite

This commit is contained in:
Bradley Bickford 2025-06-22 22:08:01 -04:00
parent b307630642
commit d9561d9179
7 changed files with 1755 additions and 14 deletions

1672
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,8 @@
"description": "",
"dependencies": {
"discord.js": "^14.20.0",
"dotenv": "^16.5.0"
"dotenv": "^16.5.0",
"sqlite3": "^5.1.7"
},
"devDependencies": {
"tsup": "^8.5.0",

View File

@ -11,13 +11,17 @@ const client : Client = new Client({
]
})
if (config.DB_MODE == "sqlite") {
const db = new utilities.sqlite.SqliteDB("breadbot_test.db")
}
client.once(Events.ClientReady, () => {
// TODO Winston should handle this
console.log("Breadbot is ready")
})
client.on(Events.GuildCreate, async (guild : Guild) => {
await utilities.command_utils.deployCommands(guild.id)
await utilities.command.deployCommands(guild.id)
})
client.on(Events.InteractionCreate, async (interaction: Interaction) => {

View File

@ -2,13 +2,14 @@ import dotenv from "dotenv"
dotenv.config()
const { DISCORD_TOKEN, DISCORD_CLIENT_ID } = process.env
const { DISCORD_TOKEN, DISCORD_CLIENT_ID, DB_MODE } = process.env
if (!DISCORD_TOKEN || !DISCORD_CLIENT_ID) {
if (!DISCORD_TOKEN || !DISCORD_CLIENT_ID || !DB_MODE) {
throw new Error("Missing environment variables")
}
export const config = {
DISCORD_TOKEN,
DISCORD_CLIENT_ID
DISCORD_CLIENT_ID,
DB_MODE
}

View File

@ -1,5 +1,7 @@
import * as command_utils from "./discord/command_utils"
import * as command from "./discord/command_utils"
import * as sqlite from "./storage/sqlite"
export const utilities = {
command_utils
command,
sqlite
}

View File

@ -0,0 +1,7 @@
export interface sql_common {
run(query: string) : Promise<number>
runParameterized(query: string, parameters: any[]): Promise<number>
getAll(query: string) : Promise<Object[]>
getAllParameterized(query: string, parameters: any[]) : Promise<Object[]>
}

View File

@ -0,0 +1,68 @@
import * as sqlite3 from 'sqlite3'
import { sql_common } from "./interfaces"
export class SqliteDB implements sql_common {
private db : sqlite3.Database;
public constructor(private readonly dbName: string) {
this.db = new sqlite3.Database(this.dbName);
}
async run(query: string): Promise<number> {
return new Promise(() => {
this.db.run(query, (result : sqlite3.RunResult, err: Error) => {
if (err) {
// TODO Winston should handle this
console.log(err)
throw err
} else {
return result.changes
}
})
})
}
async runParameterized(query: string, parameters: any[]): Promise<number> {
return new Promise(() => {
this.db.run(query, parameters, (result : sqlite3.RunResult, err: Error) => {
if (err) {
// TODO Winston should handle this
console.log(err)
throw err
} else {
return result.changes
}
})
})
}
async getAll(query: string): Promise<Object[]> {
return new Promise(() => {
this.db.all(query, (err: Error, rows: Object[]) => {
if (err) {
// TODO Winston should handle this
console.log(err)
throw err
} else {
return rows
}
})
})
}
getAllParameterized(query: string, parameters: any[]): Promise<Object[]> {
return new Promise(() => {
this.db.all(query, parameters, (err: Error, rows: Object[]) => {
if (err) {
// TODO Winston should handle this
console.log(err)
throw err
} else {
return rows
}
})
})
}
}