不要怂,就是干,撸起袖子干!

Commit 9866921f by Dana Woodman Committed by Sushant

docs(model): model definition docs for timestamps (#7802)

1 parent 33c5a31f
Showing with 44 additions and 13 deletions
# Model definition # Model definition
To define mappings between a model and a table, use the `define` method. Sequelize will then automatically add the attributes `createdAt` and `updatedAt` to it. So you will be able to know when the database entry went into the db and when it was updated the last time. If you do not want timestamps on your models, only want some timestamps, or you are working with an existing database where the columns are named something else, jump straight on to [configuration][0] to see how to do that. To define mappings between a model and a table, use the `define` method.
```js ```js
const Project = sequelize.define('project', { const Project = sequelize.define('project', {
...@@ -21,7 +20,7 @@ You can also set some options on each column: ...@@ -21,7 +20,7 @@ You can also set some options on each column:
```js ```js
const Foo = sequelize.define('foo', { const Foo = sequelize.define('foo', {
// instantiating will automatically set the flag to true if not set // instantiating will automatically set the flag to true if not set
flag: { type: Sequelize.BOOLEAN, allowNull: false, defaultValue: true}, flag: { type: Sequelize.BOOLEAN, allowNull: false, defaultValue: true },
// default values for dates => current time // default values for dates => current time
myDate: { type: Sequelize.DATE, defaultValue: Sequelize.NOW }, myDate: { type: Sequelize.DATE, defaultValue: Sequelize.NOW },
...@@ -29,28 +28,29 @@ const Foo = sequelize.define('foo', { ...@@ -29,28 +28,29 @@ const Foo = sequelize.define('foo', {
// setting allowNull to false will add NOT NULL to the column, which means an error will be // setting allowNull to false will add NOT NULL to the column, which means an error will be
// thrown from the DB when the query is executed if the column is null. If you want to check that a value // thrown from the DB when the query is executed if the column is null. If you want to check that a value
// is not null before querying the DB, look at the validations section below. // is not null before querying the DB, look at the validations section below.
title: { type: Sequelize.STRING, allowNull: false}, title: { type: Sequelize.STRING, allowNull: false },
// Creating two objects with the same value will throw an error. The unique property can be either a // Creating two objects with the same value will throw an error. The unique property can be either a
// boolean, or a string. If you provide the same string for multiple columns, they will form a // boolean, or a string. If you provide the same string for multiple columns, they will form a
// composite unique key. // composite unique key.
uniqueOne: { type: Sequelize.STRING, unique: 'compositeIndex'}, uniqueOne: { type: Sequelize.STRING, unique: 'compositeIndex' },
uniqueTwo: { type: Sequelize.INTEGER, unique: 'compositeIndex'} uniqueTwo: { type: Sequelize.INTEGER, unique: 'compositeIndex' },
// The unique property is simply a shorthand to create a unique constraint. // The unique property is simply a shorthand to create a unique constraint.
someUnique: {type: Sequelize.STRING, unique: true} someUnique: { type: Sequelize.STRING, unique: true },
// It's exactly the same as creating the index in the model's options. // It's exactly the same as creating the index in the model's options.
{someUnique: {type: Sequelize.STRING}}, { someUnique: { type: Sequelize.STRING } },
{indexes: [{unique: true, fields: ['someUnique']}]} { indexes: [ { unique: true, fields: [ 'someUnique' ] } ] },
// Go on reading for further information about primary keys // Go on reading for further information about primary keys
identifier: { type: Sequelize.STRING, primaryKey: true}, identifier: { type: Sequelize.STRING, primaryKey: true },
// autoIncrement can be used to create auto_incrementing integer columns // autoIncrement can be used to create auto_incrementing integer columns
incrementMe: { type: Sequelize.INTEGER, autoIncrement: true }, incrementMe: { type: Sequelize.INTEGER, autoIncrement: true },
// You can specify a custom field name via the "field" attribute: // You can specify a custom field name via the 'field' attribute:
fieldWithUnderscores: { type: Sequelize.STRING, field: "field_with_underscores" }, fieldWithUnderscores: { type: Sequelize.STRING, field: 'field_with_underscores' },
// It is possible to create foreign keys: // It is possible to create foreign keys:
bar_id: { bar_id: {
...@@ -72,6 +72,37 @@ const Foo = sequelize.define('foo', { ...@@ -72,6 +72,37 @@ const Foo = sequelize.define('foo', {
The comment option can also be used on a table, see [model configuration][0] The comment option can also be used on a table, see [model configuration][0]
## Timestamps
By default, Sequelize will add the attributes `createdAt` and `updatedAt` to your model so you will be able to know when the database entry went into the db and when it was updated last.
Note that if you are using Sequelize migrations you will need to add the `createdAt` and `updatedAt` fields to your migration definition:
```js
module.exports = {
up(queryInterface, Sequelize) {
return queryInterface.createTable('my-table', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
// Timestamps
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
})
},
down(queryInterface, Sequelize) {
return queryInterface.dropTable('my-table');
},
}
```
If you do not want timestamps on your models, only want some timestamps, or you are working with an existing database where the columns are named something else, jump straight on to [configuration ][0]to see how to do that.
## Data types ## Data types
Below are some of the datatypes supported by sequelize. For a full and updated list, see [DataTypes](/variable/index.html#static-variable-DataTypes). Below are some of the datatypes supported by sequelize. For a full and updated list, see [DataTypes](/variable/index.html#static-variable-DataTypes).
...@@ -137,7 +168,7 @@ If you are working with the PostgreSQL TIMESTAMP WITHOUT TIME ZONE and you need ...@@ -137,7 +168,7 @@ If you are working with the PostgreSQL TIMESTAMP WITHOUT TIME ZONE and you need
```js ```js
require('pg').types.setTypeParser(1114, stringValue => { require('pg').types.setTypeParser(1114, stringValue => {
return new Date(stringValue + "+0000"); return new Date(stringValue + '+0000');
// e.g., UTC offset. Use any offset that you would like. // e.g., UTC offset. Use any offset that you would like.
}); });
``` ```
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!