easy-sequelize
v2.3.0
Published
Easily bootstrap sequelize on a project.
Downloads
16
Maintainers
Readme
easy-sequelize
Easily bootstrap sequelize on a project.
Installation
Steps
- Install the library
- Add models as needed
- Add business logic as needed
- Configure the database backend
Done!
Details
- Install the library
npm install --save easy-sequelize
# the folder `node_modules/easy-sequelize/sample`
# has sample files for models and business layer
# there is a utility to create the sample folder `install-sample.js`
# (optional)
# We'll use ./db for the tutorial, but substitute with your preferred location
node node_modules/easy-sequelize/install-sample.js ./db
#
# The sample files will have the following structure
# db
# models
# Product.js
# Category.js
# business-layer
# bl-Products.js
# migrations
# 20220205-01-addTables.js
# config.json
- Add models as needed Product.js and Category.js can be used as models to create your own, their content is pretty self explanatory. For a more detailed discussion of the model definition syntax, please follow sequelize's documentation. For the tables to be created on the database, you will need to provide a migration, but they will be added automatically to the in-memory models.
const DB = require('./db');
const db = await DB.getDB();
console.log(db.Tables);
// { Categories: Categories, Products: Products }
// All the models are automatically loaded in the Tables array and connected with
// the Tables on the backend Database
let products = await db.Tables.Products.findAll();
console.log(products);
// []
- Add business logic as needed
The
business-layer
folder is where you add functions to abstract the complexities of the database structure into business functions.easy-sequelize
will load any files under the business-layer folder and merge their functionality into the main database object. That is if you add a file here calledbl-myfunctions.js
with two functions exported addProduct and deleteProduct, once you instantiate your database, you will be able to access those functions directly on the database. For example:
/* In bl-myfunctions.js */
module.exports.addProduct = async function (productID,productName,categoryID){
// notice *this*, because your function will run in the
// context of the database object
return this.Tables.Products.create({
ProductID: productID,
ProductName: productName,
CategoryID: categoryID
});
}
...
/* In your code */
const DB = require('./db');
const db = await DB.getDB();
await db.addProduct('1234','Test',1);
let products = await db.Tables.Products.findAll();
console.log(products[0]);
// Product
// { ProductID: '1234', ProductName: 'Test', CategoryID: 1 }
Configure your database backend Any migration file in the
migrations
folder will be run automatically once per database. If you are not familiar with the migrations concept, this should be your time to head to the sequelize documentation. You can ignore any of the mechanics and command line instructions, since that is all provided auto-magically byeasy-sequelize
. By default, the config file is set to use sqlite, override for your needs.Enjoy the ready-to-use code AND diagram of your database structure located in the db folder (uml.puml).
You can even embed it in your README.md file by adding the following: ```plantuml 'db/uml.puml\n``` somewhere in your README.md file.