Fix Promise/Callback variants in datasource types

Remove usage of `PromiseOrVoid`, it makes it difficult to consume
the API via `await`. For each async method, define multiple variants
to correctly describe what's returned when a callback arg was provided
(`void`) and what's returned when called with no callback
(`Promise<T>`).

Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
This commit is contained in:
Miroslav Bajtoš 2019-12-05 09:51:54 +01:00
parent 2f867576b6
commit 19048cd716
No known key found for this signature in database
GPG Key ID: 6F2304BA9361C7E3
1 changed files with 112 additions and 22 deletions

134
types/datasource.d.ts vendored
View File

@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {AnyObject, Callback, Options, PromiseOrVoid} from './common';
import {AnyObject, Callback, Options} from './common';
import {Connector} from './connector';
import {
ModelBaseClass,
@ -140,56 +140,138 @@ export declare class DataSource extends EventEmitter {
*/
attach(modelClass: ModelBaseClass): ModelBaseClass;
automigrate(models: string | string[], callback?: Callback): PromiseOrVoid;
automigrate(models: string | string[]): Promise<void>;
// legacy callback style
automigrate(models: string | string[], callback: Callback): void;
autoupdate(models: string | string[], callback?: Callback): PromiseOrVoid;
autoupdate(models: string | string[]): Promise<void>;
// legacy callback style
autoupdate(models: string | string[], callback: Callback): void;
discoverModelDefinitions(
options?: Options,
callback?: Callback<ModelDefinition[]>,
): PromiseOrVoid<ModelDefinition[]>;
): Promise<ModelDefinition[]>;
// legacy callback style (no options)
discoverModelDefinitions(
callback: Callback<ModelDefinition[]>,
): void;
// legacy callback style (with options)
discoverModelDefinitions(
options: Options,
callback: Callback<ModelDefinition[]>,
): void;
discoverModelProperties(
modelName: string,
options?: Options,
callback?: Callback<PropertyDefinition[]>,
): PromiseOrVoid<PropertyDefinition[]>;
): Promise<PropertyDefinition[]>;
// legacy callback style (no options)
discoverModelProperties(
modelName: string,
callback: Callback<PropertyDefinition[]>,
): void;
// legacy callback style (with options)
discoverModelProperties(
modelName: string,
options: Options,
callback: Callback<PropertyDefinition[]>,
): void;
discoverPrimaryKeys(
modelName: string,
options?: Options,
callback?: Callback<PropertyDefinition[]>,
): PromiseOrVoid<PropertyDefinition[]>;
): Promise<PropertyDefinition[]>;
// legacy callback style (no options)
discoverPrimaryKeys(
modelName: string,
callback: Callback<PropertyDefinition[]>,
): void;
// legacy callback style (with options)
discoverPrimaryKeys(
modelName: string,
options: Options,
callback: Callback<PropertyDefinition[]>,
): void;
discoverForeignKeys(
modelName: string,
options?: Options,
callback?: Callback<PropertyDefinition[]>,
): PromiseOrVoid<PropertyDefinition[]>;
): Promise<PropertyDefinition[]>;
// legacy callback style (no options)
discoverForeignKeys(
modelName: string,
callback: Callback<PropertyDefinition[]>,
): void;
// legacy callback style (no options)
discoverForeignKeys(
modelName: string,
options: Options,
callback: Callback<PropertyDefinition[]>,
): void;
discoverExportedForeignKeys(
modelName: string,
options?: Options,
callback?: Callback<PropertyDefinition[]>,
): PromiseOrVoid<PropertyDefinition[]>;
): Promise<PropertyDefinition[]>;
// legacy callback style (no options)
discoverExportedForeignKeys(
modelName: string,
callback: Callback<PropertyDefinition[]>,
): void;
// legacy callback style (with options)
discoverExportedForeignKeys(
modelName: string,
options: Options,
callback: Callback<PropertyDefinition[]>,
): void;
discoverAndBuildModels(
modelName: string,
options?: Options,
callback?: Callback<{[name: string]: ModelBaseClass}>,
): PromiseOrVoid<{[name: string]: ModelBaseClass}>;
): Promise<{[name: string]: ModelBaseClass}>;
// legacy callback style (no options)
discoverAndBuildModels(
modelName: string,
callback: Callback<{[name: string]: ModelBaseClass}>,
): void;
// legacy callback style (with options)
discoverAndBuildModels(
modelName: string,
options: Options,
callback: Callback<{[name: string]: ModelBaseClass}>,
): void;
discoverSchema(
tableName: string,
options?: Options,
callback?: Callback<AnyObject>,
): PromiseOrVoid<AnyObject>;
): Promise<AnyObject>;
// legacy callback style (no options)
discoverSchema(
tableName: string,
callback: Callback<AnyObject>,
): void;
// legacy callback style (with options)
discoverSchema(
tableName: string,
options: Options,
callback: Callback<AnyObject>,
): void;
discoverSchemas(
tableName: string,
options?: Options,
callback?: Callback<AnyObject[]>,
): PromiseOrVoid<AnyObject[]>;
): Promise<AnyObject[]>;
// legacy callback style (no options)
discoverSchemas(
tableName: string,
callback: Callback<AnyObject[]>,
): void;
// legacy callback style (with options)
discoverSchemas(
tableName: string,
options: Options,
callback: Callback<AnyObject[]>,
): void;
buildModelFromInstance(
modelName: string,
@ -197,9 +279,17 @@ export declare class DataSource extends EventEmitter {
options?: Options,
): ModelBaseClass;
connect(callback?: Callback): PromiseOrVoid;
disconnect(callback?: Callback): PromiseOrVoid;
ping(callback?: Callback): PromiseOrVoid;
connect(): Promise<void>;
// legacy callback style
connect(callback: Callback): void;
disconnect(): Promise<void>;
// legacy callback style
disconnect(callback: Callback): void;
ping(): Promise<void>;
// legacy callback style
ping(callback: Callback): void;
// Only promise variant, callback is intentionally not supported.
execute(