Prisma ORM
Use Locallytics with Prisma ORM.
Locallytics integrates with Prisma ORM using a clean injection pattern — you own your schema, and the CLI injects the required models.
Setup
Install Prisma
npm install @prisma/client prismaInitialize Prisma (if not already set up):
npx prisma initGenerate Models
Run the Locallytics CLI to inject models into your schema:
npx locallytics generateThis adds Pageview and Event models to your schema.prisma file.
Generate Prisma Client
npx prisma generate
npx prisma db pushUse npx prisma migrate dev instead of db push for production environments.
Create Adapter
import { PrismaClient } from "./generated/prisma/client";
import { prismaAdapter } from "locallytics";
const prisma = new PrismaClient();
export const analyticsAdapter = prismaAdapter(prisma);Use in Handler
import { createLocalyticsHandler } from "locallytics";
import { analyticsAdapter } from "@/lib/analytics";
const handler = await createLocalyticsHandler({
database: analyticsAdapter,
});
export const POST = handler.POST;Prisma 7 Support
Prisma ORM 7 requires driver adapters. Make sure to install the appropriate adapter for your database.
Install the driver adapter:
# PostgreSQL
npm install @prisma/adapter-pg pg
# MySQL
npm install @prisma/adapter-mariadb mariadb
# SQLite
npm install @prisma/adapter-better-sqlite3 better-sqlite3Update your schema:
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "postgresql" // or mysql, sqlite
}Pass the adapter to PrismaClient:
import { PrismaClient } from "./generated/prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";
import { prismaAdapter } from "locallytics";
const dbAdapter = new PrismaPg({
connectionString: process.env.DATABASE_URL,
});
const prisma = new PrismaClient({ adapter: dbAdapter });
const analyticsAdapter = prismaAdapter(prisma);See the Prisma 7 upgrade guide for more details.
Schema Models
The generate command adds these models:
model Pageview {
id Int @id @default(autoincrement())
pageUrl String @map("page_url")
referrer String?
sessionId String @map("session_id")
createdAt DateTime @default(now()) @map("created_at")
@@index([createdAt])
@@index([sessionId])
@@map("pageviews")
}
model Event {
id Int @id @default(autoincrement())
eventName String @map("event_name")
properties String?
sessionId String @map("session_id")
createdAt DateTime @default(now()) @map("created_at")
@@index([createdAt])
@@index([sessionId])
@@index([eventName])
@@map("events")
}API Reference
prismaAdapter
prismaAdapter(
prisma: PrismaClient,
options?: PrismaAdapterOptions
): DatabaseAdapter| Parameter | Type | Required | Description |
|---|---|---|---|
prisma | PrismaClient | Yes | Your Prisma Client instance |
options | PrismaAdapterOptions | No | Adapter configuration |
PrismaAdapterOptions
interface PrismaAdapterOptions {
provider?: "sqlite" | "postgresql" | "mysql";
}The provider field is optional and currently not used, but may be used for provider-specific optimizations in the future.
Query with Prisma
You can also query analytics data directly with Prisma:
// Get recent pageviews
const pageviews = await prisma.pageview.findMany({
where: {
createdAt: {
gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
},
},
orderBy: { createdAt: "desc" },
});
// Get event counts
const eventCounts = await prisma.event.groupBy({
by: ["eventName"],
_count: true,
orderBy: {
_count: {
eventName: "desc",
},
},
});