关于lin-cms的models问题

来源:15-6 现代大型Web架构讲解

如此消魂

2019-10-30

老师,lin-cms的models层是不是不支持一个文件定义多个表呀?我在一个文件里面init了多张表,为什么数据库里面只写入了最后一张表?
下面是源码

/**
 *  房源表,创建以下各表
 *  shops--商铺转让表(100) offices--写字楼表(200) warehouse--厂房仓库表(300)
 *  rentalshop--商铺出租表(400) renting--小区住宅出租表(500) selling--二手房出售表(600)
 */
'use strict';

const { InfoCrudMixin } = require('lin-mizar/lin/interface');
const { merge } = require('lodash');
const { Sequelize, Model } = require('sequelize');
const { db } = require('lin-mizar/lin/db');

//定义房源共有字段
const housePublicFields = {
  //房源编号
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  //标题
  title: {
    //最长30个字
    type: Sequelize.STRING(30),
    //不可为空
    allowNull: false
  },
  //租金
  rent: {
    //租金,单位元/月,最多八位.两位小数
    type: Sequelize.FLOAT(8, 2),
    //不可为空
    allowNull: false
  },
  //面积
  area: {
    //面积,单位平方米,最多五位.两位小数
    type: Sequelize.FLOAT(5, 2),
    //不可为空
    allowNull: false
  },
  //对应行政区域
  region: {
    //对应安阳行政区域
    type: Sequelize.TINYINT,
    //不可为空
    allowNull: false
  },
  //房源描述
  desc: {
    //描述内容10-5000个字
    type: Sequelize.TEXT,
    //不可为空
    allowNull: false
  },
  //联系人电话
  tel: {
    type: Sequelize.STRING(20),
    //不可为空
    allowNull: false
  },
  //地址
  addr: {
    type: Sequelize.STRING,
    //不可为空
    allowNull: false
  },
  //浏览量
  browse_num: {
    type: Sequelize.INTEGER,
    //默认为0
    defaultValue: 0
  },
  //收藏数
  fav_num: {
    type: Sequelize.INTEGER,
    //默认为0
    defaultValue: 0
  }
}

class Shops extends Model {
  toJSON () {
    let origin = {
      id: this.id,
      title: this.title,
      rent: this.rent,
      area: this.area,
      region: this.region,
      desc: this.desc,
      tel: this.tel,
      addr: this.addr,
      browse_num: this.browseNum,
      fav_num: this.favNum,
      cost: this.cost,
      trade: this.trade,
      shops_type: this.shopsType,
      state: this.state,
      isfacingstreet: this.isfacingstreet
    };
    return origin;
  }
}

class Offices extends Model {
  toJSON () {
    let origin = {
      id: this.id,
      title: this.title,
      rent: this.rent,
      area: this.area,
      region: this.region,
      desc: this.desc,
      tel: this.tel,
      addr: this.addr,
      browse_num: this.browseNum,
      fav_num: this.favNum,
      type: this.type,
      isregister: this.isregister,
      division: this.division,
      renovation: this.renovation,
      floor: this.floor,
      paymode: this.paymode
    };
    return origin;
  }
}

class Warehouse extends Model {
  toJSON () {
    let origin = {
      id: this.id,
      title: this.title,
      rent: this.rent,
      area: this.area,
      region: this.region,
      desc: this.desc,
      tel: this.tel,
      addr: this.addr,
      browse_num: this.browseNum,
      fav_num: this.favNum,
      division: this.division,
      paymode: this.paymode,
      floor: this.floor
    };
    return origin;
  }
}

class Rentalshop extends Model {
  toJSON () {
    let origin = {
      id: this.id,
      title: this.title,
      rent: this.rent,
      area: this.area,
      region: this.region,
      desc: this.desc,
      tel: this.tel,
      addr: this.addr,
      browse_num: this.browseNum,
      fav_num: this.favNum,
      type: this.type,
      isfacingstreet: this.isfacingstreet,
    };
    return origin;
  }
}

class Renting extends Model {
  toJSON () {
    let origin = {
      id: this.id,
      title: this.title,
      rent: this.rent,
      area: this.area,
      region: this.region,
      desc: this.desc,
      tel: this.tel,
      addr: this.addr,
      browse_num: this.browseNum,
      fav_num: this.favNum,
      community: this.community,
      type: this.type,
      floor: this.floor,
      facing: this.facing,
      decoration: this.decoration,
      elevator: this.elevator,
      parking: this.parking,
      claim: this.claim,
      paymode: this.paymode,
    };
    return origin;
  }
}

class Selling extends Model {
  toJSON () {
    let origin = {
      id: this.id,
      title: this.title,
      rent: this.rent,
      area: this.area,
      region: this.region,
      desc: this.desc,
      tel: this.tel,
      addr: this.addr,
      browse_num: this.browseNum,
      fav_num: this.favNum,
      community: this.community,
      housetype: this.housetype,
      type: this.type,
      floor: this.floor,
      facing: this.facing,
      decoration: this.decoration,
      heating: this.heating,
      elevator: this.elevator,
      parking: this.parking,
      property: this.property,
      years: this.years,
      price: this.price
    };
    return origin;
  }
}

//商铺转让表初始化
Shops.init(
  merge(
    housePublicFields, 
    {
      //转让费 
      cost: {
        //五位数,接受两位小数
        type: Sequelize.FLOAT(5, 2),
        //不可为空
        allowNull: false
      },
      //行业 
      trade: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //商铺类型
      shops_type: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //经营状态: 0->空置中 1->经营中
      state: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //是否临街: 0->不临街 1->临街
      isfacingstreet: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      }
    }
  ),
  merge(
    {
      tableName: 'shops',
      modelName: 'house',
      sequelize: db
    },
    InfoCrudMixin.options
  )
);

//写字楼表初始化
Offices.init(
  merge(
    housePublicFields, 
    {
      //写字楼类型
      type: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //是否可注册公司
      isregister: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //是否可分割
      division: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //装修情况
      state: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //楼层
      floor: {
        type: Sequelize.STRING(10),
        //不可为空
        allowNull: false
      },
      //支付方式
      paymode: {
        type: Sequelize.STRING(10),
        //不可为空
        allowNull: false
      },
    }
  ),
  merge(
    {
      tableName: 'offices',
      modelName: 'house',
      sequelize: db
    },
    InfoCrudMixin.options
  )
);

//厂房仓库表初始化
Warehouse.init(
  merge(
    housePublicFields, 
    {
      //是否可分割
      division: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //楼层
      floor: {
        type: Sequelize.STRING(10),
        //不可为空
        allowNull: false
      },
      //支付方式
      paymode: {
        type: Sequelize.STRING(10),
        //不可为空
        allowNull: false
      }
    }
  ),
  merge(
    {
      tableName: 'warehouse',
      modelName: 'house',
      sequelize: db
    },
    InfoCrudMixin.options
  )
);

//商铺出租表初始化
Rentalshop.init(
  merge(
    housePublicFields, 
    {
      //商铺类型
      type: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //是否临街
      isfacingstreet: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //楼层
      floor: {
        type: Sequelize.STRING(10),
        //不可为空
        allowNull: false
      },
      //支付方式
      paymode: {
        type: Sequelize.STRING(10),
        //不可为空
        allowNull: false
      }
    }
  ),
  merge(
    {
      tableName: 'rentalshop',
      modelName: 'house',
      sequelize: db
    },
    InfoCrudMixin.options
  )
);

//小区住房出租表初始化
Renting.init(
  merge(
    housePublicFields, 
    {
      //小区名称
      community: {
        type: Sequelize.STRING(30),
        //不可为空
        allowNull: false
      },
      //户型
      type: {
        type: Sequelize.STRING(20),
        //不可为空
        allowNull: false
      },
      //楼层
      floor: {
        type: Sequelize.STRING(10),
        //不可为空
        allowNull: false
      },
      //房屋朝向
      facing: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //装修情况
      decoration: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //有无电梯
      elevator: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //有无车位
      parking: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //出租要求
      claim: {
        type: Sequelize.STRING(30),
        //不可为空
        allowNull: false
      },
      //付款方式
      paymode: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      }
    }
  ),
  merge(
    {
      tableName: 'renting',
      modelName: 'house',
      sequelize: db
    },
    InfoCrudMixin.options
  )
);

//小区住房出售表初始化
Selling.init(
  merge(
    housePublicFields, 
    {
      //小区名称
      community: {
        type: Sequelize.STRING(30),
        //不可为空
        allowNull: false
      },
      //户型
      type: {
        type: Sequelize.STRING(20),
        //不可为空
        allowNull: false
      },
      //楼层
      floor: {
        type: Sequelize.STRING(10),
        //不可为空
        allowNull: false
      },
      //房屋朝向
      facing: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //装修情况
      decoration: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //有无电梯
      elevator: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //有无车位
      parking: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      },
      //出租要求
      claim: {
        type: Sequelize.STRING(30),
        //不可为空
        allowNull: false
      },
      //付款方式
      paymode: {
        type: Sequelize.TINYINT,
        //不可为空
        allowNull: false
      }
    }
  ),
  merge(
    {
      tableName: 'senting',
      modelName: 'house',
      sequelize: db
    },
    InfoCrudMixin.options
  )
);

module.exports = { Shops, Offices, Warehouse, Rentalshop, Renting, Selling };

写回答

1回答

7七月

2019-10-30

这个和写几个model没关系,模型只有用到了才会创建

1
4
如此消魂
回复
7七月
谢谢老师,我知道我哪里错了。
2019-10-31
共4条回复

Node.js+Koa2+MySQL打造前后端分离精品项目《旧岛》

理解异步编程本质/培养面向对象思维,独立完成Node.js服务端开发

2223 学习 · 878 问题

查看课程