Skip to content

Instantly share code, notes, and snippets.

@khumphrey
Last active January 28, 2018 02:33
Show Gist options
  • Save khumphrey/e525b7feced2d7acf1d041aca9b23168 to your computer and use it in GitHub Desktop.
Save khumphrey/e525b7feced2d7acf1d041aca9b23168 to your computer and use it in GitHub Desktop.

Models

  • Creating a db instance with new Sequelize and a connection string
  • Creating models with db.define(modelname, fields, options)
    • Specifying schema fields (attributes)
    • Specifying attribute types, e.g. Sequelize.STRING
    • Specifying attribute validations, e.g. allowNull
    • Specifying attribute defaultValues
    • Specifying model options
      • Getters & Setters (aka virtuals)
      • Hooks, e.g. beforeValidate
      • Class methods
      • Instance methods
      • this value in custom methods
        • getters: the instance
        • hooks: the model (instance is 1st arg of the hook func)
        • Instance methods: instance
        • Class methods: class
    • Associating models, e.g. hasOne, belongsTo, etc.
      • Which model has the foreignKey
      • Which Sequelize model is given new methods
    • Synchronizing models with db.sync() -- what does the option force: true do?

Routes

  • app.use vs app.all vs app.get vs app.post vs app.put vs app.delete
  • How to interact with data from the request
    • req.param vs req.query vs req.body (URI match vs ? that Express parses vs body-parser, respectively)
  • Using models in an Express app
    • Exporting and requiring models
    • Using a model within a route to query, creations, updates or deletions
      • Model.create, instance.destroy, etc.
      • More complex queries
        • Using $in, $ne, and other operators
        • Using Eager Loading (the include syntax) to do joins (i.e. populate assocations)
  • Handling asynchronicity (from Sequelize methods, but really any promises)
    • Using next with error handling
    • Sending a response once the async data is fetched
    • Error handling middleware (4 parameters - error, request, response, next)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment