I'm trying to do something like the following:
model.updateAttributes({syncedAt: 'NOW()'});
Obviously, that doesn't work because it just gets passed as a string. I want to avoid passing a node constructed timestamp, because later I compare it to another 'ON UPDATE CURRENT_TIMESTAMP' field and the database and source could be running different times.
Is my only option to just make a database procedure and call that?
You can use Sequelize.fn
to wrap it appropriately:
instance.updateAttributes({syncedAt: sequelize.fn('NOW')});
Here's a full working example:
'use strict'; var Sequelize = require('sequelize'); var sequelize = new Sequelize(/*database*/'test', /*username*/'test', /*password*/'test', {host: 'localhost', dialect: 'postgres'}); var model = sequelize.define('model', { syncedAt: {type: Sequelize.DATE} }); sequelize.sync({force: true}) .then(function () { return model.create({}); }) .then(function () { return model.find({}); }) .then(function(instance){ return instance.updateAttributes({syncedAt: sequelize.fn('NOW')}); }) .then(function () { process.exit(0); }) .catch(function(err){ console.log('Caught error! ' + err); });
That produces
UPDATE "models" SET "syncedAt"=NOW(),"updatedAt"='2015-02-09 18:05:28.989 +00:00' WHERE "id"=1
Worth mentioning (for people coming here via search) that NOW() isn't standard and doesn't work on SQL server - so don't do this if you care about portability.
sequelize.literal('CURRENT_TIMESTAMP')
may work better
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With