This article describes how to access PlanetScale from Sequelize. The related resources are shown below.
The source code before supporting access PlanetScale is shown below.
The source code after supporting access PlanetScale is shown below.
The points are shown below.
- Specify
ssl: {rejectUnauthorized: true}
for dialectOptions when creating an instance of Sequelize to enable SSL connections. - [Optional] Add a prefix to the table name.
- [Optional] Specify
as
for options when creating an association so that it can be accessed without a prefix. - Specify
constraints: false
as an option when creating associations to prevent foreign key constraints from being created. - [Optional] Add a prefix to the table name when issuing a query.
The SQL statement issued by executing the source code (before) is shown below. Only the CREATE statement and the INSERT statement are excerpted.
CREATE TABLE IF NOT EXISTS `users` (
`id` INTEGER NOT NULL auto_increment ,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `articles` (
`id` INTEGER NOT NULL auto_increment ,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NOT NULL,
`userId` INTEGER NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`userId`) REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE CASCADE
) ENGINE=InnoDB;
INSERT INTO `users` VALUES (NULL, NOW(), NOW())
The command example for outputting the execution result is show below.
DB_URL=mysql://ps_user:ps_pass@localhost:3306/ps_db \
node before.js
The SQL statement issued by executing the source code (before) is shown below. Only the CREATE statement and the INSERT statement are excerpted.
CREATE TABLE IF NOT EXISTS `psUsers` (
`id` INTEGER NOT NULL auto_increment ,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `psArticles` (
`id` INTEGER NOT NULL auto_increment ,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NOT NULL,
`userId` INTEGER NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
INSERT INTO `psUsers` VALUES (NULL, NOW(), NOW());
The command example for outputting the execution result is show below.
DB_URL=mysql://xxxxxxxxxxxx:pscale_pw_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@xxxxxxxxxxxx.ap-northeast-2.psdb.cloud/xxxx \
node after.js
The following two points can be read from the execution result.
- The ps prefix is added to the table names
- FOREIGN KEY constraints are not created
Since SSL is required on PlanetScale, you need to enable SSL by specifying ssl: {rejectUnauthorized: true}
for dialectOptions when creating an instance of sequelize. Also, since PlanetScale does not allow foreign key constraints, you neet to specify constraints: false
as options when creating associations to prevent foreign key constraints from being created. Tak_iwamoto explains in detail the point that foreign key constraints cannot be set in the following article.
What is PlanetScale and why it doesn't support foreign key constraints
PlanetScale's Free plan allows you to create only one database, so add a prefix to the table name to avoid duplicate table names. If you can create multiple databases for each app, you don't need to do this.
PlanetScale Pricing offers a Hobby plan that can be used free of charge up to a certain resource consumption. When combined with Vercel etc., even apps that require RDB can be operated for free. However, keep in mind that Vercel's Hobby plan is limited to non-commercial & hobby sites according to Vercel Pricing.
Thanks! That was helpful.