本文整理汇总了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) => {