使用PostgreSQL CURRENT_DATE函数与Node和pg-native

时间:2016-02-02 17:50:14

标签: node.js postgresql

我正在使用Node with Express作为应用。我正在使用pg-native模块连接到PostgreSQL。我试图在我想要使用PostgreSQL CURRENT_DATE函数的地方插入一些数据。我知道它在PostgreSQL中有效,但是如何使用pg-native和Node?

//regular db connection code..
//insert statement..
db.query ('INSERT INTO users( firstname, lastname, email, createddate) 
           VALUES ($1, $2, $3, $4) 
           RETURNING firstName, lastName, email', [user.username, user.firstname, user.lastname, 
           `CURRENT_DATE`], true)

2 个答案:

答案 0 :(得分:0)

  

如何使用pg-native和Node?

像这样:

//regular db connection code..
//insert statement..
db.query('INSERT INTO users(firstname, lastname, email, createddate) 
           VALUES ($1, $2, $3, CURRENT_DATE) 
           RETURNING firstName, lastName, email', [user.username, user.firstname, user.lastname], true)

答案 1 :(得分:0)

插入tea数据时pg和pg本机性能之间的巨大差异

架构:

CREATE TABLE test (
id SERIAL PRIMARY KEY,
data bytea 
);
CREATE UNIQUE INDEX test_pkey ON test(id int4_ops);

test.js:

import fs from 'fs';
import pg from 'pg';

const image = fs.readFileSync('./test.jpg');

const main = async () => {
const client = new pg.native.Client();

await client.connect();

  let index = 1000;

 while (index--) {
 await client.query('INSERT INTO "test" (data) VALUES ($1) RETURNING id', [
  image
  ]);
 }

 await client.end();
};

 main();

评估:

$ time node ./test.js