Node+TS+Koa+vue 商城全栈(二) sequelize 创建数据库及分析数据库结构

创建数据库
.\node_modules\.bin\sequelize db:create
创建迁移文件
.\node_modules\.bin\sequelize migration:create --name user
#### 创建数据库

> sequelize db:create

#### 构建数据迁移结构

> sequelize migration:create --name [迁移文件的名称]

### 数据库结构

- user : 用户表

用来存放用户基本信息(用户名、密码等)

- user-profile : 用户扩展信息

用来存放用户扩展信息的(性别、昵称、真实姓名、生日等)

- login-log : 用户登录日志

用来存放用户登录日志,用户每一次登录时间和登录IP等信息可以存放在这里

- category : 美食分类

用来存放美食相关的分类信息

- cookbook : 美食信息

用来存放具体的美食信息(标题、图片等)

- step : 美食烹饪步骤

用来存放美食具体步骤,在每个具体的步骤中有一个字段与cookbook表的id进行关联

- comment : 评论表

用来存放用户对某个具体的cookbook的评论

- favorite : 收藏

用来存放用户收藏的美食

创建迁移文件(用户表):

.\node_modules\.bin\sequelize migration:create --name user

migrations/20200204173530-create-user.js
'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
    /*
      Add altering commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.createTable('users', { id: Sequelize.INTEGER });
    */
    return queryInterface.createTable('user', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
        comment: '用户id'
      },

      username: {
        type: Sequelize.STRING(50),
        unique: true,
        allowNull: false,
        defaultValue: '',
        comment: '用户名'
      },

      password: {
        type: Sequelize.CHAR(32),
        allowNull: false,
        defaultValue: '',
        comment: '密码'
      },

      disabled: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false,
        comment: '是否是禁用状态'
      },

      mobile: {
        type: Sequelize.CHAR(12),
        unique: true,
        allowNull: false,
        defaultValue: '',
        comment: '手机号码'
      },

      email: {
        type: Sequelize.STRING(50),
        unique: true,
        allowNull: false,
        defaultValue: '',
        comment: '邮箱'
      },

      createdIpAt: {
        type: Sequelize.CHAR(15),
        allowNull: false,
        field: 'created_ip_at',
        defaultValue: '',
        comment: '用户注册的时候的ip'
      },

      updatedIpAt: {
        type: Sequelize.CHAR(15),
        allowNull: false,
        field: 'updated_ip_at',
        defaultValue: '',
        comment: '用户最新一次登录的ip'
      },

      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
        field: 'created_at',
        comment: '用户的注册时间'
      },

      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
        field: 'updated_at',
        comment: '用户最新一次登录时间'
      }

    }, {
        tableName: 'user',
        charset: 'utf8mb4',
        collate: 'utf8mb4_bin',
        indexes: [
          {}
        ]
      });
  },

  down: (queryInterface, Sequelize) => {
    /*
      Add reverting commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.dropTable('users');
    */
    return queryInterface.dropTable('user');
  }
};
根据迁移文件,创建数据库:
.\node_modules\.bin\sequelize db:migrate

 

posted @ 2020-01-26 21:30  每天都要进步一点点  阅读(374)  评论(0编辑  收藏  举报