feat: add model

This commit is contained in:
Siwoo Jeon 2023-11-29 21:08:43 +09:00
parent e6b73d8507
commit 20df7b3fda
Signed by: migan
GPG key ID: C4151385FFD2082A
4 changed files with 121 additions and 1 deletions

View file

@ -1,6 +1,6 @@
{
"name": "muffin-ai-arujak",
"version": "2.0.0-73-oreo",
"version": "2.0.0-74-oreo",
"main": "dist/index.js",
"private": true,
"dependencies": {

View file

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

View file

@ -0,0 +1,61 @@
import { type Pool } from 'mysql2/promise'
import run from '../run'
import type { BaseTable, LearnData } from '../type'
import { Snowflake } from 'discord.js'
export class LearnTable implements BaseTable<LearnData, string> {
public name = 'learn'
public constructor(private _database: Pool) {}
public async all(): Promise<LearnData[]> {
const [rows] = await this._database.execute<LearnData[]>(
'SELECT * FROM learn;',
)
return rows
}
public async findOne(key: string): Promise<LearnData[]> {
const [rows] = await this._database.execute<LearnData[]>(
'SELECT * FROM learn 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 learn (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 learn 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 learn WHERE command = ?;', [key])
})
}
}

View file

@ -0,0 +1,57 @@
import { type Pool } from 'mysql2/promise'
import run from '../run'
import type { BaseTable, NSFWData } from '../type'
export class NsfwContentTable implements BaseTable<NSFWData, number> {
public name = 'nsfw_content'
public constructor(private _database: Pool) {}
public async all(): Promise<NSFWData[]> {
const [rows] = await this._database.execute<NSFWData[]>(
'SELECT * FROM nsfw_content;',
)
return rows
}
public async findOne(key: number): Promise<NSFWData[]> {
const [rows] = await this._database.execute<NSFWData[]>(
'SELECT * FROM nsfw_content WHERE id = ?;',
[key],
)
return rows
}
public async insert(data: {
id: number
text: string
persona: string
}): Promise<void> {
const db = await this._database.getConnection()
await run(db, async () => {
await db.execute(
'INSERT INTO nsfw_content (id, text, persona) VALUES (?, ?, ?);',
[data.id, 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 nsfw_content 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 nsfw_content WHERE id = ?;', [key])
})
}
}