Skip to content

Commit

Permalink
Add typescript type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed May 3, 2018
1 parent cc99cd8 commit 1fe5308
Show file tree
Hide file tree
Showing 15 changed files with 2,321 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.json
*.md
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"bracketSpacing": false,
"singleQuote": true,
"printWidth": 80,
"trailingComma": "all"
}
20 changes: 20 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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

// Type definitions for loopback-datasource-juggler 3.x
// Project: https://github.com/strongloop/loopback-datasource-juggler
// Definitions by: Raymond Feng <https://github.com/raymondfeng>
// TypeScript Version: 2.8

export * from './types/common';
export * from './types/model';
export * from './types/relation';
export * from './types/query';
export * from './types/datasource';
export * from './types/kv-model';
export * from './types/persisted-model';
export * from './types/scope';
export * from './types/transaction-mixin';
export * from './types/relation-mixin';
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"should": "^8.4.0"
},
"dependencies": {
"@types/node": "^10.0.3",
"async": "~2.1.4",
"bluebird": "^3.1.1",
"debug": "^3.1.0",
Expand Down
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noImplicitAny": true,
"strictNullChecks": true,

"lib": ["es2018", "dom"],
"module": "commonjs",
"moduleResolution": "node",
"target": "es2017",
"sourceMap": true,
"declaration": true
},
"include": ["types", "index.d.ts"],
"exclude": ["node_modules/**"]
}
29 changes: 29 additions & 0 deletions types/common.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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

// tslint:disable:no-any
/**
* Objects with open properties
*/
export interface AnyObject<T = any> {
[property: string]: T;
}

/**
* Type alias for options object
*/
export type Options = AnyObject | null | undefined;

/**
* Type alias for Node.js callback functions
*/
export type Callback<T = any> = (err: any, result?: T) => void;

/**
* Return export type for promisified Node.js async methods.
*
* Note that juggler uses Bluebird, not the native Promise.
*/
export type PromiseOrVoid<T = any> = PromiseLike<T> | void;
169 changes: 169 additions & 0 deletions types/datasource.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// 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

import {Callback, PromiseOrVoid, Options, AnyObject} from './common';
import {
ModelBuilder,
ModelBase,
ModelDefinition,
PropertyDefinition,
} from './model';

/**
* LoopBack models can manipulate data via the DataSource object.
* Attaching a `DataSource` to a `Model` adds instance methods and static methods to the `Model`.
*
* Define a data source to persist model data.
* To create a DataSource programmatically, call `createDataSource()` on the LoopBack object; for example:
* ```js
* var oracle = loopback.createDataSource({
* connector: 'oracle',
* host: '111.22.333.44',
* database: 'MYDB',
* username: 'username',
* password: 'password'
* });
* ```
*
* All classes in single dataSource share same the connector type and
* one database connection.
*
* For example, the following creates a DataSource, and waits for a connection callback.
*
* ```
* var dataSource = new DataSource('mysql', { database: 'myapp_test' });
* dataSource.define(...);
* dataSource.on('connected', function () {
* // work with database
* });
* ```
* @class DataSource
* @param {String} [name] Optional name for datasource.
* @options {Object} settings Database-specific settings to establish connection (settings depend on specific connector).
* The table below lists a typical set for a relational database.
* @property {String} connector Database connector to use. For any supported connector, can be any of:
*
* - The connector module from `require(connectorName)`.
* - The full name of the connector module, such as 'loopback-connector-oracle'.
* - The short name of the connector module, such as 'oracle'.
* - A local module under `./connectors/` folder.
* @property {String} host Database server host name.
* @property {String} port Database server port number.
* @property {String} username Database user name.
* @property {String} password Database password.
* @property {String} database Name of the database to use.
* @property {Boolean} debug Display debugging information. Default is false.
*
* The constructor allows the following styles:
*
* 1. new DataSource(dataSourceName, settings). For example:
* - new DataSource('myDataSource', {connector: 'memory'});
* - new DataSource('myDataSource', {name: 'myDataSource', connector: 'memory'});
* - new DataSource('myDataSource', {name: 'anotherDataSource', connector: 'memory'});
*
* 2. new DataSource(settings). For example:
* - new DataSource({name: 'myDataSource', connector: 'memory'});
* - new DataSource({connector: 'memory'});
*
* 3. new DataSource(connectorModule, settings). For example:
* - new DataSource(connectorModule, {name: 'myDataSource})
* - new DataSource(connectorModule)
*/
export declare class DataSource {
name: string;
settings: AnyObject;

connected?: boolean;
connecting?: boolean;

DataAccessObject: AnyObject & {prototype: AnyObject};

constructor(name?: string, settings?: AnyObject, modelBuilder?: ModelBuilder);

constructor(
connectorModule: AnyObject,
settings?: AnyObject,
modelBuilder?: ModelBuilder,
);

/**
* Create a model class
* @param name Name of the model
* @param properties An object of property definitions
* @param options Options for model settings
*/
createModel<T extends typeof ModelBase>(
name: string,
properties?: AnyObject,
options?: Options,
): T;

automigrate(
models: string | string[],
callback?: Callback,
): PromiseOrVoid<any>;

autoupdate(
models: string | string[],
callback?: Callback,
): PromiseOrVoid<any>;

discoverModelDefinitions(
options?: Options,
callback?: Callback<ModelDefinition[]>,
): PromiseOrVoid<ModelDefinition[]>;

discoverModelProperties(
modelName: string,
options?: Options,
callback?: Callback<PropertyDefinition[]>,
): PromiseOrVoid<PropertyDefinition[]>;

discoverPrimaryKeys(
modelName: string,
options?: Options,
callback?: Callback<PropertyDefinition[]>,
): PromiseOrVoid<PropertyDefinition[]>;

discoverForeignKeys(
modelName: string,
options?: Options,
callback?: Callback<PropertyDefinition[]>,
): PromiseOrVoid<PropertyDefinition[]>;

discoverExportedForeignKeys(
modelName: string,
options?: Options,
callback?: Callback<PropertyDefinition[]>,
): PromiseOrVoid<PropertyDefinition[]>;

discoverAndBuildModels(
modelName: string,
options?: Options,
callback?: Callback<{[name: string]: typeof ModelBase}>,
): PromiseOrVoid<{[name: string]: typeof ModelBase}>;

discoverSchema(
tableName: string,
options?: Options,
callback?: Callback<AnyObject>,
): PromiseOrVoid<AnyObject>;

discoverSchemas(
tableName: string,
options?: Options,
callback?: Callback<AnyObject[]>,
): PromiseOrVoid<AnyObject[]>;

buildModelFromInstance(
modelName: string,
jsonObject: AnyObject,
options?: Options,
): typeof ModelBase;

connect(callback?: Callback): PromiseOrVoid;
disconnect(callback?: Callback): PromiseOrVoid;
ping(callback?: Callback): PromiseOrVoid;
}
Loading

0 comments on commit 1fe5308

Please sign in to comment.