DB
This commit is contained in:
@ -12,31 +12,39 @@ export class DatabasePlugin extends Plugin {
|
||||
super(name);
|
||||
}
|
||||
|
||||
static init(
|
||||
static async init(
|
||||
name: string,
|
||||
config: {
|
||||
host: string;
|
||||
port: number;
|
||||
user: string;
|
||||
password: string;
|
||||
database: string;
|
||||
}
|
||||
): DatabasePlugin {
|
||||
const dbConnection = mysql.createConnection({
|
||||
): Promise<DatabasePlugin> {
|
||||
const dbConnection = await mysql.createConnection({
|
||||
host: config.host,
|
||||
user: config.user,
|
||||
port: config.port,
|
||||
password: config.password,
|
||||
database: config.database,
|
||||
enableKeepAlive: true,
|
||||
});
|
||||
|
||||
return new DatabasePlugin(name, dbConnection);
|
||||
}
|
||||
|
||||
async run(query): Promise<{
|
||||
rows: any[];
|
||||
fields: any[];
|
||||
}> {
|
||||
const [rows, fields] = await this.dbConnection.execute(query);
|
||||
return { rows, fields };
|
||||
async run(query): Promise<any> {
|
||||
try {
|
||||
const [rows, fields] = await this.dbConnection.execute(query);
|
||||
|
||||
return JSON.stringify({
|
||||
rows: rows,
|
||||
fields: fields ?? [],
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
}
|
||||
}
|
||||
|
||||
onFinish() {
|
||||
|
||||
Reference in New Issue
Block a user