Sequelize ORM model definition patterns and best practices for PostgreSQL. Use when defining models, setting up associations, configuring validations, or optimizing Sequelize usage.
View on GitHubfrancanete/fran-marketplace
database-expert
database-expert/skills/sequelize-models/SKILL.md
January 20, 2026
Select agents to install to:
npx add-skill https://github.com/francanete/fran-marketplace/blob/main/database-expert/skills/sequelize-models/SKILL.md -a claude-code --skill sequelize-modelsInstallation paths:
.claude/skills/sequelize-models/# Sequelize Model Patterns
## Model Definition
### Basic Model Structure
```javascript
const { DataTypes, Model } = require('sequelize');
class User extends Model {
// Instance methods
getFullName() {
return `${this.firstName} ${this.lastName}`;
}
// Class methods
static async findByEmail(email) {
return this.findOne({ where: { email: email.toLowerCase() } });
}
}
User.init({
id: {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
},
email: {
type: DataTypes.TEXT,
allowNull: false,
unique: true,
validate: {
isEmail: true,
notEmpty: true,
},
set(value) {
this.setDataValue('email', value.toLowerCase());
},
},
firstName: {
type: DataTypes.TEXT,
allowNull: false,
field: 'first_name', // Maps to snake_case column
},
lastName: {
type: DataTypes.TEXT,
allowNull: false,
field: 'last_name',
},
status: {
type: DataTypes.TEXT,
allowNull: false,
defaultValue: 'active',
validate: {
isIn: [['active', 'inactive', 'suspended']],
},
},
metadata: {
type: DataTypes.JSONB,
allowNull: false,
defaultValue: {},
},
}, {
sequelize,
modelName: 'User',
tableName: 'users',
underscored: true, // Automatically use snake_case for columns
timestamps: true, // Adds createdAt, updatedAt
paranoid: false, // Set true for soft deletes (adds deletedAt)
indexes: [
{ fields: ['email'], unique: true },
{ fields: ['status'] },
{ fields: ['created_at'] },
],
});
module.exports = User;
```
## Data Type Mappings
### PostgreSQL ↔ Sequelize
| PostgreSQL | Sequelize DataType | Notes |
|------------|-------------------|-------|
| `BIGINT` | `DataTypes.BIGINT` | Use for IDs and large numbers |
| `INTEGER` | `DataTypes.INTEGER` | Standard integers |
| `NUMERIC(p,s)` | `DataTypes.DECIMAL(p,s)` | Money, precise decimals |
| `DOUBLE PRECISION` | `DataTypes.DOUBLE` | Floating point |
| `TEXT` | `DataTypes.T