lundi 28 septembre 2015

Set future expiry date as default value in sequelize

I'm saving logintokens with a lifetime of 365 days using the following sequelize beforeCreate Hook:

let hooks = {
    setExpires: (instance, options, done) => {
        if(instance.get('expires')) {
            return done();
        }
        instance.set('expires', Sequelize.literal('NOW() + INTERVAL 1 YEAR'));
        return done();
    }
};

Logintoken.beforeCreate(hooks.setExpires);
Logintoken.beforeBulkCreate(hooks.setExpires);

It works great as long as I use MySQL. Other dialects such as SQLite don't understand NOW() + INTERVAL 1 YEAR, which is bad. Is there a built-in cross-dialect way to achieve what I am trying to do here?

I've already studied the docs, googled like hell and even had a look at the source code but couldn't find anything that looks like date calculation.

Aucun commentaire:

Enregistrer un commentaire