Skip to content

Instantly share code, notes, and snippets.

@mmyoji
Last active December 15, 2023 01:06
Show Gist options
  • Save mmyoji/d457bfa2da959c7227f933398f727fee to your computer and use it in GitHub Desktop.
Save mmyoji/d457bfa2da959c7227f933398f727fee to your computer and use it in GitHub Desktop.
timestamps behaviour on Sequelize v5

timestamps: boolean behaviours on Sequelize (v5)

  • MySQL v5.7
  • sequelize@5.22.3
const { Sequelize, DataTypes, literal } = require("sequelize");

const sequelize = new Sequelize({/* ... */});

const Test = sequelize.define(
  "tests",
  {
    id: {
      field: "id",
      type: DataTypes.BIGINT({ length: 20 }).UNSIGNED,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false,
    },
    createdAt: {
      field: "created_at",
      type: "TIMESTAMP",
      allowNull: false,
      defaultValue: literal("CURRENT_TIMESTAMP"),
    },
    updatedAt: {
      field: "updated_at",
      type: "TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
      allowNull: false,
      defaultValue: literal("CURRENT_TIMESTAMP"),
    },
  },
  {
    timestamps: true, // or false
  }
);

/**
 * @param {Date} d
 * @returns {number}
 */
const unix = (d) => Math.floor(d.getTime() / 1000);
const assert = require("node:assert");
(async () => {
{
const createdAt = new Date("2023-08-13 11:00:00");
const updatedAt = new Date("2023-09-10 10:00:00");
let noArgs;
let withArgs;
noArgs = Test.build({});
assert(noArgs.createdAt);
assert(!(noArgs.createdAt instanceof Date)); // unexpected
// returns `literal {"val":"CURRENT_TIMESTAMP"}`
assert(noArgs.updatedAt);
assert(!(noArgs.updatedAt instanceof Date)); // unexpected
// returns `literal {"val":"CURRENT_TIMESTAMP"}`
noArgs = await noArgs.save();
assert(noArgs.createdAt);
assert(!(noArgs.createdAt instanceof Date)); // unexpected
assert(noArgs.updatedAt);
assert(!(noArgs.updatedAt instanceof Date)); // unexpected
noArgs = await Test.findByPk(noArgs.id);
assert(noArgs.createdAt instanceof Date);
assert(noArgs.updatedAt instanceof Date);
withArgs = Test.build({ createdAt, updatedAt });
assert.equal(withArgs.createdAt, createdAt);
assert.equal(withArgs.updatedAt, updatedAt);
withArgs = await withArgs.save();
assert.equal(withArgs.createdAt, createdAt);
assert(withArgs.updatedAt);
assert(!(withArgs.updatedAt instanceof Date)); // unexpected
withArgs = await Test.findByPk(withArgs.id);
assert.equal(unix(withArgs.createdAt), unix(createdAt));
assert.notEqual(unix(withArgs.updatedAt), unix(updatedAt));
noArgs = Test.build({ id: noArgs.id }, { isNewRecord: false });
assert.equal(noArgs.createdAt, undefined);
assert.equal(noArgs.updatedAt, undefined);
noArgs = await noArgs.save();
assert.equal(noArgs.createdAt, undefined);
assert(noArgs.updatedAt);
assert(!(noArgs.updatedAt instanceof Date)); // unexpected
noArgs = await Test.findByPk(noArgs.id);
assert(unix(noArgs.createdAt) > unix(createdAt));
assert(unix(noArgs.updatedAt) > unix(updatedAt));
withArgs = Test.build(
{ id: withArgs.id, createdAt, updatedAt },
{ isNewRecord: false }
);
assert.equal(withArgs.createdAt, undefined); // unexpected
assert.equal(withArgs.updatedAt, undefined); // unexpected
withArgs = await withArgs.save();
assert.equal(withArgs.createdAt, undefined);
assert(withArgs.updatedAt);
assert(!(withArgs.updatedAt instanceof Date)); // unexpected
withArgs = await Test.findByPk(withArgs.id);
assert.equal(unix(withArgs.createdAt), unix(createdAt));
assert(unix(withArgs.updatedAt) > unix(updatedAt));
}
})();
const assert = require("node:assert");
(async () => {
{
const createdAt = new Date("2023-08-13 11:00:00");
const updatedAt = new Date("2023-09-10 10:00:00");
let noArgs;
let withArgs;
noArgs = Test.build({});
assert(noArgs.createdAt);
assert(!(noArgs.createdAt instanceof Date)); // unexpected
assert(noArgs.updatedAt);
assert(!(noArgs.updatedAt instanceof Date)); // unexpected
noArgs = await noArgs.save();
assert(noArgs.createdAt);
assert(!(noArgs.createdAt instanceof Date)); // unexpected
assert(noArgs.updatedAt);
assert(!(noArgs.updatedAt instanceof Date)); // unexpected
noArgs = await Test.findByPk(noArgs.id);
assert(noArgs.createdAt instanceof Date);
assert(noArgs.updatedAt instanceof Date);
withArgs = Test.build({ createdAt, updatedAt });
assert.equal(withArgs.createdAt, createdAt);
assert.equal(withArgs.updatedAt, updatedAt);
withArgs = await withArgs.save();
assert.equal(withArgs.createdAt, createdAt);
assert.equal(withArgs.updatedAt, updatedAt);
withArgs = await Test.findByPk(withArgs.id);
assert.equal(unix(withArgs.createdAt), unix(createdAt));
assert.equal(unix(withArgs.updatedAt), unix(updatedAt));
noArgs = Test.build({ id: noArgs.id }, { isNewRecord: false });
assert.equal(noArgs.createdAt, undefined);
assert.equal(noArgs.updatedAt, undefined);
noArgs = await noArgs.save();
assert.equal(noArgs.createdAt, undefined);
assert.equal(noArgs.updatedAt, undefined);
noArgs = await Test.findByPk(noArgs.id);
assert(unix(noArgs.createdAt) > unix(createdAt)); // unexpected
assert(unix(noArgs.updatedAt) > unix(updatedAt));
withArgs = Test.build(
{ id: withArgs.id, createdAt, updatedAt },
{ isNewRecord: false }
);
assert.equal(withArgs.createdAt, createdAt);
assert.equal(withArgs.updatedAt, updatedAt);
withArgs = await withArgs.save();
assert.equal(withArgs.createdAt, createdAt);
assert.equal(withArgs.updatedAt, updatedAt);
withArgs = await Test.findByPk(withArgs.id);
assert.equal(unix(withArgs.createdAt), unix(createdAt));
assert.equal(unix(withArgs.updatedAt), unix(updatedAt));
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment