Helpcenter +918010117117 https://cdn.storehippo.com/s/573db3149f0d58741f0cc63b/ms.settings/5256837ccc4abf1d39000001/57614ef64256dc6851749879-480x480.png" [email protected] https://www.facebook.com/StoreHippohttps://twitter.com/StoreHippohttps://www.linkedin.com/company/hippoinnovations/https://plus.google.com/+Storehippo/posts
B4,309-10 Spaze iTech Park, Sector 49, Sohna Road, 122001 Gurgaon India
call to replace anchor tags contains '/admin' in href

Commands and Hooks

All entities have standard commands that are used to perform standard CRUD operations.

Standard Commands

Following are standard commands that are available by default to all global or store entities.

  • get 
  • list
  • add
  • edit
  • delete
  • duplicate
  • import
  • export

Custom Commands

Custom commands are available only in Enterprise plan.


Apart from standard commands, the user can create custom commands for any entity. 

Adding a custom command

To create a custom command for an entity, please follow these steps:

  • Go to Advance Settings > Entities in StoreHippo Admin Panel.
  • Search the entity to which you want to add the command.
  • Click on the edit button.
  • On the edit page, go toCommandstab.

<Image for commands tab will go here>

Click on Add New button. A form will appear withfollowingfields:

name

Enter the name of the command

method

Enter the HTTP method that can be used for the command. Possible values are:

  • GET
  • POST
  • PUT
  • DELETE

handler

Handler is the function that gets executed when the command is called.  Please refer to the section below on how to write command handlers.

roles

Select the roles that have permission to call this command.
 

Extending standard commands

Hooks can be used to extend thebehaviourof standard commands. There are two types of hooks

Pre Hooks

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

Post hooks are executed after the command is executed. If there are multiple pre hooks then they are executed in the order of priority.
 

Writing Command Handlers and Hooks

Both command handlers and hooks follow the same middleware syntax which looks as follows:

function(req, res, next) {

}

Request

Request contains the information about the request that is available in the handlers and hooks.

query
filtersArray of filters to apply when listing records
q (deprecated)Filters in the form of an object with name value pair
sortPass the fields that you want to sort eg. '-price'
since_idIf you want to get the records since a specific record id
entityEntity for which this command was triggered
commandThe command name that was triggered
resourceIdThe id of the record for which command is triggered (optional)
headersContains 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
user

req.ms.user contains details about the current user.

devicereq.ms.device contains details about the current device
substorereq.ms.substore contains details about current substore
appreq.ms.app is the internal SDK that you can use to call other entity commands/microservices.
settingsreq.ms.settings contains the current store settings

Response

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.

Examples 

Adding Record 

let product_id = await req.ms.app.addRecord("ms.products", {name: 'test', price: 10});

Editing Record

await req.ms.app.updateRecord("ms.products", '62999791c1ce4e288076d4da' {name: 'test', price: 10});

Delete Record

await req.ms.app.deleteRecord("ms.products", '62999791c1ce4e288076d4da');

list Records

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'}] });

Get Record

let record = await req.ms.app.getRecord("ms.products", '62999791c1ce4e288076d4da');

2022-06-03T08:31:32.036Z