Prisma

import { PrismaClient } from "@prisma/client";
 
// Define an entity representing an item in your domain.
class Item {
  constructor(public id: string, public name: string) {}
}
 
// Define a repository interface for managing items.
interface ItemRepository {
  findById(id: string): Promise<Item | undefined>;
  save(item: Item): Promise<void>;
  remove(id: string): Promise<void>;
}
 
// Create a concrete implementation of the repository using Prisma.
class PrismaItemRepository implements ItemRepository {
  constructor(private prisma: PrismaClient) {}
 
  async findById(id: string): Promise<Item | undefined> {
    const item = await this.prisma.item.findUnique({ where: { id } });
    return item ? new Item(item.id, item.name) : undefined;
  }
 
  async save(item: Item): Promise<void> {
    await this.prisma.item.upsert({
      where: { id: item.id },
      create: { id: item.id, name: item.name },
      update: { name: item.name },
    });
  }
 
  async remove(id: string): Promise<void> {
    await this.prisma.item.delete({ where: { id } });
  }
}
 
// Usage
const prisma = new PrismaClient();
const itemRepository: ItemRepository = new PrismaItemRepository(prisma);
 
const item1 = new Item("1", "Item 1");
const item2 = new Item("2", "Item 2");
 
// Save items to the repository
itemRepository.save(item1);
itemRepository.save(item2);
 
// Retrieve an item from the repository
const retrievedItem = await itemRepository.findById("1");
console.log(retrievedItem);
 
// Remove an item from the repository
await itemRepository.remove("2");
 
// Verify the item is removed
const removedItem = await itemRepository.findById("2");
console.log(removedItem); // This should be undefined