Kysely Data API
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
-
AccessDeniedException: User is not authorized to perform rds-data:ExecuteStatement on resource: arn:aws:rds:...
cause The AWS credentials used (e.g., Lambda execution role) lack the necessary IAM permissions to interact with the RDS Data API or the specific database resource.fixReview and update the IAM policy attached to your AWS role or user, ensuring it includes `rds-data:ExecuteStatement` (and other `rds-data:*` actions as needed) on the `resourceArn` and `secretArn`. -
ValidationException: Missing required parameter: secretArn
cause The `secretArn` property was not provided or was empty in the `DataApiDialect` driver configuration.fixEnsure that the `secretArn` field is correctly populated with the ARN of your AWS Secrets Manager secret containing database credentials. -
BadRequestException: There is no data-api enabled DB cluster with this ARN: arn:aws:rds:...
cause The `resourceArn` provided does not correspond to an existing RDS database cluster that has the Data API enabled, or the ARN is incorrect.fixVerify that the `resourceArn` is accurate and points to an RDS cluster that specifically has the Data API feature turned on in its configuration.
Warnings
- breaking Version 2.0.0 is a major release and likely contains breaking changes from 1.x versions. Always review the release notes when upgrading between major versions, especially concerning dialect configuration or specific driver options.
- gotcha Proper IAM permissions are crucial for the RDS Data API. The AWS Lambda execution role or credentials used by `RDSDataService` must have `rds-data:*` permissions on the specified `resourceArn` and `secretArn`.
- gotcha The `secretArn` and `resourceArn` are mandatory for configuring the `DataApiDialect`. Incorrect or missing ARNs will lead to authentication or connection failures.
- gotcha Compatibility with `kysely` versions is important. While peer dependency specifies `^0.27.4`, newer `kysely` versions might introduce changes that require `kysely-data-api` updates.
Install
-
npm install kysely-data-api -
yarn add kysely-data-api -
pnpm add kysely-data-api
Imports
- DataApiDialect
const DataApiDialect = require('kysely-data-api').DataApiDialect;import { DataApiDialect } from 'kysely-data-api'; - Kysely
import Kysely from 'kysely';
import { Kysely } from 'kysely'; - RDSDataService
import * as RDSDataService from '@aws-sdk/client-rds-data';
import { RDSDataService } from '@aws-sdk/client-rds-data';
Quickstart
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();