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

sequelize-strict-attributes

v1.0.2

Published

Plugin that configures Sequelize to throw when accessing attributes omitted from a select.

Downloads

398

Readme

sequelize-strict-attributes

npm package typescript MIT license test status

sequelize-strict-attributes is a plugin for Sequelize that adds stricter treatment of attribute access after specifying attributes in a query. This way, attempts to access instance attributes omitted from the query using attributes will throw rather than silently failing by returning undefined. This is especially useful for scenarios where the column is expected to sometimes be null so a fallback value is provided for calculations.

Note: this is a runtime check. Types are included for the plugin itself, but the types of the returned instances will not be changed. To type checking, the instances will appear to still have omitted attributes.

Installation

npm install sequelize-strict-attributes

and include as a JavaScript or TypeScript module (types included):

import sequelizeStrictAttributes from 'sequelize-strict-attributes';

…or a CommonJS module:

const sequelizeStrictAttributes = require('sequelize-strict-attributes');

Supports the latest stable Sequelize, version 6.

Usage

Call the plugin with the active Sequelize instance immediately after it's instantiated.

const sequelize = new Sequelize(…);
sequelizeStrictAttributes(sequelize);

From this point on, any Model.findAll or Model.findOne query that specifies attributes:—and does not use raw: true—will throw if you try to access an attribute not included in the select list.

Example

For example, given a model that looks something like this:

const Cart = sequelize.define("Cart", {
  subtotal: DataTypes.STRING,
  tax: DataTypes.INTEGER,
});

And an instance fetched like this:

const cart = await Cart.findOne({
  attributes: ["subtotal"],
});

When accessing the omitted attribute to determine if tax needs to be calculated, the program will throw:

if (cart.tax === null) { // <-- Throws!
  cart.tax = cart.subtotal * TAX_RATE;
} 
if (cart.get("tax") === null) { // <-- Also throws

Includes

Included models will similarly be restricted if their attributes are specified on the include.

const cart = await Cart.findOne({
  attributes: [],
  include: {
    model: Customer,
    attributes: ["name"],
  },
});

await sendReceiptEmail({
  name: cart.Customer.name,
  email: cart.Customer.email, // <-- Throws here!
});

Setting attributes

Setting the attribute directly or using Model::set will still work. These changes can be saved as expected, though you won't be able to read them back on the same instance, even after a .reload(). (Though shortcuts like addition–assignment will naturally throw.)

Motivation

Disallowing access of attributes that were excluded from a select is a common feature of other ORMs for good reason (eg Prisma excludes it from the returned type, ActiveRecord and Django throw errors on access). However, the Sequelize authors declined to support it in the core library. In lieu of core support, this plugin will help guard against the hazard.

Author

Alec Perkins

License

This package is licensed under the MIT License.

See ./LICENSE for more information.