Skip to content
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

insert or update native javascript array doesn't work #220

Closed
mren opened this issue Nov 19, 2012 · 10 comments
Closed

insert or update native javascript array doesn't work #220

mren opened this issue Nov 19, 2012 · 10 comments

Comments

@mren
Copy link

mren commented Nov 19, 2012

Hey folks,

It is currently not possible to pass an javascript array to pg.

If I have a table

CREATE TABLE array (
    a integer[]
);

i can't use this

connection.query 'INSERT INTO array (a) VALUES ($1)', [ [1,2,3] ]

I receive: 'error: array value must start with "{" or dimension information]

but have to do this

connection.query 'INSERT INTO array VALUES ($1)', [ "{#{[1,2,3].join ','}}" ]

It would be great if pg could auto convert javascript arrays to pg arrays.

What do you guys think?

@booo
Copy link
Contributor

booo commented Nov 19, 2012

Have you seen the prepareValue function? https://github.com/brianc/node-postgres/blob/master/lib/utils.js#L98 Somehow it should be possible to overwrite this function and get the desired functionality.

@booo
Copy link
Contributor

booo commented Nov 19, 2012

E.g. https://gist.github.com/4113898

@brianc
Copy link
Owner

brianc commented Nov 19, 2012

yeah, that's there for exactly to let you override or extend the type coercion. One thing to note in your example, you'll want to pass an array as the first item of the params array:

client.query('INSERT INTO awesome_array_table(val) VALUES($1)', [ [1,2,3] ], ...)

@booo
Copy link
Contributor

booo commented Nov 19, 2012

@brianc but how do I overwrite the prepareValue function if I don't want to do something like:

require('./node_modules/pg/lib/utils')

Where do we export the prepareValue function other than the utils module? Or what is the recommended way to overwrite the function?

@brianc
Copy link
Owner

brianc commented Nov 19, 2012

Yeah, it's not currently exported on the main object returned from require('pg'). I suppose it could be exported there for easier overwriting. Right now you would have to do what you suggest with requiring the file directly & monkey-patching the function.

@mren
Copy link
Author

mren commented Nov 20, 2012

thanks @booo for hinting at prepareValue

arrays are a base data type in postgres, are there any reasons not to make a pull request and add a conversion mechanism for arrays?

(i corrected my example code)

@brianc
Copy link
Owner

brianc commented Nov 20, 2012

There's no reason not to. It's rather difficult to encode all the
different types of arrays properly. If you submit a pull request with
tests I'll almost always merge it happily and thankfully.

On Tue, Nov 20, 2012 at 2:10 AM, Mark Engel notifications@github.comwrote:

thanks @booo https://github.com/booo for hinting at prepareValue

arrays are a base data type in postgres, are there any reasons not to make
a pull request and add a conversion mechanism for arrays?


Reply to this email directly or view it on GitHubhttps://github.com//issues/220#issuecomment-10545786.

@mren
Copy link
Author

mren commented Jan 9, 2013

back from holiday :)

it would be awesome if node-pg could translate all different types of arrays, for sure.
would it be ok, if we start with integers and then move on to the next type as we encounter them and submit another pull request?

@brianc
Copy link
Owner

brianc commented Jan 9, 2013

absolutely.

On Wed, Jan 9, 2013 at 6:05 AM, Mark Engel notifications@github.com wrote:

back from holiday :)

it would be awesome if node-pg could translate all different types of
arrays, for sure.
would it be ok, if we start with integers and then move on to the next
type as we encounter them and submit another pull request?


Reply to this email directly or view it on GitHubhttps://github.com//issues/220#issuecomment-12041332.

@brianc
Copy link
Owner

brianc commented Jan 24, 2013

Closing. Awaiting pull request(s) for array types.

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

3 participants