欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Node.js连接postgreSQL并进行数据操作

程序员文章站 2023-10-24 20:55:28
前言 postgresql是一个面向对象的关系数据库,postgis是一个基于postgresql的空间数据库插件,主要用于管理地理空间数据。因此在gis领域,广泛使用p...

前言

postgresql是一个面向对象的关系数据库,postgis是一个基于postgresql的空间数据库插件,主要用于管理地理空间数据。因此在gis领域,广泛使用postgresql作为空间数据库。

首先使用npm安装数据库连接模块:

npm install --save pg 

连接池创建

然后代码中引入pg模块,并编写数据库配置:

var pg = require('pg');

// 数据库配置
var config = { 
 user:"postgres",
 database:"ghost",
 password:"123456",
 port:5432,

 // 扩展属性
 max:20, // 连接池最大连接数
 idletimeoutmillis:3000, // 连接最大空闲时间 3s
}

pg模块中有两种数据库连接方式,先讲连接池模式,下面是创建连接池:

// 创建连接池
var pool = new pg.pool(config); 

传入配置后就创建好了连接池。

查询数据

查询首先创建好连接,然后调用api进行查询:

// 查询
pool.connect(function(err, client, done) { 
 if(err) {
 return console.error('数据库连接出错', err);
 }
 // 简单输出个 hello world
 client.query('select $1::varchar as out', ["hello world"], function(err, result) {
 done();// 释放连接(将其返回给连接池)
 if(err) {
 return console.error('查询出错', err);
 }
 console.log(result.rows[0].out); //output: hello world
 });
});

输出:

hello world 

参数done是一个函数,调用这个函数可以将关闭连接(即将连接还给连接池)。

上面的是需要写回调的异步查询,可以使用es 7中await和async(但需安装最新版本的pg,另外,需要使用7.2以上的nodejs,最好就是用最新的nodejs)优化代码,如下:

// async & await 方式(需 node ^7.2.1,运行时使用 node --harmony-async-await index.js)
var query = async () => { 
 // 同步创建连接
 var connect = await pool.connect()
 try {
 // 同步等待结果
 var res = await connect.query('select $1::varchar as out', ['hello world by async&await'])
 console.log(res.rows[0].out) // 可以通过rows遍历数据
 } finally {
 connect.release()
 }
}

// 异步进行数据库处理
query().catch(e => console.error(e.message, e.stack)); 

在升级了nodejs之后,执行代码的时候,需要加参数--harmony-async-await

npm --harmony-async-await index.js 

当然,都支持到es7了,es6的promise方法肯定是支持的,如下:

pool.connect().then(client=>{ 
 client.query('select $1::varchar as out', ['hello world by promise']).then(res=>{
 client.release()
 console.log(res.rows[0].out)
 }).catch(e => {
 client.release()
 console.error('query error', e.message, e.stack)
 })
})

插入、修改、删除数据

插入、修改、删除数据和查询的差不多

// 在表test中插入、修改、删除数据,共两个字段 (name, age)
pool.connect().then(client=>{ 
 // insert 数据
 client.query("insert into test(name, age) values($1::varchar, $2::int)", ["xiaoming","20"]).then(res=>{
 console.log("insert success")
 // 如果是自增id,有返回值的,在res里
 return res;
 }).then(res=>{
 // 查询xiaoming
 return client.query("select * from test where name = $1", ["xiaoming"]);
 }).then(res=>{
 // 输出结果,看是否插入成功
 console.log(res.rows[0])
 }).then(res=>{
 // update 数据,将age改为21
 return client.query("update test set age=$1 where name=$2", [21, "xiaoming"])
 }).then(res=>{
 // 再查询一次xiaoming
 return client.query("select * from test where name = $1", ["xiaoming"]);
 }).then(res=>{
 // 再输出结果,看是否改为了21
 console.log(res.rows[0])
 }).then(res=>{
 // 删除数据
 client.query("delete from test where name=$1", ["xiaoming"])
 }).then(res=>{
 // 最后再查询一次xiaoming
 res = client.query("select * from test where name = $1", ["xiaoming"]);
 // 释放连接
 client.release()
 return res
 }).then(res=>{
 // 再输出结果,没数据 undefined
 console.log(res.rows[0])
 })
})

上面插入、更新里代码都没有进行错误处理,按道理是要加的,但如果要加try...catch...的话,就太麻烦了(毕竟只是示例).

事件监听

可以添加error事件方法监听连接池情况

pool.on("error", function(err, client){ 
 console.log("error --> ", err)
})

现在连接池的最大空闲时间是3s,也就是3s还没使用连接,就释放连接,可将这个时间设置得长一些,比如30s,这就让我们有足够的时间关掉数据库进行测试(与数据库连接一断开,这个事件就被触发了,生产环境中,可以用来写日志啊、发邮件短信通知什么的。。。)。

另外,还可以监听acquire和connect事件,前者在连接被客户端获取时触发,后者在连接生成以及客户端与数据库交互时触发。

pool.on('acquire', function (client) { 
 console.log("acquire event")
})

pool.on('connect', function () { 
 console.log("connect event")
})

不使用连接池的客户端

不使用连接池时,直接创建客户端即可:

var client = new pg.client(); 

连接池只是用来管理(缓存)连接(即客户端)的,查询之类的方法跟它没关系。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能有一定的帮助,如果有疑问大家可以留言交流。