Sets the bookshelf.js attribute not in the database

I have a Bookshelf.js model. I want to be able to set and get attributes for this model that are not constant in the database.

For example, let's say I have a model that looks like this:

var Domain = bookshelf.Model.extend({ tableName: 'domains', initialize: function() { this.on('creating', this.setDomainName); }, setDomainName: function() { this.set('name', getDomainFromUrl(this.url)); } }); 

With a diagram that looks like this:

 knex.schema.createTable('domains', function (table) { table.increments().index(); table.text('name').index(); table.timestamps(); }); 

I want to save the url attribute and then parse the URL in the domain before saving it.

When I try something like this:

 new Domain({url: 'http://someurl.com/foo/bar'}).save() 

I get an error message:

 "column \"url\" of relation \"domains\" does not exist" 

I looked and looked. I cannot find a way to add mutable attributes to the bookshelf.js model. I also could not find anything about adding custom getter and setter methods to the bookshelf.js model.

Any help or understanding appreciated!

+5
source share
1 answer

Skip the short answer on my phone, but what you want is called "virtual" or "composite."

https://github.com/tgriesser/bookshelf/wiki/Plugin:-Virtuals

Each database blockbuster has them, but when you do not know what they are called, it is, for obvious reasons, difficult for Google to solve.

+6
source

All Articles