nodejs连接PostgreSQL实现增删改查操作代码

代码语言:nodejs

所属分类:其他

代码描述:nodejs连接PostgreSQL实现增删改查操作代码

代码标签: nodejs 连接 PostgreSQL 增删改查 操作 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

const { Pool, Client } = require('pg');
const uuid = require('node-uuid');

let pool;
class pgUtil {

	constructor(){
		if(!pool){
			const pgConfig = {
			  user: 'test_user',
			  host: 'localhost',
			  database: 'test_db',
			  password: 'abc123',
			  port: 5432,
			  max:20,
			  idleTimeoutMillis:3000,
			}

			pool = new Pool(pgConfig);
		}
	}

	/**
	 * rowCount
	 * rows
	 */
	async queryAll() {
		let msg = await pool.query(`SELECT * FROM COMPANY`);
		return msg;
	}

	/**
	 * 条件查询
	 */
	async queryItem(username){
		let msg = await pool.query(`SELECT * FROM COMPANY WHERE name = $1`,[username]);
		return msg; 
	}

	/**
	 * insert
	 */
	async insert(obj){
		let {name, age} = obj;
		let id = uuid.v4();
		let msg = await pool.query(`INSERT INTO COMPANY (id, username, age) VALUES ($1, $2, $3)`, [id, name, age]);
		return msg;
	}

	/**
	 * upda.........完整代码请登录后点击上方下载按钮下载查看

网友评论0