Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/rest-crud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"build": "lb-tsc",
"clean": "lb-clean loopback-rest-crud*.tgz dist *.tsbuildinfo package",
"pretest": "npm run build",
"test": "lb-mocha \"dist/__tests__/**/*.js\"",
"test": "lb-mocha \"dist/__tests__/**/crud-rest-controller.unit.js\"",
"verify": "npm pack && tar xf loopback-rest-crud*.tgz && tree package && npm run clean"
},
"publishConfig": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Entity, model, property} from '@loopback/repository';
import {
Entity,
EntityCrudRepository,
Filter,
model,
property,
} from '@loopback/repository';
import {expect} from '@loopback/testlab';
import {defineCrudRestController} from '../..';
import {CrudRestControllerOptions, defineCrudRestController} from '../..';

describe('defineCrudRestController', () => {
it('should generate controller based on Model name', async () => {
Expand Down Expand Up @@ -45,4 +51,74 @@ describe('defineCrudRestController', () => {
// It must generate read methods
expect(CrudRestController.prototype.find).not.equal(undefined);
});

it('passes options parameter to repository methods', async () => {
@model()
class Product extends Entity {
@property({id: true})
id: number;
}

const controllerOptions = {
basePath: '/products',
};

const CrudRestController = defineCrudRestController<
Product,
typeof Product.prototype.id,
'id'
>(Product, controllerOptions);

let passedOptions: CrudRestControllerOptions = {basePath: '/'};
const mockRepo = {
find: async (
filter: Filter<Product>,
options: CrudRestControllerOptions,
) => {
passedOptions = options;
return [];
},
} as unknown as EntityCrudRepository<Product, typeof Product.prototype.id>;

const controller = new CrudRestController(mockRepo);
await controller.find();
expect(passedOptions).to.deepEqual(controllerOptions);
});

it('passes options parameter to findById repository method at correct offset', async () => {
@model()
class Product extends Entity {
@property({id: true})
id: number;
}

const controllerOptions = {
basePath: '/products',
};

const CrudRestController = defineCrudRestController<
Product,
typeof Product.prototype.id,
'id'
>(Product, controllerOptions);

let passedOptions: CrudRestControllerOptions | undefined;
const mockRepo = {
findById: async (
id: number,
filter?: Filter<Product>,
options?: CrudRestControllerOptions,
) => {
passedOptions = options;
return {id};
},
} as unknown as EntityCrudRepository<Product, typeof Product.prototype.id>;

const controller = new CrudRestController(mockRepo) as unknown as {
findById(id: number, filter?: Filter<Product>): Promise<Product>;
};

await controller.findById(1);
expect(passedOptions).to.deepEqual(controllerOptions);
});
});
13 changes: 8 additions & 5 deletions packages/rest-crud/src/crud-rest.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export function defineCrudRestController<
@param.filter(modelCtor)
filter?: Filter<T>,
): Promise<(T & Relations)[]> {
return this.repository.find(filter);
return this.repository.find(filter, options);
}

@get('/{id}', {
Expand All @@ -182,7 +182,7 @@ export function defineCrudRestController<
)
filter?: FilterExcludingWhere<T>,
): Promise<T & Relations> {
return this.repository.findById(id, filter);
return this.repository.findById(id, filter, options);
}

@get('/count', {
Expand All @@ -192,7 +192,7 @@ export function defineCrudRestController<
@param.where(modelCtor)
where?: Where<T>,
): Promise<Count> {
return this.repository.count(where);
return this.repository.count(where, options);
}
}

Expand All @@ -218,6 +218,7 @@ export function defineCrudRestController<
// FIXME(bajtos) Improve repository API to support this use case
// with no explicit type-casts required
data as DataObject<T>,
options,
);
}

Expand All @@ -236,6 +237,7 @@ export function defineCrudRestController<
// with no explicit type-casts required
data as DataObject<T>,
where,
options,
);
}

Expand All @@ -253,6 +255,7 @@ export function defineCrudRestController<
// FIXME(bajtos) Improve repository API to support this use case
// with no explicit type-casts required
data as DataObject<T>,
options,
);
}

Expand All @@ -265,7 +268,7 @@ export function defineCrudRestController<
@param(idPathParam) id: IdType,
@body(modelCtor) data: T,
): Promise<void> {
await this.repository.replaceById(id, data);
await this.repository.replaceById(id, data, options);
}

@del('/{id}', {
Expand All @@ -274,7 +277,7 @@ export function defineCrudRestController<
},
})
async deleteById(@param(idPathParam) id: IdType): Promise<void> {
await this.repository.deleteById(id);
await this.repository.deleteById(id, options);
}
}

Expand Down
Loading