This commit is contained in:
2021-01-13 13:02:27 +01:00
parent 13c72409c5
commit 845f3fd400
3 changed files with 100 additions and 48 deletions

View File

@ -1,27 +1,47 @@
"use strict";
const mariadb = require('mariadb');
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mariadb_1 = __importDefault(require("mariadb"));
let conn;
mariadb.createConnection({
host: 'database',
user: 'heroes',
password: 'test123',
database: 'heroes'
})
.then(conn => {
conn.query("SELECT * FROM hero")
.then((rows) => {
console.log(rows);
return rows;
class DbHandle {
constructor() { }
async connect() {
this._conn = await mariadb_1.default.createConnection({
host: '172.16.10.18',
user: 'heroes',
password: 'test123',
database: 'heroes'
});
}
get(stmt) {
var _a, _b;
return (_b = (_a = this._conn) === null || _a === void 0 ? void 0 : _a.query(stmt)) !== null && _b !== void 0 ? _b : Promise.reject(new Error('Connection not ready 1'));
}
close() {
var _a, _b;
return (_b = (_a = this._conn) === null || _a === void 0 ? void 0 : _a.end()) !== null && _b !== void 0 ? _b : Promise.reject(new Error('Connection not ready 2'));
}
}
const dbHandle = new DbHandle();
/*
dbHandle.connect()
.then(() => dbHandle.get("SELECT * FROM hero"))
.then((result) => {
console.log(result)
return dbHandle.close()
})
.then((res) => {
console.log(res);
conn.end();
})
.catch(err => {
console.log(err);
conn.end();
});
.catch((err) => {
console.log(err)
}
*/
dbHandle.get("SELECT * FROM hero")
.then((result) => {
console.log(result);
return dbHandle.close();
})
.catch(err => {
console.log('not connected');
.catch((err) => {
console.log(err.message);
});