This commit is contained in:
2021-01-13 15:23:04 +01:00
parent 845f3fd400
commit da51077869
2 changed files with 45 additions and 16 deletions

View File

@ -15,17 +15,21 @@ class DbHandle {
database: 'heroes' database: 'heroes'
}); });
} }
get(stmt) { async getHeroes() {
var _a, _b; 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() { close() {
var _a, _b; 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')); 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() dbHandle.connect()
.then(() => dbHandle.get("SELECT * FROM hero")) .then(() => dbHandle.get("SELECT * FROM hero"))
.then((result) => { .then((result) => {
@ -34,14 +38,27 @@ dbHandle.connect()
}) })
.catch((err) => { .catch((err) => {
console.log(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") dbHandle.get("SELECT * FROM hero")
.then((result) => { .then((result) => {
console.log(result); console.log(result)
return dbHandle.close(); return dbHandle.close()
}) })
.catch((err) => { .catch((err) => {
console.log(err.message); console.log(err.message)
}); })
*/

View File

@ -1,8 +1,14 @@
import mariadb from 'mariadb' import mariadb from 'mariadb'
import { stringify } from 'querystring'
let conn : mariadb.Connection let conn : mariadb.Connection
interface Hero {
id: number
name: string
}
class DbHandle { class DbHandle {
private _conn? : mariadb.Connection private _conn? : mariadb.Connection
@ -17,8 +23,11 @@ class DbHandle {
}) })
} }
public get(stmt : string) : Promise<any> { public async getHeroes() : Promise<Hero[]> {
return this._conn?.query(stmt) ?? Promise.reject(new Error('Connection not ready 1')) const result = await this._conn?.query("SELECT id, name FROM hero") ?? Promise.reject(new Error('Connection not ready 1'))
return result.map((row: Record<string, unknown>) => {
return { id: row.id, name: row.name ?? 'unknown' }
})
} }
public close() : Promise<void> { public close() : Promise<void> {
@ -43,10 +52,13 @@ dbHandle.connect()
async function exec() : Promise<void> { async function exec() : Promise<void> {
const dbHandle = new DbHandle() const dbHandle = new DbHandle()
await dbHandle.connect() try {
const result = await dbHandle.get("SELECT * FROM hero") await dbHandle.connect()
console.log(result) const heroes : Hero[] = await dbHandle.getHeroes()
await dbHandle.close() console.log(heroes)
} finally {
await dbHandle.close()
}
} }
exec().catch((err) => console.log(err.message)) exec().catch((err) => console.log(err.message))