npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-postgresql

v1.0.8

Published

postgresql for node

Downloads

17

Readme

node-postgresql

系统要求

需支持es6环境

Class

  • PostgreSQL
  • Transaction

PostgreSQL 方法说明

构造函数
const options = {
    user: 'admin',
    password: '12345678',
	host: 'localhost',				//可选,默认localhost
	port: 5432,						//可选,默认5432
	max: 10,						//连接池连接数量,可选,默认10
	idleTimeoutMillis: 30000,		//连接最大空闲时间,可选,默认30000
	database: 'sample',				
	initSQL: 'path/to/sqlfile' 		//可选,仅供sync方法使用。
};
const pg = new PostgreSQL(options);
同步数据库(sync)

读取options.initSQL所指向文件包含的sql并执行执行,初始化数据库。

const pg = new PostgreSQL(options);
pg.sync().then(() => console.log('sync success');)

无初始化数据库需要无需调用

查询(query)
const pg = new PostgreSQL(options);
pg.query('select * from users').then(result => console.log(result));

//输出结构如下:
{ 
	command: 'SELECT', 
	rowCount: 1, 
	rows: [...], 
	fields: [...] 
}

其中fileds为rows返回结果中各字段的类型描述
获取事务对象(transaction)

事务对象为Transaction实例,两种方式获取事务对象:

const pg = new PostgreSQL(options);

//自动事务
pg.transaction(t => {
	//do something
})

//手动事务
pg.transaction().then(t => {
	//do something
})

Transaction 方法说明

构造

无需手动构造,通过PostgreSQL::transaction方法获取。

查询(query)

同PostgreSQL::query方法

开始事务(begin)

开始事务时调用。

提交事务(commit)

查询成功,提交事务时调用。

回滚事务(rollback)

查询出错,回滚事务时调用

事务使用示例
//自动事务对象使用示例

//自动事务对象在调用transaction时需传递一个function作为参数,
//系统以事务对象作为参数回调这个function,
//所有该次事务相关查询在该function中处理即可。

const pg = new PostgreSQL(options);

pg.transaction(t => {
	//无需调用t.begin
	return t.query('some sql')
		.then(() => {
			return t.query('some sql');
		})事务
})
.then(() => {
	//无需调用t.commit
})
.catch((err) => {
	//无需调用t.rollback
});


//手动事务对象使用示例
const pg = new PostgreSQL(options);

pg.transaction()
	.then(t => {
				//开始事务
		return t.begin()	
					.then(() => {
						return t.query('some sql');
					})
					.then(() => {
						return t.query('some sql');
					})
					.then(() => {
						//提交事务
						return t.commit();
					})
					.catch(err => {
						//回滚事务
						t.rollback();
					})
	})