A
A
Alexander2021-10-02 00:03:04
PostgreSQL
Alexander, 2021-10-02 00:03:04

How to add an array of strings to a sequelize model?

Can you please tell me how to add an array of strings to the sequelize model?
I have a Product model that should have an array of strings with product colors. How can I implement the ability to add colors to the model at creation?

const Product = sequelize.define('product', {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  img: { type: DataTypes.STRING, allowNull: false },
  name: { type: DataTypes.STRING, unique: true, allowNull: false },
  price: { type: DataTypes.INTEGER, allowNull: false },
  rating: { type: DataTypes.INTEGER, defaultValue: 0 },
  //colors []
})

async create(req: Request & { files: any }, res: Response, next: NextFunction) {
    try {
      const { name, price, typeId, categoryId, colorsArr } = req.body
      const { img } = req.files
      let fileName = uuidv4() + '.jpg' //create filename to send db
      img.mv(path.resolve(__dirname, '..', 'static', fileName)) 
      const product = await Product.create({ name, price, img: fileName, typeId, categoryId, colorsArr })
      return res.json(product)
    } catch (e) {
      next(ApiError.BadRequest(e.message))
    }
  }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question