Skip to content

Instantly share code, notes, and snippets.

@tgriesser
Forked from stefanhenze/gist:9821183
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save tgriesser/9822343 to your computer and use it in GitHub Desktop.

Select an option

Save tgriesser/9822343 to your computer and use it in GitHub Desktop.

Revisions

  1. tgriesser revised this gist Mar 28, 2014. 1 changed file with 7 additions and 12 deletions.
    19 changes: 7 additions & 12 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -13,18 +13,13 @@ var Child = Bookshelf.Model.extend({
    }
    });

    var parent, child;

    Parent.forge({name: "Parent"}).save()
    .then(function (_parent) {
    parent = _parent;
    return Child.forge({name: "Child"}).save();
    })
    .then(function (_child) {
    child = _child;
    child.related('parent').set(parent);
    return child.save();
    .then(function (parent) {
    // or parent.child().save({name: 'Child'})
    // using .related just throws it on the
    // parent's .relations hash
    return parent.related('child').save({name: 'Child'});
    })
    .then(function(saved) {
    res.send(saved);
    .then(function (child) {
    res.send(child);
    });
  2. @stefanhenze stefanhenze created this gist Mar 27, 2014.
    30 changes: 30 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    var Parent = Bookshelf.Model.extend({
    tableName: 'parent',
    child: function() {
    return this.hasOne(Child);
    }
    });

    var Child = Bookshelf.Model.extend({
    tableName: 'child',
    //this has an attribute parent_id
    parent: function() {
    return this.belongsTo(Parent);
    }
    });

    var parent, child;

    Parent.forge({name: "Parent"}).save()
    .then(function (_parent) {
    parent = _parent;
    return Child.forge({name: "Child"}).save();
    })
    .then(function (_child) {
    child = _child;
    child.related('parent').set(parent);
    return child.save();
    })
    .then(function(saved) {
    res.send(saved);
    });