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
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned about the design of this change.

The options argument passed to defineCrudRestController() is controller configuration (basePath, readonly), not repository operation options.

By forwarding this object to every repository method, we're mixing REST-layer configuration with data-access concerns.

The PR description mentions transaction contexts and execution options, but I don't see how those would be available through this static controller configuration.

Could we clarify the actual use case this is intended to solve? If we need to propagate request-specific repository options, they should come from an appropriate execution context rather than the controller's configuration.

I'm also not seeing why this change is required for the referencesMany filtering issue. Can we demonstrate that dependency before making a cross-cutting change to all CRUD operations?

I would suggest reconsidering the approach rather than forwarding the existing options object.

}

@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