@janiscommerce/lambda-model-wrapper
v4.0.0
Published
A package to simplify wrapper model lambda functions in Janis Services
Downloads
1,036
Readme
Lambda Model Wrapper
A package to simplify wrapper model lambda functions in Janis Services
:inbox_tray: Installation
npm install @janiscommerce/lambda-model-wrapper
:page_with_curl: Usage
LambdaGet
LambdaGet is used to wrap the get()
and getPaged()
method from models.
Configuration
- The getter
modelClass
should return the Model for our entity. - You can use
mustHaveClient
to defines if the function will be used for Client models. defaulttrue
. - You can use
mustHavePayload
to make payload mandatory or not. defaultfalse
.
ℹ️ The payload is used to apply params to the
get()
method, this will be explained below.
Parameters
All parameters are optional
fields
. String Array to reduce response. For more information see @janiscommerce/model.excludeFields
. String Array to reduce response. Since 2.1.0. For more information see @janiscommerce/model.allItems
. Boolean to obtain all items, usinggetPaged()
method. defaultfalse
. Since 2.0.0calculateTotals
. Boolean to calculate totals withgetTotals()
method. defaultfalse
. Since 2.0.0filters
,page
,limit
,order
,changeKeys
. Classicget()
parameters. For more information see @janiscommerce/model.formatParams
. Object to pass parameters toformat()
method. Since 2.1.0. See Formatting results below.onlyTotals
. Boolean to only calculate totals with optionalfilters
. Since 4.0.0.
Formatting results
The optional method async format(items, formatParams)
allows you to format every item.
The formatParams
can be used give different behaviors to the function.
Totals
To obtain the totals object is required to
Response
The response of the lambda functions is explained in the @janiscommerce/lambda package.
Examples
First you need to create your lambda function.
'use strict';
const { Handler } = require('@janiscommerce/lambda');
const { LambdaGet } = require('@janiscommerce/lambda-wrapper-model');
const ProductModel = require('../../models/product');
class GetProduct extends LambdaGet {
get modelClass() {
return ProductModel;
}
async format(items, { countImages }) {
return items.map(item => ({
...item,
...countImages && { imagesCount: item?.images.length || 0 }
}));
}
}
module.exports.handler = (...args) => Handler.handle(GetProduct, ...args);
For using the lambda you need to invoke with the @janiscommerce/lambda package.
'use strict';
const { Invoker } = require('@janiscommerce/lambda');
async () => {
const response = await Invoker.clientCall('GetProduct', 'my-client-code');
/**
* response.payload: {
items: [
{ id: 1, referenceId: 'coke-2lt', name: 'Coke lts', stock: 100, images: ['coke-2lt.jpg'] },
{ id: 2, referenceId: 'pepsi-2lt', name: 'Pepsi 2lts', stock: 100 },
{ id: 3, referenceId: 'fanta-2lt', name: 'Fanta 2lts', stock: 95 }
]
}
*/
const filteredResponse = await Invoker.clientCall('GetProduct', 'my-client-code', {
filters: { stock: 100 },
fields: ['referenceId', 'images'],
calculateTotals: true,
formatParams: { countImages: true }
});
/**
* filteredResponse.payload: {
items: [
{ id: 1, referenceId: 'coke-2lt', images: ['coke-2lt.jpg'], imagesCount: 1 },
{ id: 2, referenceId: 'pepsi-2lt', imagesCount: 0 }
],
totals: { total: 2, page: 1 }
}
*/
};