メインコンテンツまでスキップ
バージョン: 0.6.0 (Latest)

🛍️ Product Handler Blocks

Product handler blocks provide core business logic functions for product management operations in Nodeblocks applications. These handlers encapsulate common patterns for product database operations, data transformation, and response formatting.


🎯 Overview

Product handler blocks are designed to:

  • Encapsulate product business logic in reusable functions
  • Handle product database operations with proper error management
  • Transform product data between different formats
  • Ensure type safety with TypeScript integration
  • Support composition with other product blocks

📋 Product Handler Types

Product Async Handlers

Functions that perform asynchronous product operations (database calls, API requests, etc.).

Product Sync Handlers

Functions that perform synchronous product operations (data transformation, validation, etc.).

Product Terminator Handlers

Functions that format and return the final product response.


🔧 Available Product Handlers

createProduct

Creates a new product in the database with validation and entity management.

Purpose: Handles creation of single products with validation and entity management

Parameters:

  • payload: RouteHandlerPayload containing request context and data

Returns: Result<RouteHandlerPayload, Error> - success with productId or error

Example Usage:

// Used in composition:
compose(createProduct, terminator);

Key Features:

  • Validates request body presence
  • Creates base entity with metadata
  • Returns product ID on success
  • Handles database insertion errors

createProductBatch

Creates multiple products in a single batch operation.

Purpose: Handles batch creation of multiple products with validation

Parameters:

  • payload: RouteHandlerPayload containing request context and data

Returns: Result<RouteHandlerPayload, Error> - success with productIds array or error

Example Usage:

// Used in composition:
compose(productBatchSchema, validateBatch, createProductBatch, terminator);

Key Features:

  • Validates array input
  • Creates base entities for each product
  • Returns array of product IDs
  • Handles batch insertion errors

getProductById

Retrieves a single product by ID with existence validation.

Purpose: Fetches product data with existence validation

Parameters:

  • payload: RouteHandlerPayload containing request context and data

Returns: Result<RouteHandlerPayload, Error> - success with product or error

Example Usage:

// Used in composition:
compose(getProductById, normalizeTerminator);

Key Features:

  • Validates product ID presence
  • Checks product existence
  • Returns full product object
  • Handles database query errors

getProductsByIds

Retrieves multiple products by their IDs in a single operation.

Purpose: Fetches multiple products in a single operation with existence validation

Parameters:

  • payload: RouteHandlerPayload containing request context and data

Returns: Result<RouteHandlerPayload, Error> - success with products array or error

Example Usage:

// Used in composition:
compose(getProductsByIds, listTerminator);

Key Features:

  • Validates product IDs array
  • Uses MongoDB $in operator for efficiency
  • Returns array of products
  • Handles empty results

findProducts

Finds multiple products with optional filtering.

Purpose: Handles querying products with filter support

Parameters:

  • payload: RouteHandlerPayload containing request context and data

Returns: Result<RouteHandlerPayload, Error> - success with products array or error

Example Usage:

// Used in composition:
compose(findProducts, listTerminator);

Key Features:

  • Accepts optional filter parameters
  • Returns all products if no filter provided
  • Supports complex MongoDB queries
  • Handles database query errors

updateProduct

Updates an existing product by ID with validation and conflict detection.

Purpose: Handles updating single products with validation and conflict detection

Parameters:

  • payload: RouteHandlerPayload containing request context and data

Returns: Result<RouteHandlerPayload, Error> - success with update result or error

Example Usage:

// Used in composition:
compose(updateProduct, terminator);

Key Features:

  • Validates product ID and request body
  • Checks product existence before update
  • Updates base entity metadata
  • Returns update operation result

updateProductBatch

Updates multiple products in a single batch operation.

Purpose: Handles batch updating of multiple products with validation

Parameters:

  • payload: RouteHandlerPayload containing request context and data

Returns: Result<RouteHandlerPayload, Error> - success with productIds or error

Example Usage:

// Used in composition:
compose(updateProductBatch, terminator);

Key Features:

  • Validates product IDs array and update data
  • Uses MongoDB $in operator for batch updates
  • Updates base entity metadata for all products
  • Returns array of updated product IDs

copyProduct

Creates a copy of an existing product with new entity creation.

Purpose: Handles product duplication with new entity creation

Parameters:

  • payload: RouteHandlerPayload containing request context and data

Returns: Result<RouteHandlerPayload, Error> - success with new productId or error

Example Usage:

// Used in composition:
compose(copyProduct, terminator);

Key Features:

  • Validates source product existence
  • Removes database-specific fields (_id)
  • Creates new base entity for copy
  • Returns new product ID

copyProductBatch

Creates copies of multiple products in a batch operation.

Purpose: Handles batch product duplication with new entity creation

Parameters:

  • payload: RouteHandlerPayload containing request context and data

Returns: Result<RouteHandlerPayload, Error> - success with new productIds array or error

Example Usage:

// Used in composition:
compose(copyProductBatch, terminator);

Key Features:

  • Validates source products existence
  • Removes database-specific fields from all products
  • Creates new base entities for all copies
  • Returns array of new product IDs

deleteProduct

Deletes a product by ID with safe deletion and existence validation.

Purpose: Handles safe deletion of single products with existence validation

Parameters:

  • payload: RouteHandlerPayload containing request context and data

Returns: Result<RouteHandlerPayload, Error> - success with deletion result or error

Example Usage:

// Used in composition:
compose(deleteProduct, deleteTerminator);

Key Features:

  • Validates product ID presence
  • Checks product existence before deletion
  • Returns deletion operation result
  • Handles database deletion errors

deleteProductBatch

Deletes multiple products in a single batch operation.

Purpose: Handles batch deletion of multiple products with validation

Parameters:

  • payload: RouteHandlerPayload containing request context and data

Returns: Result<RouteHandlerPayload, Error> - success with deletion result or error

Example Usage:

// Used in composition:
compose(deleteProductBatch, deleteTerminator);

Key Features:

  • Validates product IDs array
  • Uses MongoDB $in operator for batch deletion
  • Returns deletion operation result
  • Handles empty deletion scenarios

🎯 Product Terminator Handlers

normalizeProductTerminator

Normalizes product data by removing database-specific fields.

Purpose: Cleans product data for API response

Parameters:

  • result: Result containing RouteHandlerPayload or Error

Returns: Normalized product object

Example Usage:

// Used in composition:
compose(getProductById, normalizeProductTerminator);

Key Features:

  • Removes _id field from product
  • Throws error if product not found
  • Returns clean product object

normalizeProductsListTerminator

Normalizes products list by removing database-specific fields from each item.

Purpose: Cleans products array data for API response

Parameters:

  • result: Result containing RouteHandlerPayload or Error

Returns: Array of normalized product objects

Example Usage:

// Used in composition:
compose(schema, findProducts, normalizeProductsListTerminator);

Key Features:

  • Removes _id field from all products
  • Maps over products array
  • Returns clean products array

deleteProductTerminator

Terminates product deletion with proper status code.

Purpose: Formats successful single product deletion response with 204 status

Parameters:

  • result: Result containing RouteHandlerPayload or Error

Returns: Response object with 204 statusCode

Example Usage:

// Used in composition:
compose(schema, deleteProduct, deleteProductTerminator);

Key Features:

  • Returns 204 status code for successful deletion
  • Validates deletion operation result
  • Throws error if deletion failed

deleteBatchProductsTerminator

Terminates batch product deletion with proper status code.

Purpose: Formats successful batch product deletion response with 204 status

Parameters:

  • result: Result containing RouteHandlerPayload or Error

Returns: Response object with 204 statusCode

Example Usage:

// Used in composition:
compose(schema, deleteProductBatch, deleteBatchProductsTerminator);

Key Features:

  • Returns 204 status code for successful batch deletion
  • Validates batch deletion operation result
  • Throws error if batch deletion failed