fix: test db creator types (#3094)

This commit is contained in:
ChengLei Shao 2023-11-25 19:53:13 +08:00 committed by GitHub
parent fd454d78b3
commit 2dff989f70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,7 +44,7 @@ abstract class BaseClient<Client> {
} }
} }
class PostgresClient extends BaseClient<typeof pg.Client> { class PostgresClient extends BaseClient<pg.Client> {
async _removeDB(name: string): Promise<void> { async _removeDB(name: string): Promise<void> {
await this._client.query(`DROP DATABASE IF EXISTS ${name}`); await this._client.query(`DROP DATABASE IF EXISTS ${name}`);
} }
@ -54,7 +54,7 @@ class PostgresClient extends BaseClient<typeof pg.Client> {
await this._client.query(`CREATE DATABASE ${name};`); await this._client.query(`CREATE DATABASE ${name};`);
} }
async _createConnection(): Promise<typeof pg.Client> { async _createConnection(): Promise<pg.Client> {
const client = new pg.Client({ const client = new pg.Client({
host: process.env['DB_HOST'], host: process.env['DB_HOST'],
port: Number(process.env['DB_PORT']), port: Number(process.env['DB_PORT']),
@ -78,15 +78,13 @@ class MySQLClient extends BaseClient<any> {
} }
async _createConnection(): Promise<mysql.Connection> { async _createConnection(): Promise<mysql.Connection> {
const connection = await mysql.createConnection({ return mysql.createConnection({
host: process.env['DB_HOST'], host: process.env['DB_HOST'],
port: Number(process.env['DB_PORT']), port: Number(process.env['DB_PORT']),
user: process.env['DB_USER'], user: process.env['DB_USER'],
password: process.env['DB_PASSWORD'], password: process.env['DB_PASSWORD'],
database: process.env['DB_DATABASE'], database: process.env['DB_DATABASE'],
}); });
return connection;
} }
} }
@ -100,15 +98,13 @@ class MariaDBClient extends BaseClient<any> {
} }
async _createConnection(): Promise<mariadb.Connection> { async _createConnection(): Promise<mariadb.Connection> {
const connection = await mariadb.createConnection({ return await mariadb.createConnection({
host: process.env['DB_HOST'], host: process.env['DB_HOST'],
port: Number(process.env['DB_PORT']), port: Number(process.env['DB_PORT']),
user: process.env['DB_USER'], user: process.env['DB_USER'],
password: process.env['DB_PASSWORD'], password: process.env['DB_PASSWORD'],
database: process.env['DB_DATABASE'], database: process.env['DB_DATABASE'],
}); });
return connection;
} }
} }
@ -138,7 +134,6 @@ const server = http.createServer((req, res) => {
const trimmedPath = path.replace(/^\/+|\/+$/g, ''); const trimmedPath = path.replace(/^\/+|\/+$/g, '');
if (trimmedPath === 'acquire') { if (trimmedPath === 'acquire') {
const via = parsedUrl.query.via as string;
const name = parsedUrl.query.name as string | undefined; const name = parsedUrl.query.name as string | undefined;
dbClient dbClient
@ -147,8 +142,7 @@ const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' }); res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(); res.end();
}) })
.catch((error) => { .catch((error: Error) => {
console.error(error);
res.writeHead(500, { 'Content-Type': 'application/json' }); res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error })); res.end(JSON.stringify({ error }));
}); });
@ -159,8 +153,7 @@ const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' }); res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(); res.end();
}) })
.catch((error) => { .catch((error: Error) => {
console.error(error);
res.writeHead(500, { 'Content-Type': 'application/json' }); res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error })); res.end(JSON.stringify({ error }));
}); });