Database Adapters

April 15, 2025

Deprecated Feature

@c15t/backend v1 did not deliver the flexibility we wanted and fell short of our standards. It is now deprecated as we work on a full rewrite, with v2 entering canary soon. This does not affect consent.io deployments, which remain stable.

The c15t Backend package provides a flexible database adapter system that allows you to use different database backends while maintaining a consistent interface.

Overview

Database adapters provide a standardized way to interact with different database systems. Each adapter implements the DatabaseAdapter interface:

Available Adapters

Memory Adapter

The memory adapter is perfect for development and testing. It stores data in memory and is reset when the application restarts.

Features

  • In-memory storage
  • No persistence
  • Fast for development
  • Automatic cleanup

Kysely Adapter

The Kysely adapter provides type-safe SQL query building with support for multiple databases.

Supported Databases

  • PostgreSQL
  • MySQL
  • SQLite
  • Microsoft SQL Server

Features

  • Type-safe queries
  • Query building
  • Transaction support
  • Connection pooling

Prisma Adapter

The Prisma adapter integrates with Prisma ORM for type-safe database access.

Features

  • Prisma ORM integration
  • Type safety
  • Schema management
  • Migration support

Drizzle Adapter

The Drizzle adapter provides integration with Drizzle ORM.

Features

  • Drizzle ORM integration
  • Type safety
  • Schema management
  • Query building

Creating Custom Adapters

You can create custom adapters by implementing the DatabaseAdapter interface:

Query Interface

The query interface is consistent across all adapters:

Example Queries

Transaction Support

Some adapters support transactions:

Error Handling

Adapters handle errors consistently:

Best Practices

  1. Connection Management

  2. Error Handling

  3. Query Optimization

Migration Support

Adapters that support migrations provide methods for managing database schema:

Performance Considerations

  1. Connection Pooling

    • Configure appropriate pool size
    • Monitor connection usage
    • Handle connection errors
  2. Query Optimization

    • Use indexes
    • Limit result sets
    • Optimize join operations
  3. Caching

    • Implement caching where appropriate
    • Use appropriate cache invalidation
    • Monitor cache hit rates

Security

  1. Input Validation

  2. SQL Injection Prevention

    • Use parameterized queries
    • Validate input
    • Escape special characters
  3. Access Control

    • Implement row-level security
    • Use appropriate database roles
    • Monitor access patterns

Monitoring and Debugging

  1. Query Logging

  2. Performance Monitoring

    • Track query execution time
    • Monitor connection pool usage
    • Log slow queries
  3. Error Tracking

    • Log database errors
    • Track failed queries
    • Monitor connection issues