Search code examples
node.jspostgresqlone-to-manyknex.jsbookshelf.js

How to output a 1 to many object using bookshelf js


I am using bookshelf and knex to connect to a PostgreSQL database. Im trying to retrieve data that has a one to many relationship. My database looks like this:

Table: athlete

|----------------------------|
|  id | firstname | lastname |
|----------------------------|
|  0  | john      | doe      |
|  1  | jane      | doe      |
|----------------------------|

Table: activity

|------------------------------------|
|  id | athlete_id | type | distance |
|------------------------------------|
|  0  |     1      | run  |    5     |
|  1  |     0      | walk |    7     |
|------------------------------------|

My bookshelf models look like this:

const Athlete = bookshelf.Model.extend({
    tableName: 'athlete',
    activities: function() {
        return this.hasMany('Activity', 'athlete_id');
    }
});

const Activity = bookshelf.Model.extend({
    tableName: 'activity',
    athlete: function() {
        return this.belongsTo('Athlete');
    }
});

Then I call Activity.fetchAll().then(...)

This returns

[
  {
    "id": "0",
    "athlete_id": "1",
    "type": "run",
    "distance": "5",
  },
  {
    "id": "1",
    "athlete_id": "0",
    "type": "walk",
    "distance": "7",
  }
]

What I want it to return is

[
  {
    "id": "0",
    "athlete": {
        "athlete_id": "1",
        "firstname": "jane",
        "lastname": "doe"
    },
    "type": "run",
    "distance": "5",
  },
  {
    "id": "1",
    "athlete": {
        "athlete_id": "0",
        "firstname": "john"
        "lastname": "doe"
    },
    "type": "walk",
    "distance": "7",
  }
]

I found this: Activity.fetch({withRelated: 'athlete'}).then(...) but that returns a 500 error for me with no message.

I need help trying to return a nested object.


Solution

  • You are missing a pair of square brackets around athlete. That could be a possible thing causing this error.

    Activity.fetch({withRelated: ['athlete']}).then(...)
    

    EDIT

    Hey @Derekedelaney, I tried to implement the same project and did not face any issues. You can find it here. I got the output like this

    [
      { id: 1,
        athlete_id: 1,
        type: 'run',
        distance: '5',
        athlete: { id: 1, firstname: 'john', lastname: 'doe' } 
      },
      { id: 2,
        athlete_id: 2,
        type: 'walk',
        distance: '7',
        athlete: { id: 2, firstname: 'jane', lastname: 'doe' }
      } 
    ]
    

    Please note that I am using Bookshelf registry plugin so go through that once. Let me know if you have any difficulty.