⚠️ If you're new to using Sequelize with MariaDB be sure to check out the quickstart repository!
TODO is a web application that introduces you to the power, performance, and simplicity of MariaDB.
This 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.
This application is made of two parts:
- Client
- web UI that communicates with REST endpoints available through an API app (see below).
- is a React.js project located in the client folder.
- API
- uses the MariaDB Node.js Connector with Sequelize to connect to MariaDB.
- is a Node.js project located in the api folder.
This README will walk you through the steps for getting the TODO
web application up and running using MariaDB.
- Requirements
- Getting started with MariaDB
- Get the code
- Create the database and table
- Configure, build and run the app
- Support and contribution
- License
This sample application requires the following to be installed/enabled on your machine:
- Node.js (v. 12+)
- NPM (v. 6+)
- MariaDB command-line client (optional), used to connect to MariaDB database instances.
MariaDB is a community-developed, commercially supported relational database management system, and the database you'll be using for this application.
If you don't have a MariaDB database up and running you can find more information on how to download, install and start using a MariaDB database in the MariaDB Quickstart Guide.
First, use git (through CLI or a client) to retrieve the code using git clone
:
$ git clone https://github.com/mariadb-developers/todo-app-nodejs-sequelize.git
Next, because this repo uses a git submodule, you will need to pull the client application using:
$ git submodule update --init --recursive
Connect to your MariaDB database (from Step #2) and execute the following SQL scripts using the following options:
a.) Use the MariaDB command-line client to execute the SQL contained within schema.sql.
Example command:
$ mariadb --host HOST_ADDRESS --port PORT_NO --user USER --password PASSWORD < schema.sql
OR
b.) Copy, paste and execute the raw SQL commands contained in schema.sql using a client of your choice.
CREATE DATABASE todo;
CREATE TABLE todo.tasks (
id INT(11) unsigned NOT NULL AUTO_INCREMENT,
description VARCHAR(500) NOT NULL,
completed BOOLEAN NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);
This application is made of two parts:
- Client
- web UI that communicates with REST endpoints available through an API app (see below).
- is a React.js project located in the client folder.
- API
- uses the MariaDB Node.js Connector with Sequelize to connect to MariaDB.
- is a Node.js project located in the api folder.
Configure the MariaDB connection by adding an .env file to the Node.js project. Add the .env file here (the api
folder).
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
}
});
To start and run the API application you need to execute the following commands within the root folder.
- Install the Node.js packages (dependendies) for the app.
$ npm install
- Run the the app, which will expose API endpoints via http://localhost:8080.
$ npm start
c.) Build and run the UI (Client) app
Once the API project is running you can now communicate with the exposed endpoints directly (via HTTP requests) or with the application UI, which is contained with the client
folder of this repo.
To start the client
application follow the instructions here.
Please feel free to submit PR's, issues or requests to this project project directly.
If you have any other questions, comments, or looking for more information on MariaDB please check out:
Or reach out to us diretly via: