本文へ移動
Zomer Gregorio

Zomer Gregorio

ソフトウェアエンジニア

履歴書
フロントエンド
  • TypeScript
  • React
  • Next.js
  • TanStack
  • Tailwind CSS
バックエンド
  • Node.js
  • Hono
  • Express
  • Django
データ
  • PostgreSQL
  • Redis
  • Drizzle
  • Prisma
インフラ/ツール
  • Docker
  • GitHub Actions
  • Cloudflare
  • Vercel
ブログ

Integrating NestJS, Supabase, & Groq: A Full-Stack AI Application

· nestjs · supabase · groq · typescript · fullstack

Build a robust, AI-powered application by integrating NestJS, Supabase, and Groq API with TypeScript. Learn practical database management and modern build tool usage.

更新情報を受け取る

新しい記事を公開したときに短いお知らせを送ります。メールまたはブラウザの購読情報は通知配信のためだけに保存され、いつでも解除できます。アカウントや追跡用プロフィールは不要です。

通知を開始する前に確認メールでの承認が必要です。

Building AI-Powered Applications with NestJS, Supabase, and Groq

Modern web development thrives on powerful integrations. This post demonstrates how to build a scalable, AI-driven application by seamlessly combining NestJS, Supabase, and the Groq API, all powered by TypeScript and efficient build tooling.

1. Project Setup: NestJS & TypeScript

NestJS provides a robust, scalable backend framework built on Node.js, ideal for enterprise-grade applications. TypeScript ensures type safety and enhances developer experience across the stack.

First, set up your NestJS project:

// Install NestJS CLI
npm i -g @nestjs/cli
 
// Create a new project
nest new ai-app --strict --language ts
cd ai-app

NestJS uses webpack (via @nestjs/cli) for bundling and tsc for TypeScript compilation, streamlining the build process for both development and production. These tools ensure your code is efficiently compiled and bundled for deployment.

2. Database & Authentication with Supabase

Supabase offers a powerful open-source Firebase alternative, providing a PostgreSQL database, authentication, and real-time capabilities. It simplifies backend operations, allowing developers to focus on core features.

Install the Supabase client library:

npm i @supabase/supabase-js @nestjs/config

Configure Supabase in your NestJS application. Use environment variables for API keys and URLs.

// src/config/supabase.config.ts
import { registerAs } from '@nestjs/config';
 
export default registerAs('supabase', () => ({
  url: process.env.SUPABASE_URL,
  key: process.env.SUPABASE_KEY,
}));
 
// src/supabase/supabase.service.ts
import { Injectable, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { createClient, SupabaseClient } from '@supabase/supabase-js';
 
@Injectable()
export class SupabaseService implements OnModuleInit {
  public client: SupabaseClient;
 
  constructor(private configService: ConfigService) {}
 
  onModuleInit() {
    const supabaseConfig = this.configService.get('supabase');
    this.client = createClient(supabaseConfig.url, supabaseConfig.key);
  }
 
  async getItems() {
    const { data, error } = await this.client.from('items').select('*');
    if (error) throw error;
    return data;
  }
}

Database management with Supabase is intuitive, offering a web UI for schema design, data manipulation, and user management. For complex changes, consider using Supabase's migration tools or a dedicated ORM like TypeORM with its CLI.

3. Integrating Groq API for AI Inference

Groq provides incredibly fast large language model (LLM) inference, ideal for real-time AI capabilities in your application. Its speed makes it suitable for interactive features like content summarization or generation.

Install the Groq client library:

npm i groq-sdk

Create a service to interact with Groq:

// src/groq/groq.service.ts
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import Groq from 'groq-sdk';
 
@Injectable()
export class GroqService {
  private groq: Groq;
 
  constructor(private configService: ConfigService) {
    this.groq = new Groq({
      apiKey: this.configService.get<string>('GROQ_API_KEY'),
    });
  }
 
  async generateContent(prompt: string): Promise<string> {
    const chatCompletion = await this.groq.chat.completions.create({
      messages: [
        {
          role: 'user',
          content: prompt,
        },
      ],
      model: 'mixtral-8x7b-32768',
    });
    return chatCompletion.choices[0]?.message?.content || 'No content generated.';
  }
}

4. Orchestrating Data & AI in NestJS

Combine Supabase data and Groq AI within a NestJS controller or service to create powerful features. This example fetches items and uses Groq to generate a summary for each.

// src/app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { SupabaseService } from './supabase/supabase.service';
import { GroqService } from './groq/groq.service';
 
@Controller('api')
export class AppController {
  constructor(
    private readonly supabaseService: SupabaseService,
    private readonly groqService: GroqService,
  ) {}
 
  @Get('summarize-items')
  async getSummarizedItems() {
    const items = await this.supabaseService.getItems();
    const summarizedItems = await Promise.all(
      items.map(async (item: { id: number; name: string; description: string }) => {
        const prompt = `Summarize the following item: ${item.name} - ${item.description}`;
        const summary = await this.groqService.generateContent(prompt);
        return { ...item, summary };
      }),
    );
    return summarizedItems;
  }
}

Conclusion

By integrating NestJS for backend logic, Supabase for scalable data and authentication, and Groq for blazing-fast AI inference, you can build sophisticated, modern web applications. TypeScript ensures code quality, while NestJS's build tools handle the underlying compilation and bundling, allowing you to focus on delivering innovative features. This stack offers a robust foundation for your next full-stack AI project.