@janiscommerce/lambda-model-wrapper
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

Lambda Model Wrapper

Build Status Coverage Status npm version

A package to simplify wrapper model lambda functions in Janis Services

📥 Installation

npm install @janiscommerce/lambda-model-wrapper

📃 Usage

LambdaGet

LambdaGet is used to wrap the get() and getPaged() method from models.

Breaking changes Since 2.0.0 ⚠️

  • In 1.x.x the totals were always calculated. Now you need to need to send the calculateTotals parameter with true value.

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. default true.
  • You can use mustHavePayload to make payload mandatory or not. default false.

ℹ️ 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, using getPaged() method. default false. Since 2.0.0
  • calculateTotals. Boolean to calculate totals with getTotals() method. default false. Since 2.0.0
  • filters, page, limit, order, changeKeys. Classic get() parameters. For more information see @janiscommerce/model.
  • formatParams. Object to pass parameters to format() method. Since 2.1.0. See Formatting results below.

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 }
	 	}
	 */

};

Readme

Keywords

none

Package Sidebar

Install

npm i @janiscommerce/lambda-model-wrapper

Weekly Downloads

244

Version

3.0.0

License

ISC

Unpacked Size

8.39 kB

Total Files

5

Last publish

Collaborators

  • janiscommerce