Getting Started with MariaDB and Node.js

Getting Started with MariaDB and Node.js

hackernoon.com hackernoon.com3 months ago in #Dev Love58

It’s no secret that MariaDB has become a popular solution for developers over the past decade. Why? Well, it’s open source and it’s a relational database. So, for developers, that basically means it’s free and, we get the gist of it. What you may not know is that there are two groups actively contributing to MariaDB; Foundation and Corporation. MariaDB Foundation is the custodian of the MariaDB community code and guardian of the MariaDB community. MariaDB Corporation contributes to the community codebase, but also provides superior quality, enterprise grade products that thrusts MariaDB into the forefront of database vendors. MariaDB Corporation even offers columnar and HTAP based solutions, but I digress. With that in mind, I’m going to be using a Docker image from MariaDB Corporation. And don’t worry, it’s still completely free. In this post we’ll be walking through the process of connecting to MariaDB using Node.js, but enough talk, let’s get started! Requirements Before getting started you’ll need to have a few things: MariaDB client Node.js (and NPM – Node Package Manager) Docker Using a MariaDB Docker Container To pull the MariaDB Server image and spin up a container simply open a terminal window and run the following. $ docker run -p 3306:3306 -d –name mariadb -eMARIADB_ROOT_PASSWORD=Password123! mariadb/server:10.4 Now that you have a MariaDB Server container running you can connect to it with the MariaDB client and set up a database to be used by your Node.js app. Connect to MariaDB by executing the following command in a terminal window. $ mariadb –host 127.0.0.1 -P 3306 –user root -pPassword123! You should see something like the following, which means you’ve successfully connected to the MariaDB instance! Next, create a new database. Then create a new table. CREATE TABLE demo.people (name VARCHAR(50)); Finally, insert a couple records. INSERT INTO demo.people VALUES (‘rob’), (‘tracy’), (‘sam’), (‘duke’); Connecting to MariaDB with Node.js Now that you’ve download, installed, and stood up a MariaDB database, you’re ready to put it to use within a new Node.js project. To start, pick a new directory, and create a new Javascript file called “server.js”. Then, within a terminal that the directory location, execute the following. Feel free to fill out all of the prompts or you can just hit the enter key through all of the options. Either way, you’ll end up with a package.json file being generated next to server.js. Install the Express package which will be used as a lightweight web framework for the Node.js project. Install the MariaDB Node.js connector, which will be used to connect to and communicate with your MariaDB (containerized) instance. Open server.js and import the following modules. const express = require(‘express’) const pool = require(‘./db’) const app = express() Create a new module file called db.js and paste the following code into it. // import mariadb var mariadb = require(‘mariadb’); // create a new connection pool const pool = mariadb.createPool({ host: “127.0.0.1”, user: “root”, password: “Password123!”, database: “test” }); // expose the ability to create new connections module.exports={ getConnection: function(){ return new Promise(function(resolve,reject){ pool.getConnection().then(function(connection){ resolve(connection); }).catch(function(error){ reject(error); });  » Read More

Like to keep reading?

This article first appeared on hackernoon.com. If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply