From 845f3fd4009f6f6d8c4ca39c4d7e408c7dc4feb1 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 13 Jan 2021 13:02:27 +0100 Subject: [PATCH] await --- tools/app/promise-learn/build/index.js | 64 +++++++++++++------- tools/app/promise-learn/package.json | 2 +- tools/app/promise-learn/src/index.ts | 82 ++++++++++++++++++-------- 3 files changed, 100 insertions(+), 48 deletions(-) diff --git a/tools/app/promise-learn/build/index.js b/tools/app/promise-learn/build/index.js index f754be1..2cbd515 100644 --- a/tools/app/promise-learn/build/index.js +++ b/tools/app/promise-learn/build/index.js @@ -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); }); diff --git a/tools/app/promise-learn/package.json b/tools/app/promise-learn/package.json index e657bfe..654139d 100644 --- a/tools/app/promise-learn/package.json +++ b/tools/app/promise-learn/package.json @@ -4,7 +4,7 @@ "description": "", "main": "index.js", "scripts": { - "build": "rm -rf ./build && tsc", + "build": "tsc", "start": "npm run build && node build/index.js", "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/tools/app/promise-learn/src/index.ts b/tools/app/promise-learn/src/index.ts index aa08fd9..c23d321 100644 --- a/tools/app/promise-learn/src/index.ts +++ b/tools/app/promise-learn/src/index.ts @@ -1,31 +1,63 @@ -const mariadb = require('mariadb') +import mariadb from 'mariadb' let conn : mariadb.Connection -mariadb.createConnection({ - host: 'database', - user: 'heroes', - password: 'test123', - database: 'heroes' - }) - .then(conn => { - conn.query("SELECT * FROM hero") - .then((rows) => { - console.log(rows) - return rows - }) - .then((res) => { - console.log(res) - conn.end() - }) - .catch(err => { - console.log(err) - conn.end() - }) - }) - .catch(err => { - console.log('not connected') - }) + +class DbHandle { + private _conn? : mariadb.Connection + + public constructor() {} + + public async connect() : Promise { + this._conn = await mariadb.createConnection({ + host: '172.16.10.18', + user: 'heroes', + password: 'test123', + database: 'heroes' + }) + } + + public get(stmt : string) : Promise { + return this._conn?.query(stmt) ?? Promise.reject(new Error('Connection not ready 1')) + } + + public close() : Promise { + return this._conn?.end() ?? 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() + }) + .catch((err) => { + console.log(err) + }) +*/ + +async function exec() : Promise { + 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) + }) +*/ \ No newline at end of file