8000 Multi line prepared statement · Issue #1 · danneu/pg-extra · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Multi line prepared statement #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mikef-traction opened this issue Mar 29, 2017 · 3 comments
Closed

Multi line prepared statement #1

mikef-traction opened this issue Mar 29, 2017 · 3 comments

Comments

@mikef-traction
Copy link
mikef-traction commented Mar 29, 2017

Appreciate your work on this repo.
Working on my first koa/vuejs/node/postgres project so apologies if this is a silly question. I have large bulk inserts to perform. ie:
INSERT INTO
foo (col1,col2,col3)
VALUES
(1,2,3),
(4,5,6),
(7,8,9),
......
I'm not sure how to build out very long sql statements. I've attempted something like brianc/node-postgres#530 (comment) but I get an error that client.query expects either sql or __raw within client.query.

Could you suggest a way to build out the query? Could be many many lines long. Cheers.

@mikef-traction mikef-traction changed the title Multi line prepared statment Multi line prepared statement Mar 29, 2017
@danneu
Copy link
Owner
danneu commented Mar 30, 2017

I personally use a query builder like knex for more dynamic queries, like an INSERT with an arbitrary amount of VALUES or a /search endpoint that may have any number of filters.

const knex = require('knex')

exports.insertUsers = function (users) {
    const sqlString = knex('users')
        .insert(users.map((u) => ({ uname: u.uname })))
        .toString()
    return client._query(sqlString)
}

I should update the README to provide examples like this.

Notice that, in pg-extra, client._query() bypasses the sql/_unsafe requirement.

@mikef-traction
Copy link
Author

That is awesome. Thanks!

@danneu
Copy link
Owner
danneu commented Apr 25, 2017

Updated the readme with this example.

This is the same thing you'd do whether you use pg-extra or the underlying pg module directly since you can't create a dynamic query with INSERT INTO ... VALUES ($1, $2), ($3, $4), ... syntax.

@danneu danneu closed this as completed Apr 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0