New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

easy-sequelize

Package Overview
Dependencies
Maintainers
0
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

easy-sequelize

Easily bootstrap sequelize on a project.

  • 3.0.1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-96.67%
Maintainers
0
Weekly downloads
 
Created
Source

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.sequelize.models);
// { 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.sequelize.models.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.sequelize.models.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.sequelize.models.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).

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.

Keywords

FAQs

Package last updated on 26 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc