Places is a web application backed by the power, performance, and simplicity of MariaDB, allows you to record all of your favorite locations using both structured (relational) and semi-structured (JSON) data!
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 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 Places
web application up and running using MariaDB.
- Requirements
- Getting started with MariaDB and JSON
- Get the code
- Configure, build and run the apps
- JSON Data Models
- Support and contribution
- License
This sample application requires the following to be installed/enabled on your machine:
- Node.js (v. 12+) (for the Client/UI app)
- NPM (v. 6+) (for the Client/UI app)
- MariaDB command-line client (optional), used to connect to MariaDB database instances.
Set up a MariaDB database, loaded with the data this sample needs, using the MariaDB JSON Quickstart, before continuing to the next step.
First, use git (through CLI or a client) to retrieve the code using git clone
:
Ts
$ git clone https://github.com/mariadb-developers/places-app-nodejs.git
Next, because this repo uses a git submodule, you will need to pull the client application using:
$ git submodule update --init --recursive
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 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=places
Configuring db.js
The environmental variables from .env
are used within the db.js for the MariaDB Node.js Connector configuration pool settings:
var mariadb = require('mariadb');
require('dotenv').config();
const pool = mariadb.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
port: process.env.DB_PORT,
database: process.env.DB_NAME,
multipleStatements: true
});
Configuring db.js for the MariaDB cloud database service SkySQL
MariaDB SkySQL requires SSL additions to connection. It's as easy as 1-2-3 (steps below).
var mariadb = require('mariadb');
require('dotenv').config();
// 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")];
var pool = mariadb.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
port: process.env.DB_PORT,
database: process.env.DB_NAME,
connectionLimit: 5,
// 3.) Add an "ssl" property to the connection pool configuration, using the serverCert const defined above
ssl: {
ca: serverCert
}
});
To start and run the API application you need to execute the following commands within the api 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.
Below are samples of the data model per Location Type.
Attraction (A)
{
"category":"Landmark",
"lastVisitDate":"11/5/2019"
}
Restuarant (R)
{
"details":{
"foodType":"Pizza",
"menu":"www.giodanos.com/menu"
},
"favorites":[
{
"description":"Classic Chicago",
"price":24.99
},
{
"description":"Salad",
"price":9.99
}
]
}
Sports Venue (S)
{
"details":{
"yearOpened":1994,
"capacity":23500
},
"events":[
{
"date":"10/18/2019",
"description":"Bulls vs Celtics"
},
{
"date":"10/21/2019",
"description":"Bulls vs Lakers"
},
{
"date":"11/5/2019",
"description":"Bulls vs Bucks"
},
{
"date":"11/5/2019",
"description":"Blackhawks vs Blues"
}
]
}
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: