Kysely Data API

2.0.0 · active · verified Tue Apr 21

Kysely Data API is a specialized library that extends the `kysely` type-safe SQL query builder to provide seamless integration with the AWS RDS Data API. This enables serverless applications, particularly those built on AWS Lambda, to interact with Amazon RDS databases (both MySQL and PostgreSQL) without requiring traditional, long-lived database connections. The current stable version is 2.0.0, indicating a mature and actively developed project, though a specific release cadence is not publicly defined. Its key differentiator lies in abstracting the complexities of the RDS Data API into a familiar `kysely` dialect, allowing developers to leverage `kysely`'s powerful type-safe query building capabilities within a serverless, event-driven architecture, simplifying database interactions and resource management.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize `Kysely` with `DataApiDialect` for AWS RDS Data API, configuring it with essential ARN values and performing a simple select query.

import { Kysely } from 'kysely';
import { DataApiDialect } from 'kysely-data-api';
import { RDSDataService } from '@aws-sdk/client-rds-data';

// Define your database schema interface (replace with your actual schema)
interface Database {
  person: {
    id: number;
    first_name: string;
    last_name: string;
  };
  // Add other tables here
}

async function initializeDb() {
  const dataApi = new DataApiDialect({
    mode: 'mysql', // or 'postgres'
    driver: {
      client: new RDSDataService({ region: process.env.AWS_REGION ?? 'us-east-1' }),
      database: process.env.DB_NAME ?? 'my_database',
      secretArn: process.env.DB_SECRET_ARN ?? '<arn of secret containing credentials>',
      resourceArn: process.env.DB_RESOURCE_ARN ?? '<arn of database>',
    },
  });

  const db = new Kysely<Database>({ dialect: dataApi });

  try {
    const results = await db.selectFrom('person').selectAll().execute();
    console.log('Query results:', results);
  } catch (error) {
    console.error('Database query failed:', error);
  } finally {
    // In serverless environments, explicit connection closing is often not needed
    // as the Lambda invocation ends, but for local testing, consider:
    // await db.destroy(); 
  }
}

initializeDb();

view raw JSON →