In this sample application, we demonstrate how you can test your RDS migrations locally using Prisma and LocalStack. Prisma is a database ORM for Node.js applications, and this sample application showcases how you can use Prisma to run migration, generate model for the client, and run a small test inserting and querying data into the database.
- LocalStack
- Docker
- Node.js &
npm makeawslocal
Start your LocalStack container with the LOCALSTACK_AUTH_TOKEN specified:
LOCALSTACK_AUTH_TOKEN=... DEBUG=1 localstack startInstall the dependencies required for the application:
npm installYou can run the following command to see if Prisma is correctly installed:
npx prismaYou can now create a local RDS database using the awslocal CLI:
awslocal rds create-db-cluster \
--db-cluster-identifier cluster1 \
--engine=aurora-postgresql \
--database-name testNote: Check the port in the response, since you might have to adjust the .env file with the right port in the database URL.
You can now run the migration using Prisma:
npx prisma migrate dev --name initThe following output should be displayed:
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "test", schema "public" at "0.0.0.0:4510"
Applying migration `20240213041046_init`
The following migration(s) have been created and applied from new schema changes:
migrations/
└─ 20240213041046_init/
└─ migration.sql
Your database is now in sync with your schema.
✔ Generated Prisma Client (4.16.2 | library) to ./node_modules/@prisma/client in 63msYou can now generate the models for the client using Prisma:
npx generateTo run a small test inserting and querying data into the DB, you can run the following command:
npx ts-node main.tsThe following output should be displayed:
[
{
id: 1,
email: 'alice@prisma.io',
name: 'Alice',
posts: [
{
id: 1,
createdAt: 2024-02-13T04:13:39.901Z,
updatedAt: 2024-02-13T04:13:39.901Z,
title: 'Hello World',
content: null,
published: false,
authorId: 1
}
],
profile: { id: 1, bio: 'I like turtles', userId: 1 }
}
]You can run another migration. A new schema is already created for you, and you just need to specify it while running the migration command.
npx prisma migrate dev --name init --schema=./prisma/schema-test.prismaThe following output should be displayed:
Environment variables loaded from .env
Prisma schema loaded from prisma/schema-test.prisma
Datasource "db": PostgreSQL database "test", schema "public" at "0.0.0.0:4510"
Applying migration `20240213041407_init`
The following migration(s) have been created and applied from new schema changes:
migrations/
└─ 20240213041407_init/
└─ migration.sql
Your database is now in sync with your schema.
✔ Generated Prisma Client (4.16.2 | library) to ./node_modules/@prisma/client in 62msThis code is licensed under the Apache 2.0 License.