ps-conecta-firebird
Version:
58 lines (55 loc) • 1.75 kB
text/typescript
import {
Database,
ISOLATION_READ_COMMITTED,
Options,
attach,
} from "node-firebird";
import { Config } from "./index";
export class FirebirdConnection {
private dataConnection: Options;
constructor(private readonly options: Config) {
const connection: Options = {
host: this.options.host,
port: this.options.port,
database: this.options.database,
user: this.options.user,
password: this.options.password,
lowercase_keys: this.options.lowercase_keys,
role: this.options.role,
pageSize: this.options.pageSize,
retryConnectionInterval: this.options.retryConnectionInterval,
};
this.dataConnection = connection;
}
async queryExecute<T>(query: string, params = []): Promise<Array<T>> {
return new Promise((resolve, reject) => {
attach(this.dataConnection, function (err: any, db: Database) {
if (err) {
console.log("Um erro ocorreu na conexão do banco!", err);
reject(err);
} else {
db.transaction(ISOLATION_READ_COMMITTED, (err, transaction) => {
if (err) {
console.log("Um erro ocorreu na transaction!", err);
transaction.rollback();
db.detach();
reject(err);
}
transaction.query(query, params, (err: any, result: any[]) => {
if (err) {
console.log("Um erro ocorreu na query!", err);
transaction.rollback();
db.detach();
reject(err);
} else {
transaction.commit();
db.detach();
resolve(result);
}
});
});
}
});
});
}
}