Database initialized

This commit is contained in:
Kai Ritthaler 2025-05-06 21:25:39 +02:00 committed by Rudi Regentonne
parent 56c14f8471
commit dc8eede266
7 changed files with 431 additions and 10 deletions

View file

@ -1,13 +1,45 @@
import express, { Request, Response } from 'express';
import { PrismaClient } from '../src/generated/prisma'
import dotenv from 'dotenv';
dotenv.config();
const prisma = new PrismaClient();
const app = express();
const port = 3000;
type User = {
id: string;
name: string;
email: string;
password: string;
}
app.get('/', async (req: Request, res: Response) => {
try {
// Benutzer erstellen (nur einmal)
/*const createdUser = await prisma.user.create({
data: {
name: 'Alice',
email: 'alice@prisma.com',
password: '123456',
},
});*/
app.get('/', (req: Request, res: Response) => {
res.send('Hallo, Welt mit TypeScript!');
// Alle Benutzer abrufen
const users = await prisma.user.findMany();
// Namen extrahieren
const names = users.map((user:User) => user.name);
// Antwort zurück an den Client
res.send(`There are ${users.length} users: ${names.join(', ')}`);
} catch (error) {
console.error('Fehler bei der Datenbankoperation:', error);
res.status(500).send('Datenbankfehler');
}
});
app.listen(port, () => {
console.log(`Server läuft auf http://localhost:${port}`);
});