Nodejs对postgresql基本操作的封装方法

时间:2021-05-26

基于nodejs平台对postgresql的增删改查基本操作进行了封装,能满足基本的实际应用,比较复杂的SQL需另外实现。

PG.js文件如下:

var pg = require('pg');var conString = "postgres://username:password@localhost/databasename";var client = new pg.Client(conString); var PG = function(){ console.log("准备向****数据库连接...");}; PG.prototype.getConnection = function(){ client.connect(function (err) { if (err) { return console.error('could not connect to postgres', err); } client.query('SELECT NOW() AS "theTime"', function (err, result) { if (err) { return console.error('error running query', err); } console.log("hbdfxt数据库连接成功..."); }); });}; // 查询函数//@param str 查询语句//@param value 相关值//@param cb 回调函数var clientHelper = function(str,value,cb){ client.query(str,value,function(err,result){ if(err) { cb("err"); } else{ if(result.rows != undefined) cb(result.rows); else cb(); } });}//增//@param tablename 数据表名称//@param fields 更新的字段和值,json格式//@param cb 回调函数PG.prototype.save = function(tablename,fields,cb){ if(!tablename) return; var str = "insert into "+tablename+"("; var field = []; var value = []; var num = []; var count = 0; for(var i in fields){ count++; field.push(i); value.push(fields[i]); num.push("$"+count); } str += field.join(",") +") values("+num.join(",")+")"; clientHelper(str,value,cb);}; //删除//@param tablename 数据表名称//@param fields 条件字段和值,json格式//@param cb 回调函数PG.prototype.remove = function(tablename,fields,cb){ if(!tablename) return; var str = "delete from "+tablename+" where "; var field = []; var value = []; var count = 0; for(var i in fields){ count++; field.push(i+"=$" +count); value.push(fields[i]); } str += field.join(" and "); clientHelper(str,value,cb);} //修改//@param tablename 数据表名称//@param fields 更新的字段和值,json格式//@param mainfields 条件字段和值,json格式PG.prototype.update = function(tablename,mainfields,fields,cb){ if(!tablename) return; var str = "update "+tablename+" set "; var field = []; var value = []; var count = 0; for(var i in fields){ count++; field.push(i+"=$"+count); value.push(fields[i]); } str += field.join(",") +" where "; field = []; for(var j in mainfields){ count++; field.push(j+"=$"+count); value.push(mainfields[j]); } str += field.join(" and "); clientHelper(str,value,cb);} //查询//@param tablename 数据表名称//@param fields 条件字段和值,json格式//@param returnfields 返回字段//@param cb 回调函数PG.prototype.select = function(tablename,fields,returnfields,cb){ if(!tablename) return; var returnStr = ""; if(returnfields.length == 0) returnStr = '*'; else returnStr= returnfields.join(","); var str = "select "+returnStr+ " from "+tablename+" where "; var field = []; var value = []; var count = 0; for(var i in fields){ count++; field.push(i+"=$"+count); value.push(fields[i]); } str += field.join(" and "); clientHelper(str,value,cb);}; module.exports = new PG();

用法很简单,如下:

var pgclient = require('./PG');// 引用上述文件pgclient.getConnection(); // 调用上述四个函数即可pgclient.save('userinfo',{'name': admin},cb);<span style="font-family: Arial, Helvetica, sans-serif;">.</span>

以上这篇Nodejs对postgresql基本操作的封装方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章