Created
April 7, 2016 17:22
-
-
Save jomaora/fdbbb88edef5b2dca81675136f716657 to your computer and use it in GitHub Desktop.
Parsing MySQL TINYINT to Boolean using Bookshelf NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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