npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mini-mvcs-sequelize-utils

v0.1.0

Published

Sequelize Utils for loading models, providing basic CRUD operations and transforming errors for use with Mini MVCS Errors lib

Downloads

5

Readme

Mini MVCS Sequelize Utils

Small library for loading models, providing basic CRUD operations and transforming errors for use with Mini MVCS Errors lib

Extracted first from Mini MVCS Library and from Node Backend Skel for individual use without any special dependencies.

Features

  • Synchronous loading of models building associations in order, so it does not create any additional fields when a hasMany association is called before a belongsTo one.
  • Provides a withTransaction method for wrapping transactional calls to the database
  • Provides the creation of a Service for CRUD Operations that:
    • Allow easy querying with associations (no "include" keyword needed in where)
    • Run synchronous and/or asynchronous validation functions before saving/updating data
    • Return either a ValidationError or a NotFoundError from the mini-mvcs-errors lib from all unsuccessful results for easy chaining.
  • Provides an simple function for creating a ValidationError from a Sequelize Error object.

NOTE: it does not provide a dependency to Sequelize because it would lock your app to a version specified by this lib, instead it allows you to pick the version you want to work with and update it at anytime.

Installation

Using npm:

npm install --save mini-mvcs-sequelize-utils

Usage

Model Loader

Reads a folder recursively for model loading with sequelize

const Sequelize = require('sequelize');
const { modelLoader } = require('mini-mvcs-sequelize-utils');

const sequelize = new Sequelize(
  'database',
  'username',
  'password',
  // other params
);

// we could add transactional options here

// maybe you would like to ignore some files for loading
const filesToIgnoreArray = [];

const modelsLoadedInfo = modelLoader(sequelize, './models/', filesToIgnoreArray)

module.exports = {
  ...modelsLoadedInfo,
  sequelize,
};

CRUD Service

Provides the creation of a CRUD lib based on a sequelize model:

// book.crud-service.js
const { crudService } = require('mini-mvcs-sequelize-utils');
// using models from a modelLoader()
const { models } = require('./db');

const bookCrudService = crudService(models.Book);

module.exports = bookCrudService;

it provides the following methods:

1. Persistence methods

.create(properties)

Creation of an Model Instance. It does not persist the instance, but it allows its creation in a consistent way, basically it is just a wrapper for the Model.build method from sequelize.

.save(properties)

Creation and saving of an Model Instance, throws a ValidationError if the save could not be executed (by internal validation or sequelize validation)

.edit(id, properties)

Reading and modifying the properties of a Model instance

.update(id, properties)

Reading, modifying and updating the properties of a Model instance, throws a ValidationError if the update could not be executed (by internal validation or sequelize validation)

.delete(id)

Deletion of a Model instance, throws a ValidationError if the save could not be executed (by internal validation or sequelize validation)

.validate(modelInstance)

Validation of a model, by default it is empty but it could be overwritten for adding more validation logic (asynchronous or not), throws a ValidationError (of course) if the validation could not be executed (by internal validation or sequelize validation)

2. Querying methods

.list(filter)

Query a list of models according to the filter data, it provides some shortcuts for getting relationships in the same query, e.g.:

// book.model
// ...
models.Book.associate = (models) => {
  models.Book.belongsTo(models.Author, {
    as: 'author',
    foreignKey: {
      name: 'idAuthor',
      field: 'id_author',
      allowNull: false,
    },
  });
};
// ...

// book.crud-service.js
// ...
// lists all books where author.name = 'JHON'
// no need to use the 'where' nor the 'include' keyword
bookCrudService.list({
  author: {
    name: 'JHON',
  },
});
// ...

.listAndCount(filter)

The same as list but it returns an object with rows (the result of the query) and count (the total count of the rows, for pagination) properties

.find(filter)

The same as list but it returns a single object, if not even one row that matches the filter could be found, then it throws a NotFoundError

.read(id)

The same as find but it uses the id (primaryKey) of the row for filtering

SequelizeErrorBuilder

Sometimes you just want to use sequelize methods for saving or updating data, but want to keep the validation chain provided by ValidationError, in that case you can use sequelizeErrorBuilder for catching the error thrown by sequelize and build a ValidationError like this:

const { sequelizeErrorBuilder } = require('mini-mvcs-sequelize-utils');
const { models } = require('./db');

module.exports = (properties) => {
  return models.book.save(properties)
  .catch((errors) => {
    throw new ValidationError('Book', sequelizeErrorBuilder(errors));
  });
};

Examples and common usage

All examples for better usage are (or will be) at the wiki of this project

License

MIT