Skip to content

Commit

Permalink
Address review comments from Microslav
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Feng committed Jun 5, 2017
1 parent 5dc0fd5 commit 9d2664b
Show file tree
Hide file tree
Showing 23 changed files with 71 additions and 63 deletions.
2 changes: 1 addition & 1 deletion packages/authentication/package.json
Expand Up @@ -19,7 +19,7 @@
"license": "MIT",
"dependencies": {
"@loopback/core": "^4.0.0-alpha.5",
"@loopback/context": "4.0.0-alpha.6",
"@loopback/context": "^4.0.0-alpha.6",
"@types/passport": "^0.3.3",
"passport": "^0.3.2",
"passport-strategy": "^1.0.0"
Expand Down
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Options} from '../../src/common';
import {Options} from '../../src/common-types';
import {Where} from '../../src/query';
import {DataSource} from '../../src/datasource';

Expand Down
File renamed without changes.
7 changes: 3 additions & 4 deletions packages/repository/src/connector.ts
Expand Up @@ -3,7 +3,6 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {AnyType} from './common';
import {Model} from './model';

/**
Expand All @@ -13,7 +12,7 @@ export interface Connector {
name: string; // Name/type of the connector
configModel?: Model; // The configuration model
interfaces?: string[]; // A list of interfaces implemented by the connector
connect(): Promise<AnyType>; // Connect to the underlying system
disconnect(): Promise<AnyType>; // Disconnect from the underlying system
ping(): Promise<AnyType>; // Ping the underlying system
connect(): Promise<void>; // Connect to the underlying system
disconnect(): Promise<void>; // Disconnect from the underlying system
ping(): Promise<void>; // Ping the underlying system
}
2 changes: 1 addition & 1 deletion packages/repository/src/crud-connector.ts
Expand Up @@ -6,7 +6,7 @@
import { Connector } from './connector';
import { Entity } from './model';
import { Filter, Where } from './query';
import { Class, ObjectType, Options, AnyType } from './common';
import { Class, ObjectType, Options, AnyType } from './common-types';

export type EntityData = ObjectType<Entity>;

Expand Down
2 changes: 1 addition & 1 deletion packages/repository/src/datasource.ts
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {AnyType} from './common';
import {AnyType} from './common-types';
import {Connector} from './connector';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/repository/src/decorators/model.ts
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import { Class, AnyType } from '../common';
import { Class, AnyType } from '../common-types';
import { Reflector } from '@loopback/context';

export const MODEL_KEY = 'loopback:model';
Expand Down
2 changes: 1 addition & 1 deletion packages/repository/src/decorators/relation.ts
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import { Class, AnyType } from '../common';
import { Class, AnyType } from '../common-types';
import { Entity } from '../model';

import { Reflector } from '@loopback/context';
Expand Down
2 changes: 1 addition & 1 deletion packages/repository/src/decorators/repository.ts
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import { Class, AnyType } from '../common';
import { Class, AnyType } from '../common-types';
import { Model } from '../model';
import { Repository } from '../repository';
import { DataSource } from '../datasource';
Expand Down
2 changes: 1 addition & 1 deletion packages/repository/src/index.ts
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './common';
export * from './common-types';
export * from './decorators/model';
export * from './decorators/repository';
export * from './model';
Expand Down
4 changes: 2 additions & 2 deletions packages/repository/src/kv-connector.ts
Expand Up @@ -4,9 +4,9 @@
// License text available at https://opensource.org/licenses/MIT

import {Connector} from './connector';
import {Options} from './common';
import {Options} from './common-types';
import {Entity} from './model';
import {Class, ObjectType} from './common';
import {Class, ObjectType} from './common-types';
import {Filter} from './query';

export type EntityData = ObjectType<Entity>;
Expand Down
4 changes: 2 additions & 2 deletions packages/repository/src/kv-repository.ts
Expand Up @@ -4,9 +4,9 @@
// License text available at https://opensource.org/licenses/MIT

import {Repository} from './repository';
import {Options} from './common';
import {Options} from './common-types';
import {Model} from './model';
import {Class, ObjectType} from './common';
import {Class, ObjectType} from './common-types';
import {Filter} from './query';

/**
Expand Down
28 changes: 14 additions & 14 deletions packages/repository/src/legacy-juggler-bridge.ts
Expand Up @@ -6,16 +6,16 @@
export const jugglerModule = require('loopback-datasource-juggler');

import {MixinBuilder} from './mixin';
import {Class, ObjectType, Options, AnyType} from './common';
import {Class, ObjectType, Options, AnyType} from './common-types';

import {juggler} from './loopback-datasource-juggler';

export * from './loopback-datasource-juggler';

/* tslint:disable-next-line:variable-name */
export const DataSource = jugglerModule.DataSource as typeof juggler.DataSource;
export const DataSourceConstructor = jugglerModule.DataSource as typeof juggler.DataSource;
/* tslint:disable-next-line:variable-name */
export const ModelBase = jugglerModule.ModelBaseClass as typeof juggler.ModelBase;
export const ModelBaseConstructor = jugglerModule.ModelBaseClass as typeof juggler.ModelBase;

/**
* This is a bridge to the legacy DAO class. The function mixes DAO methods
Expand All @@ -39,7 +39,7 @@ function isPromise<T>(p: AnyType): p is Promise<T> {
return p !== null && typeof p === 'object' && typeof p.then === 'function';
}

function getPromise<T>(p: juggler.PromiseOrVoid<T>): Promise<T> {
function ensurePromise<T>(p: juggler.PromiseOrVoid<T>): Promise<T> {
if (isPromise(p)) {
return p;
} else {
Expand All @@ -56,11 +56,11 @@ implements EntityCrudRepository<T, ID> {
}

create(entity: ObjectType<T>, options?: Options): Promise<T> {
return getPromise(this.modelClass.create(entity, options));
return ensurePromise(this.modelClass.create(entity, options));
}

createAll(entities: ObjectType<T>[], options?: Options): Promise<T[]> {
return getPromise(this.modelClass.create(entities, options));
return ensurePromise(this.modelClass.create(entities, options));
}

save(entity: ObjectType<T>, options?: Options): Promise<T> {
Expand All @@ -80,11 +80,11 @@ implements EntityCrudRepository<T, ID> {
}

find(filter?: Filter, options?: Options): Promise<T[]> {
return getPromise(this.modelClass.find(filter, options));
return ensurePromise(this.modelClass.find(filter, options));
}

findById(id: ID, filter?: Filter, options?: Options): Promise<T> {
return getPromise(this.modelClass.findById(id, filter, options));
return ensurePromise(this.modelClass.findById(id, filter, options));
}

update(entity: ObjectType<T>, options?: Options): Promise<boolean> {
Expand All @@ -96,7 +96,7 @@ implements EntityCrudRepository<T, ID> {
}

updateAll(data: ObjectType<T>, where?: Where, options?: Options): Promise<number> {
return getPromise(this.modelClass.updateAll(where, data, options)).
return ensurePromise(this.modelClass.updateAll(where, data, options)).
then(result => result.count);
}

Expand All @@ -108,24 +108,24 @@ implements EntityCrudRepository<T, ID> {
}

replaceById(id: ID, data: ObjectType<T>, options?: Options): Promise<boolean> {
return getPromise(this.modelClass.replaceById(id, data, options)).then(result => !!result);
return ensurePromise(this.modelClass.replaceById(id, data, options)).then(result => !!result);
}

deleteAll(where?: Where, options?: Options): Promise<number> {
return getPromise(this.modelClass.deleteAll(where, options)).
return ensurePromise(this.modelClass.deleteAll(where, options)).
then(result => result.count);
}

deleteById(id: ID, options?: Options): Promise<boolean> {
return getPromise(this.modelClass.deleteById(id, options)).
return ensurePromise(this.modelClass.deleteById(id, options)).
then(result => result.count > 0);
}

count(where?: Where, options?: Options): Promise<number> {
return getPromise(this.modelClass.count(where, options));
return ensurePromise(this.modelClass.count(where, options));
}

exists(id: ID, options?: Options): Promise<boolean> {
return getPromise(this.modelClass.exists(id, options));
return ensurePromise(this.modelClass.exists(id, options));
}
}
30 changes: 18 additions & 12 deletions packages/repository/src/loopback-datasource-juggler.ts
Expand Up @@ -3,25 +3,14 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import { Class, Options, Callback, AnyObject, AnyType } from './common';
import { Class, Options, Callback, AnyObject, AnyType } from './common-types';

export declare namespace juggler {
/**
* Return type for promisified Node.js async methods
*/
export type PromiseOrVoid<T> = Promise<T> | void;

/**
* DataSource instance properties/operations
*/
export class DataSource {
name: string;
settings: AnyObject;
constructor(settings?: AnyObject);
createModel(name: string, properties?: Object, options?: Options):
(typeof ModelBase) | (typeof PersistedModel);
}

/**
* Base model class
*/
Expand All @@ -33,6 +22,23 @@ export declare namespace juggler {
[property: string]: AnyType;
}

/**
* DataSource instance properties/operations
*/
export class DataSource {
name: string;
settings: AnyObject;
constructor(settings?: AnyObject);
/**
* 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;
}

/**
* Union type for model instance or plain object representing the model instance
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/repository/src/mixin.ts
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Class, AnyType} from './common';
import {Class, AnyType} from './common-types';

/**
* Interface for functions that can mix properties/methods into a base class
Expand Down
2 changes: 1 addition & 1 deletion packages/repository/src/model.ts
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Options, AnyType, AnyObject} from './common';
import {Options, AnyType, AnyObject} from './common-types';
import {Type} from './types';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/repository/src/query.ts
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {AnyType} from './common';
import {AnyType} from './common-types';

/**
* Operators for where clauses
Expand Down
2 changes: 1 addition & 1 deletion packages/repository/src/repository.ts
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import { Entity, ValueObject, Model } from './model';
import { Class, ObjectType, AnyObject, Options } from './common';
import { Class, ObjectType, AnyObject, Options } from './common-types';
import { DataSource } from './datasource';
import { CrudConnector, EntityData } from './crud-connector';
import { Fields, Filter, Where, Operators, Inclusion } from './query';
Expand Down
2 changes: 1 addition & 1 deletion packages/repository/src/types/buffer.ts
Expand Up @@ -5,7 +5,7 @@

import * as util from 'util';
import {Type, AnyType} from './type';
import {Options} from '../common';
import {Options} from '../common-types';

/**
* Buffer (binary) type
Expand Down
4 changes: 2 additions & 2 deletions packages/repository/src/types/type.ts
Expand Up @@ -6,10 +6,10 @@
/**
* Typing system for LoopBack
*/
import {Options, AnyType} from '../common';
import {Options, AnyType} from '../common-types';
import * as util from 'util';

export {AnyType} from '../common';
export {AnyType} from '../common-types';

export interface Type<T> {
/**
Expand Down
10 changes: 5 additions & 5 deletions packages/repository/test/unit/decorator/repository.ts
Expand Up @@ -7,9 +7,9 @@ import { expect } from '@loopback/testlab';
import { Context } from '@loopback/context';
import { repository } from '../../../src/decorators/repository';

import { AnyType } from '../../../src/common';
import { AnyType } from '../../../src/common-types';
import { Repository } from '../../../src/repository';
import { jugglerModule, bindModel, DataSource, juggler, DefaultCrudRepository }
import { jugglerModule, bindModel, DataSourceConstructor, juggler, DefaultCrudRepository }
from '../../../src/legacy-juggler-bridge';

class MyController {
Expand All @@ -22,14 +22,14 @@ describe('repository decorator', () => {
let repo: Repository<AnyType>;

before(function() {
const ds: juggler.DataSource = new DataSource({
const ds: juggler.DataSource = new DataSourceConstructor({
name: 'db',
connector: 'memory',
});

/* tslint:disable-next-line:variable-name */
const Note = <typeof juggler.PersistedModel>
ds.createModel('note', { title: 'string', content: 'string' }, {});
const Note = ds.createModel<typeof juggler.PersistedModel>(
'note', { title: 'string', content: 'string' }, {});
repo = new DefaultCrudRepository(Note, ds);
ctx = new Context();
ctx.bind('repositories:noteRepo').to(repo);
Expand Down

0 comments on commit 9d2664b

Please sign in to comment.