Commercial ORM - part 1

Pre-requisites

Learning Objectives

Install and use commercial Object Relational Mapping (ORM) tools such as:

Lesson

As you saw in the previous lessons, it is possible to build an Object Relational Mapping (ORM) tool from scratch however it is time consuming and requires careful management of resources and database transactions. Your app will have a much more solid foundation (and require a lot less code to be written!) if it is built with a commercial ORM framework.

Installing your commercial ORM

// --- Install Sequelize by running: ---
`npm install sequelize`

// --- Create a file named `sequelize_index.js` with the following content: ---
const {Sequelize, DataTypes, Model} = require('sequelize');

const sequelize = new Sequelize('database', 'username', 'password', {
    dialect: 'sqlite',
    storage: './restaurants-seq.sqlite'
});

module.exports={sequelize, DataTypes, Model};

/* This file contains code that would otherwise be duplicated in the 
individual model classes. It sets up a connection to a SQLite database 
and imports Sequelize types. You can see that it would be very easy to 
specify a different type of database in the future just by changing this config */

TODO

Writing a test

Let's start with a failing test:

const {sequelize} = require('./sequelize_index');
const {Restaurant} = require('./Restaurant')

describe('Restaurant', () => {
    /**
     * This code will be run prior to any tests
     */
    beforeAll(async () => {
        /* the 'sync' method will create tables based on the model class
         - by setting 'force:true' the tables are recreated each time the 
         test suite is run */
        await sequelize.sync({ force: true });
    })

    test('can create a restaurant', async () => {
        /* this is the Sequelize code to create a Restaurant
           note that Sequelize is build using promises, so we use async await */
        const restaurant = await Restaurant.create({ name: 'Ronalds', image: 'http://some.image.url' })
        expect(restaurant.id).toBe(1)
    })
})
TODO

Defining the class-tabole mapping

Using the ORM we can define the mapping between the OO Class definition and the associated database table/

const {sequelize, DataTypes, Model} = require('./sequelize_index');

/**
 * In Sequelize, each class needs to extend the Sequelize 'Model' class
 */
class Restaurant extends Model {

    // add methods here

}

// define the names and data types for the table columns
Restaurant.init({
    name: DataTypes.STRING,
    image: DataTypes.STRING,
}, {
    sequelize,
    timestamps: false, // avoids a 'created_at' column appearing
});

module.exports = {
    Restaurant
};
TODO

Assignment

  1. Create Restaurant, Menu and Item classes to represent the UML diagram from the Basic SQL Commands
  2. Add code to map each class to a database table in a new database file (ignore relationships for now). Use the Restaurant class above as an example.
  3. Create a test based on the code above to check that your tables are created and that you can insert data into them.
  4. Commit your code into Github and share the link with your coach for review.

Assignment extension tasks

Additional resources

To support this lesson we recommend watching this video:
Sequelize: An Introduction

Additional Sequelize documentation can be found here:
Sequelize documentation