Waterline models
Default attributes
Summary
Sails.js includes default attributes (id
, createdAt
, and updatedAt
) in every model unless explicitly overridden. These are defined in config/models.js
, promoting code reusability (DRY principle). Developers can disable or modify default attributes for specific models by setting them to false
or redefining them in the model's attributes
configuration. This provides flexibility while maintaining consistency across most models.
Transcript
Hey, so let's talk about some of the attributes you might be seeing. You might wonder: "I didn't specify these attributes in my model's attributes
property. How come they’re in my model and database?" Let’s break this down.
Sails supports the concept of default attributes. These are attributes present in every model you define. To see where they’re configured, open config/models.js
. You’ll find:
attributes: {
id: { type: 'number', autoIncrement: true },
createdAt: { type: 'number', autoCreatedAt: true },
updatedAt: { type: 'number', autoUpdatedAt: true }
}
These defaults avoid repetitive definitions across models. For example, id
, createdAt
, and updatedAt
are automatically included.
To override these for a specific model (e.g., Product
), set the attribute to false
or redefine it:
// In api/models/Product.js
attributes: {
updatedAt: false, // Disables the default `updatedAt`
id: { type: 'string', isUUID: true, required: true }
}
After updating, run sails lift
and check your database schema—you’ll see updatedAt
removed for Product
.
Most use cases retain default attributes, but Sails provides flexibility. Override them globally in config/models.js
or per-model as needed.
Key Takeaways:
Default attributes streamline common columns (DRY principle).
Override by setting an attribute to
false
or redefining it.90% of projects retain defaults, but customization is straightforward.
Full Course
USD