Is it possible to have column names be underscored (postgres) but have the JavaScript getters be camelCase per language standards?
For anyone finding this later on it's now possible to explicitely define what the database field should be named:
var User = sequelize.define('user', {
isAdmin: {
type: DataTypes.BOOLEAN,
field: 'is_admin'
}
});
In Sequelize v5 you can now use the underscored: true
attribute
const User = sequelize.define('User', {
username: DataTypes.STRING,
password: DataTypes.STRING
}, {underscored: true});
https://sequelize.org/master/class/lib/model.js~Model.html#static-method-init
Not directly in your column definition, but you could take a look at getters and setters:
http://sequelizejs.com/documentation#models-getters---setters-defining-as-part-of-the-model-options
Although this options requires you to define a getter and setter for each column manually, it cannot be automated. Furthermore, both your getters and the actual column names will then be available on the object.
I think there is an issue for this functionality on github, but I cannot find it right now
actual link https://sequelize.org/master/manual/models-definition.html#getters--amp--setters
You can achieve this for both models (tables) and keys (fields) using a newer version of Sequelize.
I am using 4.29.2 and my models looks like this:
const Post = sequelize.define('post', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
},
isActive: {
type: DataTypes.BOOLEAN,
defaultValue: true,
allowNull: false,
field: 'is_active',
},
isDeleted: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
field: 'is_deleted',
},
}, {
indexes: [
{
unique: false,
fields: ['is_active'],
},
{
unique: false,
fields: ['is_deleted'],
},
],
defaultScope: {
where: {
isActive: true,
isDeleted: false,
},
},
});
const PostComments = sequelize.define('postComments', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
},
postId: {
type: DataTypes.UUID,
allowNull: false,
field: 'post_id',
},
comment: {
type: DataTypes.STRING,
allowNull: false,
},
}, {
tableName: 'post_comments',
indexes: [
{
unique: false,
fields: ['post_id'],
},
],
});
Post.hasMany(PostComments, {
foreignKey: 'postId',
constraints: true,
as: 'comments',
});
PostComments.belongsTo(Post, {
foreignKey: 'postId',
constraints: true,
as: 'post',
});
As you can see, I'm setting the tableName value for models (tables) and field values for keys (fields).
When I run:
sequelize.query('SET FOREIGN_KEY_CHECKS = 0', { raw: true })
.then(() => {
conn.sync({ force: true }).then(() => {
console.log('DONE');
});
});
The result is:
Executing (default): SET FOREIGN_KEY_CHECKS = 0
Executing (default): DROP TABLE IF EXISTS `post_comments`;
Executing (default): DROP TABLE IF EXISTS `post`;
Executing (default): CREATE TABLE IF NOT EXISTS `post` (`id` CHAR(36) BINARY NOT NULL , `is_active` TINYINT(1) NOT NULL DEFAULT true, `is_deleted` TINYINT(1) NOT NULL DEFAULT false, `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL, PRIMARY KEY (`id`) ENGINE=InnoDB;
Executing (default): CREATE TABLE IF NOT EXISTS `post_comments` (`id` CHAR(36) BINARY NOT NULL , `post_id` CHAR(36) BINARY NOT NULL, `comment` VARCHAR(255) NOT NULL, `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`post_id`) REFERENCES `post` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB;