mirror of
https://github.com/bubblecup-12/VogelSocialMedia.git
synced 2025-07-07 04:08:46 +00:00
Database initialized
This commit is contained in:
parent
56c14f8471
commit
dc8eede266
7 changed files with 431 additions and 10 deletions
|
@ -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}`);
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue