本文整理汇总了TypeScript中pg.Pool.connect方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Pool.connect方法的具体用法?TypeScript Pool.connect怎么用?TypeScript Pool.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pg.Pool
的用法示例。
在下文中一共展示了Pool.connect方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: Date
(async () => {
const client = await pool.connect();
try {
for (const message of messages) {
await client.query(
sql,
[
message.body,
message.expiresAt,
message.group,
message.localId,
message.publicId,
message.source,
message.title,
message.url,
new Date(),
userId,
message.badge,
],
);
}
await client.query('COMMIT');
} catch (e) {
await client.query('ROLLBACK');
throw e;
} finally {
client.release();
}
})().catch((e) => console.error(e.stack));
示例2:
app.post('/parent-events', (req, res) => {
pool.connect((connectionError, client, releaseClient) => {
if (connectionError) return console.error('Error fetching client from pool', connectionError);
client.query(selectParentEventsSql(req.body.event), (queryError, result) => {
if (queryError) return console.error('Error querying database', queryError);
res.send(result.rows.map(parseEvent));
releaseClient();
});
});
});
示例3: Pool
export const createPoolConnection = (conf: any): DBConnectionsPool => {
const pool: Pool = new Pool(conf);
return {
query(q) {
return pool.query(q);
},
connect() {
return pool.connect();
},
stop() {
return pool.end();
}
};
};
示例4: getClient
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);
client.release();
});
}
示例5: parseEvent
app.post('/events', (req, res) => {
pool.connect((connectionError, client, releaseClient) => {
if (connectionError) return console.error('Error fetching client from pool', connectionError);
client.query(selectRootSql(req.body.event), (queryError1, result1) => {
if (queryError1) return console.error('Error querying database', queryError1);
client.query(sql(req.body.event), (queryError2, result2) => {
if (queryError2) return console.error('Error querying database', queryError2);
res.send({
events: result2.rows.map(parseEvent),
root: parseEvent(result1.rows[0]),
});
releaseClient();
});
});
});
});
示例6: Promise
const poolClientInit: Promise<pg.Client> = new Promise((resolve, reject) => {
if (poolClient) { return resolve(poolClient) }
if (!connectionOptions && !process.env.PGUSER) {
console.warn('Not found connection settings')
return
}
const pool = new pg.Pool(Object.assign(connectionOptions ? connectionOptions : {}, {
max: 100,
idleTimeoutMillis: 500
}))
pool.connect((err, client) => {
if (err) return reject(err)
poolClient = client
resolve(poolClient)
})
setTimeout(() => reject(new Error('Pool creation timeout')), POOL_CREATION_TIMEOUT)
})
示例7: checkPrice
export function checkPrice(priceObj: Ticker): Promise < any > {
priceObj.base = getTicker(priceObj.base, priceObj.exchange.toLowerCase());
priceObj.quote = getTicker(priceObj.quote, priceObj.exchange.toLowerCase());
// tslint:disable-next-line:max-line-length
const table_name: string = "prices." + priceObj.exchange.toLowerCase() + "_" + priceObj.base.toLowerCase() + "_" + priceObj.quote.toLowerCase();
return pool.connect()
.then(client => {
// tslint:disable-next-line:max-line-length
return client.query("CREATE TABLE IF NOT EXISTS " + table_name + " (price_id bigserial PRIMARY KEY, exchange_pair_id int NOT NULL, price float NOT NULL, bid float, ask float, baseVolume float, quoteVolume float, ts int NOT NULL)").then(res => {
return client.query(
// tslint:disable-next-line:max-line-length
"INSERT INTO " + table_name + " (exchange_pair_id, price, bid, ask, baseVolume, ts) SELECT exchange_pair_id, $1, $2, $3, $4, $5 FROM exchange_pairs WHERE exchange_id = (SELECT exchange_id FROM exchanges WHERE exchange_name = ($6)) AND pair_id = (SELECT p.pair_id FROM pairs p WHERE p.base_id = (SELECT asset_id FROM assets WHERE asset_ticker = ($7)) AND p.quote_id = (SELECT asset_id FROM assets WHERE asset_ticker = ($8))) RETURNING price_id, exchange_pair_id", [priceObj.price, priceObj.bid, priceObj.ask, priceObj.baseVolume, Math.round(priceObj.ts), priceObj.exchange, priceObj.base, priceObj.quote]
)
.then(res => {
if (res.rows[0]) {
// tslint:disable-next-line:max-line-length
return client.query(
// tslint:disable-next-line:max-line-length
"UPDATE exchange_pairs SET last_price = $1 WHERE exchange_pair_id = $2", [priceObj, res.rows[0].exchange_pair_id]
).then(_ => {
client.release();
return res.rows[0];
}).catch(err => {
logger.logError("checkPrice UPDATE query", err);
});
} else {
client.release();
}
}).catch(e => {
client.release();
logger.logError("checkPrice INSERT query", e);
});
}).catch(e => {
client.release();
logger.logError("checkPrice CREATE query", e);
});
});
}
示例8: catch
(async () => {
const client = await pool.connect();
try {
for (const token of tokens) {
await client.query(
sql,
[
userId,
token.key,
token.value,
token.label,
token.persist,
],
);
}
await client.query('COMMIT');
} catch (e) {
await client.query('ROLLBACK');
throw e;
} finally {
client.release();
}
})().catch((e) => console.error(e.stack));
示例9: done
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) => {
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");
});