@enablo/sequelize v2.0.1
@enablo/sequelize
A small utility package that standardises our use of Sequelize
Usage
yarn add @enablo/sequelize sequelize
import Database from "@enablo/sequelize"
const db = new Database({
dialect: <database dialect>,
name: <database name>,
username: <database username>,
password: <database password>,
host: <database host>,
<Any other Sequelize option>
})
await db.loadModels("path/to/your/models/directory")
await db.runMigrations("path/to/your/migrations/directory")
const results = await db.YourModel.findAll()
To keep with Enablo conventions, the following default settings will be passed to Sequelize (but can be overwritter)
{
define: {
charset: "utf8mb4", // Ensure emoji support
collate: "utf8mb4_unicode_ci", // Ensure emoji support
timestamps: true,
freezeTableName: true,
underscored: true,
}
}
Convention
Type | Convention | Example |
---|---|---|
Table name | Upper_Snake_Case | UserProject (Always singluar) |
Column names | lower_snake_case | created_at |
Fk Column name | Upper_Snake_Case | UserId |
Index names | lower_snake_case | idx_user_id |
Model names | UpperCamelCase | User (Always singluar) |
Model association | UpperCamelCase | User.Projects (Pluralisation based upon join type) |
Why this? Sequelize's naming convention works up to a point - then it becomes annoying. For a good balance between Sequelize and raw queries we turn off most of Sequelize's magic.
Why singular Model names? We picked one and went with it - deal with it. Some common purals are crazy (eg Media -> Medium) Why singular Table names? To reduce cognitive overload Model names === Table names. This works for 99% of use cases. Why Upper_Snake_Case foreign column names? Its a Sequelize default and its too annoying to turn it off. No we don't want to maintain Sequelize plugins just for the sake of a naming convention. Why snake_case_column names? 1) Its the industry standard. SQL !== Javascript. 2) SQL requires you to quote spaces. 3) Keeps column names align with the rest of the database (indexs, constraints, etc)
Methods
loadModels(path: String): void
Loads all model files from a given directory. A couple of things to note:
- Write your models with an ES5 module export
module.exports = (sequelize, DataTypes) => { return Model }
- Add an
associate
function to your model to setup association after all models have been loadedModel.associate = ({ ...OtherModels }) => {}
- This function mutates the Database class and add each model as a public attribute
db.YourModel === YourModel
- Models are also available on the sequelize instance
db.sequelize.models.YourModel === YourModel
runMigrations(path: String): Promise
Runs the migrations using the migration files from the given directory
- Write your migrations with an ES5 module export
module.exports = (QueryInterface, Sequelize) => {}
runSeeders(path: String): Promise
Runs the seed files using the seeders files from the given directory
- Write your seeders with an ES5 module export
module.exports = (Models, Sequelize) => {}
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago