I am using SequelizeJS to create models for my nodejs application. I have my model definitions defined in a startup file and will sequelize.sync() upon application launch. My question is, I have many constraints in the data that is being added into the fields Eg: I have a field called 'source' which is of type Sequelize.ENUM and its values should only be one of these ['pod', 'must', 'cap']. In sqlite I have option to put these contraints in the DB directly. How do I do the same from sequelize? can it be done during sync()?
1 Answer
sequelize.define('model', {
source1: Sequelize.ENUM('pod', 'must', 'cap'),
source2: {
type: Sequelize.ENUM,
values: ['pod', 'must', 'cap']
}
The code above will create an enum type in sequelize, that is restricted to the values you gave. However, sqlite does not natively support enums, which means the field will just be created as text:
..., `status` TEXT, ...
BUT, the values will still be validated in javascript, before they are inserted.
In mysql / pg an actual enum, limited to the values will be created:
... `status` ENUM('pod', 'must', 'cap'), ...
Examples of using the enum type can also be found in the documentation at http://sequelizejs.com/documentation