Bookshelf.js - How to get "hidden" fields for just one request?
I have the following bookshelf model:
Bookshelf.model.extend({
tableName: 'users',
hidden: ['password']
}, {
async getBy(filter) {
return await this.query({where: filter}).fetch();
}
})
As you can see, the field is password
hidden (because I usually don't want it to show).
But I need it to connect my user (when comparing the hash):
const user = await userModel.getBy({email: req.body.email});
if (await bcrypt.compare(req.body.password, user.password)) {
// here user.password is undefined because it is hidden
}
Is there a way to reduce the visibility of the plugin and get the password without having to do something like direct use knex
( Bookshelf.knex.raw()
)?
Respectfully,
source to share
Ok, since I found a solution, I'll answer my own question and hope it helps some people:
14 days ago (30/06/2017) issue # 1379 was merged.
It provides the following functional function:
Adds the ability to override parameters specified during binder with parameters specified directly in JSON.
Here is a commit, tests show how it works.
In my case, I am doing the following:
const user = (await userModel.getBy({email: req.body.email})).toJSON({hidden: []});
hidden
is an empty array, so it overrides the previous hidden property ( hidden: ['password']
) and outputs passwords.
source to share