import { Pool } from 'pg';
const pool = new Pool({
user: 'blog_user', host: 'localhost', database: 'blog_db', password: 'pass' });
interface Post { id: number; title: string; content: string; date: string }
async function getPostsByTag(tag: string): Promise<Post[]> {
const result = await pool.query(
`SELECT p.id, p.title, p.content, p.date
FROM posts p
JOIN post_tags pt ON p.id = pt.post_id JOIN tags t ON t.id = pt.tag_id
WHERE t.name = $1`,
[tag]
);
return result.rows;
}
import { MongoClient } from 'mongodb';
const uri = 'mongodb://localhost:27017';
const client = new MongoClient(uri);
interface Post {
_id: string;
title: string; content: string; date: string; tags: string[] }
async function getPostsByTag(tag: string): Promise<Post[]> {
await client.connect();
const db = client.db('blog_db');
const posts = await db.collection('posts').find({ tags: tag }).toArray();
return posts as Post[];
}
{
"title": "My Post",
"content": "Post content",
"tags": ["react", "sql"]
}