官术网_书友最值得收藏!

Your first database model

We will use the Sequelize CLI to generate our first database model. Install it globally with the following command:

npm install -g sequelize-cli

This gives you the ability to run the sequelize command inside of your Terminal.

The Sequelize CLI allows us to generate the model automatically. This can be done by running the following command:

sequelize model:generate --models-path src/server/models --migrations-path src/server/migrations --name Post --attributes text:text

Sequelize expects us to run the command in the folder in which we have run sequelize init, by default. Our file structure is a bit different, because we have two layers with src/server. For this reason, we specify the path manually, with the first two parameters: --models-path and --migrations-path.

The --name parameter gives our model a name under which it can be used. The --attributes option specifies the fields that the model should include.

If you are increasingly customizing your setup, you may want to know about other options that the CLI offers. You can view the manual for every command easily, by appending --help as an option:  sequelize model:generate --help.

This command creates a post.js model file in your models folder, and a database migration file, named XXXXXXXXXXXXXX-create-post.jsin your migrations folder. The X is the timestamp when generating the files with the CLI. You will see how migrations work in the next section.

The following model file was created for us:

'use strict';

module.exports = (sequelize, DataTypes) => {
var Post = sequelize.define('Post', {
text: DataTypes.TEXT
}, {});

Post.associate = function(models) {
// associations can be defined here
};

return Post;
};

We are using the define Sequelize function to create a database model:

  • The first parameter is the name of the database model.
  • The second option is the field configuration for this model.
There are many more options that Sequelize offers us to customize our database models. If you want to look up which options are available, you can find them at http://docs.sequelizejs.com/manual/tutorial/models-definition.html.

A post object has the id, text, and user properties. The user will be a separate model, as seen in the GraphQL schema. Consequently, we only need to configure the id and text as columns of a post.

The id is the key that uniquely identifies a data record from our database. We do not specify this when running the model:generate command, because it is generated by MySQL automatically.

The text column is just a MySQL TEXT field, which allows us to write pretty long posts. Alternatively, there are other MySQL field types, with MEDIUMTEXT, LONGTEXT, and BLOB, which could save more characters. A regular TEXT column should be fine for our use case.

The Sequelize CLI created a model file, exporting a function that, after execution, returns the real database model. You will soon see why this a great way of initializing our models.

Let's take a look at the migration file that is also created by the CLI.

主站蜘蛛池模板: 包头市| 绍兴市| 怀化市| 三明市| 临西县| 阿克陶县| 普安县| 友谊县| 泸州市| 宁蒗| 游戏| 高淳县| 嘉兴市| 多伦县| 靖宇县| 井冈山市| 千阳县| 遂川县| 武夷山市| 甘南县| 鸡东县| 荆门市| 桃园市| 韩城市| 大化| 渝北区| 绥化市| 察哈| 萍乡市| 临西县| 竹山县| 筠连县| 平果县| 白银市| 永安市| 增城市| 万源市| 会东县| 韶关市| 珲春市| 汉川市|