diff --git a/.gitignore b/.gitignore index 9aee3ea..d68fec6 100644 --- a/.gitignore +++ b/.gitignore @@ -138,4 +138,4 @@ config.json db/ .idea/ -database/ \ No newline at end of file +./database/ \ No newline at end of file diff --git a/package.json b/package.json index 441fcf3..1d5938e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "muffin-ai-arujak", - "version": "2.0.0-72-oreo", + "version": "2.0.0-73-oreo", "main": "dist/index.js", "private": true, "dependencies": { diff --git a/src/modules/ChatBot.ts b/src/modules/ChatBot.ts index ec94e31..788ba6b 100644 --- a/src/modules/ChatBot.ts +++ b/src/modules/ChatBot.ts @@ -1,5 +1,5 @@ import type { Client, Message } from 'discord.js' -import database, { LearnData, ResponseData } from './database' +import { database, LearnData, ResponseData } from './database' import { TextChannel } from 'discord.js' import config from '../../config.json' import { NODE_ENV } from '.' diff --git a/src/modules/database.ts b/src/modules/database.ts deleted file mode 100644 index 01f81e7..0000000 --- a/src/modules/database.ts +++ /dev/null @@ -1,122 +0,0 @@ -import { - type RowDataPacket, - createPool, - Pool, - PoolConnection, -} from 'mysql2/promise' -import config from '../../config.json' -import { type Snowflake } from 'discord.js' - -export interface BaseData extends RowDataPacket { - id: number - text: string - created_at: string - persona: string -} - -export interface ResponseData extends BaseData { - search_text: string - conversation: string - in_response_to: string | null - search_in_response_to: string -} - -export interface LearnData extends RowDataPacket { - command: string - result: string - user_id: Snowflake - created_at: string -} - -export { BaseData as NSFWData } - -interface BassTable { - name: string - all(): Promise - findOne(key: V): Promise - insert(data: T): Promise - update(data: T): Promise - delete(key: V): Promise -} - -async function run(db: PoolConnection, fn: () => Promise) { - try { - await db.beginTransaction() - await fn() - await db.commit() - } catch (err) { - console.error(err) - await db.rollback() - } finally { - db.release() - } -} - -class StatementTable implements BassTable { - public name = 'statement' - public constructor(private _database: Pool) {} - - public async all(): Promise { - const [rows] = await this._database.execute( - 'SELECT * FROM statement;', - ) - return rows - } - - public async findOne(key: number): Promise { - const [rows] = await this._database.execute( - 'SELECT * FROM statement WHERE id = ?;', - [key], - ) - return rows - } - - public async insert(data: ResponseData): Promise { - const db = await this._database.getConnection() - await run(db, async () => { - await db.execute( - 'INSERT INTO statement (id, text, persona, in_response_to) VALUES (?, ?, ?, ?);', - [data.id, data.text, data.persona, data.in_response_to], - ) - }) - } - - public async update(data: { id: number; text: string }): Promise { - const db = await this._database.getConnection() - - await run(db, async () => { - await db.execute('UPDATE statement SET text = ? WHERE id = ?;', [ - data.text, - data.id, - ]) - }) - } - - public async delete(key: number): Promise { - const db = await this._database.getConnection() - - await run(db, async () => { - await db.execute('DELETE FROM statement WHERE id = ?;', [key]) - }) - } -} - -export class MaaDatabase { - private _database = createPool({ - ...config.mysql, - keepAliveInitialDelay: 10000, - enableKeepAlive: true, - }) - - public get statement() { - return new StatementTable(this._database) - } -} - -const database = createPool({ - ...config.mysql, - keepAliveInitialDelay: 10000, - enableKeepAlive: true, -}) - -export default database diff --git a/src/modules/database/database.ts b/src/modules/database/database.ts new file mode 100644 index 0000000..fce72d8 --- /dev/null +++ b/src/modules/database/database.ts @@ -0,0 +1,23 @@ +import { createPool } from 'mysql2/promise' +import { StatementTable } from './model' +import config from '../../../config.json' + +export class MaaDatabase { + private _database = createPool({ + ...config.mysql, + keepAliveInitialDelay: 10000, + enableKeepAlive: true, + }) + + public get statement() { + return new StatementTable(this._database) + } +} + +const database = createPool({ + ...config.mysql, + keepAliveInitialDelay: 10000, + enableKeepAlive: true, +}) + +export { database } diff --git a/src/modules/database/index.ts b/src/modules/database/index.ts new file mode 100644 index 0000000..da781eb --- /dev/null +++ b/src/modules/database/index.ts @@ -0,0 +1,3 @@ +export * from './database' +export * from './type' +export * from './model' diff --git a/src/modules/database/model/index.ts b/src/modules/database/model/index.ts new file mode 100644 index 0000000..3439c7b --- /dev/null +++ b/src/modules/database/model/index.ts @@ -0,0 +1 @@ +export * from './statement' diff --git a/src/modules/database/model/statement.ts b/src/modules/database/model/statement.ts new file mode 100644 index 0000000..4bebaab --- /dev/null +++ b/src/modules/database/model/statement.ts @@ -0,0 +1,58 @@ +import { type Pool } from 'mysql2/promise' +import run from '../run' +import type { BaseTable, ResponseData } from '../type' + +export class StatementTable implements BaseTable { + public name = 'statement' + public constructor(private _database: Pool) {} + + public async all(): Promise { + const [rows] = await this._database.execute( + 'SELECT * FROM statement;', + ) + return rows + } + + public async findOne(key: number): Promise { + const [rows] = await this._database.execute( + 'SELECT * FROM statement WHERE id = ?;', + [key], + ) + return rows + } + + public async insert(data: { + id: number + text: string + persona: string + in_response_to: string + }): Promise { + const db = await this._database.getConnection() + + await run(db, async () => { + await db.execute( + 'INSERT INTO statement (id, text, persona, in_response_to) VALUES (?, ?, ?, ?);', + [data.id, data.text, data.persona, data.in_response_to], + ) + }) + } + + public async update(data: { id: number; text: string }): Promise { + const db = await this._database.getConnection() + + await run(db, async () => { + await db.execute('UPDATE statement SET text = ? WHERE id = ?;', [ + data.text, + data.id, + ]) + }) + } + + public async delete(key: number): Promise { + const db = await this._database.getConnection() + + await run(db, async () => { + await db.execute('DELETE FROM statement WHERE id = ?;', [key]) + }) + } +} diff --git a/src/modules/database/run.ts b/src/modules/database/run.ts new file mode 100644 index 0000000..d1be66f --- /dev/null +++ b/src/modules/database/run.ts @@ -0,0 +1,14 @@ +import { type PoolConnection } from 'mysql2/promise' + +export default async function run(db: PoolConnection, fn: () => Promise) { + try { + await db.beginTransaction() + await fn() + await db.commit() + } catch (err) { + console.error(err) + await db.rollback() + } finally { + db.release() + } +} diff --git a/src/modules/database/type.ts b/src/modules/database/type.ts new file mode 100644 index 0000000..f625c53 --- /dev/null +++ b/src/modules/database/type.ts @@ -0,0 +1,34 @@ +import type { RowDataPacket } from 'mysql2/promise' +import type { Snowflake } from 'discord.js' + +export interface BaseData extends RowDataPacket { + id: number + text: string + created_at: string + persona: string +} + +export interface ResponseData extends BaseData { + search_text: string + conversation: string + in_response_to: string | null + search_in_response_to: string +} + +export interface LearnData extends RowDataPacket { + command: string + result: string + user_id: Snowflake + created_at: string +} + +export { BaseData as NSFWData } + +export interface BaseTable { + name: string + all(): Promise + findOne(key: V): Promise + insert(data: any): Promise + update(data: any): Promise + delete(key: V): Promise +} diff --git a/src/modules/index.ts b/src/modules/index.ts index 2a595fe..5319a2b 100644 --- a/src/modules/index.ts +++ b/src/modules/index.ts @@ -1,6 +1,7 @@ import ChatBot from './ChatBot' import Command from './Command' -import database, { +import { + database, ResponseData, NSFWData, LearnData,