Socket
Socket
Sign inDemoInstall

sequelize-transformations

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    sequelize-transformations

Sequelize plugin to add configurable attribute transformations.


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Install size
7.87 kB
Created
Weekly downloads
 

Readme

Source

Sequelize Transformations

Sequelize plugin to add configurable attribute transforms. It allows you to define transformation functions to run on attribute values when an instance is updated (through assignment, set, build, create etc.). The transformation functions can be enabled and configured on attribute level.

Installation

npm install sequelize-transformations

Activation

To activate the plugin for all your models, call the plugin on your sequelize instance:

var sequelizeTransformations = require('sequelize-transformations');

sequelizeTransformations(sequelize);

Usage

To use transformations for an attribute, just add them to its definition:

var Model = sequelize.define('Model', {
  email: {
    type: Sequelize.DataTypes.STRING,
    lowercase: true,
    trim: true
  }
});

With this configuration, the email attribute will always be trimmed and transformed to lower case.

Predefined Transformations

The plugin comes with the following predefined transformations:

  • trim: trim value
  • lowercase: transform value to all lower case
  • uppercase: transform value to all upper case

Custom Transformations

It is possible to override predefined transformations or add your own by passing an object as the second argument:

sequelizeTransformations(sequelize, {
  trim: function(val, defintion) {
    return val.toString().replace(/ /g, '*');
  },
  append: function(val, definition) {
    return val.toString() + definition['append'];
  },
  removeMilliseconds: function(val, definition) {
    if(val) {
      val.setMilliseconds(0);
    }

    return val;
  }
});

This would override the trim transform and add a new one called append. Every transform function is called with two parameters: the value to transform and the definition of the attribute being transformed.

Notes

  • If more than one transform is defined on an attribute, then the order in which they are executed is unpredictable. This is generally not an issue as you should not use mutually exclusive transforms together, e.g. lowercase and uppercase.
  • If an attribute is updated with the raw option set to true, then the transforms will not be run.

TypeScript

Activation

import {sequelizeTransformations, ModelAttributeDefinition, TransformationDefinitions} from "sequelizeTransformations";

type TransformationDefinitions = {
  removeMilliseconds?: boolean;
}

sequelizeTransformations(sequelize, {
  removeMilliseconds: function(date: Date, definition: ModelAttributeDefinition<TransformationDefinitions>) {
    if(definition.removeMilliseconds) {
      date?.setMilliseconds(0);
    }

    return date;
  }
});

Usage

import {DataTypes, Optional} from "sequelize";
import {ModelAttributesWithTransformations} from "sequelize-transformations";

interface ModelAttributes {
  id: number;
  email: string;
}

interface ModelCreationAttributes extends Optional<ModelAttributes, "id"> {}

var Model = sequelize.define('Model', (<ModelAttributesWithTransformations<ModelCreationAttributes>>{
  email: {
    type: DataTypes.STRING,
    lowercase: true,
    trim: true
  }
}));

Keywords

FAQs

Last updated on 28 Mar 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc