Introducing the Drizzle Session Storage Adapter for Shopify App Developers

Cover image for Introducing the Drizzle Session Storage Adapter for Shopify App Developers

More and more developers are using Turso to build Shopify Apps.

We noticed that developers using Turso and Drizzle ORM to build Shopify Apps were configuring "session storage" using workarounds and sometimes, alternate ORMs because there was no native adapter for Drizzle and Shopify.

Turso are huge fans of Drizzle, so it made sense to collaborate with them to create a new Shopify App Session Storage Adapter for SQLite.

Introducing @shopify/shopify-app-session-storage-drizzle!

This new adapter allows you to use Drizzle with your Shopify App to store session data in a SQLite database. This is a great option for developers who are already using Drizzle in their Shopify project, or for those who are looking for a more powerful and flexible way to store and work with session data.

Drizzle already works with all of the deployment platforms and frameworks that Shopify supports, so it made sense to extend the Shopify ecosystem with support for Drizzle.

Get started by installing the new adapter:

npm install @shopify/shopify-app-session-storage-drizzle

Inside your project, make sure to define the table schema for the session storage. Here is an example of how you can define the table schema using the sqlite-core package that is compatible with Turso:

import { sqliteTable, text, integer, blob } from 'drizzle-orm/sqlite-core';

export const sessionTable = sqliteTable('session', {
  id: text('id').primaryKey(),
  shop: text('shop').notNull(),
  state: text('state').notNull(),
  isOnline: integer('isOnline', { mode: 'boolean' }).notNull().default(false),
  scope: text('scope'),
  expires: text('expires'),
  accessToken: text('accessToken'),
  userId: blob('userId', { mode: 'bigint' }),
});

Now initialize a new libSQL client, and pass it to drizzle with the schema:

import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client';

import * as schema from './schema';

export const client = createClient({
  url: process.env.DATABASE_URL || 'file:./dev.db',
});

export const db = drizzle(client, { schema });

Make sure you apply the changes to the database for the adjusted schema.

Now inside your Shopify configuration, you can use the new adapter:

import { shopifyApp } from '@shopify/shopify-app-express';
import { DrizzleSessionStorageSQLite } from '@shopify/shopify-app-session-storage-drizzle';

import { db } from './db.server';
import { sessionTable } from './schema';

const storage = new DrizzleSessionStorageSQLite(db, sessionTable);

const shopify = shopifyApp({
  // ...
  sessionStorage: storage,
});

You're now ready to use the Drizzle for your App Session Storage. If you're using another database with Drizzle, we also made it possible to use Drizzle with MySQL and Postgres.

If you have any questions or feedback, please open an issue on GitHub, or join us in Discord.

scarf