diff --git a/src/client.ts b/src/client.ts index 6c0d844..4b14d0a 100644 --- a/src/client.ts +++ b/src/client.ts @@ -21,12 +21,10 @@ export interface ClientConfig { debug?: boolean; /** Connect timeout */ timeout?: number; - /** TODO: auto reconnect */ - reconnect?: boolean; - /** Number of retries that failed in the link process */ - retry?: number; /** Connection pool size default 1 */ poolSize?: number; + /** charset */ + charset?: string; } /** Transaction processor */ diff --git a/src/connection.ts b/src/connection.ts index fd38fdf..5b88a77 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -71,6 +71,10 @@ export class Connection { log.info(`connected to ${this.client.config.hostname}`); this.state = ConnectionState.CONNECTED; } + + if (this.client.config.charset) { + await this.execute(`SET NAMES ${this.client.config.charset}`); + } } /** Connect to database */ diff --git a/test.ts b/test.ts index 9330d96..41184e7 100644 --- a/test.ts +++ b/test.ts @@ -18,7 +18,7 @@ testWithClient(async function testCreateTable(client) { is_top tinyint(1) default 0, created_at timestamp not null default current_timestamp, PRIMARY KEY (id) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; `); }); @@ -37,7 +37,7 @@ testWithClient(async function testInsert(client) { testWithClient(async function testUpdate(client) { let result = await client.execute( `update users set ?? = ?, ?? = ? WHERE id = ?`, - ["name", "MYR", "created_at", new Date(), 1] + ["name", "MYR🦕", "created_at", new Date(), 1] ); assertEquals(result, { affectedRows: 1, lastInsertId: 0 }); }); @@ -47,7 +47,7 @@ testWithClient(async function testQuery(client) { "select ??,`is_top`,`name` from ?? where id = ?", ["id", "users", 1] ); - assertEquals(result, [{ id: 1, name: "MYR", is_top: false }]); + assertEquals(result, [{ id: 1, name: "MYR🦕", is_top: false }]); }); testWithClient(async function testQueryErrorOccurred(client) { @@ -72,7 +72,7 @@ testWithClient(async function testQueryList(client) { const sql = "select ??,?? from ??"; let result = await client.query(sql, ["id", "name", "users"]); assertEquals(result, [ - { id: 1, name: "MYR" }, + { id: 1, name: "MYR🦕" }, { id: 2, name: "MySQL" }, ]); }); diff --git a/test.util.ts b/test.util.ts index bb440e2..96ba8a4 100644 --- a/test.util.ts +++ b/test.util.ts @@ -15,6 +15,7 @@ const config = { username, port, db, + charset: "utf8mb4", password, };