Skip to content

Instantly share code, notes, and snippets.

@jomaora
Created April 7, 2016 17:22
Show Gist options
  • Save jomaora/fdbbb88edef5b2dca81675136f716657 to your computer and use it in GitHub Desktop.
Save jomaora/fdbbb88edef5b2dca81675136f716657 to your computer and use it in GitHub Desktop.
Parsing MySQL TINYINT to Boolean using Bookshelf NodeJS
'use strict';
const _ = require('lodash');
const bookshelf = require('../bookshelf');
const model = bookshelf.Model.extend(
{
parse: function (attrs) {
_.forEach(this.booleanFields || [], (field) => {
attrs[field] = !!attrs[field];
});
return attrs;
}
}, {})
;
module.exports = bookshelf.model('BaseModel', model);
'use strict';
const bookshelf = require('../bookshelf');
require('./basemodel');
const user = bookshelf.model('BaseModel').extend(
{
tableName: 'user',
hasTimestamps: ['creation_date', 'update_date'],
booleanFields: ['is_active', 'is_synchronized']
},
{}
);
module.exports = bookshelf.model('user', user);
CREATE TABLE IF NOT EXISTS user (
id bigint(20) NOT NULL AUTO_INCREMENT,
creation_date datetime DEFAULT NULL,
update_date datetime DEFAULT NULL,
email varchar(255) NOT NULL,
first_name varchar(50) DEFAULT NULL,
last_name varchar(50) DEFAULT NULL,
is_active tinyint(1) DEFAULT NULL,
is_synchronized tinyint(1) DEFAULT NULL
-- keys
PRIMARY KEY (id),
UNIQUE KEY idx_user_email (email)
) ENGINE=InnoDB;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment