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.

What is Prisma?

Prisma is an open-source ORM that drastically simplifies data modeling, migrations, and data access for SQL databases in Node.js and TypeScript.

Query
// Creating a new record
await prisma.user.create({
firstName: “Alice”,
email: “alice@prisma.io”
})
Table
id firstName email
1 Bobby bobby@tables.io
2 Nilufar nilu@email.com
3 Jürgen jums@dums.edu
4 Alice alice@prisma.io

How Prisma and hapi fit together

Prisma is a next-generation ORM that's used to query your database in a hapi app. You can use it as an alternative to writing plain SQL queries, to using query builders like knex.js or to traditional ORMs like TypeORM, MikroORM and Sequelize.

While Prisma works great with hapi, you can use it with any other web framework like koa.js, Fastify or FeathersJS as well. Prisma can be used to build REST and GraphQL APIs and integrates smoothly with both microservices and monolothic architectures.

Diagram showing Prisma fitting in the Server part of the Client-server-database stack
clienthapi serverdatabase

Prisma and hapi Code Examples

Prisma provides a convenient database access layer that integrates perfectly with hapi.

The code below demonstrates various uses of Prisma when using hapi for building an API server.

prismaPlugin

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.

1import { PrismaClient } from '@prisma/client'
2import Hapi from '@hapi/hapi'
3
4declare module '@hapi/hapi' {
5 interface ServerApplicationState {
6 prisma: PrismaClient
7 }
8}
9
10const prismaPlugin = {
11 name: 'prisma',
12 register: async function(server) {
13 const prisma = new PrismaClient()
14
15 server.app.prisma = prisma
16
17 server.ext({
18 type: 'onPostStop',
19 method: async (server) => {
20 server.app.prisma.$disconnect()
21 },
22 })
23 },
24}
25
26export default prismaPlugin
usersPlugin
Prisma schema

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.

1import { PrismaClient } from '@prisma/client'
2import Hapi from '@hapi/hapi'
3
4declare module '@hapi/hapi' {
5 interface ServerApplicationState {
6 prisma: PrismaClient
7 }
8}
9
10const prismaPlugin = {
11 name: 'prisma',
12 register: async function(server) {
13 const prisma = new PrismaClient()
14
15 server.app.prisma = prisma
16
17 server.ext({
18 type: 'onPostStop',
19 method: async (server) => {
20 server.app.prisma.$disconnect()
21 },
22 })
23 },
24}
25
26export default prismaPlugin

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 predictible 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, ...).

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

Get Involved

Join the hapi + Prisma Community

Join us on Slack

Get help and engage with thousands of hapi and Prisma developers on Slack

Join Prisma Slack

Showcase on Twitter

We’d love to see what you’re building with hapi and Prisma

Tweet #prisma+hapi

Get Stickers

Show your hapi + Prisma love with custom free stickers

Order stickers