sequelize-transformations
v3.0.0-beta.1
Published
Sequelize plugin to add configurable attribute transformations.
Downloads
16
Maintainers
Readme
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 valuelowercase
: transform value to all lower caseuppercase
: 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
anduppercase
. - If an attribute is updated with the
raw
option set totrue
, 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
}
}));