Giter VIP home page Giter VIP logo

catbook's Introduction

Sequelize CLI and 1:many associations

Quickstart

$ npm install

# after db setup
$ npx nodemon app.js
# now, point your browser to localhost:3000

Database setup using sequelize-cli

First, initialize sequelize in your project using the CLI

$ npx sequelize-cli init

Then, update config/config.js so you can use env vars, and update the .env file with your env vars

// config/config.js
require('dotenv').config()

module.exports = {
  development: {
    username: process.env.DATABASE_USER,
    password: process.env.DATABASE_PASSWORD,
    database: process.env.DATABASE,
    host: '127.0.0.1',
    dialect: 'postgres'
  }
}

Now, create your database

$ createdb catbook # set up your development database

Generating models and migrations with the CLI

# To generate a `Cat` model with attributes `name`, `breed`, `owner`
$ npx sequelize-cli model:generate --name Cat --attributes name:string,breed:string,owner:string

# To run migrations
$ npx sequelize-cli db:migrate

Instructions

Associating Cats and Comments

Cat hasMany Comments, Comment belongsTo Cat

To create the table associations using sequelize,

  1. Use sequelize-cli to generate a model for Comment, with attributes text:string,CatId:integer
  2. Edit the generated migration file to specify that CatId is a foreign key by adding a reference to the Cats table primary key (id)
CatId: {
        type: Sequelize.INTEGER,
        references: {
          model: {
            tableName: 'Cats'
          },
          key: 'id'
        },
        allowNull: false
      }
  1. Run the migration
  2. Set properties on the model with values of the invoked association methods so that sequelize associates the models
// models/cat.js
class Cat extends Model {
  /**
   * Helper method for defining associations.
   * This method is not a part of Sequelize lifecycle.
   * The `models/index` file will call this method automatically.
   */
  static associate(models) {
    this.Comments = this.hasMany(models.Comment, { onDelete: 'cascade' })
  }
}
// models/comment.js
class Comment extends Model {
  /**
   * Helper method for defining associations.
   * This method is not a part of Sequelize lifecycle.
   * The `models/index` file will call this method automatically.
   */
  static associate(models) {
    this.Cat = this.belongsTo(models.Cat)
  }
}

Further

  • Use sequelize-cli to generate a model Owner with a name attribute and the corresponding migration file.
  • Associate the models and tables in 1:many relationships.
An owner has many cats and has many comments
A cat belongs to an owner (this might not be representative given cats think owners belong to them, but this is just an exercise..)
A comment belongs to an owner

Resources

catbook's People

Contributors

dearshrewdwit avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.