Prismaの開発者向けデータベースツール(世界で**最も人気のあるTypeScript ORM**と、初の**コールドスタートのないサーバーレスデータベース**)で、高性能かつ型安全なNestJSアプリを構築しましょう。
NestJSをPrismaでお試しくださいユニカーネル上に構築されたPrisma Postgresは、ベアメタルサーバー上で動作し、最高のパフォーマンスと無限のスケーラビリティを提供します。
従量課金制、インフラ管理不要、そしてコールドスタートゼロの初のサーバーレスデータベースです。
任意のデータベースクエリにキャッシュ戦略を追加すると、その結果がユーザーの近くにキャッシュされ、最高のパフォーマンスとUXを実現します。
Prisma ORMは型安全なORMのパイオニアです。NestJSと組み合わせることで、強力な型付けにより高い生産性と信頼性を実現します。
Prismaのサービスベースの設計は、NestJSの依存性注入システムに自然に適合し、アプリケーションのモジュール全体でデータベースアクセスを可能にします。
NestJSとPrismaはどちらも活発なコミュニティを持っており、サポート、楽しいイベント、素晴らしい開発者を見つけることができます。
Prisma Postgresは、NestJSの依存性注入システムとモジュール型アーキテクチャに完璧に統合されます。`PrismaClient`を拡張する`PrismaService`を作成することで、アプリケーション全体にデータベースアクセスを注入できます。
// src/prisma/prisma.service.tsimport { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';import { PrismaClient } from '@prisma/client';@Injectable()export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {async onModuleInit() {await this.$connect();}async onModuleDestroy() {await this.$disconnect();}}
NestJSのコントローラー/サービスパターンは、Prismaの型安全なクエリと自然に連携し、APIエンドポイントとデータベース操作の間を明確に分離します。ユーザーの`GET`および`POST`ルートを実装する方法を以下に示します
// src/users/users.controller.tsimport { Controller, Get, Post, Body } from '@nestjs/common';import { UsersService } from './users.service';@Controller('users')export class UsersController {constructor(private usersService: UsersService) {}@Get()findAll() {return this.usersService.findAll();}@Post()create(@Body() data: { name: string; email: string }) {return this.usersService.create(data);}}
これらのルートは`PrismaService`に以下のようにアクセスします
// src/users/users.service.tsimport { Injectable } from '@nestjs/common';import { PrismaService } from '../prisma/prisma.service';@Injectable()export class UsersService {constructor(private prisma: PrismaService) {}async findAll() {return this.prisma.user.findMany();}async create(data: { name: string; email: string }) {return this.prisma.user.create({data,});}}
Prisma Postgresは、NestJSの依存性注入システムとモジュール型アーキテクチャに完璧に統合されます。`PrismaClient`を拡張する`PrismaService`を作成することで、アプリケーション全体にデータベースアクセスを注入できます。
// src/prisma/prisma.service.tsimport { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';import { PrismaClient } from '@prisma/client';@Injectable()export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {async onModuleInit() {await this.$connect();}async onModuleDestroy() {await this.$disconnect();}}
NestJSのコントローラー/サービスパターンは、Prismaの型安全なクエリと自然に連携し、APIエンドポイントとデータベース操作の間を明確に分離します。ユーザーの`GET`および`POST`ルートを実装する方法を以下に示します
// src/users/users.controller.tsimport { Controller, Get, Post, Body } from '@nestjs/common';import { UsersService } from './users.service';@Controller('users')export class UsersController {constructor(private usersService: UsersService) {}@Get()findAll() {return this.usersService.findAll();}@Post()create(@Body() data: { name: string; email: string }) {return this.usersService.create(data);}}
これらのルートは`PrismaService`に以下のようにアクセスします
// src/users/users.service.tsimport { Injectable } from '@nestjs/common';import { PrismaService } from '../prisma/prisma.service';@Injectable()export class UsersService {constructor(private prisma: PrismaService) {}async findAll() {return this.prisma.user.findMany();}async create(data: { name: string; email: string }) {return this.prisma.user.create({data,});}}
Prismaを使用して本番環境でNestJSを構築するために必要なすべてを網羅したスターターキットです。
公式のNestJSドキュメントで、NestJSとPrismaの使用方法を学びましょう。
このnpmライブラリは、NestJSアプリケーションにPrisma ORMを統合するのに役立ちます。その`PrismaModule`は、コントローラー、リゾルバー、サービス、ガードなどで依存性注入を介して使用できる`PrismaService`へのアクセスを提供します。
Prismaを使用したNestJS REST APIの構築に関する包括的なチュートリアルシリーズです。
TypeORMからPrismaへのNestJSアプリの移行プロセスに関する詳細な記事です。