Query data from MySQL, PostgreSQL & SQL Server databases in hapi apps with Prisma – a better ORM for JavaScript and TypeScript.
Prisma makes working with data easy! It offers a type-safe Node.js & TypeScript ORM, global database caching, connection pooling, and real-time database events.
// Creating a new recordawait prisma.user.create({ firstName: “Alice”, email: “alice@prisma.io”})
id firstName email 1 Bobby bobby@tables.io2 Nilufar nilu@email.com3 Jürgen jums@dums.edu4 Alice alice@prisma.io
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.
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.
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: PrismaClient7 }8}9
10const prismaPlugin = {11 name: 'prisma',12 register: async function(server) {13 const prisma = new PrismaClient()14
15 server.app.prisma = prisma16
17 server.ext({18 type: 'onPostStop',19 method: async (server) => {20 server.app.prisma.$disconnect()21 },22 })23 },24}25
26export default 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: PrismaClient7 }8}9
10const prismaPlugin = {11 name: 'prisma',12 register: async function(server) {13 const prisma = new PrismaClient()14
15 server.app.prisma = prisma16
17 server.ext({18 type: 'onPostStop',19 method: async (server) => {20 server.app.prisma.$disconnect()21 },22 })23 },24}25
26export default prismaPlugin
Prisma fits perfectly well into the flexible architecture of hapi, no matter if you're building REST or GraphQL APIs.
Prisma's gives you autocompletion for database queries, great developer experience and full type safety.
Prisma Client ensures fully type-safe database queries with benefits like autocompletion - even in JavaScript.
Prisma's declarative modeling language is simple and lets you intuitively describe your database schema.
Generate predictible and customizable SQL migrations from the declarative Prisma schema.
Prisma Client reduces boilerplates by providing queries for common API features (e.g. pagination, filters, ...).
A tutorial series for building a modern backend with hapi and Prisma
A ready-to-run example project for a REST API with a SQLite database
A ready-to-run example project for a GraphQL API with a SQLite database
We have multiple channels where you can engage with members of our community as well as the Prisma team.