Now we can get ready to run the Notes application using Sequelize. We can run this against both SQLite3 and MySQL, but let's start with SQLite. Add this entry to the scripts entry in package.json:
"start-sequelize": "SEQUELIZE_CONNECT=models/sequelize-sqlite.yaml NOTES_MODEL=sequelize node --experimental-modules ./bin/www.mjs"
Then run it as follows:
$ DEBUG=notes:* npm run start-sequelize
> notes@0.0.0 start-sequelize /Users/david/chap07/notes
> SEQUELIZE_CONNECT=models/sequelize-sqlite.yaml NOTES_MODEL=sequelize node --experimental-modules./bin/www.mjs
notes:server Listening on port 3000 +0ms
As before, the application looks exactly the same because we've not changed the View templates or CSS files. Put it through its paces and everything should work.
With Sequelize, multiple Notes application instances is as simple as adding these lines to the scripts section of package.json, and then starting both instances as before:
"server1-sequelize": "SEQUELIZE_CONNECT=models/sequelize-sqlite.yaml NOTES_MODEL=sequelize PORT=3001 node --experimental-modules ./bin/www.mjs",
"server2-sequelize": "SEQUELIZE_CONNECT=models/sequelize-sqlite.yaml NOTES_MODEL=sequelize PORT=3002 node --experimental-modules ./bin/www.mjs",
You will be able to start both instances, use separate browser windows to visit both instances, and see that they show the same set of notes.
To reiterate using the Sequelize-based model on a given database server:
- Install and provision the database server instance, or else get the connection parameters for an already-provisioned database server.
- Install the corresponding Node.js driver.
- Write a YAML configuration file corresponding to the connection parameters.
- Create new scripts entries in package.json to automate starting Notes against that database.