|
| 1 | +# Places |
| 2 | + |
| 3 | +**Places** is a web application backed by the power of the power, performance, and simplicity of [MariaDB](https://mariadb.com/), allows you to record all of your favorite locations using both structured (relational) and semi-structured (JSON) data! |
| 4 | + |
| 5 | +<p align="center" spacing="10"> |
| 6 | + <kbd> |
| 7 | + <img src="media/map.png" /> |
| 8 | + </kbd> |
| 9 | +</p> |
| 10 | + |
| 11 | +This application is made of two parts: |
| 12 | + |
| 13 | +* Client |
| 14 | + - web UI that communicates with REST endpoints available through an API app (see below). |
| 15 | + - is a React.js project located in the [client](src/client) folder. |
| 16 | +* API |
| 17 | + - uses the [MariaDB Node.js Connector](https://github.com/mariadb-corporation/mariadb-connector-nodejs) to connect to MariaDB. |
| 18 | + - is a Node.js project located in the [api](src/api) folder. |
| 19 | + |
| 20 | +This README will walk you through the steps for getting the `Places` web application up and running using MariaDB. |
| 21 | + |
| 22 | +# Table of Contents |
| 23 | +1. [Requirements](#requirements) |
| 24 | +2. [Getting started with MariaDB and JSON](#mariadb) |
| 25 | +2. [Get the code](#code) |
| 26 | +4. [Configure, build and run the apps](#app) |
| 27 | + 1. [Configure](#configure-api-app) |
| 28 | + 2. [Create and activate a Python virtual environment](#activate-virtual-env) |
| 29 | + 3. [Install Python packages](#install-python-packages) |
| 30 | + 4. [Build and run the Python API app](#build-run-api) |
| 31 | + 5. [Build and run the Client app](#build-run-client) |
| 32 | +5. [JSON Data Models](#data-models) |
| 33 | +6. [Support and contribution](#support-contribution) |
| 34 | +7. [License](#license) |
| 35 | + |
| 36 | +## Requirements <a name="requirements"></a> |
| 37 | + |
| 38 | +This sample application requires the following to be installed/enabled on your machine: |
| 39 | + |
| 40 | +* [Node.js (v. 12+)](https://nodejs.org/docs/latest-v12.x/api/index.html) (for the Client/UI app) |
| 41 | +* [NPM (v. 6+)](https://docs.npmjs.com/) (for the Client/UI app) |
| 42 | +* [MariaDB command-line client](https://mariadb.com/products/skysql/docs/clients/mariadb-clients/mariadb-client/) (optional), used to connect to MariaDB database instances. |
| 43 | + |
| 44 | +## 1.) Getting Started with MariaDB and JSON <a name="mariadb"></a> |
| 45 | + |
| 46 | +Set up a MariaDB database, loaded with the data this sample needs, using the [MariaDB JSON Quickstart](https://github.com/mariadb-developers/mariadb-json-quickstart), before continuing to the next step. |
| 47 | + |
| 48 | +## 2.) Get the code <a name="code"></a> |
| 49 | + |
| 50 | +First, use [git](git-scm.org) (through CLI or a client) to retrieve the code using `git clone`: |
| 51 | +Ts |
| 52 | +``` |
| 53 | +$ git clone https://github.com/mariadb-developers/places-app-nodejs.git |
| 54 | +``` |
| 55 | + |
| 56 | +Next, because this repo uses a [git submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules), you will need to pull the [client application](https://github.com/mariadb-developers/todo-app-client) using: |
| 57 | + |
| 58 | +```bash |
| 59 | +$ git submodule update --init --recursive |
| 60 | +``` |
| 61 | + |
| 62 | +## 3.) Configure, Build and Run the App <a name="app"></a> |
| 63 | + |
| 64 | +This application is made of two parts: |
| 65 | + |
| 66 | +* Client |
| 67 | + - web UI that communicates with REST endpoints available through an API app (see below). |
| 68 | + - is a React.js project located in the [client](src/client) folder. |
| 69 | +* API |
| 70 | + - uses the [MariaDB Node.js Connector](https://github.com/mariadb-corporation/mariadb-connector-nodejs) to connect to MariaDB. |
| 71 | + - is a Node.js project located in the [api](src/api) folder. |
| 72 | + |
| 73 | +### a.) Configure the app <a name="configure-app"></a> |
| 74 | + |
| 75 | +Configure the MariaDB connection by [adding an .env file to the Node.js project](https://github.com/mariadb-corporation/mariadb-connector-nodejs/blob/master/documentation/promise-api.md#security-consideration). Add the .env file [here](src/api) (the `api` folder). |
| 76 | + |
| 77 | +Example implementation: |
| 78 | + |
| 79 | +``` |
| 80 | +DB_HOST=<host_address> |
| 81 | +DB_PORT=<port_number> |
| 82 | +DB_USER=<username> |
| 83 | +DB_PASS=<password> |
| 84 | +DB_NAME=places |
| 85 | +``` |
| 86 | + |
| 87 | +**Configuring db.js** |
| 88 | + |
| 89 | +The environmental variables from `.env` are used within the [db.js](src/api/db.js) for the MariaDB Node.js Connector configuration pool settings: |
| 90 | + |
| 91 | +```javascript |
| 92 | +var mariadb = require('mariadb'); |
| 93 | +require('dotenv').config(); |
| 94 | + |
| 95 | +const pool = mariadb.createPool({ |
| 96 | + host: process.env.DB_HOST, |
| 97 | + user: process.env.DB_USER, |
| 98 | + password: process.env.DB_PASS, |
| 99 | + port: process.env.DB_PORT, |
| 100 | + database: process.env.DB_NAME, |
| 101 | + multipleStatements: true |
| 102 | +}); |
| 103 | +``` |
| 104 | + |
| 105 | +**Configuring db.js for the MariaDB cloud database service [SkySQL](https://mariadb.com/products/skysql/)** |
| 106 | + |
| 107 | +MariaDB SkySQL requires SSL additions to connection. It's as easy as 1-2-3 (steps below). |
| 108 | + |
| 109 | +```javascript |
| 110 | +var mariadb = require('mariadb'); |
| 111 | +require('dotenv').config(); |
| 112 | + |
| 113 | +// 1.) Access the Node File System package |
| 114 | +const fs = require("fs"); |
| 115 | + |
| 116 | +// 2.) Retrieve the Certificate Authority chain file (wherever you placed it - notice it's just in the Node project root here) |
| 117 | +const serverCert = [fs.readFileSync("skysql_chain.pem", "utf8")]; |
| 118 | + |
| 119 | +var pool = mariadb.createPool({ |
| 120 | + host: process.env.DB_HOST, |
| 121 | + user: process.env.DB_USER, |
| 122 | + password: process.env.DB_PASS, |
| 123 | + port: process.env.DB_PORT, |
| 124 | + database: process.env.DB_NAME, |
| 125 | + connectionLimit: 5, |
| 126 | + // 3.) Add an "ssl" property to the connection pool configuration, using the serverCert const defined above |
| 127 | + ssl: { |
| 128 | + ca: serverCert |
| 129 | + } |
| 130 | +}); |
| 131 | +``` |
| 132 | + |
| 133 | +### b.) Build and run the app <a name="build-run-api"></a> |
| 134 | + |
| 135 | +To start and run the API application you need to execute the following commands within the [api root folder](src/api). |
| 136 | + |
| 137 | +1. Install the Node.js packages (dependendies) for the app. |
| 138 | + |
| 139 | +```bash |
| 140 | +$ npm install |
| 141 | +``` |
| 142 | + |
| 143 | +2. Run the the app, which will expose API endpoints via http://localhost:8080. |
| 144 | + |
| 145 | +```bash |
| 146 | +$ npm start |
| 147 | +``` |
| 148 | + |
| 149 | +### c.) Build and run the [UI (Client) app](src/client) <a name="build-run-client"></a> |
| 150 | + |
| 151 | +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](src/client) folder of this repo. |
| 152 | + |
| 153 | +To start the [client](src/client) application follow the instructions [here](https://github.com/mariadb-developers/places-app-client). |
| 154 | + |
| 155 | +## JSON Data Models <a name="data-models"></a> |
| 156 | + |
| 157 | +Below are samples of the data model per Location Type. |
| 158 | + |
| 159 | +**Attraction (A)** |
| 160 | +```json |
| 161 | +{ |
| 162 | + "category":"Landmark", |
| 163 | + "lastVisitDate":"11/5/2019" |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +**Restuarant (R)** |
| 168 | +```json |
| 169 | +{ |
| 170 | + "details":{ |
| 171 | + "foodType":"Pizza", |
| 172 | + "menu":"www.giodanos.com/menu" |
| 173 | + }, |
| 174 | + "favorites":[ |
| 175 | + { |
| 176 | + "description":"Classic Chicago", |
| 177 | + "price":24.99 |
| 178 | + }, |
| 179 | + { |
| 180 | + "description":"Salad", |
| 181 | + "price":9.99 |
| 182 | + } |
| 183 | + ] |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +**Sports Venue (S)** |
| 188 | +```json |
| 189 | +{ |
| 190 | + "details":{ |
| 191 | + "yearOpened":1994, |
| 192 | + "capacity":23500 |
| 193 | + }, |
| 194 | + "events":[ |
| 195 | + { |
| 196 | + "date":"10/18/2019", |
| 197 | + "description":"Bulls vs Celtics" |
| 198 | + }, |
| 199 | + { |
| 200 | + "date":"10/21/2019", |
| 201 | + "description":"Bulls vs Lakers" |
| 202 | + }, |
| 203 | + { |
| 204 | + "date":"11/5/2019", |
| 205 | + "description":"Bulls vs Bucks" |
| 206 | + }, |
| 207 | + { |
| 208 | + "date":"11/5/2019", |
| 209 | + "description":"Blackhawks vs Blues" |
| 210 | + } |
| 211 | + ] |
| 212 | +} |
| 213 | +``` |
| 214 | + |
| 215 | +## Support and Contribution <a name="support-contribution"></a> |
| 216 | + |
| 217 | +Please feel free to submit PR's, issues or requests to this project project directly. |
| 218 | + |
| 219 | +If you have any other questions, comments, or looking for more information on MariaDB please check out: |
| 220 | + |
| 221 | +* [MariaDB Developer Hub](https://mariadb.com/developers) |
| 222 | +* [MariaDB Community Slack](https://r.mariadb.com/join-community-slack) |
| 223 | + |
| 224 | +Or reach out to us diretly via: |
| 225 | + |
| 226 | + |
| 227 | +* [MariaDB Twitter](https://twitter.com/mariadb) |
| 228 | + |
| 229 | +## License <a name="license"></a> |
| 230 | +[](https://opensource.org/licenses/MIT) |
0 commit comments