2.2.9 • Published 2 months ago

easy-sequelize v2.2.9

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
2 months ago

easy-sequelize

Easily bootstrap sequelize on a project.

Installation

Steps

  1. Install the library
  2. Add models as needed
  3. Add business logic as needed
  4. Configure the database backend

Done!

Details

  1. 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
  1. 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);
// []
  1. 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 called bl-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 }
  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 by easy-sequelize. By default, the config file is set to use sqlite, override for your needs.

  2. Enjoy the ready-to-use code AND diagram of your database structure located in the db folder (uml.puml).

npm.io 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.

2.2.9

2 months ago

2.2.8

2 months ago

2.2.7

3 months ago

2.2.6

4 months ago

2.2.5

5 months ago

2.2.3

1 year ago

2.2.2

1 year ago

2.2.4

1 year ago

2.2.1

1 year ago

2.1.2

1 year ago

2.2.0

1 year ago

2.1.1

1 year ago

2.0.2

2 years ago

2.1.4

1 year ago

2.0.5

2 years ago

2.0.6

2 years ago

2.1.0

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.24

2 years ago

0.0.25

2 years ago

0.0.26

2 years ago

0.0.27

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.14

2 years ago

0.0.10

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago