How to use MySQL JSON datatype with Sequelize
Asked Answered
F

1

10

I'm trying to use MySQL 5.7 with Sequelize, and I want to use the JSON datatype for the attributes field on my users table to keep track of user attributes such as home_phone, mobile_phone, work_phone, address and several other attributes/settings that every user may or may not have.

I've been able to locate the documentation for performing selects here: http://docs.sequelizejs.com/manual/tutorial/querying.html#json.

I'm struggling to find documentation on how I would perform create, update and delete.

I guess I could always just do a raw query, but is there a sequelize way to do this?

Update 1

I'm specifically looking for how to perform a query like this in sequelize:

update Users 
set user_attributes = JSON_SET(user_attributes, "$.phone", "5554443333") 
where id=7;
Florinda answered 4/4, 2018 at 20:2 Comment(0)
M
6

Use Sequelize.fn method:

User.update({
  user_attributes: Sequelize.fn("JSON_SET", Sequelize.col('user_attributes'), "$.phone", "5554443333")
}, {
  where: {id: 7}
})
Medina answered 30/11, 2018 at 22:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.