All entities have standard commands that are used to perform standard CRUD operations.
Following are standard commands that are available by default to all global or store entities.
Apart from standard commands, the user can create custom commands for any entity.
To create a custom command for an entity, please follow these steps:
<Image for commands tab will go here>
Click on Add New button. A form will appear with
Enter the name of the command
Enter the HTTP method that can be used for the command. Possible values are:
Handler is the function that gets executed when the command is called. Please refer to the section below on how to write command handlers.
Select the roles that have permission to call this command.
Hooks can be used to extend the
pre hooks are executed before the command handler. If there are multiple pre hooks then they are executed in the order of priority.
Post hooks are executed after the command is executed. If there are multiple pre hooks then they are executed in the order of priority.
Both command handlers and hooks follow the same middleware syntax which looks as follows:
function(req, res, next) {
}
Request contains the information about the request that is available in the handlers and hooks.
query |
| ||||||||||
entity | Entity for which this command was triggered | ||||||||||
command | The command name that was triggered | ||||||||||
resourceId | The id of the record for which command is triggered (optional) | ||||||||||
headers | Contains the request headers. You can use the headers to determine if the request is from storefront or backend e.g. req.headers.frontend == '1' or req.headers.backed == '1' | ||||||||||
ms |
|
send(data) | will stop the execution and return the control from the current handler/hook. It will also stop the execution of any subsequent handler/hooks. |
error(data) | will stop execution and also return error |
next() | will return control from current handler but enable execution of subsequent handlers/hooks. |
let product_id = await req.ms.app.addRecord("ms.products", {name: 'test', price: 10});
await req.ms.app.updateRecord("ms.products", '62999791c1ce4e288076d4da' {name: 'test', price: 10});
await req.ms.app.deleteRecord("ms.products", '62999791c1ce4e288076d4da');
let records = await req.ms.app.listRecords("ms.products");
let records = await req.ms.app.listRecords("ms.products", { filters: [{field: 'alias', value: 'test'}] });
let record = await req.ms.app.listOne("ms.products", { filters: [{field: 'alias', value: 'test'}] });
let record = await req.ms.app.getRecord("ms.products", '62999791c1ce4e288076d4da');