Skip to content

Commit

Permalink
Merge branch 'master' into feature/inject-send-reject
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Jun 27, 2017
2 parents daac287 + eab02da commit 63c3577
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 49 deletions.
14 changes: 7 additions & 7 deletions package.json
Expand Up @@ -11,18 +11,18 @@
"license": "MIT",
"devDependencies": {
"@types/mocha": "^2.2.39",
"@types/node": "^8.0.1",
"@types/request": "0.0.44",
"@types/node": "^8.0.4",
"@types/request": "0.0.45",
"@types/request-promise": "^4.1.35",
"coveralls": "^2.13.1",
"lerna": "^2.0.0-rc.4",
"mocha": "^3.2.0",
"nyc": "^11.0.1",
"lerna": "^2.0.0-rc.5",
"mocha": "^3.4.0",
"nyc": "^11.0.3",
"request": "^2.79.0",
"request-promise": "^4.1.1",
"ts-node": "^3.0.6",
"ts-node": "^3.1.0",
"tslint": "^5.4.3",
"typescript": "^2.2.1"
"typescript": "^2.4.1"
},
"scripts": {
"bootstrap": "lerna bootstrap",
Expand Down
1 change: 0 additions & 1 deletion packages/authentication/package.json
Expand Up @@ -27,7 +27,6 @@
"devDependencies": {
"@loopback/openapi-spec-builder": "^4.0.0-alpha.4",
"@loopback/testlab": "^4.0.0-alpha.5",
"mocha": "^3.2.0",
"passport-http": "^0.3.0"
},
"keywords": [
Expand Down
6 changes: 2 additions & 4 deletions packages/context/package.json
Expand Up @@ -21,10 +21,8 @@
},
"devDependencies": {
"@loopback/testlab": "^4.0.0-alpha.5",
"@types/bluebird": "^3.5.2",
"bluebird": "^3.5.0",
"mocha": "^3.2.0",
"typescript": "^2.3.2"
"@types/bluebird": "^3.5.8",
"bluebird": "^3.5.0"
},
"keywords": [
"LoopBack",
Expand Down
2 changes: 1 addition & 1 deletion packages/context/src/reflect.ts
Expand Up @@ -158,7 +158,7 @@ export class NamespacedReflect {
target: Object,
targetKey?: string | symbol,
descriptor?: PropertyDescriptor,
): PropertyDescriptor {
): PropertyDescriptor | Function {
if (targetKey) {
return Reflect.decorate(decorators, target, targetKey, descriptor);
} else {
Expand Down
6 changes: 2 additions & 4 deletions packages/core/package.json
Expand Up @@ -20,7 +20,7 @@
"dependencies": {
"@loopback/context": "^4.0.0-alpha.9",
"@loopback/openapi-spec": "^4.0.0-alpha.6",
"@types/bluebird": "^3.5.2",
"@types/bluebird": "^3.5.8",
"@types/http-errors": "^1.5.34",
"bluebird": "^3.5.0",
"body": "^5.1.0",
Expand All @@ -30,9 +30,7 @@
},
"devDependencies": {
"@loopback/openapi-spec-builder": "^4.0.0-alpha.4",
"@loopback/testlab": "^4.0.0-alpha.5",
"mocha": "^3.2.0",
"typescript": "^2.3.2"
"@loopback/testlab": "^4.0.0-alpha.5"
},
"files": [
"README.md",
Expand Down
3 changes: 1 addition & 2 deletions packages/example-codehub/package.json
Expand Up @@ -15,7 +15,6 @@
"@loopback/core": "^4.0.0-alpha.9"
},
"devDependencies": {
"@loopback/testlab": "^4.0.0-alpha.5",
"mocha": "^3.2.0"
"@loopback/testlab": "^4.0.0-alpha.5"
}
}
3 changes: 0 additions & 3 deletions packages/openapi-spec-builder/package.json
Expand Up @@ -32,8 +32,5 @@
"repository": {
"type": "git",
"url": "https://github.com/strongloop/loopback-next.git"
},
"devDependencies": {
"typescript": "^2.3.2"
}
}
3 changes: 0 additions & 3 deletions packages/openapi-spec/package.json
Expand Up @@ -28,8 +28,5 @@
"repository": {
"type": "git",
"url": "https://github.com/strongloop/loopback-next.git"
},
"devDependencies": {
"typescript": "^2.3.2"
}
}
5 changes: 2 additions & 3 deletions packages/repository/package.json
Expand Up @@ -18,12 +18,11 @@
"author": "IBM",
"license": "MIT",
"devDependencies": {
"@loopback/testlab": "^4.0.0-alpha.5",
"mocha": "^3.2.0"
"@loopback/testlab": "^4.0.0-alpha.5"
},
"dependencies": {
"@loopback/context": "^4.0.0-alpha.9",
"loopback-datasource-juggler": "^3.9.0"
"loopback-datasource-juggler": "^3.9.2"
},
"files": [
"README.md",
Expand Down
7 changes: 4 additions & 3 deletions packages/repository/src/legacy-juggler-bridge.ts
Expand Up @@ -42,7 +42,8 @@ export function bindModel<T extends typeof juggler.ModelBase>(
* Ensure the value is a promise
* @param p Promise or void
*/
function ensurePromise<T>(p: juggler.PromiseOrVoid<T>): Promise<T> {
/* tslint:disable-next-line:no-any */
function ensurePromise(p: juggler.PromiseOrVoid<any>): Promise<any> {
if (p && isPromise(p)) {
return p;
} else {
Expand Down Expand Up @@ -80,7 +81,7 @@ export class DefaultCrudRepository<T extends Entity, ID>
return ensurePromise(this.modelClass.create(entities, options));
}

save(entity: ObjectType<T>, options?: Options): Promise<T> {
save(entity: ObjectType<T>, options?: Options): Promise<T | null> {
const idName = this.modelClass.definition.idName();
let id;
if (typeof entity.getId === 'function') {
Expand All @@ -92,7 +93,7 @@ export class DefaultCrudRepository<T extends Entity, ID>
return this.create(entity, options);
} else {
return this.replaceById(id, entity, options).then(
result => (result ? entity : null),
result => (result ? entity as T: null),
);
}
}
Expand Down
27 changes: 18 additions & 9 deletions packages/repository/src/repository.ts
Expand Up @@ -84,9 +84,9 @@ export interface EntityCrudRepository<T extends Entity, ID>
* Save an entity. If no id is present, create a new entity
* @param entity Entity to be saved
* @param options Options for the operations
* @returns A promise of an entity saved
* @returns A promise of an entity saved or null if the entity does not exist
*/
save(entity: ObjectType<T>, options?: Options): Promise<T>;
save(entity: ObjectType<T>, options?: Options): Promise<T | null>;

/**
* Update an entity
Expand Down Expand Up @@ -183,23 +183,32 @@ export class CrudRepositoryImpl<T extends Entity, ID>
this.connector = dataSource.connector as CrudConnector;
}

private toModels(data: Promise<ObjectType<Entity>[]>): Promise<T[]> {
return data.then(items => items.map(i => new this.model(i)));
}

private toModel(data: Promise<ObjectType<Entity>>): Promise<T> {
return data.then(d => new this.model(d));
}

create(entity: ObjectType<T>, options?: Options): Promise<T> {
return this.connector.create(this.model, entity, options);
return this.toModel(this.connector.create(this.model, entity, options));
}

createAll(entities: ObjectType<T>[], options?: Options): Promise<T[]> {
return this.connector.create(this.model, entities, options);
return this.toModels(
this.connector.createAll!(this.model, entities, options));
}

save(entity: ObjectType<T>, options?: Options): Promise<T> {
save(entity: ObjectType<T>, options?: Options): Promise<T | null> {
if (typeof this.connector.save === 'function') {
return this.connector.save(this.model, entity, options);
return this.toModel(this.connector.save(this.model, entity, options));
} else {
if (entity.getId() != null) {
return this.replaceById(entity.getId(), entity, options).then(
(result: boolean) =>
result
? Promise.resolve(entity)
? this.toModel(Promise.resolve(entity))
: Promise.reject(new Error('Not found')),
);
} else {
Expand All @@ -209,12 +218,12 @@ export class CrudRepositoryImpl<T extends Entity, ID>
}

find(filter?: Filter, options?: Options): Promise<T[]> {
return this.connector.find(this.model, filter, options);
return this.toModels(this.connector.find(this.model, filter, options));
}

findById(id: ID, options?: Options): Promise<T> {
if (typeof this.connector.findById === 'function') {
return this.connector.findById(this.model, id, options);
return this.toModel(this.connector.findById(this.model, id, options));
}
const where = this.model.buildWhereForId(id);
return this.connector
Expand Down
Expand Up @@ -147,16 +147,16 @@ describe('DefaultCrudRepository', () => {
it('implements Repository.save() without id', async () => {
const repo = new DefaultCrudRepository(Note, ds);
const note = await repo.save({title: 't3', content: 'c3'});
const result = await repo.findById(note.id);
expect(result.toJSON()).to.eql(note.toJSON());
const result = await repo.findById(note!.id);
expect(result.toJSON()).to.eql(note!.toJSON());
});

it('implements Repository.save() with id', async () => {
const repo = new DefaultCrudRepository(Note, ds);
const note1 = await repo.create({title: 't3', content: 'c3'});
note1.content = 'c4';
const note = await repo.save(note1);
const result = await repo.findById(note.id);
const result = await repo.findById(note!.id);
expect(result.toJSON()).to.eql(note1.toJSON());
});

Expand Down
8 changes: 2 additions & 6 deletions packages/testlab/package.json
Expand Up @@ -14,18 +14,14 @@
},
"author": "IBM",
"license": "MIT",
"devDependencies": {
"mocha": "^3.3.0",
"typescript": "^2.2.2"
},
"dependencies": {
"@types/shot": "^3.4.0",
"@types/sinon": "^1.16.34",
"@types/sinon": "^2.3.2",
"@types/supertest": "^2.0.0",
"shot": "^3.4.0",
"should": "^11.2.1",
"should-sinon": "0.0.5",
"sinon": "^1.17.7",
"sinon": "^2.3.5",
"supertest": "^3.0.0"
},
"files": [
Expand Down

0 comments on commit 63c3577

Please sign in to comment.