Giter VIP home page Giter VIP logo

sequelize-bounties's Introduction

Sequelize Bounty Hunters

Intergalactic Bounty Hunter Database

You've been going to meetup events and networking. You've been telling everyone you're so excited to get a dev job that you'll take any job.

You run into a shadowy stranger, who asks you three times 'Really? Any job?' and you continue to agree enthusiastically. Things go dark, and you wake up in a strange place.

The shadowy stranger greets you and says 'Welcome to your new job! You are now our dev who will be building an intergalactic bounty hunter database for us!'

You look around, notice some high end coffee and tea machines, an air hockey table, nap rooms and floor to ceiling windows with a view of outer space. The shadowy stranger takes you to your desk which has a fancy sit-to stand adjustable hight desk with a swing bar, two big monitors, and Herman Miller chair. You say to yourself 'Not bad! Not bad at all!'

Basic setup

We will be building a database with two tables: bounties and hunters. Here are the columns they should have:

  • bounties

    • name: text
    • wantedFor: text
    • reward: integer
    • captured: boolean
  • hunters

    • name: text
    • client: text
    • active: boolean

Here's how to proceed:

  1. In this folder, follow steps 1 - 8 from the Make your own table section of https://github.com/SEI-SEA-1-25/intro-to-sequelize.
  2. Use steps 9 - 12 to create the bounties table, and then use them again to create the hunters table.

Basic CRUD

Make a dbTest.js file to run the following steps from. After you finish each step, you can comment it out as you move onto the next one.

  1. Make a bounty with a name of Han Solo, wantedFor of Owing Money, reward of 500000, and captured of false.
  2. Make a hunter with a name of Bobba Fett, a client of Jabba the Hut, and an active of true.
  3. Make a hunter with a name of Dengar, a client of Mercurial Swift, and an active of false. Try to do this using findOrCreate (knowing that there isn't one to find, and that sequelize will create it).
  4. Find all bounties, assign them to a variable, and console.log them.
  5. Find all hunters with active = true, assign them to a variable, and console.log them.
  6. Modify Han Solo to have a captured of true.
  7. Delete Dengar. Nobody remembers this guy anyway.
  8. Ok recreate Dengar. The deletion was just for practice. We'll actually need him later.

Commit after finishing this section!

Creating a relationship

We want to create a relationship in which a bounty can have many hunters hunting them, and a hunter can be hunting one bounty. In order to create a relationship, we need to make a change on the db side (using a migration), and a separate change on the js side (in the model file).

The migration side:

Answer the following questions:

Is this relationship 1-to-many or many-to-many? 1-to-many: a bounty can have many hunters, but a hunter belongs to just 1 bounty.
For this kind of relationship, do you need a foreign key or a join table? Foreign key: join tables are for m-t-m relationships.
Which table should get the foreign key? hunters: the child entity always gets the foriegn key in a 1-t-m relationship.
What should the foreign key be called? bountyId: this convention is what sequelize is expecting, where the name of the parent table is singularized and the whole word is camel cased.
What data type should the foreign key be? integer: it references the id column of bounties, which is an integer. So this column should also be an integer.

With these answers in mind, we'll add the column using these steps:

  1. Generate a new migration with sequelize migration:generate --name=add-bountyId-to-hunters. Note that we're not generating a model here, because we don't want to make a new table. We just want to make a single change to an existing table.
  2. This migration starts off empty, it's a blank vehicle for us to send commands to our database. Underneath the section that says "Add altering commands here", we will put the command to add our foreign key column. You can find the syntax for this command here https://sequelize.org/master/manual/query-interface.html, look at the Adding a Column to a Table section.
Spoiler for what should go in the Add altering commands here section of your migration await queryInterface.addColumn('hunters', 'bountyId', { type: Sequelize.DataTypes.INTEGER })
  1. Run this migration with sequelize db:migrate, then look at the table in psql to confirm that the column was added.

The model side

For the parent model, go to its model file and find the association block. Inside it, add

models.<parent-name>.hasMany(models.<child-name>)

Remember that <parent-name> and <child-name> both must match the modelName from the parent & child model files!

Then, in the child model's association block, add the other half of the 1-t-m relationship:

models.<child-name>.belongsTo(models.<parent-name>)

Using our association

Note that if these aren't working, you should check your associations from the previous section!

Back in our dbTest.js file, complete these one at a time:

  1. Look up Han Solo and save him to a variable. Look up Bobba Fett and assign him to a variable too. Associate the two with hanSolo.addHunters(bobbaFett)
  2. Look up Han Solo, save him to a variable. Look up his hunters with hanSolo.getHunters() and save the result to a variable, then log that variable.
  3. Look up Bobba Fett, save him to a variable. Look up his bounty with bobbaFett.getBounty(), and log that variable.
  4. Add Dengar to Han's hunters, the same way we did in step 1.
  5. Repeat step 2, and the list should now include both Bobba Fett and Dengar.
  6. Repeat step 3, but with Dengar instead of Bobba Fett.

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.