UNPKG

create-filip-app

Version:

A modern CLI tool for creating Next.js applications with best practices, shadcn components, and MongoDB integration.

151 lines (127 loc) 4.25 kB
// Default Prisma schema setup const prismaSchema = `// This is your Prisma schema file, // learn more about it in the docs: https://pris.ly/d/prisma-schema generator client { provider = "prisma-client-js" } datasource db { provider = "mongodb" url = env("DATABASE_URL") } model User { id String @id @default(auto()) @map("_id") @db.ObjectId name String? email String? @unique emailVerified DateTime? image String? accounts Account[] sessions Session[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } model Account { id String @id @default(auto()) @map("_id") @db.ObjectId userId String @db.ObjectId type String provider String providerAccountId String refresh_token String? @db.String access_token String? @db.String expires_at Int? token_type String? scope String? id_token String? @db.String session_state String? user User @relation(fields: [userId], references: [id], onDelete: Cascade) @@unique([provider, providerAccountId]) } model Session { id String @id @default(auto()) @map("_id") @db.ObjectId sessionToken String @unique userId String @db.ObjectId expires DateTime user User @relation(fields: [userId], references: [id], onDelete: Cascade) } model VerificationToken { id String @id @default(auto()) @map("_id") @db.ObjectId identifier String token String @unique expires DateTime @@unique([identifier, token]) } `; // MongoDB connection utility for NextAuth const mongodbClientUtil = `// lib/mongodb.ts import { MongoClient } from "mongodb" if (!process.env.DATABASE_URL) { throw new Error("Please add your MongoDB connection string to .env") } const uri = process.env.DATABASE_URL let client: MongoClient let clientPromise: Promise<MongoClient> if (process.env.NODE_ENV === "development") { // In development mode, use a global variable so that the value // is preserved across module reloads caused by HMR (Hot Module Replacement). let globalWithMongo = global as typeof globalThis & { _mongoClientPromise?: Promise<MongoClient> } if (!globalWithMongo._mongoClientPromise) { client = new MongoClient(uri) globalWithMongo._mongoClientPromise = client.connect() } clientPromise = globalWithMongo._mongoClientPromise } else { // In production mode, it's best to not use a global variable. client = new MongoClient(uri) clientPromise = client.connect() } export { clientPromise } `; // Example .env file with MongoDB database URL const envExample = `# Application NEXT_PUBLIC_APP_URL=http://localhost:3000 # NextAuth Configuration NEXTAUTH_URL=http://localhost:3000 NEXTAUTH_SECRET=$(openssl rand -base64 32) # OAuth Providers (examples) GITHUB_ID=your_github_client_id GITHUB_SECRET=your_github_client_secret GOOGLE_CLIENT_ID=your_google_client_id GOOGLE_CLIENT_SECRET=your_google_client_secret # Database - MongoDB DATABASE_URL="mongodb+srv://username:password@your-cluster.mongodb.net/your-database?retryWrites=true&w=majority" `; // Example of a simple API route using Prisma const userApiExample = `// app/api/users/route.ts import { NextResponse } from 'next/server'; import prisma from '@/lib/prisma'; export async function GET() { try { const users = await prisma.user.findMany(); return NextResponse.json(users); } catch (error) { return NextResponse.json({ error: 'Failed to fetch users' }, { status: 500 }); } } export async function POST(request: Request) { try { const { name, email } = await request.json(); const user = await prisma.user.create({ data: { name, email, }, }); return NextResponse.json(user, { status: 201 }); } catch (error) { return NextResponse.json({ error: 'Failed to create user' }, { status: 500 }); } } `; // ES module exports export { prismaSchema, mongodbClientUtil, envExample, userApiExample };