The perfect ORM for hapi developers
Query data from MySQL, PostgreSQL & SQL Server databases in hapi apps with Prisma – a better ORM for JavaScript and TypeScript.
Why Prisma and hapi?
Smooth integration
Prisma fits perfectly well into the flexible architecture of hapi, no matter if you're building REST or GraphQL APIs.
Higher productivity
Prisma's gives you autocompletion for database queries, great developer experience and full type safety.
Type-safe database client
Prisma Client ensures fully type-safe database queries with benefits like autocompletion - even in JavaScript.
Intuitive data modeling
Prisma's declarative modeling language is simple and lets you intuitively describe your database schema.
Easy database migrations
Generate predictable and customizable SQL migrations from the declarative Prisma schema.
Designed for building APIs
Prisma Client reduces boilerplates by providing queries for common API features (e.g. pagination, filters, ...).
How Prisma and hapi fit together
prismaPlugin
A prismaPlugin is the foundation for the domain- or model-specific plugins. The PrismaClient instance it contains provides the database interface to the rest of the application.
While Prisma works great with hapi, you can use it with any other web framework like koa.js, Fastify or FeathersJS as well.
import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from '../generated/client'
import Hapi from '@hapi/hapi'
declare module '@hapi/hapi' {
interface ServerApplicationState {
prisma: PrismaClient
}
}
const prismaPlugin = {
name: 'prisma',
register: async function(server) {
const prisma = new PrismaClient({
adapter: new PrismaPg({
connectionString: process.env.DATABASE_URL,
}),
})
server.app.prisma = prisma
server.ext({
type: 'onPostStop',
method: async (server) => { server.app.prisma.$disconnect() },
})
},
}
export default prismaPluginusersPlugin
Once your prismaPlugin is ready, you can use it to query your database inside other plugins, for example to query your models.
In this case, it's demonstrated how a new user record can be created via the plugin.
import Hapi from '@hapi/hapi'
const usersPlugin = {
name: 'app/users',
dependencies: ['prisma'],
register: async function(server) {
server.route([
{
method: 'POST',
path: '/user',
handler: createUserHandler,
},
])
},
}
export default usersPlugin
async function createUserHandler(request, h) {
const { prisma } = request.server.app
const payload = request.payload
const createdUser = await prisma.user.create({
data: { name: payload.name, email: payload.email },
})
return h.response(createdUser).code(201)
}Prisma schema
The Prisma schema uses Prisma's declarative modeling language to define your database schema. It makes data modeling easy and intuitive, especially when it comes to modeling relations.
model User {
id Int @default(autoincrement()) @id
name String?
email String @unique
posts Post[]
}
model Post {
id Int @default(autoincrement()) @id
published Boolean? @default(false)
title String
content String?
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}Featured Prisma & hapi Examples
Building a modern backend with TypeScript, PostgreSQL & Prisma
A tutorial series for building a modern backend with hapi and Prisma.
REST API starter kit
A ready-to-run example project for a REST API with a SQLite database.
GraphQL API starter kit
A ready-to-run example project for a GraphQL API with a SQLite database.