2.0.1 • Published 5 years ago

@enablo/sequelize v2.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

@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

TypeConventionExample
Table nameUpper_Snake_CaseUserProject (Always singluar)
Column nameslower_snake_casecreated_at
Fk Column nameUpper_Snake_CaseUserId
Index nameslower_snake_caseidx_user_id
Model namesUpperCamelCaseUser (Always singluar)
Model associationUpperCamelCaseUser.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 loaded Model.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) => {}
2.0.1

5 years ago

2.0.0

5 years ago

1.3.0

5 years ago

1.3.0-9

5 years ago

1.3.0-8

5 years ago

1.3.0-7

5 years ago

1.3.0-6

5 years ago

1.3.0-5

5 years ago

1.3.0-4

5 years ago

1.3.0-3

5 years ago

1.3.0-2

5 years ago

1.3.0-1

5 years ago

1.3.0-0

5 years ago

1.2.0-0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago