|
| 1 | +## Quick Start: Sequelize (Node.js) and MariaDB |
| 2 | + |
| 3 | +This repository will walk you through the process of quickly getting started with [Sequelize](https://sequelize.org/), a promise-based Node.js object-relational mapping (ORM) library, to connect to and communicate with a [MariaDB](https://mariadb.com) using the [MariaDB Node.js connector](https://github.com/mariadb-corporation/mariadb-connector-nodejs). |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +* [Node.js](https://nodejs.org/en/download/) |
| 8 | +* [MariaDB](https://mariadb.com) - to get started with MariaDB check out [this guide](https://github.com/mariadb-developers/mariadb-getting-started)! |
| 9 | + |
| 10 | +## Getting Started |
| 11 | + |
| 12 | +1.) Clone this repo. |
| 13 | + |
| 14 | +```bash |
| 15 | +$ git clone https://github.com/mariadb-developers/nodejs-sequelize-quickstart.git |
| 16 | +``` |
| 17 | + |
| 18 | +2.) Create the database schema and load test data using the [schema.sql file](schema.sql). |
| 19 | + |
| 20 | +You can do this by either copying, pasting and executing the SQL in [schema.sql](schema.sql) or with the MariaDB command-line client (from within the `nodejs-sequelize-quickstart` directory, which you just pulled down). |
| 21 | + |
| 22 | +_For example:_ |
| 23 | +```bash |
| 24 | +$ mariadb --host 127.0.0.1 --user root --pRootPassword123! < schema.sql |
| 25 | +``` |
| 26 | + |
| 27 | +3.) Step in to the [src](src) directory and install the MariaDB Node.js driver (connector) using [npm](npmjs.com). |
| 28 | + |
| 29 | + |
| 30 | +```bash |
| 31 | +$ npm install mariadb sequelize |
| 32 | +``` |
| 33 | + |
| 34 | +4.) Within [src](src), create an environment file (e.g. `$ touch .env`) and add _your_ database connection settings. |
| 35 | + |
| 36 | +_For example:_ |
| 37 | +``` |
| 38 | +DB_HOST=127.0.0.1 |
| 39 | +DB_PORT=3306 |
| 40 | +DB_USER=root |
| 41 | +DB_PASS=RootPassword123! |
| 42 | +DB_NAME=demo |
| 43 | +``` |
| 44 | + |
| 45 | +5.) Update the connection configuration to point to _your_ database in the JavaScript sample files. For example, [here](src/db.js#L4-L11). |
| 46 | + |
| 47 | +6.) Execute the sample JavaScript files using the `node` [CLI command](https://nodejs.org/api/cli.html). |
| 48 | + |
| 49 | +_For example:_ |
| 50 | +```bash |
| 51 | +$ node src/queries/customers_basic.js |
| 52 | + |
| 53 | +$ node src/queries/customers_relationships.js |
| 54 | + |
| 55 | +$ node src/queries/orders.js |
| 56 | + |
| 57 | +... |
| 58 | +``` |
| 59 | + |
| 60 | +**The scripts:** |
| 61 | + |
| 62 | +* [customers_basics.js](src/queries/customers_basics.js) - select all customers using a [Sequelize model](https://sequelize.org/master/manual/model-basics.html) and `findAll` function. |
| 63 | + |
| 64 | +* [customers_relationaships.js] - - select all customers using a [Sequelize model](https://sequelize.org/master/manual/model-basics.html) and `findAll` function, which includes loaded child models based configured [associations](https://sequelize.org/master/manual/assocs.html) (in [db.js](src/db.js#L18-L24)). |
| 65 | + |
| 66 | +* [orders.js] - select all orders using a [Sequelize model](https://sequelize.org/master/manual/model-basics.html) and `findAll` function, which includes loaded child models based configured [associations](https://sequelize.org/master/manual/assocs.html) (in [db.js](src/db.js#L18-L24)). |
| 67 | + |
| 68 | +## Helpful Resources |
| 69 | + |
| 70 | +* [Quick Start: Node.js and MariaDB](https://github.com/mariadb-developers/nodejs-quickstart) - Start here if you're just getting started with Node.js and MariaDB! |
| 71 | +* [Official MariaDB Documentation](https://mariadb.com/docs) |
| 72 | +* [MariaDB Connector/Node.js Source Code](https://github.com/mariadb-corporation/mariadb-connector-nodejs) |
| 73 | +* [MariaDB Quickstart Guide](https://github.com/mariadb-developers/mariadb-getting-started) |
| 74 | +* [Official Sequelize Documentation](https://sequelize.org/master/index.html) |
| 75 | + |
| 76 | +## Support and Contribution |
| 77 | + |
| 78 | +Please feel free to submit PR's, issues or requests to this project directly. |
| 79 | + |
| 80 | +If you have any other questions, comments, or looking for more information on MariaDB please check out: |
| 81 | + |
| 82 | +* [MariaDB Developer Hub](https://mariadb.com/developers) |
| 83 | +* [MariaDB Community Slack](https://r.mariadb.com/join-community-slack) |
| 84 | + |
| 85 | +Or reach out to us directly via: |
| 86 | + |
| 87 | + |
| 88 | +* [MariaDB Twitter](https://twitter.com/mariadb) |
| 89 | + |
| 90 | +## License <a name="license"></a> |
| 91 | +[](https://opensource.org/licenses/MIT) |
0 commit comments