-
Notifications
You must be signed in to change notification settings - Fork 108
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
Add return type definitions to datasources #63
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,20 +7,37 @@ export default abstract class Base { | |
static get label(): string { | ||
throw new Error("Not Implemented"); | ||
} | ||
static get configSchema(): ConfigSchemaType { | ||
throw new Error("Not Implemented"); | ||
} | ||
|
||
constructor(config) { | ||
this.config = config; | ||
} | ||
|
||
abstract execute(query); | ||
abstract execute(query: string): Promise<any>; | ||
|
||
abstract cancel(); | ||
// @todo Set return type as Promise<void> ? | ||
abstract cancel(): void | Promise<void>; | ||
|
||
abstract connectionTest(); | ||
// @todo Define type of the result (boolean or void ?) | ||
abstract connectionTest(): Promise<any>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Datasources have different return value type. |
||
|
||
abstract fetchTables(); | ||
abstract fetchTables(): Promise<{ name: string; type: string; schema?: string }[]>; | ||
|
||
abstract descriptionTable(); | ||
abstract descriptionTable(): string; | ||
|
||
abstract fetchTableSummary(args); | ||
abstract fetchTableSummary( | ||
args: any | ||
): Promise<{ name: string; defs: { fields: string[]; rows: (string | null)[][] }; schema?: string }>; | ||
} | ||
|
||
export type ConfigSchemaType = { | ||
readonly name: string; | ||
readonly label: string; | ||
readonly type: string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be expressed like: readonly type: "radio" | "checkbox" | "password" | "string" | "number"; I prefer to use union type. |
||
readonly placeholder?: string | number; | ||
readonly required?: boolean; | ||
readonly values?: string[]; | ||
readonly default?: string; | ||
}[]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import bigquery from "@google-cloud/bigquery"; | ||
import Base from "./Base"; | ||
import Base, { ConfigSchemaType } from "./Base"; | ||
import { flatten } from "lodash"; | ||
|
||
export default class BigQuery extends Base { | ||
_cancel: any; | ||
|
||
static get key() { | ||
static get key(): string { | ||
return "bigquery"; | ||
} | ||
static get label() { | ||
static get label(): string { | ||
return "BigQuery"; | ||
} | ||
static get configSchema() { | ||
static get configSchema(): ConfigSchemaType { | ||
return [ | ||
{ | ||
name: "project", | ||
|
@@ -28,7 +28,7 @@ export default class BigQuery extends Base { | |
]; | ||
} | ||
|
||
execute(query) { | ||
execute(query: string) { | ||
this._cancel = null; | ||
return new Promise((resolve, reject) => { | ||
bigquery(this.config).startQuery(query, (err, job) => { | ||
|
@@ -52,18 +52,17 @@ export default class BigQuery extends Base { | |
}); | ||
} | ||
|
||
cancel() { | ||
cancel(): void { | ||
return this._cancel && this._cancel(); | ||
} | ||
|
||
async connectionTest() { | ||
async connectionTest(): Promise<void> { | ||
await bigquery(this.config).query("select 1"); | ||
return true; | ||
} | ||
|
||
async fetchTables() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOTE: If we update (But I don't have the environment for testing) |
||
const [datasets] = await bigquery(this.config).getDatasets(); | ||
const promises = datasets.map(async dataset => { | ||
async fetchTables(): Promise<{ name: string; type: string; schema?: string }[]> { | ||
const [datasets]: [any[]] = await bigquery(this.config).getDatasets(); | ||
const promises = datasets.map<Promise<{ name: string; type: string; schema?: string }>>(async dataset => { | ||
const [tables] = await dataset.getTables(); | ||
return tables.map(table => ({ | ||
schema: dataset.id, | ||
|
@@ -75,7 +74,13 @@ export default class BigQuery extends Base { | |
return flatten(results); | ||
} | ||
|
||
async fetchTableSummary({ schema, name }) { | ||
async fetchTableSummary({ | ||
schema, | ||
name | ||
}: { | ||
schema: string; | ||
name: string; | ||
}): Promise<{ name: string; defs: { fields: string[]; rows: (string | null)[][] }; schema?: string }> { | ||
const [metadata] = await bigquery(this.config) | ||
.dataset(schema) | ||
.table(name) | ||
|
@@ -88,7 +93,7 @@ export default class BigQuery extends Base { | |
return { schema, name, defs }; | ||
} | ||
|
||
descriptionTable() { | ||
descriptionTable(): string { | ||
return `|project|${this.config.project}|`; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BigQuery, MySQL and PostgreSQL uses Promise, but other not use.
I suggest to every datasource returns
Promise<void>
.