Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript Type Definitions #49

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ Below is a table containing all of the possible configuration options for the `d
| resourceArn | `string` | The ARN of your Aurora Serverless Cluster. This value is *required*, but can be overridden when querying. | |
| secretArn | `string` | The ARN of the secret associated with your database credentials. This is *required*, but can be overridden when querying. | |
| database | `string` | *Optional* default database to use with queries. Can be overridden when querying. | |
| hydrateColumnNames | `boolean` | When `true`, results will be returned as objects with column names as keys. If `false`, results will be returned as an array of values. | `true` |
| keepAlive | `boolean` | Enables HTTP Keep-Alive for calls to the AWS SDK. This dramatically decreases the latency of subsequent calls. | `true` |
| sslEnabled | `boolean` | *Optional* Enables SSL HTTP endpoint. Can be disable for local development. | `true` |
| hydrateColumnNames | `boolean` | *Optional* When `true`, results will be returned as objects with column names as keys. If `false`, results will be returned as an array of values. | `true` |
| keepAlive | `boolean` | *Optional* Enables HTTP Keep-Alive for calls to the AWS SDK. This dramatically decreases the latency of subsequent calls. | `true` |
| sslEnabled | `boolean` | *Optional* Enables SSL HTTP endpoint. Can be disabled for local development. | `true` |
| options | `object` | An *optional* configuration object that is passed directly into the RDSDataService constructor. See [here](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/RDSDataService.html#constructor-property) for available options. | `{}` |
| region | `string` | *Optional* AWS region to use. | `aws-sdk default` |

Expand Down
44 changes: 44 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
declare module 'data-api-client' {
import type { ClientConfiguration } from 'aws-sdk/clients/rdsdataservice';

export interface iParams {
secretArn: string;
resourceArn: string;
database?: string;
keepAlive?: boolean;
hydrateColumnNames?: boolean;
sslEnabled?: boolean;
options?: ClientConfiguration;
region?: string;
}

interface iTransaction {
query: iQuery<iTransaction>;
rollback: (error: Error, status: any) => void;
commit: () => Promise<void>;
}

interface iQuery<T> {
(sql: string, params?: [] | unknown): Promise<T>; // params can be [] or {}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for the generics, you can do something like this (instead of making iQuery itself a generic):

Suggested change
(sql: string, params?: [] | unknown): Promise<T>; // params can be [] or {}
<T = any>(sql: string, params?: [] | unknown): Promise<T>; // params can be [] or {}

Usage:

client.query<{ name: string; age: number }[]>('SELECT * from table';)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realised this is the same as @rraziel 's suggestion 😅

(obj: { sql: string; parameters: [] | unknown; database?: string; hydrateColumnNames?: boolean }): Promise<T>;
}

export interface iDataAPIClient {
query: iQuery<iDataAPIQueryResult>;
transaction(): iTransaction; // needs to return an interface with

// promisified versions of the RDSDataService methods
// TODO add actual return types
batchExecuteStatement: (...args) => Promise<any>;
beginTransaction: (...args) => Promise<any>;
commitTransaction: (...args) => Promise<any>;
executeStatement: (...args) => Promise<any>;
rollbackTransaction: (...args) => Promise<any>;
}

export interface iDataAPIQueryResult {
records: Array<any>;
}

export default function (params: iParams): iDataAPIClient;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.1",
"description": "A lightweight wrapper that simplifies working with the Amazon Aurora Serverless Data API",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "jest",
"test-ci": "eslint . && jest",
Expand Down