For all the examples below

import {PgDb, PgSchema} from "pogi";

let pgdb:PgDb     = PgDb.connect(..);
let schema:PgSchema = pgdb.schemas['test1'];  

Properties

db

db:PgDb Back reference to the db instance

tables

tables:{[name:string]:PgTable} Tables, also merged to schema object.

fn

fn:{[name:string]:Function} Stored procedures and functions

Functions

toString

toString()

returns the name of the schema


setLogger

setLogger(logger:PgDbLogger)

Note: inherited.

Sets the fallback logger for all queries (if no schema, table or query level logger is set, this will be used).

pgdb.setLogger(console);

Functions - async

query

query(sql:string, params?:any[]|{}, options?:SqlQueryOptions):Promise<any[]>

Note: inherited, uses schema level log if present (if not then the db level log).

Executes an arbitrary sql string with parameters / named parameters;

let res1 = await schema.query('SELECT MAX(point) from game1.scores WHERE name=$1 ', ['player1']);
let res2 = await schema.query('SELECT MAX(point) from !:schema.scores WHERE name=:name ', {schema:'game1', name:'player1'});

queryOneField

queryOneField(sql:string, params?:any[]|{}, options?:SqlQueryOptions):Promise<any>

Note: inherited, uses schema level log if present (if not then the db level log).

If there is only one record and one field that we are interested in. For the params usage see query.

let winner = await schema.getOneField(`SELECT 'The winner is ' || name FROM test1.users LIMIT 1`);
console.log(winner); //The winner is Admin

queryOneColumn

queryOneColumn(sql:string, params?:any[]|{}, options?:SqlQueryOptions):Promise<any[]>

Note: inherited, uses schema level log if present (if not then the db level log).

If there is only one column that we are interested in. For the params usage see query.

let userList = await schema.getOneColumn('SELECT name FROM test1.users');
console.dir(userList); //['Admin', 'User1', 'User2']

queryAsStream

queryAsStream(sql:string, params?:any[]|{}, options?:SqlQueryOptions):Promise<any[]>

see streams


run

run(sql:string):Promise<any[]>

Executes an arbitrary sql string. Note: inherited, uses schema level log if present (if not then the db level log).

Executes an arbitrary sql string;

await schema.run('CREATE schema myschema');