I have spent most of the night trying to get a very basic project up doing the most basic CRUD operations.
I have not seen a table anywhere in my career that did not have an auto increment primary key, and a timestamp in it somewhere.
The first insert example in the documentation has (name, email), as the only two fields. I have never seen anything like that with just two fields: name, email.
While I appreciate the simplicity there, is there anyway the owner of this project can provide some additional documentation that would show how to insert into a postgres table that has an auto increment id (serial data type), and also a timestamp field?
PLEASE. As the first example, it's very basic stuff. There are more data types in postgres than just TEXT.
Anything less is useless documentation and a waste of space on the web. Please demonstrate how to get a current timestamp and insert into a DB.
I have need to do a bulk insert (thousands of rows in a list), any pointers there on how to do this quickly, instead of iterating through each row and doing things one by one as in the initial examples would be greatful.
I will keep digging, trying things, seeing error messages and failing.
I'm searching the web and not seeing anyone anywhere post anything useful that would help me use this library.
Well, in my humble opinion what you are asking is beyond the scope of this project documentation.
Having said that, what you are asking is quite simple:
pool.query('INSERT INTO table (name, timestamp) VALUES ($1,$2)',['myName',new Date()])
For bulk inserts:
pool.query('INSERT INTO table (name, timestamp) VALUES ($1,$2), ($3,$4), ($5,$6)',['myName1',new Date(),'myName2',new Date(),'myName3',new Date()])
And for autoincrement:
pool.query('INSERT INTO table (name, timestamp) VALUES ($1,$2) RETURNING auto_incr_column',['myName',new Date()])
The generated auto_incr_column will be accessible in rows[0].auto_incr_column
Schema design is broad topic and likely outside the scope of issues for the driver. If there's a question about handling a particular data type feel free to ask about it though.
For bulk inserts look into passing arrays (the PostgreSQL data type) of values. There's a couple examples in the GitHub issues of how to use them with this driver (search "array insert").
For really huge inserts look into using COPY ... FROM STDIN. This driver supports COPY streams via an additional module. Again, there's many examples in the GitHub issues.
There's also the main website for this driver: https://node-postgres.com/. The source for the website is at https://github.com/brianc/node-postgres-docs and you can submit a PR to improve or add additional examples there.
If you just want to see a bunch of code examples, here's a repo I put together for an intro talk that walks through a lot of the features of the driver: https://github.com/sehrope/postgres-with-nodejs-code/tree/master/src
Well, in my humble opinion what you are asking is beyond the scope of this project documentation.
seriously? how to use the module is beyond project documentation? unbelievable. Hoping that you are never the author/producer of a module or publish code for consumption of any kind. Please warn us if you do.
Schema design is broad topic and likely outside the scope of issues for the driver. If there's a question about handling a particular data type feel free to ask about it though.
For bulk inserts look into passing arrays (the PostgreSQL data type) of values. There's a couple examples in the GitHub issues of how to use them with this driver (search "array insert").
For really huge inserts look into using
COPY ... FROM STDIN. This driver supports COPY streams via an additional module. Again, there's many examples in the GitHub issues.There's also the main website for this driver: https://node-postgres.com/. The source for the website is at https://github.com/brianc/node-postgres-docs and you can submit a PR to improve or add additional examples there.
If you just want to see a bunch of code examples, here's a repo I put together for an intro talk that walks through a lot of the features of the driver: https://github.com/sehrope/postgres-with-nodejs-code/tree/master/src
This is not about schema design, not at all JESUS!!!! It's about how to use the damn module to insert a F* row.
Having said that, what you are asking is quite simple:
pool.query('INSERT INTO table (name, timestamp) VALUES ($1,$2)',['myName',new Date()])
This completely avoids the real issue, I am looking to insert a row into a table that has an auto-increment ID. where is that in the response? jesus, how hard can this really be?
I'm not sure anyone here has ever built a table, used it in production. Do you folks not use primary keys, auto-increment at all? anywhere? You like writing gratuitous code, keeps you busy?
I explained in more detail here. In the documentation, an insert example covering field types is just a a very basic thing.
No one creates a table with two text fields and uses that in a production environment, There are keys, foreign keys, there are a multitude of data types, it's really not too much to ask from the product or documenation owner, it's something everyone everywhere lands on the first time they use this module.
If you do not have any constructive helpful answer, then please don't answer, don't respond, thank you.
Schema design is broad topic and likely outside the scope of issues for the driver. If there's a question about handling a particular data type feel free to ask about it though.
For bulk inserts look into passing arrays (the PostgreSQL data type) of values. There's a couple examples in the GitHub issues of how to use them with this driver (search "array insert").
For really huge inserts look into using
COPY ... FROM STDIN. This driver supports COPY streams via an additional module. Again, there's many examples in the GitHub issues.There's also the main website for this driver: https://node-postgres.com/. The source for the website is at https://github.com/brianc/node-postgres-docs and you can submit a PR to improve or add additional examples there.
If you just want to see a bunch of code examples, here's a repo I put together for an intro talk that walks through a lot of the features of the driver: https://github.com/sehrope/postgres-with-nodejs-code/tree/master/src
Can you point me to a specific example in your copious documentation that would be helpful for the question asked?
Do you have an example of inserting a row with an auto-increment ID? Have you ever done that, every tried it? ever needed to?
If you want to insert into a table users (name text, email text), the query looks like what鈥檚 given in the documentation:
INSERT INTO users(name, email) VALUES($1, $2) RETURNING *
If your table has an id column, however, like users (id serial PRIMARY KEY, name text, email text), the query looks like this:
INSERT INTO users(name, email) VALUES($1, $2) RETURNING *
and if it has a creation timestamp column too, like users (id serial PRIMARY KEY, name text, email text, created timestamp with time zone DEFAULT now()), the query looks like this:
INSERT INTO users(name, email) VALUES($1, $2) RETURNING *
Teaching PostgreSQL is outside of the scope of the Node.js driver, but if, once you鈥檝e learned it, you can think of examples the documentation would benefit from, feel free to come back with a pull request and a better attitude.
No words. :|
There's a couple examples in the GitHub issues of how to use them with this driver (search "array insert").
+1 to the author's concern. Documentation is lacking basic use cases. Requiring users to trawl through Github issues to find how to use the library is going to drive people away.
Github issues is for raising valid bugs/features.
Documentation is for helping people use the library.
What the author asks is not about using the library but about using postgres, sql and node.js.
Definitely out of the scope of this library docs.