forked from alexyoung/nodejsinaction
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const pg = require('pg'); | ||
const db = new pg.Client({ database: 'articles' }); | ||
|
||
db.connect((err, client) => { | ||
db.query(` | ||
CREATE TABLE IF NOT EXISTS snippets ( | ||
id SERIAL, | ||
PRIMARY KEY(id), | ||
body text | ||
); | ||
` , (err, result) => { | ||
if (err) throw err; | ||
console.log('Created table "snippets"'); | ||
db.end(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const pg = require('pg'); | ||
const db = new pg.Client({ database: 'articles' }); | ||
|
||
db.connect((err, client) => { | ||
db.query(` | ||
CREATE TABLE IF NOT EXISTS snippets ( | ||
id SERIAL, | ||
PRIMARY KEY(id), | ||
body text | ||
); | ||
` , (err, result) => { | ||
if (err) throw err; | ||
console.log('Created table "snippets"'); | ||
db.end(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const pg = require('pg'); | ||
const db = new pg.Client({ database: 'articles' }); | ||
|
||
db.connect((err, client) => { | ||
if (err) throw err; | ||
console.log('Connected to database', db.database); | ||
|
||
const body = 'hello world'; | ||
db.query(` | ||
INSERT INTO snippets (body) VALUES ( | ||
'${body}' | ||
) | ||
RETURNING id | ||
`, (err, result) => { | ||
if (err) throw err; | ||
const id = result.rows[0].id; | ||
const updatedBody = 'greetings, world'; | ||
|
||
console.log('Inserted row with id %s', id); | ||
|
||
db.query(` | ||
UPDATE snippets SET (body) = ( | ||
'${body}' | ||
) WHERE id=${id}; | ||
`, (err, result) => { | ||
if (err) throw err; | ||
console.log('Updated %s rows.', result.rowCount); | ||
db.end(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "listing9_4", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"pg": "^6.1.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const pg = require('pg'); | ||
const db = new pg.Client({ database: 'articles' }); | ||
|
||
db.connect((err, client) => { | ||
db.query(` | ||
CREATE TABLE IF NOT EXISTS snippets ( | ||
id SERIAL, | ||
PRIMARY KEY(id), | ||
body text | ||
); | ||
` , (err, result) => { | ||
if (err) throw err; | ||
console.log('Created table "snippets"'); | ||
db.end(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const pg = require('pg'); | ||
const db = new pg.Client({ database: 'articles' }); | ||
|
||
db.connect((err, client) => { | ||
if (err) throw err; | ||
console.log('Connected to database', db.database); | ||
|
||
db.query(` | ||
SELECT * FROM snippets ORDER BY id | ||
`, (err, result) => { | ||
if (err) throw err; | ||
console.log(result.rows); | ||
db.end(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "listing9_5", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"pg": "^6.1.2" | ||
} | ||
} |