Orders App with MariaDB Connector/Node.js

Orders is a sample web application for e-commerce. The application is compatible with MariaDB Connector/Node.js and MariaDB Platform for Transactions using the HA (Primary/Replica), Galera, or Transactional Standalone topologies on MariaDB SkySQL.

Orders demonstrates Online Transactional Processing (OLTP) by simulating online e-commerce traffic.

Orders Application

Orders has three components:

  • React.js front-end web application

  • Node.js-based back-end application service

  • MariaDB Platform for Transactions back-end database service, running on MariaDB SkySQL

Prerequisites

Clone the Repository

Orders is available from a MariaDB Corporation repository on GitHub. The repository contains the SQL code to initialize the database, the back-end application service code in Node.js and other languages, and the React.js front-end web application that connects to the back-end.

To download the Orders code, clone the repository from GitHub:

$ git clone https://github.com/mariadb-corporation/dev-example-orders.git

This creates a dev-example-orders directory on your file system.

Provision Database Services

Orders requires a back-end database service using the InnoDB storage engine. The back-end app is compatible with all topologies of MariaDB Platform for Transactions, including HA (Primary/Replica), Galera, and Transactional Standalone. For the purposes of demonstration, this guide uses the Transactional Standalone topology on MariaDB SkySQL.

For additional information, see "Launch".

Create the Database Schema

There is a create.sql script in the schema directory. This script configures the database schema for the Orders database back-end service.

Use the MariaDB Client to connect to the database service and initialize the schema:

$ mysql --host example.skysql.net --port 5001 \
      --user db_user --password \
      --ssl-ca skysql_chain.pem \
      < schema/create.sql

Install Back-end Application Service

The Orders front-end web application connects to the database service using a back-end application service. The repository contains different versions of the back-end app, allowing you to test and experiment with different languages and different MariaDB Connectors. These instructions are for Node.js using MariaDB Connector/Node.js.

Navigate to the correct api directory:

$ cd /path/to/dev-example-orders/api/nodejs

Configure the Back-end Application Service

To connect the back-end app to your MariaDB SkySQL database service, configure the back-end app to use the appropriate credentials:

  1. Create a .env file in the api/nodejs directory, set the variables to the login credentials for your MariaDB SkySQL service:

    DB_HOST_1 = example.skysql.net
    DB_USER_1 = db_user
    DB_PASS_1 = db_user_passwd
    DB_PORT_1 = 5001
    DB_NAME_1 = orders
    
  2. Edit the db.js file in the api/nodejs directory, uncomment the configuration variables for TLS connections:

    // SSL (e.g. SkySQL) connections
    // * Remember to change the location of "skysql_chain.pem" to wherever you placed it!
    // * To use just uncomment the two lines below and the 'ssl' property (and value) within the connection pool configuration
    
    const fs = require("fs");
    const serverCert = [fs.readFileSync("skysql_chain.pem", "utf8")];
    
  3. In setting the serverCert constant, adjust the path to point to the location of the skysql_chain.pem file on your file system:

    const serverCert = [fs.readFileSync("/path/to/skysql_chain.pem", "utf8")];
    
  4. In the mariadb.createPool() configuration, uncomment the ssl entry, so that MariaDB Connector/Node.js uses the CA certificate:

    var pools = [
      mariadb.createPool({
        host: process.env.DB_HOST_1,
        user: process.env.DB_USER_1,
        password: process.env.DB_PASS_1,
        port: process.env.DB_PORT_1,
        database: process.env.DB_NAME_1,
        multipleStatements: true,
        connectionLimit: 5,
        ssl: {
          ca: serverCert
        }
      })
    ];
    

Build the Back-end Application Service

To build the Node.js back-end application service:

$ npm install

Start the Back-end Application Service

To start the Node.js back-end application service:

$ npm start

The back-end application service is now running. The next step is to install and start the React.js web application to use Orders.

Install Front-end Web Application

Orders uses a React.js front-end web application. The front-end connects to the back-end application service and provides a web interface for the user.

To install and start the web app:

  1. Navigate to the client directory:

    $ cd /path/to/dev-example-orders/client/
    
  2. Use NPM to install the React.js web application:

    $ npm install
    
  3. Use NPM to start the React.js web application:

    $ npm start
    

React.js web application is now running. The web app is available through your web browser at http://localhost:3000.

Live Demonstration

When the front-end web application and the back-end application service are running, Orders simulates online e-commerce traffic on MariaDB Platform for Transactions.

Orders issues a series of read and write statements to the database service. Using the context menus in the web application, you can adjust the percentage of reads, percentage of writes, and variation between them.

Orders Application

Orders includes basic monitoring, noting the number of queries per second, the average latency, as well as charts showing the number of reads and writes executed per second and the transaction latency.