# sequelize-transformations

> Sequelize plugin to add configurable attribute transformations.

Latest version **3.0.0-beta.1** (published 2021-03-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install sequelize-transformations
pnpm add sequelize-transformations
yarn add sequelize-transformations
bun add sequelize-transformations
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0-beta.1 |
| Published | 2021-03-28 |
| First published | 2021-03-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jared Crimmins |
| Maintainers | jaredcrimmins |
| Keywords | sequelize, transformations |

## Links

- npm: https://www.npmjs.com/package/sequelize-transformations
- Repository: https://github.com/JaredCrimmins/sequelize-transformations
- Homepage: https://github.com/JaredCrimmins/sequelize-transformations#readme
- Issues: https://github.com/JaredCrimmins/sequelize-transformations/issues
- npm.io page: https://npm.io/package/sequelize-transformations

## Alternatives

- [@libsql/sqlite3](https://npm.io/package/@libsql/sqlite3.md) — 39.8K weekly downloads
- [@fortemi/core](https://npm.io/package/@fortemi/core.md) — 461 weekly downloads
- [cdb-converter](https://npm.io/package/cdb-converter.md) — 341 weekly downloads
- [@uplo/adapter-prisma](https://npm.io/package/@uplo/adapter-prisma.md) — 75 weekly downloads
- [typeorm-aios](https://npm.io/package/typeorm-aios.md) — 30 weekly downloads

## Recent versions

- 3.0.0-beta.1 (latest) — 2021-03-28

## README

# Sequelize Transformations

[Sequelize](https://github.com/sequelize/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

```sh
npm install sequelize-transformations
```

## Activation

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

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

sequelizeTransformations(sequelize);
```

## Usage

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

```js
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:

```js
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

```ts
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

```ts
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
  }
}));
```

---
_Source: https://npm.io/package/sequelize-transformations · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
