@@ -90,7 +90,7 @@ Sequelize will keep the connection open by default, and use the same connection
## Modeling a table
Models are defined with `Sequelize.Model.init(attributes, options)`:
A model is a class that extends `Sequelize.Model`. Models can be defined in two equivalent ways. The first, with `Sequelize.Model.init(attributes, options)`:
```js
constModel=Sequelize.Model;
...
...
@@ -111,7 +111,7 @@ User.init({
});
```
Alternatively (legacy):
Alternatively, using `sequelize.define`:
```js
constUser=sequelize.define('User',{
...
...
@@ -129,7 +129,9 @@ const User = sequelize.define('User', {
});
```
The above code tells Sequelize to expect a table named `users` in the database with the fields `firstName` and `lastName`. The table name is automatically pluralized by default (a library called [inflection](https://www.npmjs.com/package/inflection) is used under the hood to do this). This behavior can be stopped for a specific model by using the `freezeTableName: true` option, or for all models by using the `define` option from the Sequelize constructor.
The above code tells Sequelize to expect a table named `users` in the database with the fields `firstName` and `lastName`. The table name is automatically pluralized by default (a library called [inflection](https://www.npmjs.com/package/inflection) is used under the hood to do this). This behavior can be stopped for a specific model by using the `freezeTableName: true` option, or for all models by using the `define` option from the [Sequelize constructor](http://docs.sequelizejs.com/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor).
Sequelize also defines by default the fields `id` (primary key), `createdAt` and `updatedAt` to every model. This behavior can also be changed, of course (check the API Reference to learn more about the available options).
You can read more about creating models in the [define API Reference](/class/lib/sequelize.js~Sequelize.html#instance-method-define) and the [Model API reference](/class/lib/model.js~Model.html).
You can read more about creating models in the [Model.init API Reference](/class/lib/model.js~Model.html#static-method-init), or in the [sequelize.define API reference](/class/lib/sequelize.js~Sequelize.html#instance-method-define).
Since v5 Sequelize provides it's own TypeScript definitions.
Please note that only TS >= 3.1 is supported.
Since v5, Sequelize provides its own TypeScript definitions. Please note that only TS >= 3.1 is supported.
## Installation
In order to avoid installation bloat for non TS users you must install the following packages manually:
As Sequelize heavily relies on runtime property assignments, TypeScript won't be very useful out of the box. A decent amount of manual type declarations are needed to make models workable.
-`@types/node` this is universally required
-`@types/validator`
-`@types/bluebird`
## Note
## Installation
As Sequelize heavily relies on runtime property assignments, TypeScript won't be very useful out of the box.
A decent amount of manual type declarations are needed to make models workable.
In order to avoid installation bloat for non TS users, you must install the following typing packages manually:
-`@types/node` (this is universally required)
-`@types/validator`
-`@types/bluebird`
## Usage
...
...
@@ -129,11 +124,9 @@ async function stuff() {
}
```
## Legacy `.define` usage
## Usage of `sequelize.define`
TypeScript doesn't know how to generate a `class` definition when we use the legacy `.define`,
therefor we need to do some manual work and declare an interface and a type and eventually cast
the result of `.define` to the _static_ type.
TypeScript doesn't know how to generate a `class` definition when we use the `sequelize.define` method to define a Model. Therefore, we need to do some manual work and declare an interface and a type, and eventually cast the result of `.define` to the _static_ type.
```ts
// We need to declare an interface for our model that is basically what our class would be
...
...
@@ -147,7 +140,7 @@ type MyModelStatic = typeof Model & {