From da51077869bb7eb024573e7e32fb21c222c8cda3 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 13 Jan 2021 15:23:04 +0100 Subject: [PATCH] works --- tools/app/promise-learn/build/index.js | 37 +++++++++++++++++++------- tools/app/promise-learn/src/index.ts | 24 ++++++++++++----- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/tools/app/promise-learn/build/index.js b/tools/app/promise-learn/build/index.js index 2cbd515..78df05b 100644 --- a/tools/app/promise-learn/build/index.js +++ b/tools/app/promise-learn/build/index.js @@ -15,17 +15,21 @@ class DbHandle { database: 'heroes' }); } - get(stmt) { + async getHeroes() { 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')); + const result = (_b = await ((_a = this._conn) === null || _a === void 0 ? void 0 : _a.query("SELECT id, name FROM hero"))) !== null && _b !== void 0 ? _b : Promise.reject(new Error('Connection not ready 1')); + return result.map((row) => { + var _a; + return { id: row.id, name: (_a = row.name) !== null && _a !== void 0 ? _a : 'unknown' }; + }); } 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(); /* +const dbHandle = new DbHandle() dbHandle.connect() .then(() => dbHandle.get("SELECT * FROM hero")) .then((result) => { @@ -34,14 +38,27 @@ dbHandle.connect() }) .catch((err) => { console.log(err) - } - + }) */ +async function exec() { + const dbHandle = new DbHandle(); + try { + await dbHandle.connect(); + const heroes = await dbHandle.getHeroes(); + console.log(heroes); + } + finally { + await dbHandle.close(); + } +} +exec().catch((err) => console.log(err.message)); +/* dbHandle.get("SELECT * FROM hero") .then((result) => { - console.log(result); - return dbHandle.close(); -}) + console.log(result) + return dbHandle.close() + }) .catch((err) => { - console.log(err.message); -}); + console.log(err.message) + }) +*/ diff --git a/tools/app/promise-learn/src/index.ts b/tools/app/promise-learn/src/index.ts index c23d321..5dbc826 100644 --- a/tools/app/promise-learn/src/index.ts +++ b/tools/app/promise-learn/src/index.ts @@ -1,8 +1,14 @@ import mariadb from 'mariadb' +import { stringify } from 'querystring' let conn : mariadb.Connection +interface Hero { + id: number + name: string +} + class DbHandle { private _conn? : mariadb.Connection @@ -17,8 +23,11 @@ class DbHandle { }) } - public get(stmt : string) : Promise { - return this._conn?.query(stmt) ?? Promise.reject(new Error('Connection not ready 1')) + public async getHeroes() : Promise { + const result = await this._conn?.query("SELECT id, name FROM hero") ?? Promise.reject(new Error('Connection not ready 1')) + return result.map((row: Record) => { + return { id: row.id, name: row.name ?? 'unknown' } + }) } public close() : Promise { @@ -43,10 +52,13 @@ dbHandle.connect() 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() + try { + await dbHandle.connect() + const heroes : Hero[] = await dbHandle.getHeroes() + console.log(heroes) + } finally { + await dbHandle.close() + } } exec().catch((err) => console.log(err.message))