Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascading save not working #13

Open
khushroo-mistry opened this issue Oct 7, 2016 · 0 comments
Open

Cascading save not working #13

khushroo-mistry opened this issue Oct 7, 2016 · 0 comments

Comments

@khushroo-mistry
Copy link

khushroo-mistry commented Oct 7, 2016

Hi there,

I am trying to perform a cascading save but not found any success yet:
In summary all I want to do it update the parent model and the child collection (add the new one remove the old not required ones and update what is updated). Excuse my code its just POC. Please help answer my query.

DB Config file

'use strict';

var knex = require('knex')({
    client: 'mysql',
    connection: {
        host     : 'localhost',
        user     : 'root',
        password : 'root',
        database : 'rocket-poc',
        charset  : 'utf8'
    }
});

module.exports = require('bookshelf')(knex);

Model Declarations

'use strict';

var bookshelf = require('./db-config');

bookshelf.plugin('bookshelf-manager');

exports.getOpportunityModel = function(){

    var Opportunity = bookshelf.Model.extend({
        tableName: 'opportunity',
        livingExpenses: function() {
            return this.hasMany(LivingExpenses);
        }
    });

    var LivingExpenses = bookshelf.Model.extend({
        tableName: 'living_expenses'
    });

    return {
        Opportunity:Opportunity,
        LivingExpenses:LivingExpenses

    };

};

The Consumer

'use strict';

var opty = require('./opportunityModel');

var bookshelf = require('./db-config');

bookshelf.plugin('bookshelf-manager');

exports.operations = function(){

    return new opty.getOpportunityModel().Opportunity.where('id', 1).fetch({withRelated: ['livingExpenses']})
        .then(function(opportunity){
            //console.log(opportunity.toJSON());
            var livingExp1 = {description: "some this", value: 900};
            var livingExp2 = {description: "some this", value: 1900};

            //var le = new opty.getOpportunityModel().LivingExpenses();

            //console.log(le);

            return opportunity;

        })
        .then(function(opportunity){
            //console.log(opportunity);

            var optyOriginal  = opportunity;

            //opportunity.livingExpenses().a;
            var livingExp1 = {description: "some this", value: 1900, opportunity_id: 1};
            var livingExp2 = {description: "some this", value: 2300, opportunity_id: 1};

            var optyEx = opportunity.related('livingExpenses').set([livingExp1, livingExp2]);

            console.log(opportunity.toJSON());

            //return opportunity.save(null, {withRelated: ['livingExpenses']});

            return bookshelf.manager.save(optyOriginal, optyEx.toJSON());





        }).then(function(model){

            console.log(JSON.stringify(model));

            return Promise.resolve("done");
        })
        .catch(function(err){
            console.error(err);
            return Promise.reject(err);
        });


};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant