本文整理匯總了TypeScript中pg.Client.on方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Client.on方法的具體用法?TypeScript Client.on怎麽用?TypeScript Client.on使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pg.Client
的用法示例。
在下文中一共展示了Client.on方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: Client
.then((location) => {
let client = new Client({
host: location.hostname,
port: location.port || 5432,
database: database,
user: user,
password: password,
parseInputDatesAsUTC: true // not in the type
} as any);
client.on('drain', client.end.bind(client)); // disconnect client when all queries are finished
client.connect();
//query is executed once connection is established and PostgreSQL server is ready for a query
let q = client.query(new (Query as any)(query) as any);
// ToDo: use node-pg-cursor or node-pg-query-stream here instead
q.on('row', function(row: any) {
stream.push(row);
});
q.on('error', function(err: any) {
stream.emit('error', err); // Pass on any errors
});
q.on('end', function() {
stream.push(null); // pushing null, indicating EOF
});
})
示例2: async
const createClient = async () => {
const pgClient = new Client({
host: settings.host || 'localhost',
port: options.port || 5432,
user: settings.user,
ssl: settings.ssl ? true : undefined,
database: settings.database,
password: options.password,
})
pgClient.on('error', err => {
if (currentReject) {
currentReject(err)
currentReject = undefined
}
})
await pgClient.connect()
return pgClient
}
示例3: NOW
client.connect(err => {
if (err) {
return console.error("Could not connect to postgres", err);
}
client.query("SELECT NOW() AS 'theTime'", (err, result) => {
if (err) {
return console.error("Error running query", err);
}
console.log(result.rowCount);
console.log(result.rows[0]["theTime"]);
client.end();
return null;
});
return null;
});
client.on('end', () => console.log("Client was disconnected."));
// 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
Promise,
};
var pool = new pg.Pool(config);
pool.connect((err, client, done) => {