Locallytics

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 prisma

Initialize Prisma (if not already set up):

npx prisma init

Generate Models

Run the Locallytics CLI to inject models into your schema:

npx locallytics generate

This adds Pageview and Event models to your schema.prisma file.

Generate Prisma Client

npx prisma generate
npx prisma db push

Use npx prisma migrate dev instead of db push for production environments.

Create Adapter

lib/analytics.ts
import { PrismaClient } from "./generated/prisma/client";
import { prismaAdapter } from "locallytics";

const prisma = new PrismaClient();
export const analyticsAdapter = prismaAdapter(prisma);

Use in Handler

app/api/analytics/route.ts
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-sqlite3

Update your schema:

schema.prisma
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
ParameterTypeRequiredDescription
prismaPrismaClientYesYour Prisma Client instance
optionsPrismaAdapterOptionsNoAdapter 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",
    },
  },
});

On this page