await
This commit is contained in:
@ -1,27 +1,47 @@
|
|||||||
"use strict";
|
"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;
|
let conn;
|
||||||
mariadb.createConnection({
|
class DbHandle {
|
||||||
host: 'database',
|
constructor() { }
|
||||||
user: 'heroes',
|
async connect() {
|
||||||
password: 'test123',
|
this._conn = await mariadb_1.default.createConnection({
|
||||||
database: 'heroes'
|
host: '172.16.10.18',
|
||||||
})
|
user: 'heroes',
|
||||||
.then(conn => {
|
password: 'test123',
|
||||||
conn.query("SELECT * FROM hero")
|
database: 'heroes'
|
||||||
.then((rows) => {
|
});
|
||||||
console.log(rows);
|
}
|
||||||
return rows;
|
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) => {
|
.catch((err) => {
|
||||||
console.log(res);
|
console.log(err)
|
||||||
conn.end();
|
}
|
||||||
})
|
|
||||||
.catch(err => {
|
*/
|
||||||
console.log(err);
|
dbHandle.get("SELECT * FROM hero")
|
||||||
conn.end();
|
.then((result) => {
|
||||||
});
|
console.log(result);
|
||||||
|
return dbHandle.close();
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch((err) => {
|
||||||
console.log('not connected');
|
console.log(err.message);
|
||||||
});
|
});
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "rm -rf ./build && tsc",
|
"build": "tsc",
|
||||||
"start": "npm run build && node build/index.js",
|
"start": "npm run build && node build/index.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
|
@ -1,31 +1,63 @@
|
|||||||
const mariadb = require('mariadb')
|
import mariadb from 'mariadb'
|
||||||
|
|
||||||
let conn : mariadb.Connection
|
let conn : mariadb.Connection
|
||||||
|
|
||||||
mariadb.createConnection({
|
|
||||||
host: 'database',
|
class DbHandle {
|
||||||
user: 'heroes',
|
private _conn? : mariadb.Connection
|
||||||
password: 'test123',
|
|
||||||
database: 'heroes'
|
public constructor() {}
|
||||||
})
|
|
||||||
.then(conn => {
|
public async connect() : Promise<void> {
|
||||||
conn.query("SELECT * FROM hero")
|
this._conn = await mariadb.createConnection({
|
||||||
.then((rows) => {
|
host: '172.16.10.18',
|
||||||
console.log(rows)
|
user: 'heroes',
|
||||||
return rows
|
password: 'test123',
|
||||||
})
|
database: 'heroes'
|
||||||
.then((res) => {
|
})
|
||||||
console.log(res)
|
}
|
||||||
conn.end()
|
|
||||||
})
|
public get(stmt : string) : Promise<any> {
|
||||||
.catch(err => {
|
return this._conn?.query(stmt) ?? Promise.reject(new Error('Connection not ready 1'))
|
||||||
console.log(err)
|
}
|
||||||
conn.end()
|
|
||||||
})
|
public close() : Promise<void> {
|
||||||
})
|
return this._conn?.end() ?? Promise.reject(new Error('Connection not ready 2'))
|
||||||
.catch(err => {
|
}
|
||||||
console.log('not connected')
|
}
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
const dbHandle = new DbHandle()
|
||||||
|
dbHandle.connect()
|
||||||
|
.then(() => dbHandle.get("SELECT * FROM hero"))
|
||||||
|
.then((result) => {
|
||||||
|
console.log(result)
|
||||||
|
return dbHandle.close()
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err)
|
||||||
|
})
|
||||||
|
*/
|
||||||
|
|
||||||
|
async function exec() : Promise<void> {
|
||||||
|
const dbHandle = new DbHandle()
|
||||||
|
await dbHandle.connect()
|
||||||
|
const result = await dbHandle.get("SELECT * FROM hero")
|
||||||
|
console.log(result)
|
||||||
|
await dbHandle.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
exec().catch((err) => console.log(err.message))
|
||||||
|
|
||||||
|
/*
|
||||||
|
dbHandle.get("SELECT * FROM hero")
|
||||||
|
.then((result) => {
|
||||||
|
console.log(result)
|
||||||
|
return dbHandle.close()
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err.message)
|
||||||
|
})
|
||||||
|
*/
|
Reference in New Issue
Block a user