انتقل إلى المحتوى الرئيسي

Session Management

Qumra apps use Prisma to persist OAuth sessions. The SDK requires a Session model in your database.

Database Schema

Add the following Session model to your Prisma schema:

prisma/schema.prisma
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}

datasource db {
provider = "sqlite"
}

model Session {
id String @id @unique @default(cuid())
store String @unique
isOnline Boolean @default(false)
accessToken String
userId String?
firstName String?
lastName String?
email String?
emailVerified Boolean? @default(false)
refreshToken String?
createdAt DateTime @default(now())
lastSeenAt DateTime @default(now())
}

Session Fields

FieldTypeRequiredDescription
idStringAutoAuto-generated unique identifier (CUID).
storeStringYesUnique store domain. One session per store.
isOnlineBooleanNoWhether this is an online (interactive) session.
accessTokenStringYesThe OAuth access token for API requests.
userIdString?NoThe ID of the authenticated user.
firstNameString?NoFirst name of the authenticated user.
lastNameString?NoLast name of the authenticated user.
emailString?NoEmail address of the authenticated user.
emailVerifiedBoolean?NoWhether the user's email has been verified.
refreshTokenString?NoOAuth refresh token for obtaining new access tokens.
createdAtDateTimeAutoTimestamp when the session was created.
lastSeenAtDateTimeAutoTimestamp of the last authenticated request.

Prisma Setup

1. Prisma Configuration

Create a prisma.config.ts file in your project root:

prisma.config.ts
import path from "node:path";
import type { PrismaConfig } from "prisma";

export default {
earlyAccess: true,
schema: path.join("prisma", "schema.prisma"),
} satisfies PrismaConfig;

2. Prisma Client

Create the Prisma client module with the BetterSqlite3 adapter:

prisma/lib/prisma.ts
import { PrismaClient } from "../../generated/prisma";
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";

const adapter = new PrismaBetterSqlite3({
url: process.env.DATABASE_URL ?? "file:./dev.db",
});

export const prisma = new PrismaClient({ adapter });

3. Running Migrations

Generate the Prisma client and run migrations:

# Generate the Prisma client
npx prisma generate

# Create and apply migrations
npx prisma migrate dev --name init

# (Optional) Open Prisma Studio to inspect data
npx prisma studio

Session Storage Interface

The SDK expects a sessionStorage object that conforms to the following interface:

interface SessionStorage {
session: {
upsert(params: {
where: { store: string };
update: Partial<Session>;
create: Session;
}): Promise<Session>;

findUnique(params: {
where: { store: string };
}): Promise<Session | null>;
};
}
Prisma Compatibility

The standard Prisma client with the Session model automatically satisfies this interface. No additional adapters or wrappers are needed.

Database Provider for Production

Use SQLite for development and switch to PostgreSQL for production by changing the provider and DATABASE_URL:

prisma/schema.prisma (production)
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
.env
DATABASE_URL="postgresql://user:password@localhost:5432/myapp?schema=public"