Files

TODO - Node.js API (with Sequelize)

This Node.js project uses the MariaDB Node.js connector in combination with the Sequelize object-relational mapping module to connect to and communicate to a MariaDB database instance.

  1. Requirements
  2. Getting started with the app
    1. Configure the code
    2. Build the code
    3. Run the app

Requirements

This sample was created using the following techologies and they must be installed before proceeding.

Getting started with the app

Configure the code

Configure the MariaDB connection by adding an .env file to the Node.js project.

Example implementation:

DB_HOST=<host_address>
DB_PORT=<port_number>
DB_USER=<username>
DB_PASS=<password>
DB_NAME=todo

Configuring db.js

The environmental variables from .env are used within the db.js for the MariaDB Node.js Connector configuration Sequelize settings:

const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASS, {
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  dialect: 'mariadb',
  define: {
    timestamps: false
  }
});

Configuring db.js for the MariaDB cloud database service SkySQL

MariaDB SkySQL requires SSL additions to dialectOptions within the Sequelize settings. It's as easy as 1-2-3 (steps below).

// 1.) Access the Node File System package
const fs = require("fs");

// 2.) Retrieve the Certificate Authority chain file (wherever you placed it - notice it's just in the Node project root here)
const serverCert = [fs.readFileSync("skysql_chain.pem", "utf8")];

const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASS, {
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  dialect: 'mariadb',
  // 3.) Add an "ssl" property to the dialectOptions configuration, using the serverCert const defined above
  dialectOptions: {
    ssl: {
      ca: serverCert
    }
  },
  define: {
    timestamps: false
  }
});

Build the code

Once you have retrieved a copy of the code you're ready to build and run the project! However, before running the code it's important to point out that the application uses several Node Packages.

Executing the CLI command

$ npm install

Doing this targets relative package.json file and install all dependencies.

IMPORTANT: Be sure that the Node modules are installed for the client. This can be done manually executing the following CLI command for client:

$ npm install

Run the app

Once you've pulled down the code and have verified that all of the required Node packages are installed you're ready to run the application!

  1. Execute the following CLI command
$ npm start

The following steps also exist within the "Build and run" section of the root README, and are for startin the React.js project once this API project has been started.

  1. Navigate to the ../../client folder and execute the following CLI command to start the React.js application.
$ npm start
  1. Open a browser window and navigate to http://localhost:3000.