|
4 | 4 | [](https://www.npmjs.com/package/sequelize-typescript)
|
5 | 5 |
|
6 | 6 | # sequelize-typescript
|
7 |
| -Decorators and some other features for sequelize (v5). |
| 7 | +Decorators and some other features for sequelize (v6). |
8 | 8 |
|
9 | 9 | - [Installation](#installation)
|
| 10 | + - [Upgrade to `sequelize-typescript@2`](#upgrade-to-sequelize-typescript2) |
10 | 11 | - [Upgrade to `sequelize-typescript@1`](#upgrade-to-sequelize-typescript1)
|
11 | 12 | - [Model Definition](#model-definition)
|
12 | 13 | - [`@Table` API](#table-api)
|
@@ -37,29 +38,97 @@ Decorators and some other features for sequelize (v5).
|
37 | 38 | - [Recommendations and limitations](#recommendations-and-limitations)
|
38 | 39 |
|
39 | 40 | ## Installation
|
40 |
| -*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) |
| 41 | + |
| 42 | +- this assumes usage of `sequelize@6` |
| 43 | +- *sequelize-typescript* requires [sequelize](https://github.com/sequelize/sequelize) |
| 44 | +- additional typings as documented [here](https://sequelize.org/master/manual/typescript.html) and [reflect-metadata](https://www.npmjs.com/package/reflect-metadata) |
| 45 | + |
41 | 46 | ```
|
42 | 47 | npm install sequelize
|
43 |
| -npm install @types/bluebird @types/node @types/validator |
| 48 | +npm install @types/node @types/validator |
44 | 49 | npm install reflect-metadata
|
45 | 50 | ```
|
| 51 | + |
46 | 52 | ```
|
47 | 53 | npm install sequelize-typescript
|
48 | 54 | ```
|
| 55 | + |
49 | 56 | Your `tsconfig.json` needs the following flags:
|
| 57 | + |
50 | 58 | ```json
|
51 | 59 | "target": "es6", // or a more recent ecmascript version
|
52 | 60 | "experimentalDecorators": true,
|
53 | 61 | "emitDecoratorMetadata": true
|
54 | 62 | ```
|
55 | 63 |
|
| 64 | +### ⚠️ sequelize@5 |
| 65 | +`sequelize@5` requires `sequelize-typescript@1`. See |
| 66 | +[documentation](https://github.com/RobinBuschmann/sequelize-typescript/tree/1.0.0) for version `1.0`. |
| 67 | +``` |
| 68 | + |
| 69 | +``` |
| 70 | + |
56 | 71 | ### ⚠️ sequelize@4
|
57 | 72 | `sequelize@4` requires `[email protected]`. See
|
58 | 73 | [documentation](https://github.com/RobinBuschmann/sequelize-typescript/tree/0.6.X) for version `0.6`.
|
59 | 74 | ```
|
60 | 75 |
|
61 | 76 | ```
|
62 | 77 |
|
| 78 | +## Upgrade to `sequelize-typescript@2` |
| 79 | + |
| 80 | +- `sequelize-typescript@2` only works with `[email protected]>=`. |
| 81 | +For `sequelize@5` use `[email protected]`. |
| 82 | + |
| 83 | +### Breaking Changes |
| 84 | + |
| 85 | +- All breaking changes of `sequelize@6` are also valid for `sequelize-typescript@2`. |
| 86 | +See [Upgrade to v6](https://sequelize.org/master/manual/upgrade-to-v6.html) for details. |
| 87 | +- `@types/bluebird` is no longer needed, `sequelize@6` removed usage of `bluebird` |
| 88 | +- Sequelize v6.2 introduced additional model attributes typings, which affects how the model is defined. |
| 89 | +- See below comparison between V5 and V6 model definition to show how to upgrade models. |
| 90 | +- For more details, see [sequelize typescript docs](https://sequelize.org/master/manual/typescript.html). |
| 91 | + |
| 92 | +#### V5 Model definition |
| 93 | + |
| 94 | +```typescript |
| 95 | +import { Table, Model } from 'sequelize-typescript'; |
| 96 | + |
| 97 | +@Table |
| 98 | +class Person extends Model<Person> {} |
| 99 | +``` |
| 100 | + |
| 101 | +#### V6 Model definition (less strict) |
| 102 | + |
| 103 | +```typescript |
| 104 | +import { Table, Model } from 'sequelize-typescript'; |
| 105 | + |
| 106 | +@Table |
| 107 | +class Person extends Model {} |
| 108 | +``` |
| 109 | + |
| 110 | +#### V6 Model definition (more strict) |
| 111 | + |
| 112 | +- ⚠️ not yet implemented in `sequelize-typescript` |
| 113 | +- to allow more strict model attributes type-checks, you can define `ModelAttributes` and `ModelCreationAttributes` interfaces |
| 114 | + |
| 115 | + |
| 116 | +```typescript |
| 117 | +import { Optional } from 'sequelize'; |
| 118 | +import { Table, Model } from 'sequelize-typescript'; |
| 119 | + |
| 120 | +interface PersonAttributes { |
| 121 | + id: number; |
| 122 | + name: string; |
| 123 | +} |
| 124 | + |
| 125 | +interface PersonCreationAttributes extends Optional<PersonAttributes, 'id'> { |
| 126 | +} |
| 127 | + |
| 128 | +@Table |
| 129 | +class Person extends Model<PersonAttributes, PersonCreationAttributes> {} |
| 130 | +``` |
| 131 | + |
63 | 132 | ## Upgrade to `sequelize-typescript@1`
|
64 | 133 | `sequelize-typescript@1` only works with `sequelize@5>=`.
|
65 | 134 | For `sequelize@4` & `sequelize@3` use `[email protected]`.
|
|
0 commit comments