8
8
Decorators and some other features for sequelize (v6).
9
9
10
10
- [ Installation] ( #installation )
11
- - [ Upgrade to ` sequelize-typescript@2 ` ] ( #upgrade-to-sequelize-typescript2 )
12
- - [ Upgrade to ` sequelize-typescript@1 ` ] ( #upgrade-to-sequelize-typescript1 )
13
11
- [ Model Definition] ( #model-definition )
14
12
- [ ` @Table ` API] ( #table-api )
15
13
- [ ` @Column ` API] ( #column-api )
@@ -27,7 +25,7 @@ Decorators and some other features for sequelize (v6).
27
25
- [ Indexes] ( #indexes )
28
26
- [ ` @Index ` API] ( #index )
29
27
- [ ` createIndexDecorator() ` API] ( #createindexdecorator )
30
- - [ Repository mode] ( #repository-mode-1 )
28
+ - [ Repository mode] ( #repository-mode )
31
29
- [ How to enable repository mode?] ( #how-to-enable-repository-mode )
32
30
- [ How to use repository mode?] ( #how-to-use-repository-mode )
33
31
- [ How to use associations with repository mode?] ( #how-to-use-associations-with-repository-mode )
@@ -45,13 +43,8 @@ Decorators and some other features for sequelize (v6).
45
43
- additional typings as documented [ here] ( https://sequelize.org/master/manual/typescript.html ) and [ reflect-metadata] ( https://www.npmjs.com/package/reflect-metadata )
46
44
47
45
``` sh
48
- npm install sequelize
49
- npm install @types/node @types/validator
50
- npm install reflect-metadata
51
- ```
52
-
53
- ``` sh
54
- npm install sequelize-typescript
46
+ npm install --save-dev @types/node @types/validator
47
+ npm install sequelize reflect-metadata sequelize-typescript
55
48
```
56
49
57
50
Your ` tsconfig.json ` needs the following flags:
@@ -62,48 +55,46 @@ Your `tsconfig.json` needs the following flags:
62
55
"emitDecoratorMetadata" : true
63
56
```
64
57
65
- ### ⚠️ sequelize@5
58
+ ### Sequelize Options
66
59
67
- ` sequelize@5 ` requires ` sequelize-typescript@1 ` . See
68
- [ documentation] ( https://github.com/RobinBuschmann/sequelize-typescript/tree/1.0.0 ) for version ` 1.0 ` .
69
-
70
- ``` sh
71
-
72
- ```
60
+ - ` SequelizeConfig ` renamed to ` SequelizeOptions `
61
+ - ` modelPaths ` property renamed to ` models `
73
62
74
- ### ⚠️ sequelize@4
63
+ ### Scopes Options
75
64
76
- ` sequelize@4 ` requires
` [email protected] ` . See
77
- [ documentation] ( https://github.com/RobinBuschmann/sequelize-typescript/tree/0.6.X ) for version ` 0.6 ` .
65
+ The ` @Scopes ` and ` @DefaultScope ` decorators now take lambda's as options
78
66
79
- ``` sh
80
-
67
+ ``` ts
68
+ @DefaultScope (() => ({... }))
69
+ @Scopes (() => ({... }))
81
70
```
82
71
83
- ## Upgrade to ` sequelize-typescript@2 `
84
-
85
- - ` sequelize-typescript@2 ` only works with
` [email protected] >=` .
86
- For
` sequelize@5 ` use
` [email protected] ` .
87
-
88
- ### Breaking Changes
72
+ instead of deprecated way:
89
73
90
- - All breaking changes of ` sequelize@6 ` are also valid for ` sequelize-typescript@2 ` .
91
- See [ Upgrade to v6] ( https://sequelize.org/master/manual/upgrade-to-v6.html ) for details.
92
- - ` @types/bluebird ` is no longer needed, ` sequelize@6 ` removed usage of ` bluebird `
93
- - Sequelize v6.2 introduced additional model attributes typings, which affects how the model is defined.
94
- - See below comparison between V5 and V6 model definition to show how to upgrade models.
95
- - For more details, see [ sequelize typescript docs] ( https://sequelize.org/master/manual/typescript.html ) .
74
+ ``` ts
75
+ @DefaultScope ({... })
76
+ @Scopes ({... }))
77
+ ```
96
78
97
- #### V5 Model definition
79
+ ## Model definition
98
80
99
81
``` typescript
100
- import { Table , Model } from ' sequelize-typescript'
82
+ import { Table , Column , Model , HasMany } from ' sequelize-typescript'
101
83
102
84
@Table
103
- class Person extends Model <Person > {}
85
+ class Person extends Model {
86
+ @Column
87
+ name: string
88
+
89
+ @Column
90
+ birthday: Date
91
+
92
+ @HasMany (() => Hobby )
93
+ hobbies: Hobby []
94
+ }
104
95
```
105
96
106
- #### V6 Model definition (less strict)
97
+ ### Less strict
107
98
108
99
``` typescript
109
100
import { Table , Model } from ' sequelize-typescript'
@@ -112,10 +103,7 @@ import { Table, Model } from 'sequelize-typescript'
112
103
class Person extends Model {}
113
104
```
114
105
115
- #### V6 Model definition (more strict)
116
-
117
- - ⚠️ not yet implemented in ` sequelize-typescript `
118
- - to allow more strict model attributes type-checks, you can define ` ModelAttributes ` and ` ModelCreationAttributes ` interfaces
106
+ ### More strict
119
107
120
108
``` typescript
121
109
import { Optional } from ' sequelize'
@@ -132,70 +120,6 @@ interface PersonCreationAttributes extends Optional<PersonAttributes, 'id'> {}
132
120
class Person extends Model <PersonAttributes , PersonCreationAttributes > {}
133
121
```
134
122
135
- ## Upgrade to ` sequelize-typescript@1 `
136
-
137
- ` sequelize-typescript@1 ` only works with ` sequelize@5>= ` .
138
- For
` sequelize@4 ` &
` sequelize@3 ` use
` [email protected] ` .
139
-
140
- ### Breaking Changes @5
141
-
142
- All breaking changes of ` sequelize@5 ` are also valid for ` sequelize-typescript@1 ` .
143
- See [ Upgrade to v5] ( https://sequelize.org/v5/manual/upgrade-to-v5.html ) for details.
144
-
145
- #### Official Sequelize Typings
146
-
147
- sequelize-typescript now uses the official typings bundled with sequelize
148
- (See [ this] ( https://sequelize.org/v5/manual/upgrade-to-v5.html#typescript-support ) ).
149
- Please note the following details:
150
-
151
- - Most of the sequelize-typescript interfaces of the previous version are replaced by the official ones
152
- - ` @types/sequelize ` is no longer used
153
- - ` @types/bluebird ` is no longer an explicit dependency
154
- - The official typings are less strict than the former sequelize-typescript ones
155
-
156
- #### Sequelize Options
157
-
158
- - ` SequelizeConfig ` renamed to ` SequelizeOptions `
159
- - ` modelPaths ` property renamed to ` models `
160
-
161
- #### Scopes Options
162
-
163
- The ` @Scopes ` and ` @DefaultScope ` decorators now take lambda's as options
164
-
165
- ``` ts
166
- @DefaultScope (() => ({... }))
167
- @Scopes (() => ({... }))
168
- ```
169
-
170
- instead of deprecated way:
171
-
172
- ``` ts
173
- @DefaultScope ({... })
174
- @Scopes ({... }))
175
- ```
176
-
177
- ### Repository Mode
178
-
179
- With ` sequelize-typescript@1 ` comes a repository mode. See [ docs] ( #repository-mode-1 ) for details.
180
-
181
- ## Model definition
182
-
183
- ``` typescript
184
- import { Table , Column , Model , HasMany } from ' sequelize-typescript'
185
-
186
- @Table
187
- class Person extends Model {
188
- @Column
189
- name: string
190
-
191
- @Column
192
- birthday: Date
193
-
194
- @HasMany (() => Hobby )
195
- hobbies: Hobby []
196
- }
197
- ```
198
-
199
123
The model needs to extend the ` Model ` class and has to be annotated with the ` @Table ` decorator. All properties that
200
124
should appear as a column in the database require the ` @Column ` annotation.
201
125
@@ -791,6 +715,8 @@ class Person extends Model {
791
715
792
716
## Repository mode
793
717
718
+ With ` sequelize-typescript@1 ` comes a repository mode. See [ docs] ( #repository-mode-1 ) for details.
719
+
794
720
The repository mode makes it possible to separate static operations like ` find ` , ` create ` , ... from model definitions.
795
721
It also empowers models so that they can be used with multiple sequelize instances.
796
722
0 commit comments