remove: Previous database system files

This commit is contained in:
Siwoo Jeon 2024-09-22 19:01:42 +09:00
parent 7a4a724e14
commit 63864b073f
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
10 changed files with 1 additions and 338 deletions

View file

@ -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`)
);

View file

@ -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()
})
}
}

View file

@ -1,3 +0,0 @@
export * from './database'
export * from './type'
export * from './model'

View file

@ -1,3 +0,0 @@
export * from './statement'
export * from './learn'
export * from './nsfwContent'

View file

@ -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<LearnData, string> {
public readonly name = 'learn'
public constructor(private _database: Pool) {}
public async all(): Promise<LearnData[]> {
const [rows] = await this._database.execute<LearnData[]>(
`SELECT * FROM ${this.name};`,
)
return rows
}
public async findOne(key: string): Promise<LearnData[]> {
const [rows] = await this._database.execute<LearnData[]>(
`SELECT * FROM ${this.name} WHERE command = ?;`,
[key],
)
return rows
}
public async insert(data: {
command: string
result: string
user_id: Snowflake
}): Promise<void> {
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<void> {
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<void> {
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<LearnData[]> {
const [rows] = await this._database.execute<LearnData[]>(
`SELECT * FROM ${this.name} WHERE ${key} = ?;`,
[data],
)
return rows
}
}

View file

@ -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<NSFWData, number> {
public readonly name = 'nsfw_content'
public constructor(private _database: Pool) {}
public async all(): Promise<NSFWData[]> {
const [rows] = await this._database.execute<NSFWData[]>(
`SELECT * FROM ${this.name};`,
)
return rows
}
public async findOne(key: number): Promise<NSFWData[]> {
const [rows] = await this._database.execute<NSFWData[]>(
`SELECT * FROM ${this.name} WHERE id = ?;`,
[key],
)
return rows
}
public async insert(data: { text: string; persona: string }): Promise<void> {
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<void> {
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<void> {
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<NSFWData[]> {
const [rows] = await this._database.execute<NSFWData[]>(
`SELECT *
FROM ${this.name}
WHERE ${key} = ?;`,
[data],
)
return rows
}
}

View file

@ -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<ResponseData, number> {
public readonly name = 'statement'
public constructor(private _database: Pool) {}
public async all(): Promise<ResponseData[]> {
const [rows] = await this._database.execute<ResponseData[]>(
`SELECT * FROM ${this.name};`,
)
return rows
}
public async findOne(key: number): Promise<ResponseData[]> {
const [rows] = await this._database.execute<ResponseData[]>(
`SELECT * FROM ${this.name} WHERE id = ?;`,
[key],
)
return rows
}
public async insert(data: {
text: string
persona: string
in_response_to: string
}): Promise<void> {
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<void> {
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<void> {
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<ResponseData[]> {
const [rows] = await this._database.execute<ResponseData[]>(
`SELECT * FROM ${this.name} WHERE ${key} = ?;`,
[data],
)
return rows
}
}

View file

@ -1,14 +0,0 @@
import { type PoolConnection } from 'mysql2/promise'
export default async function run(db: PoolConnection, fn: () => Promise<any>) {
try {
await db.beginTransaction()
await fn()
await db.commit()
} catch (err) {
console.error(err)
await db.rollback()
} finally {
db.release()
}
}

View file

@ -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<T, V> {
name: string
all(): Promise<T[]>
findOne(key: V): Promise<T[]>
insert(data: any): Promise<void>
update(data: any): Promise<void>
delete(key: V): Promise<void>
findOneAnotherKey(key: string, data: any): Promise<T[]>
}

View file

@ -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 }