Skip to content

janis-commerce/lambda-model-wrapper

Repository files navigation

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.

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.
  • onlyTotals. Boolean to only calculate totals with optional filters. 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 }
	 	}
	 */

};

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published