-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdemo04.js
More file actions
28 lines (26 loc) · 776 Bytes
/
demo04.js
File metadata and controls
28 lines (26 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const config = require('./dbConfig').postgreSql;
const display = require('./display');
const knex = require('knex')(config);
display.clear();
const doc = {firstname:'Dr', lastname:'sess'};
const books = [
{title: "The cat in the hat", rating: 1},
{title:"The another world", rating:100}
];
knex.transaction(function(trx){
return trx
.insert(doc, "id").into("author")
.then(function(idArray){
let authorId = idArray[0];
for(let i=0; i<books.length; i++){
books[i].author_id = authorId;
}
return trx.insert(books).into('book');
});
}).then(function(){
display.write(books.length + ' books Added', "pretty");
}).catch(function(err){
console.error(err);
}).finally(function(){
knex.destroy();
});