@ncodefactory/pg-db-provider
v0.1.3
Published
simple db-provider for pg
Downloads
1
Readme
pg-db-provider
connect/disconnect
const dbConnProps = {
dbHost: "127.0.0.1",
dbPort: 5432,
dbName: "db_name",
dbUser: "db_user",
dbPasswd: "db_password",
};
const dbProvider = new DBProvider({...dbConnProps});
await dbProvider.connect();
console.log(dbProvider.isConnected); // true
await dbProvider.disconnect();
console.log(dbProvider.isConnected); // false
execute query
const dbConnProps = {
dbHost: "127.0.0.1",
dbPort: 5432,
dbName: "db_name",
dbUser: "db_user",
dbPasswd: "db_password",
};
const dbProvider = new DBProvider({...dbConnProps});
await dbProvider.connect();
const result = await dbProvider.executeQuery("select 1 num");
console.log(result?.rows[0].num); // 1
await dbProvider.disconnect();
use client
const dbConnProps = {
dbHost: "127.0.0.1",
dbPort: 5432,
dbName: "db_name",
dbUser: "db_user",
dbPasswd: "db_password",
};
const dbProvider = new DBProvider({...dbConnProps});
await dbProvider.connect();
const result = await dbProvider.useClient(async (client) => {
return await client.query("select 1 num");
});
console.log(result?.rows[0].num); // 1
await dbProvider.disconnect();
open cursor
const dbConnProps = {
dbHost: "127.0.0.1",
dbPort: 5432,
dbName: "db_name",
dbUser: "db_user",
dbPasswd: "db_password",
};
const dbProvider = new DBProvider({...dbConnProps});
await dbProvider.connect();
const cursor = await dbProvider.openCursor("select 1 num");
const rows = await cursor?.read(1);
console.log(rows[0].num); // 1
await cursor?.close();
await dbProvider.disconnect();