I am building an application that uses Node/Express and MySQL with Sequelize as the ORM. I want to have a datatype of Array, but the sequelize docs says this is limited to postgres only.
Basically, if I have a users table that has 3 columns for example (name, phone, favColors), I want favColors to get populated with an array of string values retrieved from the user. How can I do this?
Although an array is one of the most common data types in the world of programming, MySQL actually doesn't support saving an array type directly. You can't create a table column of array type in MySQL. The easiest way store array type data in MySQL is to use the JSON data type.
If you give the datatype as TEXT in Sequelize It will automatically convert that field to NVARCHAR(MAX ) for SQL Server database. But it is mentioned in Sequelize github repository from line 79 to 90. Show activity on this post. postgres supports Upto 1G for BOTH TEXT AND VARCHAR.
Sequelize is a Node. js-based Object Relational Mapper that makes it easy to work with MySQL, MariaDB, SQLite, PostgreSQL databases, and more. An Object Relational Mapper performs functions like handling database records by representing the data as objects.
To insert new rows into your SQL table, you need to first create a Sequelize Model object. In Sequelize, a Model is an object that represents your SQL table, giving information to Sequelize about the name, the columns, and their data types.
You can use getter/setter functions for this:
favColors: {
type: Sequelize.STRING,
allowNull: false,
get() {
return this.getDataValue('favColors').split(';')
},
set(val) {
this.setDataValue('favColors',val.join(';'));
},
}
more info: https://sequelize.org/master/manual/getters-setters-virtuals.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With