Friday 5 May 2017

Node.js/Bookshelf - Mapping model where multiple columns in a table reference the ID of another table

I am using node.js with bookshelf as an ORM. I am a serious novice with this technology.

I have a situation where I have several columns in a database table. For the sake of this question, these columns shall be named 'sold_by_id', 'signed_off_by_id' and 'lead_developer_id', and are all columns that will reference a User table with an ID.

In other words, different User's in the system would at any point be associated with three different roles, not necessarily uniquely.

Going forward, I would need to be able to retrieve information in such ways as:

let soldByLastName = JobTicket.soldBy.get('last_name');

I've tried searching around and reading the documentation but I'm still very uncertain about how to achieve this. Obviously the below doesn't work and I'm aware that the second parameter is meant for the target table, but it illustrates the concept of what I'm trying to achieve.

// JobTicket.js
soldBy: function() {
    return this.belongsTo(User, 'sold_by_id');
},

signedOffBy: function() {
    return this.belongsTo(User, 'signed_off_by_id');
},

leadDeveloper: function() {
    return this.belongsTo(User, 'lead_developer_id');
}

Obviously I would need a corresponding set of methods in User.js

I'm not sure where to start, can anyone point me in the right direction??

Or am I just a total idiot? ^_^



via David

No comments:

Post a Comment