Skip to content

Commit

Permalink
Merge pull request #865 from lukashroch/sequelize6-docs
Browse files Browse the repository at this point in the history
README update for sequelize v6
  • Loading branch information
RobinBuschmann authored Jan 17, 2021
2 parents 71d55cd + 2a183c5 commit 8d2290d
Showing 1 changed file with 72 additions and 3 deletions.
75 changes: 72 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
[![Dependency Status](https://img.shields.io/david/RobinBuschmann/sequelize-typescript.svg)](https://www.npmjs.com/package/sequelize-typescript)

# sequelize-typescript
Decorators and some other features for sequelize (v5).
Decorators and some other features for sequelize (v6).

- [Installation](#installation)
- [Upgrade to `sequelize-typescript@2`](#upgrade-to-sequelize-typescript2)
- [Upgrade to `sequelize-typescript@1`](#upgrade-to-sequelize-typescript1)
- [Model Definition](#model-definition)
- [`@Table` API](#table-api)
Expand Down Expand Up @@ -37,29 +38,97 @@ Decorators and some other features for sequelize (v5).
- [Recommendations and limitations](#recommendations-and-limitations)

## Installation
*sequelize-typescript* requires [sequelize](https://github.com/sequelize/sequelize), additional typings as documented [here](https://docs.sequelizejs.com/manual/typescript.html) and [reflect-metadata](https://www.npmjs.com/package/reflect-metadata)

- this assumes usage of `sequelize@6`
- *sequelize-typescript* requires [sequelize](https://github.com/sequelize/sequelize)
- additional typings as documented [here](https://sequelize.org/master/manual/typescript.html) and [reflect-metadata](https://www.npmjs.com/package/reflect-metadata)

```
npm install sequelize
npm install @types/bluebird @types/node @types/validator
npm install @types/node @types/validator
npm install reflect-metadata
```

```
npm install sequelize-typescript
```

Your `tsconfig.json` needs the following flags:

```json
"target": "es6", // or a more recent ecmascript version
"experimentalDecorators": true,
"emitDecoratorMetadata": true
```

### ⚠️ sequelize@5
`sequelize@5` requires `sequelize-typescript@1`. See
[documentation](https://github.com/RobinBuschmann/sequelize-typescript/tree/1.0.0) for version `1.0`.
```
npm install [email protected]
```

### ⚠️ sequelize@4
`sequelize@4` requires `[email protected]`. See
[documentation](https://github.com/RobinBuschmann/sequelize-typescript/tree/0.6.X) for version `0.6`.
```
npm install [email protected]
```

## Upgrade to `sequelize-typescript@2`

- `sequelize-typescript@2` only works with `[email protected]>=`.
For `sequelize@5` use `[email protected]`.

### Breaking Changes

- All breaking changes of `sequelize@6` are also valid for `sequelize-typescript@2`.
See [Upgrade to v6](https://sequelize.org/master/manual/upgrade-to-v6.html) for details.
- `@types/bluebird` is no longer needed, `sequelize@6` removed usage of `bluebird`
- Sequelize v6.2 introduced additional model attributes typings, which affects how the model is defined.
- See below comparison between V5 and V6 model definition to show how to upgrade models.
- For more details, see [sequelize typescript docs](https://sequelize.org/master/manual/typescript.html).

#### V5 Model definition

```typescript
import { Table, Model } from 'sequelize-typescript';

@Table
class Person extends Model<Person> {}
```

#### V6 Model definition (less strict)

```typescript
import { Table, Model } from 'sequelize-typescript';

@Table
class Person extends Model {}
```

#### V6 Model definition (more strict)

- ⚠️ not yet implemented in `sequelize-typescript`
- to allow more strict model attributes type-checks, you can define `ModelAttributes` and `ModelCreationAttributes` interfaces


```typescript
import { Optional } from 'sequelize';
import { Table, Model } from 'sequelize-typescript';

interface PersonAttributes {
id: number;
name: string;
}

interface PersonCreationAttributes extends Optional<PersonAttributes, 'id'> {
}

@Table
class Person extends Model<PersonAttributes, PersonCreationAttributes> {}
```

## Upgrade to `sequelize-typescript@1`
`sequelize-typescript@1` only works with `sequelize@5>=`.
For `sequelize@4` & `sequelize@3` use `[email protected]`.
Expand Down

0 comments on commit 8d2290d

Please sign in to comment.