Skip to content

Commit 3f19b72

Browse files
committed
Initial additions
1 parent 72266ba commit 3f19b72

9 files changed

+270
-0
lines changed

README.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
[![License](https://img.shields.io/badge/License-MIT-blue.svg?style=plastic)](https://opensource.org/licenses/MIT)

schema.sql

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Create schema*/
2+
CREATE DATABASE `demo`;
3+
4+
USE `demo`;
5+
6+
CREATE TABLE `customers` (
7+
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
8+
`first_name` VARCHAR(100) NOT NULL,
9+
`last_name` VARCHAR(100) NOT NULL,
10+
PRIMARY KEY (`id`)
11+
);
12+
13+
CREATE TABLE `products` (
14+
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
15+
`name` VARCHAR(50) NOT NULL,
16+
`amount` DECIMAL(15,2) NOT NULL,
17+
PRIMARY KEY (`id`)
18+
);
19+
20+
CREATE TABLE `orders` (s
21+
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
22+
`customer_id` INT UNSIGNED NOT NULL,
23+
`product_id` INT UNSIGNED NOT NULL,
24+
`quantity` INT UNSIGNED NOT NULL,
25+
PRIMARY KEY (`id`),
26+
KEY `FK_Customer` (`customer_id`),
27+
KEY `FK_Product` (`product_id`),
28+
CONSTRAINT `FK_Customer` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
29+
CONSTRAINT `FK_Product` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
30+
);
31+
32+
/* Load test data */
33+
INSERT INTO customers (`first_name`, `last_name`) VALUES
34+
('Jane', 'Brown'), ('Ryan', 'Hall'), ('Mary', 'Stark');
35+
36+
INSERT INTO products (`name`, `amount`) VALUES
37+
('T-shirt', 19.99), ('Hat', 9.99);
38+
39+
INSERT INTO orders (customer_id, product_id, quantity) VALUES
40+
(1, 1, 1), (2, 1, 3), (3, 2, 2);

src/db.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require('dotenv').config();
2+
3+
const Sequelize = require("sequelize");
4+
const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASS, {
5+
host: process.env.DB_HOST,
6+
port: process.env.DB_PORT,
7+
dialect: 'mariadb',
8+
define: {
9+
timestamps: false
10+
}
11+
});
12+
13+
const db = {};
14+
15+
db.Sequelize = Sequelize;
16+
db.sequelize = sequelize;
17+
18+
db.customers = require("./models/customer.model.js")(sequelize, Sequelize);
19+
db.products = require("./models/product.model.js")(sequelize, Sequelize);
20+
db.orders = require("./models/order.model.js")(sequelize, Sequelize);
21+
22+
db.customers.hasMany(db.orders,{ as: 'orders', foreignKey: 'customer_id' });
23+
db.orders.belongsTo(db.products,{ as: 'product', foreignKey: 'product_id' });
24+
db.orders.belongsTo(db.customers,{ as: 'customer', foreignKey: 'customer_id' });
25+
26+
module.exports = db;

src/models/customer.model.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = (sequelize, Sequelize) => {
2+
const Customer = sequelize.define("customer", {
3+
id: {
4+
type: Sequelize.INTEGER,
5+
primaryKey: true,
6+
autoIncrement: true
7+
},
8+
first_name: {
9+
type: Sequelize.STRING
10+
},
11+
last_name: {
12+
type: Sequelize.STRING
13+
}
14+
});
15+
16+
return Customer;
17+
};

src/models/order.model.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = (sequelize, Sequelize) => {
2+
const Order = sequelize.define("order", {
3+
id: {
4+
type: Sequelize.INTEGER,
5+
primaryKey: true,
6+
autoIncrement: true
7+
},
8+
customer_id: {
9+
type: Sequelize.INTEGER
10+
},
11+
product_id: {
12+
type: Sequelize.INTEGER
13+
},
14+
quantity: {
15+
type: Sequelize.INTEGER
16+
}
17+
});
18+
19+
return Order;
20+
};

src/models/product.model.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = (sequelize, Sequelize) => {
2+
const Product = sequelize.define("product", {
3+
id: {
4+
type: Sequelize.INTEGER,
5+
primaryKey: true,
6+
autoIncrement: true
7+
},
8+
name: {
9+
type: Sequelize.STRING
10+
},
11+
amount: {
12+
type: Sequelize.DECIMAL
13+
}
14+
});
15+
16+
return Product;
17+
};

src/queries/customers_basic.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const db = require("./db.js");
2+
const Customer = db.customers;
3+
4+
async function asyncFunction() {
5+
const customers = await Customer.findAll();
6+
console.log(JSON.stringify(customers));
7+
}
8+
9+
asyncFunction();
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const db = require("../db_sequelize.js");
2+
const Customer = db.customers;
3+
const Order = db.orders;
4+
const Product = db.products;
5+
6+
Customer.findAll({
7+
include: [
8+
{
9+
model: Order,
10+
as: 'orders',
11+
attributes: ['id','quantity'],
12+
include: [{
13+
model: Product,
14+
as: 'product',
15+
attributes: ['id','name','amount']
16+
}]
17+
}
18+
]
19+
})
20+
.then(results => {
21+
console.log(JSON.stringify(results));
22+
})
23+
.catch(err => {
24+
console.log(err);
25+
});

src/queries/orders.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const db = require("../db_sequelize.js");
2+
const Order = db.orders;
3+
const Customer = db.customers;
4+
const Product = db.products;
5+
6+
Order.findAll({
7+
include: [
8+
{
9+
model: Customer,
10+
as: 'customer',
11+
attributes: ['id','first_name','last_name']
12+
},
13+
{
14+
model: Product,
15+
as: 'product',
16+
attributes: ['id','name','amount']
17+
},
18+
]
19+
})
20+
.then(results => {
21+
console.log(JSON.stringify(results));
22+
})
23+
.catch(err => {
24+
console.log(err);
25+
});

0 commit comments

Comments
 (0)