Using ORMs and Migration Frameworks with Encore.ts
Encore provides built-in support for ORMs and migration frameworks by offering named databases and SQL-based migration files. For developers who prefer not to write raw SQL, Encore allows seamless integration with popular ORMs and migration tools.
Overview
Encore’s approach to database management is flexible. It uses standard SQL migration files, allowing integration with ORMs like Sequelize and migration tools like Atlas.
- ORM Compatibility: If your ORM can connect to a database via a standard SQL driver, it will work with Encore.
- Migration Tool Compatibility: If your migration tool generates SQL migration files without additional customization, it can be used with Encore.
Connecting to a Database
Encore provides the SQLDatabase
class, which allows you to create a named database and retrieve its connection string. This connection string can be used by your chosen ORM or migration framework to establish a database connection.
Example setup:
import { SQLDatabase } from "encore.dev/storage/sqldb";
// Initialize a named database with migration directory
const SiteDB = new SQLDatabase("siteDB", {
migrations: "./migrations",
});
// Retrieve the connection string for ORM use
const connStr = SiteDB.connectionString;
Example ORM implementations
Here are some guides to using different ORMs with Encore:
- Using Knex.js with Encore
- Using Sequelize with Encore
- Using Drizzle with Encore
- Using Prisma with Encore
This setup enables Encore to support a wide variety of ORMs and migration frameworks, making database management both flexible and straightforward.