Skip to content

Commit b5634d1

Browse files
committed
Added MySQL support
1 parent 5ec2901 commit b5634d1

6 files changed

Lines changed: 54 additions & 12 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Kysely Data Api
2+
3+
This library adds AWS RDS Data Api support for [kysely](github.com/koskimas/kysely). It has support for both MySQL and Postgres
4+
5+
### Usage
6+
7+
```typescript
8+
const dataApi = new DataApiDialect({
9+
mode: "mysql",
10+
driver: {
11+
client: new RDSDataService(),
12+
database: "bench",
13+
secretArn: "<arn of secret containing credentials",
14+
resourceArn: "<arn of database>",
15+
},
16+
});
17+
18+
export const db = new Kysely<Database>({ dialect: dataApi });
19+
```

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
"esbuild": "^0.13.6",
2727
"esbuild-runner": "^2.2.1",
2828
"jest": "^27.2.5",
29-
"kysely": "^0.7.1",
29+
"kysely": "^0.7.7",
3030
"perf_hooks": "^0.0.1"
3131
},
3232
"peerDependencies": {
3333
"aws-sdk": "^2.1008.0",
34-
"kysely": "^0.7.1"
34+
"kysely": "^0.7.7"
3535
},
3636
"files": [
3737
"dist"

src/data-api-dialect.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import { Dialect } from "kysely";
55
import { DatabaseIntrospector } from "kysely";
66
import { PostgresIntrospector } from "kysely";
77
import { DataApiDriver, DataApiDriverConfig } from "./data-api-driver";
8-
import { DataApiQueryCompiler } from "./data-api-query-compiler";
8+
import {
9+
PostgresDataApiQueryCompiler,
10+
MysqlDataApiQueryCompiler,
11+
} from "./data-api-query-compiler";
912

1013
type DataApiDialectConfig = {
14+
mode: "postgres" | "mysql";
1115
driver: DataApiDriverConfig;
1216
};
1317

@@ -27,8 +31,11 @@ export class DataApiDialect implements Dialect {
2731
}
2832

2933
createQueryCompiler(): QueryCompiler {
30-
// The default query compiler is for postgres dialect.
31-
return new DataApiQueryCompiler();
34+
if (this.#config.mode === "postgres")
35+
return new PostgresDataApiQueryCompiler();
36+
if (this.#config.mode === "mysql") return new MysqlDataApiQueryCompiler();
37+
38+
throw new Error("Unknown mode " + this.#config.mode);
3239
}
3340

3441
createIntrospector(db: Kysely<any>): DatabaseIntrospector {

src/data-api-driver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class DataApiConnection implements DatabaseConnection {
9898
if (!r.columnMetadata) {
9999
return {
100100
numUpdatedOrDeletedRows: r.numberOfRecordsUpdated,
101+
rows: [],
101102
};
102103
}
103104
const rows = r.records
@@ -117,7 +118,7 @@ class DataApiConnection implements DatabaseConnection {
117118
) as O
118119
);
119120
const result: QueryResult<O> = {
120-
rows,
121+
rows: rows || [],
121122
};
122123
return result;
123124
}

src/data-api-query-compiler.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
import { Field } from "aws-sdk/clients/rdsdataservice";
2-
import { PostgresQueryCompiler } from "kysely";
2+
import { MysqlQueryCompiler, PostgresQueryCompiler } from "kysely";
33

4-
export class DataApiQueryCompiler extends PostgresQueryCompiler {
4+
export class PostgresDataApiQueryCompiler extends PostgresQueryCompiler {
5+
protected override appendValue(value: any) {
6+
const name = this.numBindings;
7+
this.append(this.getCurrentParameterPlaceholder());
8+
this.addBinding({
9+
name: name.toString(),
10+
value: serialize(value),
11+
});
12+
}
13+
14+
protected override getCurrentParameterPlaceholder() {
15+
return ":" + this.numBindings;
16+
}
17+
}
18+
19+
export class MysqlDataApiQueryCompiler extends MysqlQueryCompiler {
520
protected override appendValue(value: any) {
621
const name = this.numBindings;
722
this.append(this.getCurrentParameterPlaceholder());

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,10 +2003,10 @@ kleur@^3.0.3:
20032003
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
20042004
integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==
20052005

2006-
kysely@^0.7.1:
2007-
version "0.7.1"
2008-
resolved "https://registry.yarnpkg.com/kysely/-/kysely-0.7.1.tgz#64aace1262f86114c89950f01996c5d46a40b8d4"
2009-
integrity sha512-PPUIaggFHNxkAdUCfdlr/94QsT4IRQvRXgP6oa/lrcKScUTMnaaYxg3cTlrPj8ZN5uoHu5YzTF+8gBGhtHHcuw==
2006+
kysely@^0.7.7:
2007+
version "0.7.7"
2008+
resolved "https://registry.yarnpkg.com/kysely/-/kysely-0.7.7.tgz#7812f834be5c63c273dcdf1907afec7804813ee5"
2009+
integrity sha512-CVOmPJRkkU9Gsn5YXuuV/RS/0UsEhcP+/C3MV2durH21iGHIkJzxpUIf0ykVdrih6oUhw6wj2FbBSOkVglMR+A==
20102010
dependencies:
20112011
"@types/node" "^16.10.9"
20122012

0 commit comments

Comments
 (0)