Configuring your ORM
Objectârelational mapping libraries (ORMs) are the best way to bring your Sequin-synced data into your application. ORMs let us write expressive queries for our data using our programming language. This ORM query for Prisma generates a complex SQL query under the hood:Migrations
Most ORMs come with a migration tool. The migration tool lets you define a database migration in a file. That migration might add a new table or drop a column. When using Sequin-synced tables, Sequin is the âownerâ of those tables. That means that all migrations are taken care of for you. If you want to add a table, add the table in Sequinâs console and Sequin will add the table to your database. Most developers connect Sequin to the databases that they use in both production and development. This means managing migrations is not necessary for those environments. However, if you want to write automated tests against your Sequin tables, youâll likely need to add migrations to make Sequinâs tables and columns available to your test code.Defining ORM models
In an ORM, a model is a class that represents a table in the database. It serves as the link between your code and your relational database. A model typically corresponds to a single table in the database. The attributes or properties of the model class correspond to the columns of the table. An instance of the model represents a single row in the database table. Each ORM requires some effort to setup these models. Some, like Prisma, require a full schema definition which describes the database structure in an ORM-specific format (the ORMâs DSL or domain-specific language). Others, like ActiveRecord, only require that you setup the relationships between tables. In general, the process for setting up an ORM is:- For each table, list the schema
- Paste each tableâs schema into your ORM schema definitions, translating to your ORMâs DSL
- Setup the relationships between tables
- Set the primary key type for your ORM to the right type
customer
, you can run:
customer
, you might paste in a schema definition that looks like this:
- Reformat each line into a valid declaration
- Swap out data types as necessary
customer_id
on subscription
which references a customer
. With this field in place, we can now call subscription.customer
on any subscription to retrieve its corresponding customer. This is called a forward reference and is a key value that ORMs provide.
The easiest approach is to setup the relationship on the child first, as the field for the relationship lives there. Then you can add the corresponding field on the parent later. All forward references end in _id
, so an easy way to see which tables a given table is the child of is to run this query:
text
, as thatâs the ID type used by all of Sequinâs sources.
info
You may be able to skip some of these steps if your ORM has a schema generator. While itâs worth a Google search to see if your ORM has one, youâll usually still want to take a manual pass of the auto-generated files. Some of these generators miss relationships between tables or give odd names to back-refs or forward-refs.
Notes for specific ORMs
Below are setup notes for a few different ORMs. Even if you donât see your ORM below, many of the configuration notes apply across ORMs:Ecto (Elixir)
Ecto does not have schema generator. But fortunately Ecto declarations are concise and easy to generate (see above). Youâll want to set three module attributes for each one of your Ecto schemas:string
. If youâre syncing to a Postgres schema other than public
, youâll also want to set the @schema_prefix
.
If youâre generating many schemas, you can roll some of these module attributes up to a shared module like this:
ActiveRecord (Ruby)
ActiveRecord does not require schema definitions. So, for each table, you just need to:- Declare a model
- Specify the tableâs name (with the schema)
- Establish relationships between tables
Prisma (JavaScript)
While Prisma has a schema generator, there are two considerations:- Verbose default names for forward-refs and back-refs.
- No support for querying across schemas
stripe_
prefix, like stripe_customer
and stripe_subscription
. This feature is not exposed in our UI at the moment, so just get in touch if you need it.