API 文档暂时还没有,下面是使用的 demo
use node-postgres easier
const {PgHelper} = require('@c_kai/pg-helper');
// detail https://node-postgres.com/api/pool
const pgHelper = new PgHelper({
host,
user,
password,
database,
port: 5432,
max: 20,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
});
const result = await pgHelper.insert([{
percentage: 0,
type: 'public',
params: {},
created_by: 1,
status: 'created',
job_uuid: '103',
},{
percentage: 0,
type: 'public',
params: {},
created_by: 1,
status: 'created',
job_uuid: '104',
}], {
tableName: 'jobs',
returning: true,
});
const result = await pgHelper.delete({}, {
tableName: 'jobs',
transaction,
});
const result = await pgHelper.update({
type: 'pravate',
status: 'gg',
}, {
update: ['type', 'status'],
tableName: 'jobs',
returning: ['id'],
transaction,
});
const result = await pgHelper.select({
percentage: 0,
}, {
where: {
percentage: '= {percentage}'
or: {
id: '=1',
}
},
schemaName: 'public',
tableName: 'jobs',
autoHump: false,
count: true,
});
await pgHelper.runSql('select now()');
// with params
await pgHelper.runSql('select power({a}, {b})', { a: 2, b: 4 });
await pgHelper.runTSql([
{
sql: 'select now()',
},
{
sql: 'select power({a}, {b})',
params: { a: 2, b: 4}
}
])
OR
let transaction;
try {
transaction = await pgHelper.getTransaction();
let result4 = await pgHelper.runSql('select {str1} || {str2}', {
str1: 'xiao',
str2: 'hong',
}, {
transaction,
});
transaction.commit();
console.log(result4)
} catch (error) {
transaction.rollback();
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.