Rod
Rod

Reputation: 15423

Is there a way to disable timestamp columns globally for all models?

So I can disable the timestamp columns for a particular model but is there a way to disable it for all models?

const Contract = sequelize.define('Contract', {
    idContract: {
        type: Sequelize.INTEGER,
        primaryKey: true
    },
    AccountNo_Lender: {
        type: Sequelize.STRING
    }
}, { timestamps: false });

Upvotes: 11

Views: 16602

Answers (2)

Josiah Nyarega
Josiah Nyarega

Reputation: 683

The Sequelize constructor takes a define option which will change the default options for all defined models.

const sequelize = new Sequelize(connectionURI, {
  define: {
    // The `timestamps` field specify whether or not the `createdAt` and `updatedAt` fields will be created.
    // This was true by default, but now is false by default
    timestamps: false
  }
});

// Here `timestamps` will be false, so the `createdAt` and `updatedAt` fields will not be created.
class Foo extends Model {}
Foo.init({ /* ... */ }, { sequelize });

// Here `timestamps` is directly set to true, so the `createdAt` and `updatedAt` fields will be created.
class Bar extends Model {}
Bar.init({ /* ... */ }, { sequelize, timestamps: true });

docs

Upvotes: 2

AbhinavD
AbhinavD

Reputation: 7282

You can define this when initializing the sequelize object.

const sequelize = new Sequelize('test', 'postgres', 'postgres', {
  host: '127.0.0.1',
  dialect: 'postgres',
  define: {
    timestamps: false
  },
});

Options under define are as same as when we define them inside the model. So all the options which can be used inside the model can be used here as well. From the docs

// Specify options, which are used when sequelize.define is called.
// The following example: // define: { timestamps: false } // is basically the same as: // sequelize.define(name, attributes, { timestamps: false }) // so defining the timestamps for each model will be not necessary

Upvotes: 24

Related Questions