Configuration

You can also influence the way Sequelize handles your column names:

  1. class Bar extends Model {}
  2. Bar.init({ /* bla */ }, {
  3. // The name of the model. The model will be stored in `sequelize.models` under this name.
  4. // This defaults to class name i.e. Bar in this case. This will control name of auto-generated
  5. // foreignKey and association naming
  6. modelName: 'bar',
  7. // don't add the timestamp attributes (updatedAt, createdAt)
  8. timestamps: false,
  9. // don't delete database entries but set the newly added attribute deletedAt
  10. // to the current date (when deletion was done). paranoid will only work if
  11. // timestamps are enabled
  12. paranoid: true,
  13. // Will automatically set field option for all attributes to snake cased name.
  14. // Does not override attribute with field option already defined
  15. underscored: true,
  16. // disable the modification of table names; By default, sequelize will automatically
  17. // transform all passed model names (first parameter of define) into plural.
  18. // if you don't want that, set the following
  19. freezeTableName: true,
  20. // define the table's name
  21. tableName: 'my_very_custom_table_name',
  22. // Enable optimistic locking. When enabled, sequelize will add a version count attribute
  23. // to the model and throw an OptimisticLockingError error when stale instances are saved.
  24. // Set to true or a string with the attribute name you want to use to enable.
  25. version: true,
  26. // Sequelize instance
  27. sequelize,
  28. })

If you want sequelize to handle timestamps, but only want some of them, or want your timestamps to be called something else, you can override each column individually:

  1. class Foo extends Model {}
  2. Foo.init({ /* bla */ }, {
  3. // don't forget to enable timestamps!
  4. timestamps: true,
  5. // I don't want createdAt
  6. createdAt: false,
  7. // I want updatedAt to actually be called updateTimestamp
  8. updatedAt: 'updateTimestamp',
  9. // And deletedAt to be called destroyTime (remember to enable paranoid for this to work)
  10. deletedAt: 'destroyTime',
  11. paranoid: true,
  12. sequelize,
  13. })

You can also change the database engine, e.g. to MyISAM. InnoDB is the default.

  1. class Person extends Model {}
  2. Person.init({ /* attributes */ }, {
  3. engine: 'MYISAM',
  4. sequelize
  5. })
  6. // or globally
  7. const sequelize = new Sequelize(db, user, pw, {
  8. define: { engine: 'MYISAM' }
  9. })

Finally you can specify a comment for the table in MySQL and PG

  1. class Person extends Model {}
  2. Person.init({ /* attributes */ }, {
  3. comment: "I'm a table comment!",
  4. sequelize
  5. })