-
Notifications
You must be signed in to change notification settings - Fork 364
feat: add TS types #1904
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
base: master
Are you sure you want to change the base?
feat: add TS types #1904
Changes from 19 commits
8fb2b6f
6281ebe
b500cd7
6c66be8
2cbf624
6479173
b460b94
68521cb
27496ae
ecb1a7b
54aa233
f75a599
17ff165
7234599
fe45124
181578d
2e7b0cf
aedf47a
8b56310
055efd3
884776b
db34269
e1bb7a1
de9ce11
5a52edb
7d59e16
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 |
---|---|---|
|
@@ -3,28 +3,348 @@ | |
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {Callback, DataSource, Options, PromiseOrVoid} from '..'; | ||
import { AnyObject } from 'strong-globalize/lib/config'; | ||
import {Callback, DataSource, Filter, ModelBase, ModelBaseClass, ModelDefinition, ModelProperties, Options, PromiseOrVoid, PropertyDefinition, PropertyType, Schema, Where} from '..'; | ||
|
||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: loopback-datasource-juggler | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
export interface ConnectorSettings extends Options { | ||
name?: string; | ||
/** | ||
* Overrides {@link ConnectorSettings.adapter} if defined. | ||
*/ | ||
connector?: ConnectorStatic | string; | ||
/** | ||
* @deprecated Use {@link ConnectorSettings.connector} instead. | ||
*/ | ||
adapter?: ConnectorStatic | string; | ||
connectionTimeout?: number; | ||
maxOfflineRequests?: number; | ||
lazyConnect?: boolean; | ||
debug?: boolean; | ||
|
||
// Postgres-specific | ||
defaultIdSort?: boolean | 'numericIdOnly'; | ||
onError?: (err: Error | unknown) => unknown | 'ignore'; | ||
// END Postgres-specific | ||
} | ||
achrinza marked this conversation as resolved.
Show resolved
Hide resolved
achrinza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export interface IDPropertiesDiscoveryOptions { | ||
owner?: string; | ||
schema?: string; | ||
} | ||
|
||
export interface DiscoveryScopeOptions { | ||
owner?: string; | ||
all?: boolean; | ||
views?: boolean; | ||
limit?: number; | ||
offset?: number; | ||
} | ||
|
||
export interface SchemaDiscoveryOptions { | ||
|
||
/** | ||
* Sets if all owners are included. | ||
*/ | ||
all?: boolean; | ||
|
||
/** | ||
* Sets if views are included. | ||
*/ | ||
views?: boolean; | ||
|
||
/** | ||
* Sets if the database foreign key column names should be transformed. | ||
* | ||
* @remarks | ||
* Used by the default built-in {@link NameMapper} implementation to transform | ||
* the database column names to use camel-case, LoopBack's default naming | ||
* conventions. | ||
* | ||
* @defaultValue `false` | ||
*/ | ||
disableCamelCase?: boolean; | ||
|
||
/** | ||
* A custom database table name, model, and foreign key transformer. | ||
* | ||
* @remarks | ||
* If `null`, no transform is performed. | ||
* If `undefined`, default built-in transformer is used. | ||
*/ | ||
nameMapper?: NameMapper | null; | ||
|
||
/** | ||
* Sets if associations/relations should be navigated. | ||
* | ||
* @remarks | ||
* Alias of {@link SchemaDiscoveryOptions.relations} | ||
*/ | ||
associations?: boolean; | ||
|
||
/** | ||
* Sets if associations/relations should be navigated. | ||
* | ||
* @remarks | ||
* Alias of {@link SchemaDiscoveryOptions.associations}. | ||
*/ | ||
relations?: boolean; | ||
|
||
owner?: string; | ||
schema?: string; | ||
} | ||
|
||
export type NameMapper = (type: 'table' | 'model' | 'fk' | string, name: string) => string | null; | ||
|
||
export interface DiscoveredPrimaryKeys { | ||
owner: string | null; | ||
tableName: string; | ||
columnName: string; | ||
keySeq: number; | ||
pkName: string | null; | ||
} | ||
|
||
export interface DiscoveredForeignKeys { | ||
fkOwner: string | null; | ||
fkName: string | null; | ||
fkTableName: string; | ||
fkColumnName: string; | ||
keySeq: number; | ||
|
||
pkOwner: string | null; | ||
pkName: string | null; | ||
pkTableName: string; | ||
pkColumnName: string; | ||
} | ||
|
||
export interface DiscoveredModelProperties { | ||
owner?: string; | ||
tableName?: string; | ||
columnName?: string; | ||
dataType?: string; | ||
dataLength?: number; | ||
dataPrecision?: number; | ||
dataScale?: number; | ||
nullable?: boolean; | ||
} | ||
|
||
// #TODO(achrinza): The codebase suggets that `context` differs | ||
// depending on the situation, and that there's no cohesive interface. | ||
// Hence, we'll need to identify all the possible contexts. | ||
export interface Context { | ||
Model: ModelBaseClass; | ||
instance?: object; | ||
query?: Filter; | ||
where?: Where; | ||
data?: AnyObject; | ||
hookState: object; | ||
options: Options; | ||
isNewInstance?: boolean; | ||
currentInstance?: Readonly<ModelBase>; | ||
} | ||
Comment on lines
+242
to
+255
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. #TODO(achrinza): The codebase suggets that |
||
|
||
export interface ConnectorHookBeforeExecuteContext { | ||
req: Record<string, unknown>; | ||
end: Callback<ConnectorHookBeforeExecuteContext>; | ||
} | ||
|
||
export interface ConnectorHookAfterExecuteContext extends ConnectorHookBeforeExecuteContext { | ||
res: Record<string, unknown>; | ||
} | ||
/** | ||
* Connector from `loopback-connector` module | ||
*/ | ||
export interface Connector { | ||
name: string; // Name/type of the connector | ||
dataSource?: DataSource; | ||
connect(callback?: Callback): PromiseOrVoid; // Connect to the underlying system | ||
disconnect(callback?: Callback): PromiseOrVoid; // Disconnect from the underlying system | ||
ping(callback?: Callback): PromiseOrVoid; // Ping the underlying system | ||
|
||
achrinza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @internal | ||
*/ | ||
_models?: Record<string, ModelBaseClass>; | ||
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. Not sure if this is correct. 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. Most probably |
||
connect?(callback?: Callback): PromiseOrVoid; // Connect to the underlying system | ||
disconnect?(callback?: Callback): PromiseOrVoid; // Disconnect from the underlying system | ||
|
||
/** | ||
* Ping the underlying connector to test the connections. | ||
* | ||
* @remarks | ||
* Unlike {@link DataSource.ping}, if no callback is provided, a | ||
* {@link Promise} return value is not guaranteed. | ||
* | ||
* @param callback Callback function | ||
* @returns a {@link Promise} or `void` | ||
*/ | ||
ping?(callback?: Callback): PromiseOrVoid; // Ping the underlying system | ||
execute?(...args: any[]): Promise<any>; | ||
|
||
/** | ||
* Get the connector's types collection. | ||
* | ||
* @remarks | ||
* For example, ['db', 'nosql', 'mongodb'] would be represent a datasource of | ||
* type 'db', with a subtype of 'nosql', and would use the 'mongodb' connector. | ||
* | ||
* Alternatively, ['rest'] would be a different type altogether, and would have | ||
* no subtype. | ||
* | ||
* @returns The connector's type collection. | ||
*/ | ||
getTypes?(): string[]; | ||
define?(def: {model: ModelBaseClass, properties: PropertyDefinition, settings: ModelDefinition['settings']}): void; | ||
|
||
/** | ||
* Define a property on the target model. | ||
* | ||
* @param model Name of model | ||
* @param prop Name of property | ||
* @param params Property settings | ||
*/ | ||
defineProperty?(model: string, prop: string, params: PropertyDefinition): void; | ||
defineForeignKey?(modelName: string, key: string, foreignModelName: string, cb: Callback<PropertyType>): void; | ||
defineForeignKey?(modelName: string, key: string, cb: Callback<PropertyType>): void; | ||
achrinza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
getDefaultIdType(): object; | ||
isRelational(): boolean; | ||
|
||
/** | ||
* Discover existing database tables. | ||
* | ||
* @param options Discovery options | ||
* @param cb Callback function | ||
*/ | ||
discoverModelDefinitions?(options: DiscoveryScopeOptions, cb: Callback): Promise<ModelDefinition[]>; | ||
|
||
/** | ||
* {@inheritDoc Connector.discoverModelDefinitions} | ||
* @deprecated | ||
*/ | ||
discoverModelDefinitionsSync?(options: DiscoveryScopeOptions): ModelDefinition[] | ||
|
||
/** | ||
* Discover properties for a given model. | ||
* | ||
* @param modelName Target table/view name | ||
* @param options Discovery options | ||
* @param cb Callback function | ||
*/ | ||
discoverModelProperties?(modelName: string, options: DiscoveryScopeOptions, cb: Callback<DiscoveredModelProperties>): Promise<DiscoveredModelProperties>; | ||
/** | ||
* {@inheritDoc Connector.discoverModelProperties} | ||
* @deprecated | ||
*/ | ||
discoverModelPropertiesSync?(modelName: string, options: DiscoveryScopeOptions): DiscoveredModelProperties; | ||
|
||
/** | ||
* Discover primary keys for a given owner/model name. | ||
* | ||
* @param modelName Target model name | ||
* @param options Discovery options | ||
* @param cb Callback function | ||
*/ | ||
discoverPrimaryKeys?(modelName: string, options: IDPropertiesDiscoveryOptions, cb: Callback<DiscoveredPrimaryKeys>): Promise<DiscoveredPrimaryKeys>; | ||
|
||
/** | ||
* {@inheritDoc Connector.discoverPrimaryKeys} | ||
* @deprecated | ||
*/ | ||
discoverPrimaryKeysSync?(modelName: string, options: IDPropertiesDiscoveryOptions): DiscoveredPrimaryKeys; | ||
|
||
/** | ||
* Discover foreign keys for a given owner/model name. | ||
* | ||
* @param modelName Target model name | ||
* @param options Discovery options | ||
* @param cb Callback function | ||
*/ | ||
discoverForeignKeys?(modelName: string[], options: IDPropertiesDiscoveryOptions, cb: Callback<DiscoveredForeignKeys>): Promise<DiscoveredForeignKeys>; | ||
/** | ||
* {@inheritDoc Connector.discoverForeignKeys} | ||
* @deprecated | ||
*/ | ||
discoverForeignKeysSync?(modelName: string[], options: IDPropertiesDiscoveryOptions): DiscoveredForeignKeys; | ||
|
||
/** | ||
* Retrieve a description of the foreign key columns that reference the given | ||
* table's primary key columns (i.e. The foreign keys exported by a table), | ||
* ordered by `fkOwner`, `fkTableName`, and `keySeq`. | ||
* | ||
* @param modelName Target model name | ||
* @param options Discovery options | ||
* @param cb Callback function | ||
*/ | ||
discoverExportedForeignKeys?(modelName: string, options: IDPropertiesDiscoveryOptions, cb: Callback<DiscoveredForeignKeys>): Promise<DiscoveredForeignKeys>; | ||
|
||
/** | ||
* {@inheritDoc Connector/discoverExportedForeignKeys} | ||
* @deprecated | ||
*/ | ||
discoverExportedForeignKeysSync?(modelName: string, options?: {owner?: string}): DiscoveredForeignKeys; | ||
|
||
/** | ||
* Discover schema from a given table name / view name. | ||
* | ||
* @param tableName Target table name | ||
* @param options Discovery options | ||
* @param cb Callback function | ||
*/ | ||
discoverSchemas(tableName: string, options: SchemaDiscoveryOptions, cb: Callback<Schema>): Promise<Schema>; | ||
|
||
/** | ||
* Check whether or not migrations are required for the database schema to match | ||
* the model definitions attached to the {@link DataSource}. | ||
* | ||
* @param models Name of models to check. If not defined, all models are checked. | ||
* @param cb | ||
*/ | ||
isActual?(models?: string | string[], cb?: Callback<boolean>): void; | ||
|
||
/** | ||
* Freeze the datasource. Behaviour depends on the {@link Connector}. | ||
*/ | ||
freezeDataSource?(): void; | ||
|
||
/** | ||
* {@inheritDoc Connector.freezeDataSource} | ||
* | ||
* @remarks | ||
* This is kept for backwards-compatibility with JugglingDB connectors. | ||
* Connectors should implement {@link Connector.freezeDataSource}. | ||
*/ | ||
freezeSchema?(): void; | ||
|
||
/** | ||
* Normalize connector-specific return data into standardised Juggler context | ||
* data. | ||
* | ||
* @remarks | ||
* Depending on the connector, the database response can contain information | ||
* about the updated record(s). This object usually has a database-specific | ||
* structure and does not match model properties. For example, MySQL returns | ||
* `OkPacket: {fieldCount, affectedRows, insertId, ... , changedRows}`. | ||
* | ||
* The return value is normalised data. | ||
* | ||
* If the connector DDL and DML functions map directly to a hash-map of | ||
* model properties and their values, this function does not need to be | ||
* implemented. | ||
* | ||
* @param context | ||
* @param dbResponse | ||
*/ | ||
generateContextData?(context: Context, dbResponse: unknown): Context; | ||
|
||
achrinza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[property: string]: any; // Other properties that vary by connectors | ||
} | ||
|
||
export interface BuiltConnector extends Connector { | ||
achrinza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dataSource: DataSource; | ||
log: DataSource['log']; | ||
logger(query: string, start: number): (query: string) => void; | ||
} | ||
|
||
/** | ||
* Base connector class | ||
* | ||
* @internal | ||
*/ | ||
export declare class ConnectorBase implements Connector { | ||
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. May need to extract this to |
||
name: string; // Name/type of the connector; | ||
|
@@ -43,6 +363,16 @@ export declare class ConnectorBase implements Connector { | |
static initialize(dataSource: DataSource, callback?: Callback): void; | ||
|
||
constructor(settings?: Options); | ||
[property: string]: any; | ||
_models?: Record<string, ModelBaseClass>; | ||
getDefaultIdType(): object; | ||
isRelational(): boolean; | ||
discoverSchemas(tableName: string, options: SchemaDiscoveryOptions, cb: Callback<Schema>): Promise<Schema>; | ||
} | ||
|
||
export declare interface ConnectorStatic { | ||
initialize(this: DataSource, callback: Callback): void; | ||
new (settings: ConnectorSettings): Connector; | ||
} | ||
|
||
export declare class Memory extends ConnectorBase {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Options } from ".."; | ||
|
||
export interface DaoDmlOptions extends Options { | ||
validate?: boolean; | ||
notify?: boolean; | ||
} | ||
|
||
export interface DaoUpsertOptions extends DaoDmlOptions { | ||
validateUpsert?: boolean; | ||
} | ||
|
||
export interface DaoUpdateOptions extends DaoDmlOptions { | ||
validateUpdate?: boolean; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.