Skip to content

Commit fff4008

Browse files
authored
Merge pull request #63 from mtgto/base_type
Add return type definitions to datasources
2 parents c6e1d6b + 6e66d31 commit fff4008

File tree

8 files changed

+113
-72
lines changed

8 files changed

+113
-72
lines changed

src/lib/DataSourceDefinition/Athena.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import AthenaClient from "../AthenaClient";
2-
import Base from "./Base";
2+
import Base, { ConfigSchemaType } from "./Base";
33
import Util from "../Util";
44

55
export default class Athena extends Base {
66
client: AthenaClient;
77

8-
static get key() {
8+
static get key(): string {
99
return "athena";
1010
}
11-
static get label() {
11+
static get label(): string {
1212
return "Amazon Athena";
1313
}
14-
static get configSchema() {
14+
static get configSchema(): ConfigSchemaType {
1515
return [
1616
{
1717
name: "region",
@@ -57,21 +57,24 @@ export default class Athena extends Base {
5757
return { fields, rows };
5858
}
5959

60-
cancel() {
61-
return this.client.cancel();
60+
cancel(): void {
61+
this.client.cancel();
6262
}
6363

64-
async connectionTest() {
64+
async connectionTest(): Promise<void> {
6565
await this.client.execute("select 1");
66-
return;
6766
}
6867

69-
async fetchTables() {
68+
async fetchTables(): Promise<{ name: string; type: string; schema?: string }[]> {
7069
const rows = await this.client.execute("show tables");
71-
return rows.map(row => ({ name: row[0], type: "table" }));
70+
return rows.map(row => ({ name: row[0]!, type: "table" }));
7271
}
7372

74-
async fetchTableSummary({ name }) {
73+
async fetchTableSummary({
74+
name
75+
}: {
76+
name: string;
77+
}): Promise<{ name: string; defs: { fields: string[]; rows: (string | null)[][] }; schema?: string }> {
7578
const rows = await this.client.execute(`describe ${name}`);
7679
const defs = {
7780
fields: ["name", "type"],
@@ -84,7 +87,7 @@ export default class Athena extends Base {
8487
return { name, defs };
8588
}
8689

87-
descriptionTable() {
90+
descriptionTable(): string {
8891
return Util.stripHeredoc(`
8992
|region|${this.config.region}|
9093
|database|${this.config.database}|

src/lib/DataSourceDefinition/Base.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,37 @@ export default abstract class Base {
77
static get label(): string {
88
throw new Error("Not Implemented");
99
}
10+
static get configSchema(): ConfigSchemaType {
11+
throw new Error("Not Implemented");
12+
}
1013

1114
constructor(config) {
1215
this.config = config;
1316
}
1417

15-
abstract execute(query);
18+
abstract execute(query: string): Promise<any>;
1619

17-
abstract cancel();
20+
// @todo Set return type as Promise<void> ?
21+
abstract cancel(): void | Promise<void>;
1822

19-
abstract connectionTest();
23+
// @todo Define type of the result (boolean or void ?)
24+
abstract connectionTest(): Promise<any>;
2025

21-
abstract fetchTables();
26+
abstract fetchTables(): Promise<{ name: string; type: string; schema?: string }[]>;
2227

23-
abstract descriptionTable();
28+
abstract descriptionTable(): string;
2429

25-
abstract fetchTableSummary(args);
30+
abstract fetchTableSummary(
31+
args: any
32+
): Promise<{ name: string; defs: { fields: string[]; rows: (string | null)[][] }; schema?: string }>;
2633
}
34+
35+
export type ConfigSchemaType = {
36+
readonly name: string;
37+
readonly label: string;
38+
readonly type: string;
39+
readonly placeholder?: string | number;
40+
readonly required?: boolean;
41+
readonly values?: string[];
42+
readonly default?: string;
43+
}[];

src/lib/DataSourceDefinition/BigQuery.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import bigquery from "@google-cloud/bigquery";
2-
import Base from "./Base";
2+
import Base, { ConfigSchemaType } from "./Base";
33
import { flatten } from "lodash";
44

55
export default class BigQuery extends Base {
66
_cancel: any;
77

8-
static get key() {
8+
static get key(): string {
99
return "bigquery";
1010
}
11-
static get label() {
11+
static get label(): string {
1212
return "BigQuery";
1313
}
14-
static get configSchema() {
14+
static get configSchema(): ConfigSchemaType {
1515
return [
1616
{
1717
name: "project",
@@ -28,7 +28,7 @@ export default class BigQuery extends Base {
2828
];
2929
}
3030

31-
execute(query) {
31+
execute(query: string) {
3232
this._cancel = null;
3333
return new Promise((resolve, reject) => {
3434
bigquery(this.config).startQuery(query, (err, job) => {
@@ -52,18 +52,17 @@ export default class BigQuery extends Base {
5252
});
5353
}
5454

55-
cancel() {
55+
cancel(): void {
5656
return this._cancel && this._cancel();
5757
}
5858

59-
async connectionTest() {
59+
async connectionTest(): Promise<void> {
6060
await bigquery(this.config).query("select 1");
61-
return true;
6261
}
6362

64-
async fetchTables() {
65-
const [datasets] = await bigquery(this.config).getDatasets();
66-
const promises = datasets.map(async dataset => {
63+
async fetchTables(): Promise<{ name: string; type: string; schema?: string }[]> {
64+
const [datasets]: [any[]] = await bigquery(this.config).getDatasets();
65+
const promises = datasets.map<Promise<{ name: string; type: string; schema?: string }>>(async dataset => {
6766
const [tables] = await dataset.getTables();
6867
return tables.map(table => ({
6968
schema: dataset.id,
@@ -75,7 +74,13 @@ export default class BigQuery extends Base {
7574
return flatten(results);
7675
}
7776

78-
async fetchTableSummary({ schema, name }) {
77+
async fetchTableSummary({
78+
schema,
79+
name
80+
}: {
81+
schema: string;
82+
name: string;
83+
}): Promise<{ name: string; defs: { fields: string[]; rows: (string | null)[][] }; schema?: string }> {
7984
const [metadata] = await bigquery(this.config)
8085
.dataset(schema)
8186
.table(name)
@@ -88,7 +93,7 @@ export default class BigQuery extends Base {
8893
return { schema, name, defs };
8994
}
9095

91-
descriptionTable() {
96+
descriptionTable(): string {
9297
return `|project|${this.config.project}|`;
9398
}
9499
}

src/lib/DataSourceDefinition/Mysql.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import mysql from "mysql2";
2-
import Base from "./Base";
2+
import Base, { ConfigSchemaType } from "./Base";
33
import Util from "../Util";
44
import { zipObject } from "lodash";
55

66
export default class Mysql extends Base {
77
currentConnection: any;
88

9-
static get key() {
9+
static get key(): string {
1010
return "mysql";
1111
}
12-
static get label() {
12+
static get label(): string {
1313
return "MySQL";
1414
}
15-
static get configSchema() {
15+
static get configSchema(): ConfigSchemaType {
1616
return [
1717
{ name: "host", label: "Host", type: "string", placeholder: "localhost" },
1818
{ name: "port", label: "Port", type: "number", placeholder: 3306 },
@@ -31,7 +31,7 @@ export default class Mysql extends Base {
3131
return this._execute(query);
3232
}
3333

34-
cancel() {
34+
cancel(): Promise<void> {
3535
const tid = this.currentConnection && this.currentConnection.threadId;
3636
if (!tid) return Promise.resolve();
3737

@@ -43,7 +43,7 @@ export default class Mysql extends Base {
4343
return true;
4444
}
4545

46-
async fetchTables() {
46+
async fetchTables(): Promise<{ name: string; type: string; schema?: string }[]> {
4747
const query = Util.stripHeredoc(`
4848
select table_name as name, table_type as type
4949
from information_schema.tables
@@ -55,14 +55,18 @@ export default class Mysql extends Base {
5555
return rows.map(row => zipObject(fields, row));
5656
}
5757

58-
async fetchTableSummary({ name }) {
58+
async fetchTableSummary({
59+
name
60+
}: {
61+
name: string;
62+
}): Promise<{ name: string; defs: { fields: string[]; rows: (string | null)[][] }; schema?: string }> {
5963
const sql = "show columns from ??";
6064
const defs = await this._execute(sql, name);
6165

6266
return { name, defs };
6367
}
6468

65-
_execute(query, ...args): Promise<any> {
69+
_execute(query: string, ...args): Promise<any> {
6670
if (this.currentConnection) {
6771
return Promise.reject(new Error("A query is running"));
6872
}
@@ -96,7 +100,7 @@ export default class Mysql extends Base {
96100
});
97101
}
98102

99-
descriptionTable() {
103+
descriptionTable(): string {
100104
return Util.stripHeredoc(`
101105
|host|${this.config.host}|
102106
|port|${this.config.port}|

src/lib/DataSourceDefinition/Postgres.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pg from "pg";
2-
import Base from "./Base";
2+
import Base, { ConfigSchemaType } from "./Base";
33
import Util from "../Util";
44
import { zipObject } from "lodash";
55

@@ -50,13 +50,13 @@ import { zipObject } from "lodash";
5050
export default class Postgres extends Base {
5151
currentClient: any;
5252

53-
static get key() {
53+
static get key(): string {
5454
return "postgres";
5555
}
56-
static get label() {
56+
static get label(): string {
5757
return "PostgreSQL";
5858
}
59-
static get configSchema() {
59+
static get configSchema(): ConfigSchemaType {
6060
return [
6161
{ name: "host", label: "Host", type: "string", placeholder: "localhost" },
6262
{ name: "port", label: "Port", type: "number", placeholder: 5432 },
@@ -72,27 +72,27 @@ export default class Postgres extends Base {
7272
];
7373
}
7474

75-
async execute(query, options: any = {}) {
75+
async execute(query: string, options: any = {}): Promise<any> {
7676
try {
7777
return await this._execute(query);
7878
} catch (err) {
7979
throw this._errorWithLine(err, query, options.startLine || 1);
8080
}
8181
}
8282

83-
cancel() {
83+
cancel(): Promise<void> {
8484
const pid = this.currentClient && this.currentClient.processID;
8585
if (!pid) return Promise.resolve();
8686

8787
return new Postgres(this.config)._execute(`select pg_cancel_backend(${pid})`);
8888
}
8989

90-
async connectionTest() {
90+
async connectionTest(): Promise<any> {
9191
await this._execute("select 1");
9292
return true;
9393
}
9494

95-
async fetchTables() {
95+
async fetchTables(): Promise<{ name: string; type: string; schema?: string }[]> {
9696
const query = Util.stripHeredoc(`
9797
select table_schema as schema, table_name as name, table_type as type
9898
from information_schema.tables
@@ -104,7 +104,13 @@ export default class Postgres extends Base {
104104
return rows.map(row => zipObject(fields, row));
105105
}
106106

107-
async fetchTableSummary({ schema, name }) {
107+
async fetchTableSummary({
108+
schema,
109+
name
110+
}: {
111+
schema: string;
112+
name: string;
113+
}): Promise<{ name: string; defs: { fields: string[]; rows: (string | null)[][] }; schema?: string }> {
108114
const query = Util.stripHeredoc(`
109115
select
110116
pg_attribute.attname as name,
@@ -130,7 +136,7 @@ export default class Postgres extends Base {
130136
return { schema, name, defs };
131137
}
132138

133-
descriptionTable() {
139+
descriptionTable(): string {
134140
return Util.stripHeredoc(`
135141
|host|${this.config.host}|
136142
|port|${this.config.port}|

0 commit comments

Comments
 (0)