40

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
Ben
  • 458
  • 1
  • 5
  • 9
  • How about `new Date().toString()` – laggingreflex Feb 09 '15 at 16:31
  • @laggingreflex I could just use moment().format(), but I wanted to avoid later comparing a database time to a client side generated time. – Ben Feb 10 '15 at 15:52
  • Why don't you enable timestamps, so updatedAt will be updated if you simply call model.update({},{where:{my_primary_key:value}}) – Winster Sep 29 '16 at 11:00

3 Answers3

53

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
srlm
  • 3,186
  • 2
  • 27
  • 40
  • Thanks! sequelize.fn('NOW') is exactly what I needed. – Ben Feb 10 '15 at 15:52
  • 1
    is sequelize.fn('NOW(6)') supported for storing fractional seconds/microseconds? since Sequelize.DATE(6) datatype is now supported if we are using mysql >= 5.6 – saraf May 11 '16 at 10:50
  • 2
    OK: sequelize.fn('NOW', 6) is supported for storing fractional seconds/microseconds. – saraf May 11 '16 at 12:17
  • Note that this sets the instance's syncedAt attribute to `{fn: "NOW", args: []}` and not the current timestamp (since this is determined by the database). – mauvm Nov 23 '16 at 09:42
  • Good Job! Thanks. – unknown May 30 '21 at 02:47
36

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

user2643726
  • 461
  • 4
  • 2
3

you can use: sequelize.literal('CURRENT_TIMESTAMP'). Example:

await PurchaseModel.update( {purchase_date : sequelize.literal('CURRENT_TIMESTAMP') }, { where: {id: purchaseId} } );
Arthur Mastropietro
  • 673
  • 1
  • 7
  • 22