nodejs express knex master detail record update

207
March 21, 2018, at 09:37 AM

I have master(Workbook) & Child(WorkBookDataset). I am trying to update master and at the same time (insert or update or delete) child records. Everything works fine except it is not returning the updated child records. I know I am doing something wrong while specifying the 'then', and as it is always asyc operation, the result is already returned before child updates complete.

var Promise = require('bluebird'); 
     
    return knex.transaction(function(trx) { 
        return knex('workbook').where('workbookid',workbook.workbookid).andWhere('userid', workbook.userid)             
        .update(workbook) 
        .then(function(updatedrecords) { 
            return Promise.map(datasets, function(dataset) { 
                if(dataset.workbookdatasetid && dataset.workbookdatasetid == -1){                         
                    //remove 
                    return knex('workbookdataset').where('workbookid',workbook.workbookid).andWhere('datasetid', dataset.datasetid) 
                    .delete(); 
                } else { 
                dataset.workbookid = workbook.workbookid; 
                 
                knex('workbookdataset').where('workbookid',workbook.workbookid).andWhere('datasetid', dataset.datasetid) 
                .then(function(alreadyds) {                         
                    if(alreadyds.length == 1){ 
                        //update                             
                        return knex('workbookdataset').where('workbookid',workbook.workbookid).andWhere('datasetid', dataset.datasetid) 
                        .update(dataset) 
                    }else{ 
                        //insert 
                        if(dataset.workbookdatasetid){ 
                            delete dataset.workbookdatasetid; 
                        }                             
                        return knex('workbookdataset') 
                        .insert(dataset) 
                    } 
                    })    
                } 
                }); 
        }) 
    }) 
    .then(function(updatedrecords) { 
        return getWorkBook(workbook.userid, workbook.workbookid); //this returns updated workbook information, but not updated workbookdataset information 
    });

I tried putting then() to Promise, but still same. Any help/pointer would be a great help! Thanks in advance!

Answer 1

You need a return on the line where you have knex('workbookdataset').where('workbookid'

Answer 2

Have a look at a .returning method from knex documentation. It allows you to return inserted/updated/deleted records from a database.

Also, I'd recommend you to check your promise chain to be sure you are returning correct values in correct places.

P.S. This is not related to question but if you are using transactions add to your queries .transacting(trx) to actually run them in one transaction instead of separately.

knex('workbook')
  .where('workbookid',workbook.workbookid)
  .andWhere('userid', workbook.userid)            
  .update(workbook)
  .returning('*')
  .transacting(trx)
  .then(data => {
     console.log(data) // => [{id: 1, workbookid: 1, userid: 981,...}, ...]
  })
READ ALSO
Mongoose: Recursively iterate to update property in tree-like structure

Mongoose: Recursively iterate to update property in tree-like structure

I have a "tree-like" structure that I have implemented for storing commentsBasically every "comment" is a node which contains a "parent" property that links to another "comment" node

111
How to wait for meteor call response and then execute other statements in javascript?

How to wait for meteor call response and then execute other statements in javascript?

I have two meteor call in client events in meteor, where i want to execute one after anotherBut as i debugged the flow dosen't follow the way i want it to be

174
Angular CLI external components project

Angular CLI external components project

I am developing an Angular 4 (Angular CLI) projectMy main app uses one git repository (http://

166
Stuck at starting Selenium Server Nightwatch end-to-end tests

Stuck at starting Selenium Server Nightwatch end-to-end tests

Nightwatchjs (node) End to End Mocha Tests on Chrome suddenly are not running

157