This article explores how the array data type works in PostgreSQL both through SQL queries and through TypeORM
Storing arrays is not an obvious thing in the world of SQL databases. Solutions such as MySQL, MariaDB, or Microsoft SQL Server don’t have a straightforward column type for arrays.
This article explores how the array data type works in PostgreSQL both through SQL queries and through TypeORM. By learning how to operate on arrays through SQL, we can better understand what the Postgres database is capable of. This will helps us quite a bit in using arrays through TypeORM.
Because databases such as MySQL don’t have an array data type, we might have had to work around this issue. One solution would be to create additional tables to store data that we would conceptualize as an array. Another solution would be to utilize the JSON data type available in MySQL and PostgreSQL.
To define the array data type column, we can append the square brackets or use the ARRAY keyword. Let’s play with our post table a bit and add the paragraphs column instead of content.
ALTER TABLE post
DROP COLUMN content,
ADD COLUMN paragraphs text
ALTER TABLE post
DROP COLUMN content ,
ADD COLUMN paragraphs text ARRAY
The text is a column data type that stores strings of any length
Although we could provide the array’s size, it does not affect the behavior of the database and might only serve as documentation.
Building Modern Nodejs Application using Nestjs and TypeScript - In this article, we will see how to build REST API using Nestjs with TypeScript. Building Modern Nodejs Application using Nestjs and TypeScript