本文整理匯總了TypeScript中pg.Pool.on方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Pool.on方法的具體用法?TypeScript Pool.on怎麽用?TypeScript Pool.on使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pg.Pool
的用法示例。
在下文中一共展示了Pool.on方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: Pool
(async () => {
const pgPool = new Pool(pgConfig);
pgPool.on('error', err => {
// tslint:disable-next-line no-console
console.error('PostgreSQL client generated error: ', err.message);
});
const { getGraphQLSchema } = getPostgraphileSchemaBuilder(pgPool, schemas, postgraphileOptions);
await getGraphQLSchema();
if (!watchPg) {
await pgPool.end();
}
})().then(null, e => {
示例2: done
password: 'secret', //env var: PGPASSWORD
port: 5432, //env var: PGPORT
max: 10, // max number of clients in the pool
idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
Promise,
};
var pool = new pg.Pool(config);
pool.connect((err, client, done) => {
if(err) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT $1::int AS number', ['1'], (err, result) => {
done();
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].number);
});
});
pool.on('error', (err, client) => {
console.error('idle client error', err.message, err.stack)
})
pool.end();
pool.end(() => {
console.log("pool is closed");
});
示例3: Pool
import config from "../var/config";
import * as pg from "pg";
import {Pool, Client, QueryResult} from "pg";
// `postgresql://${config.DATABASE_USERNAME}:${config.DATABASE_PASSWORD}@#${config.DATABASE_HOST}/${config.DATABASE_NAME}:${config.DATABASE_PORT}`;
var pool: Pool = new Pool({
host: config.DATABASE_HOST,
port: config.DATABASE_PORT,
database: config.DATABASE_NAME,
user: config.DATABASE_USERNAME,
password: config.DATABASE_PASSWORD
});
pool.on("error", (error, client) => {
return error;
});
export async function getClient(): Promise<Client> {
var client: Client;
try {
client = await pool.connect();
} catch (error) {
if (client)
client.release();
throw error;
}
return new Promise<Client>((resolve: (client: Client) => void, reject) => {
resolve(client);
示例4: function
});
// client pooling
var config = {
user: 'foo', //env var: PGUSER
database: 'my_db', //env var: PGDATABASE
password: 'secret', //env var: PGPASSWORD
port: 5432, //env var: PGPORT
max: 10, // max number of clients in the pool
idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
};
var pool = new pg.Pool(config);
pool.connect(function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT $1::int AS number', ['1'], function(err, result) {
done();
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].number);
});
});
pool.on('error', function (err, client) {
console.error('idle client error', err.message, err.stack)
})
示例5: releaseClient
});
});
});
app.post('/event-types', (req, res) => {
pool.connect((connectionError, client, releaseClient) => {
if (connectionError) return console.error('Error fetching client from pool', connectionError);
client.query(`SELECT event_type.id as key, event_type.title as value, event_type.body FROM event_type WHERE event_type.title LIKE '%${req.body.query}%';`, (queryError, result) => {
if (queryError) return console.error('Error querying database', queryError);
res.send(result.rows);
releaseClient();
});
});
});
pool.on('error', function (err, client) {
// if an error is encountered by a client while it sits idle in the pool
// the pool itself will emit an error event with both the error and
// the client which emitted the original error
// this is a rare occurrence but can happen if there is a network partition
// between your application and the database, the database restarts, etc.
// and so you might want to handle it and at least log it out
// console.error('idle client error', err.message, err.stack);
});
const port = 3999;
app.listen(port, () => console.log(`Listening on port ${port}!`));