This repository will walk you through the process of quickly getting started with Sequelize, a promise-based Node.js object-relational mapping (ORM) library, to connect to and communicate with a MariaDB using the MariaDB Node.js connector.
- Node.js
- MariaDB - to get started with MariaDB check out this guide!
1.) Clone this repo.
$ git clone https://github.com/mariadb-developers/nodejs-sequelize-quickstart.git
2.) Create the database schema and load test data using the schema.sql file.
You can do this by either copying, pasting and executing the SQL in schema.sql or with the MariaDB command-line client (from within the nodejs-sequelize-quickstart
directory, which you just pulled down).
For example:
$ mariadb --host 127.0.0.1 --user root --pRootPassword123! < schema.sql
3.) Step in to the src directory and install the MariaDB Node.js driver (connector) using npm, Sequelize and dotenv (a zero-dependency module that loads environment variables from a .env
file).
$ npm install mariadb sequelize
4.) Within src, create an environment file (e.g. $ touch .env
) and add your database connection settings.
For example:
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=root
DB_PASS=RootPassword123!
DB_NAME=demo
5.) Update the connection configuration to point to your database in the JavaScript sample files. For example, here.
6.) Execute the sample JavaScript files using the node
CLI command.
For example:
$ node src/queries/customers_basic.js
$ node src/queries/customers_relationships.js
$ node src/queries/orders.js
...
The scripts:
-
customers_basics.js - select all customers using a Sequelize model and
findAll
function. -
[customers_relationaships.js] - - select all customers using a Sequelize model and
findAll
function, which includes loaded child models based configured associations (in db.js). -
[orders.js] - select all orders using a Sequelize model and
findAll
function, which includes loaded child models based configured associations (in db.js).
- Quick Start: Node.js and MariaDB - Start here if you're just getting started with Node.js and MariaDB!
- Official MariaDB Documentation
- MariaDB Connector/Node.js Source Code
- MariaDB Quickstart Guide
- Official Sequelize Documentation
Please feel free to submit PR's, issues or requests to this project directly.
If you have any other questions, comments, or looking for more information on MariaDB please check out:
Or reach out to us directly via: