How it works...

Postgres offers slight SQL variants (along with a non-standard superset) of SQL functionalities compared to MySQL.

Accepted convention in Postgres is to expect a database named after the user account that owns a given process. This is why in the getting ready section we ran createdb `whoami` on the command line.

When we instantiate pg.Client (storing the instance to the db variable), it reads the USER (or USERNAME on Windows) environment variable and attempts to connect to a database named after the current user, as that user, on the default Postgres port. By default, the database is passwordless.

The upshot is that we don't need to supply a configuration to pg.Client.

We call pg.connect to connect to the Postgres server and use the database (as named after the user). This will trigger the callback we supplied. Since we're writing a command-line application, we will throw any errors that come through any callbacks for instant user feedback.

From this point, we're simply sending SQL to the Postgres server via the db.query method. If the user has supplied both author and quote arguments, we perform an INSERT. We clean the user input by using placeholders (VALUES($1, $2)) and supplying an array of values matching the placeholder index.

The list function sends a SELECT query. This time we clean user input using db.escapeLiteral.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset