FlintSable
5/28/2017 - 4:00 AM

Create models

Create models

module.exports = function(sequelize, DataTypes) {
  var WorkOrder = sequelize.define("WorkOrder", {
    request: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        len: [1]
      }
    },
    apartment_number: DataTypes.STRING,
    status: DataTypes.STRING,
    notes: DataTypes.TEXT,
    service_cost: DataTypes.DECIMAL(10,2),
    permission_to_enter: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: "no"
    },
    paid: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    completed: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    date_completed: DataTypes.STRING,
    soft_delete: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    }
  },
    {
      // We're saying that we want our Tenant to have Work Orders
      classMethods: {
        associate: function(models) {
          // A Resident (foreignKey) is required or a Work Order can't be made
          WorkOrder.belongsTo(models.Tenant);
          WorkOrder.belongsTo(models.Contractor, {
            foreignKey: {
              allowNull: true
            }
          });
        }
      }
    });
  return WorkOrder;
  };

module.exports = function(sequelize, DataTypes) {
  var Tenant = sequelize.define("Tenant", {
    name: DataTypes.STRING,
    email: {
      type: DataTypes.STRING,
      validate: {
      isEmail: true// checks for email format (foo@bar.com)
      }
    },
    street_address: DataTypes.STRING,
    apartment_number: DataTypes.STRING,
    city: DataTypes.STRING,
    state: DataTypes.STRING,
    zip_code: DataTypes.STRING,
    no_of_occupants: DataTypes.INTEGER,
    rent: DataTypes.DECIMAL(10,2),
    rent_last_paid: DataTypes.DATEONLY,
    total_rent_paid: DataTypes.DECIMAL(10,2),
    lease_start: DataTypes.DATEONLY,
    lease_end: DataTypes.DATEONLY,
    // tenants default to false parking status
    parking_permit: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    parking_permit_no: DataTypes.INTEGER,
    parking_permit_status: DataTypes.STRING,
    soft_delete: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    }
  },
    // Here we'll pass a second "classMethods" object into the define method
    // This is for any additional configuration we want to give our models
    {
      // We're saying that we want our Tenant to have Work Orders
      classMethods: {
        associate: function(models) {
          Tenant.hasMany(models.WorkOrder);
          // Tenant.belongsTo(models.Apartment);
        }
      }
    }
  );
  return Tenant;
};
npm install --save sequelize
npm install --save databaseOfChoice

npm install -g sequelize-cli
sequelize model:create
sequelize help:model:create


ex) creates a model named Page and an associated migration file
sequelize model:create --name Page --attributes "name:string, text:text, url:string"

sequelize model:create --name User --attributes first_name:string, last_name:string, bio:text

sequelize model:create --name User --attributes 'first_name:string, last_name:string, bio:text'

--name SingularModelName // referes to plural table name