(2 minute read)
I've created a patch for Sequelize which enables one to use primary key fields as foreign keys too, something I find myself doing often with my relational schemas as I don't like using the standard id primary key field unless it's the most sensible primary key to have. In practice the changes mean you can do the following (not currently possible in vanilla Sequelize):
var User = sequelize.define('user', {
name: Sequelize.STRING
}, {
underscored: true
})
var Profile = sequelize.define('profile', {
user_id: {
type: Sequelize.INTEGER,
primaryKey: true
},
dob: Sequelize.DATE
}, {
underscored: true
})
var Profile2 = sequelize.define('profile2', {
dob: Sequelize.DATE
}, {
underscored: true
})
User.hasOne(Profile)
User.hasOne(Profile2)
/*
At this point Profile.user_id will act as both primary and foreign key, whereas Profile2.user_id and Profile.id will have been auto-added by Sequelize and will act as foreign key and primary key respectively.
*/
Date.js has been causing me a number of problems due to its nature of extending/modifying the built-in Date object. It really messed up my use of cookie sessions in Express, specifically to do with setting the cookie expiry dates. I still haven't figured out exactly why cookies weren't getting set properly but I suspect that it's some internal change to Date made by date.js which was causing the problem. I then also had problems with it on the browser side of things.
Looking for an alternative I'm happy to have found XDate, a more light-weight and better-designed date library than date.js. It wraps the default Date object whilst still exposing an identical API as well as providing its own. The nice thing about XDate is that its custom API methods are almost identical to those provided by date.js so switching one out for the other didn't require any massive code changes.
XDate was very recently created and it's on Github too so I expect it will only get better with time. There isn't a node module for it yet, as far as I'm aware. Then again, I've just had a look and found Moment, another nice-looking date library available through NPM. I might give that a whirl for my server side.