Database

Get started | Expo Starter

Overview

Expo Starter uses Drizzle ORM for managing database schemas.

Why choose Drizzle ORM? Unlike other ORMs that abstract SQL and require learning both SQL and their API, Drizzle embraces SQL directly. This means you can use your existing SQL knowledge with minimal learning curve and fully leverage SQL’s capabilities. Drizzle is also dialect-specific, slim and serverless-ready.

Expo Starter v1 only supports PostgreSQL database, but we are working on adding support for other databases. Reach out to us if you need early access for other databases.

Database structure

The project includes a straightforward database schema to help you get started. It features the following tables:

database structure

Update schema workflow

When making changes to your database schema, such as adding a new table or modifying a column, you must update the schema in your codebase and then apply those changes to your database. Below is a step-by-step guide on how to do this, along with examples.

Example Schema Update

Let’s say you want to add a new column bio to an existing users table.

  1. Update the Schema in Code: Modify your Drizzle ORM schema definition in your project:
schema.ts
import { pgTable, serial, text } from 'drizzle-orm/pg-core';
 
export const userTable = pgTable('users', {
  id: serial('id').primaryKey(),
  name: text('name').notNull(),
  bio: text('bio').notNull(), // New column added
});
  1. Generate a Migration Script: To apply this schema change to your database, you need to create a migration script.
terminal
pnpm db:generate

This will create a script in your migrations directory (e.g., packages/db/migrations/0000_abnormal_wolverine.sql):

  1. Run the Migration Script: To apply the migration to your database, run the following script:
terminal
pnpm db:migrate

Here's an enhanced version of the documentation with added details and examples:


Using the updated schema

Reading the User Bio

Here’s how you might fetch and display a user's bio:

route.ts
import { db } from '@app/db';
import { userTable } from './schema';
 
const user = await db.select().from(userTable).where(userTable.id.eq(userId)).first();
console.log('User bio:', user.bio);

Updating the User Bio

To update a user's bio, you would do something like this:

updateUserBio.ts
import { db } from '@app/db';
import { userTable } from './schema';
 
 await db.update(userTable)
    .set({ bio: newBio })
    .where(userTable.id.eq(userId));

Drizzle Studio

Expo Starter Kit comes with bundled Drizzle Studio database browser and lets you launch it locally with one command :

terminal
pnpm db:studio

Open browser and navigate to https://local.drizzle.studio/ to access the Drizzle Studio.

Drizzle Studio

For more details how to use Drizzle ORM, refer to the official documentation.

On this page