Skip to content

Commit

Permalink
More of chapter 9's source
Browse files Browse the repository at this point in the history
  • Loading branch information
alexyoung committed Dec 14, 2016
1 parent b221c5d commit 82ec554
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ch09-databases/listing9_3/create-tables.js
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();
});
});
1 change: 1 addition & 0 deletions ch09-databases/listing9_3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ db.connect((err, client) => {
if (err) throw err;
const id = result.rows[0].id;
console.log('Inserted row with id %s', id);
db.end();
});
});
});
16 changes: 16 additions & 0 deletions ch09-databases/listing9_4/create-tables.js
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();
});
});
31 changes: 31 additions & 0 deletions ch09-databases/listing9_4/index.js
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();
});
});
});
15 changes: 15 additions & 0 deletions ch09-databases/listing9_4/package.json
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"
}
}
16 changes: 16 additions & 0 deletions ch09-databases/listing9_5/create-tables.js
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();
});
});
15 changes: 15 additions & 0 deletions ch09-databases/listing9_5/index.js
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();
});
});
15 changes: 15 additions & 0 deletions ch09-databases/listing9_5/package.json
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"
}
}

0 comments on commit 82ec554

Please sign in to comment.