From 63864b073fdd2b204302d29f4e7b85a90023b5ab Mon Sep 17 00:00:00 2001 From: Siwoo Jeon Date: Sun, 22 Sep 2024 19:01:42 +0900 Subject: [PATCH] remove: Previous database system files --- scripts/create_table.sql | 31 --------- src/modules/database/database.ts | 27 -------- src/modules/database/index.ts | 3 - src/modules/database/model/index.ts | 3 - src/modules/database/model/learn.ts | 72 --------------------- src/modules/database/model/nsfwContent.ts | 66 -------------------- src/modules/database/model/statement.ts | 76 ----------------------- src/modules/database/run.ts | 14 ----- src/modules/database/type.ts | 36 ----------- src/modules/index.ts | 11 +--- 10 files changed, 1 insertion(+), 338 deletions(-) delete mode 100755 scripts/create_table.sql delete mode 100644 src/modules/database/database.ts delete mode 100644 src/modules/database/index.ts delete mode 100644 src/modules/database/model/index.ts delete mode 100644 src/modules/database/model/learn.ts delete mode 100644 src/modules/database/model/nsfwContent.ts delete mode 100644 src/modules/database/model/statement.ts delete mode 100644 src/modules/database/run.ts delete mode 100644 src/modules/database/type.ts diff --git a/scripts/create_table.sql b/scripts/create_table.sql deleted file mode 100755 index 58c9447..0000000 --- a/scripts/create_table.sql +++ /dev/null @@ -1,31 +0,0 @@ -CREATE TABLE - `statement` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `text` varchar(255) DEFAULT NULL, - `search_text` varchar(255) NOT NULL DEFAULT '', - `conversation` varchar(32) NOT NULL DEFAULT '', - `created_at` datetime DEFAULT current_timestamp(), - `in_response_to` varchar(255) DEFAULT NULL, - `search_in_response_to` varchar(255) NOT NULL DEFAULT '', - `persona` varchar(50) NOT NULL DEFAULT '', - PRIMARY KEY (`id`) - ); - -CREATE TABLE - `nsfw_content` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `text` varchar(255) NOT NULL DEFAULT '', - `created_at` datetime DEFAULT current_timestamp(), - `persona` varchar(50) NOT NULL DEFAULT '', - PRIMARY KEY (`id`) - ); - -CREATE TABLE - `learn` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `command` varchar(255) NOT NULL, - `result` varchar(255) NOT NULL, - `user_id` varchar(255) NOT NULL, - `created_at` datetime NOT NULL DEFAULT current_timestamp(), - primary key(`id`) - ); \ No newline at end of file diff --git a/src/modules/database/database.ts b/src/modules/database/database.ts deleted file mode 100644 index 0bf4f7d..0000000 --- a/src/modules/database/database.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { LearnTable, NSFWContentTable, StatementTable } from './model' -import { container } from '@sapphire/framework' -import { createPool } from 'mysql2/promise' - -export class MaaDatabase { - public readonly database = createPool({ - ...container.config.mysql, - keepAliveInitialDelay: 10000, - enableKeepAlive: true, - }) - .on('release', conn => { - container.logger.debug(`[MaaDatabase] ${conn.threadId} Released.`) - }) - .on('connection', conn => { - container.logger.debug(`[MaaDatabase] ${conn.threadId} Connected.`) - }) - public statement = new StatementTable(this.database) - public nsfwContent = new NSFWContentTable(this.database) - public learn = new LearnTable(this.database) - - public ping() { - this.database.getConnection().then(conn => { - conn.ping() - conn.release() - }) - } -} diff --git a/src/modules/database/index.ts b/src/modules/database/index.ts deleted file mode 100644 index da781eb..0000000 --- a/src/modules/database/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -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 deleted file mode 100644 index 9e10d1f..0000000 --- a/src/modules/database/model/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './statement' -export * from './learn' -export * from './nsfwContent' diff --git a/src/modules/database/model/learn.ts b/src/modules/database/model/learn.ts deleted file mode 100644 index 10ca1f6..0000000 --- a/src/modules/database/model/learn.ts +++ /dev/null @@ -1,72 +0,0 @@ -import type { BaseTable, LearnData } from '../type' -import { type Pool } from 'mysql2/promise' -import { Snowflake } from 'discord.js' -import run from '../run' - -export class LearnTable implements BaseTable { - public readonly name = 'learn' - public constructor(private _database: Pool) {} - - public async all(): Promise { - const [rows] = await this._database.execute( - `SELECT * FROM ${this.name};`, - ) - return rows - } - - public async findOne(key: string): Promise { - const [rows] = await this._database.execute( - `SELECT * FROM ${this.name} WHERE command = ?;`, - [key], - ) - return rows - } - - public async insert(data: { - command: string - result: string - user_id: Snowflake - }): Promise { - const db = await this._database.getConnection() - - await run(db, async () => { - await db.execute( - `INSERT INTO ${this.name} (command, result, user_id) VALUES (?, ?, ?);`, - [data.command, data.result, data.user_id], - ) - }) - } - - public async update(data: { - command: string - result: string - }): Promise { - const db = await this._database.getConnection() - - await run(db, async () => { - await db.execute( - `UPDATE ${this.name} SET result = ? WHERE command = ?;`, - [data.command, data.result], - ) - }) - } - - public async delete(key: string): Promise { - const db = await this._database.getConnection() - - await run(db, async () => { - await db.execute(`DELETE FROM ${this.name} WHERE id = ?;`, [key]) - }) - } - - public async findOneAnotherKey( - key: 'id' | 'command' | 'result' | 'user_id' | 'created_at', - data: any, - ): Promise { - const [rows] = await this._database.execute( - `SELECT * FROM ${this.name} WHERE ${key} = ?;`, - [data], - ) - return rows - } -} diff --git a/src/modules/database/model/nsfwContent.ts b/src/modules/database/model/nsfwContent.ts deleted file mode 100644 index 4ffdaf5..0000000 --- a/src/modules/database/model/nsfwContent.ts +++ /dev/null @@ -1,66 +0,0 @@ -import type { BaseTable, NSFWData } from '../type' -import { type Pool } from 'mysql2/promise' -import run from '../run' - -export class NSFWContentTable implements BaseTable { - public readonly name = 'nsfw_content' - public constructor(private _database: Pool) {} - - public async all(): Promise { - const [rows] = await this._database.execute( - `SELECT * FROM ${this.name};`, - ) - return rows - } - - public async findOne(key: number): Promise { - const [rows] = await this._database.execute( - `SELECT * FROM ${this.name} WHERE id = ?;`, - [key], - ) - return rows - } - - public async insert(data: { text: string; persona: string }): Promise { - const db = await this._database.getConnection() - - await run(db, async () => { - await db.execute( - `INSERT INTO ${this.name} (text, persona) VALUES (?, ?);`, - [data.text, data.persona], - ) - }) - } - - public async update(data: { id: number; text: string }): Promise { - const db = await this._database.getConnection() - - await run(db, async () => { - await db.execute(`UPDATE ${this.name} 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 ${this} WHERE id = ?;`, [key]) - }) - } - - public async findOneAnotherKey( - key: 'id' | 'text' | 'persona' | 'created_at', - data: any, - ): Promise { - const [rows] = await this._database.execute( - `SELECT * - FROM ${this.name} - WHERE ${key} = ?;`, - [data], - ) - return rows - } -} diff --git a/src/modules/database/model/statement.ts b/src/modules/database/model/statement.ts deleted file mode 100644 index 0847204..0000000 --- a/src/modules/database/model/statement.ts +++ /dev/null @@ -1,76 +0,0 @@ -import type { BaseTable, ResponseData } from '../type' -import { type Pool } from 'mysql2/promise' -import run from '../run' - -export class StatementTable implements BaseTable { - public readonly name = 'statement' - public constructor(private _database: Pool) {} - - public async all(): Promise { - const [rows] = await this._database.execute( - `SELECT * FROM ${this.name};`, - ) - return rows - } - - public async findOne(key: number): Promise { - const [rows] = await this._database.execute( - `SELECT * FROM ${this.name} WHERE id = ?;`, - [key], - ) - return rows - } - - public async insert(data: { - text: string - persona: string - in_response_to: string - }): Promise { - const db = await this._database.getConnection() - - await run(db, async () => { - await db.execute( - `INSERT INTO ${this.name} (text, persona, in_response_to) VALUES (?, ?, ?);`, - [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 ${this.name} 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 ${this.name} WHERE id = ?;`, [key]) - }) - } - - public async findOneAnotherKey( - key: - | 'id' - | 'text' - | 'persona' - | 'created_at' - | 'search_text' - | 'conversation' - | 'in_response_to' - | 'search_in_response_to', - data: any, - ): Promise { - const [rows] = await this._database.execute( - `SELECT * FROM ${this.name} WHERE ${key} = ?;`, - [data], - ) - return rows - } -} diff --git a/src/modules/database/run.ts b/src/modules/database/run.ts deleted file mode 100644 index 46362e1..0000000 --- a/src/modules/database/run.ts +++ /dev/null @@ -1,14 +0,0 @@ -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 deleted file mode 100644 index 2f42527..0000000 --- a/src/modules/database/type.ts +++ /dev/null @@ -1,36 +0,0 @@ -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 { - id: number - 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 - findOneAnotherKey(key: string, data: any): Promise -} diff --git a/src/modules/index.ts b/src/modules/index.ts index a948b85..42df732 100644 --- a/src/modules/index.ts +++ b/src/modules/index.ts @@ -1,14 +1,5 @@ -import { ResponseData, NSFWData, LearnData, MaaDatabase } from './database' import { NODE_ENV } from './env' import ChatBot from './ChatBot' import noPerm from './noPerm' -export { - ResponseData, - MaaDatabase, - LearnData, - NODE_ENV, - ChatBot, - NSFWData, - noPerm, -} +export { NODE_ENV, ChatBot, noPerm }