All files / src/engines mysql-engine.ts

100% Statements 9/9
100% Branches 0/0
100% Functions 8/8
100% Lines 8/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30      1x   1x       7x       8x       13x       2x 2x         21x    
import { DatabaseEngine } from "../database-engine";
import * as mysql from "mysql2/promise";
import { DatabaseType, KeyedDatabaseResult } from "../base-model";
import SQLGrammarCompiler from "../query/compiler";
 
export default class MySQLEngine implements DatabaseEngine {
    private connection: mysql.Connection;
 
    constructor(connection: mysql.Connection) {
        this.connection = connection;
    }
 
    query(sql: string, params: DatabaseType[]): Promise<void> {
        return this.connection.execute(sql, params).then(x => {});
    }
 
    get(sql: string, params: DatabaseType[]): Promise<KeyedDatabaseResult[]> {
        return this.connection.execute<mysql.RowDataPacket[]>(sql, params).then(x => x[0]);
    }
 
    insertAndGetId(table: string, sql: string, params: DatabaseType[]): Promise<number> {
        return this.connection.query<mysql.OkPacket>(sql, params).then(results => {
            return results[0].insertId;
        });
    }
 
    getGrammarCompiler(): SQLGrammarCompiler {
        return new SQLGrammarCompiler();
    }
}