Services - Options
You can pass various options when performing operations or commands.
CrudOptions
CrudOptions
is a shared set of parameters that can be set from the client.
export interface ICrudOptions {
populate?: string[];
mockRole?: string;
fields?: string[];
limit?: number;
orderBy?: Record<string, string>[];
offset?: number;
cached?: boolean;
allowIdOverride?: boolean;
skipServiceHooks?: boolean;
returnUpdatedEntity?: boolean;
}
You can pass it when calling service methods.
import { OpParams } from "@eicrud/core/crud";
const query: Partial<Profile> = {
astroSign: "Aries"
}
const opParams: OpParams = {
options: {
limit: 20
}
}
const {data, total, limit} = await profileService.$find(query, null, opParams);
Note
Check out the client options page to use CrudOptions
in your front-end.
populate
Corresponds to MikroOrm's populate option.
mockRole
Requests that include this option will perform as if the logged user has the role mockRole
. To activate, CrudRole->canMock
must be set.
Note
mockRole
is useful for testing authorizations without switching accounts. You can set ClientConfig->globalMockRole
to mock roles from the client.
fields
Filter output to include specified fields only. Corresponds to MikroOrm's fields option.
limit
Limit the number of results. Corresponds to MikroOrm's limit option.
orderBy
Allows for sorting query results on specific fields. Corresponds to MikroOrm's orderBy option.
offset
Allows for skipping several results, to be used with limit
to obtain paginated results. Corresponds to MikroOrm's offset option.
cached
Indicates if findOne
results should be fetched from the cache.
Note
cached
only works in client calls.
allowIdOverride
Allows Entity primary keys to be pre-generated in $create operations.
Warning
Letting users set their Entities' ID opens security risks. For example, impersonation of deleted entities.
skipServiceHooks
Allows skipping of all service hooks.
Note
skipServiceHooks
doesn't affect controller hooks.
returnUpdatedEntity
Returns the updated/deleted entity in patchOne and deleteOne operations.
Note
returnUpdatedEntity
impacts the operation' performance.
OpParams
OpParams
are parameters only accessible from the server.
interface OpParams {
options?: CrudOptions;
secure?: boolean;
em?: EntityManager;
noFlush?: boolean;
}
options
The CrudOptions for the operation.
secure
Adds extra checks depending on the operation (i.e: verify maxItemsInDb
for create, check if the entity exists for patch). Usually you want to set this parameter if the method call results from a user interaction.
em
Provide a specific entity manager to perform the operation.
noFlush
Disable the entity manager flush (for create operations only).