2.48.0 • Published 5 months ago

@memberjunction/actions-base v2.48.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

@memberjunction/actions-base

Base classes and interfaces for the MemberJunction Actions framework. This library provides the foundational components for implementing and executing actions across both server and client environments.

Overview

The Actions framework in MemberJunction provides a flexible, metadata-driven system for executing business logic and operations. This base package contains the core classes and interfaces that enable:

  • Action Engine: Core engine for loading, configuring, and executing actions
  • Entity Actions: Actions that operate on specific entities with various invocation contexts
  • Code Generation: Support for dynamically generated action code with library management
  • Execution Logging: Built-in logging and result tracking for all action executions

Installation

npm install @memberjunction/actions-base

Core Components

ActionEngineBase

The singleton base class that manages all action metadata and provides the foundation for action execution.

import { ActionEngineBase } from '@memberjunction/actions-base';

// Get the singleton instance
const actionEngine = ActionEngineBase.Instance;

// Configure the engine (required before use)
await actionEngine.Config(false, userInfo);

// Access action metadata
const allActions = actionEngine.Actions;
const coreActions = actionEngine.CoreActions;
const actionParams = actionEngine.ActionParams;
const actionFilters = actionEngine.ActionFilters;

EntityActionEngineBase

Manages entity-specific actions and their various invocation contexts (single record, view-based, list-based).

import { EntityActionEngineBase } from '@memberjunction/actions-base';

// Get the singleton instance
const entityActionEngine = EntityActionEngineBase.Instance;

// Configure the engine
await entityActionEngine.Config(false, userInfo);

// Get actions for a specific entity
const customerActions = entityActionEngine.GetActionsByEntityName('Customers', 'Active');

// Get actions by invocation type
const viewActions = entityActionEngine.GetActionsByEntityNameAndInvocationType(
    'Orders', 
    'View', 
    'Active'
);

Action Types and Models

ActionParam

Represents input/output parameters for actions:

import { ActionParam } from '@memberjunction/actions-base';

const param: ActionParam = {
    Name: 'CustomerID',
    Value: '12345',
    Type: 'Input' // 'Input' | 'Output' | 'Both'
};

RunActionParams

Configuration for running an action:

import { RunActionParams } from '@memberjunction/actions-base';

const runParams: RunActionParams = {
    Action: actionEntity,
    ContextUser: userInfo,
    SkipActionLog: false, // Optional
    Filters: [], // Optional filters to run before action
    Params: [
        { Name: 'Input1', Value: 'test', Type: 'Input' }
    ]
};

ActionResult

The result object returned from action execution:

import { ActionResult } from '@memberjunction/actions-base';

// ActionResult contains:
// - Success: boolean indicating if action succeeded
// - Result: ActionResultCodeEntity with the specific result code
// - LogEntry: ActionExecutionLogEntity for tracking
// - Message: Optional message about the outcome
// - Params: All parameters including outputs

EntityActionInvocationParams

Parameters for invoking entity-specific actions:

import { EntityActionInvocationParams } from '@memberjunction/actions-base';

const invocationParams: EntityActionInvocationParams = {
    EntityAction: entityActionExtended,
    InvocationType: invocationTypeEntity,
    ContextUser: userInfo,
    // One of these based on invocation type:
    EntityObject: customerEntity, // For single record
    ViewID: 'view-123', // For view-based
    ListID: 'list-456' // For list-based
};

Extended Entity Classes

ActionEntityExtended

Enhanced action entity with additional functionality:

import { ActionEntityExtended } from '@memberjunction/actions-base';

// Provides additional properties:
const action = actionEngine.Actions[0] as ActionEntityExtended;
console.log(action.IsCoreAction); // true if core MJ action
console.log(action.ProgrammaticName); // Code-friendly name
console.log(action.ResultCodes); // Possible result codes
console.log(action.Params); // Action parameters
console.log(action.Libraries); // Required libraries

EntityActionEntityExtended

Enhanced entity action with related data:

import { EntityActionEntityExtended } from '@memberjunction/actions-base';

// Provides lazy-loaded related data:
const entityAction = entityActionEngine.EntityActions[0] as EntityActionEntityExtended;
console.log(entityAction.Filters); // Associated filters
console.log(entityAction.Invocations); // Invocation configurations
console.log(entityAction.Params); // Action parameters

Code Generation Support

The framework includes support for generated code with library tracking:

import { GeneratedCode, ActionLibrary } from '@memberjunction/actions-base';

// GeneratedCode structure
const generatedCode: GeneratedCode = {
    Success: true,
    Code: 'function execute() { ... }',
    LibrariesUsed: [
        {
            LibraryName: 'lodash',
            ItemsUsed: ['map', 'filter']
        }
    ],
    Comments: 'Processes customer data',
    ErrorMessage: undefined
};

Usage Examples

Basic Action Engine Configuration

import { ActionEngineBase } from '@memberjunction/actions-base';
import { UserInfo } from '@memberjunction/core';

async function initializeActionEngine(user: UserInfo) {
    const engine = ActionEngineBase.Instance;
    
    // Initial configuration
    await engine.Config(false, user);
    
    // Force refresh if needed
    await engine.Config(true, user);
    
    // Access loaded metadata
    console.log(`Loaded ${engine.Actions.length} actions`);
    console.log(`Core actions: ${engine.CoreActions.length}`);
}

Working with Entity Actions

import { EntityActionEngineBase } from '@memberjunction/actions-base';

async function getEntityActions(entityName: string, user: UserInfo) {
    const engine = EntityActionEngineBase.Instance;
    await engine.Config(false, user);
    
    // Get all active actions for an entity
    const actions = engine.GetActionsByEntityName(entityName, 'Active');
    
    // Filter by invocation type
    const singleRecordActions = actions.filter(a => 
        a.Invocations.some(i => i.InvocationType === 'Single Record')
    );
    
    return singleRecordActions;
}

Action Parameter Handling

import { ActionParam } from '@memberjunction/actions-base';

function prepareActionParams(inputs: Record<string, any>): ActionParam[] {
    return Object.entries(inputs).map(([name, value]) => ({
        Name: name,
        Value: value,
        Type: 'Input'
    }));
}

// Example usage
const params = prepareActionParams({
    CustomerID: '123',
    OrderDate: new Date(),
    TotalAmount: 150.00
});

Dependencies

  • @memberjunction/global: Global utilities and registration system
  • @memberjunction/core: Core MemberJunction interfaces and base classes
  • @memberjunction/core-entities: Entity definitions for MemberJunction metadata

Integration with Other MemberJunction Packages

This package serves as the foundation for:

  • @memberjunction/actions-server: Server-side action execution implementation
  • @memberjunction/actions-client: Client-side action execution
  • Custom action implementations in your applications

Best Practices

  1. Always Configure Before Use: Call Config() on the engine instances before accessing any metadata
  2. Use Singleton Instances: Always use the .Instance property to get engine instances
  3. Handle Async Operations: All configuration and many operations are asynchronous
  4. Check Action Status: Filter actions by status ('Active', 'Pending', 'Disabled') when appropriate
  5. Validate Parameters: Ensure all required input parameters are provided before execution

TypeScript Support

This package is written in TypeScript and provides full type definitions. All classes and interfaces are properly typed for optimal development experience.

License

ISC License - see LICENSE file in the root of the repository.

2.48.0

5 months ago

2.47.0

5 months ago

2.46.0

5 months ago

2.45.0

5 months ago

2.44.0

5 months ago

2.43.0

5 months ago

2.42.1

5 months ago

2.42.0

5 months ago

2.41.0

5 months ago

2.40.0

5 months ago

2.39.0

5 months ago

2.38.0

5 months ago

2.37.1

6 months ago

2.37.0

6 months ago

2.36.1

6 months ago

2.36.0

6 months ago

2.35.1

6 months ago

2.35.0

6 months ago

2.34.2

6 months ago

2.34.1

6 months ago

2.34.0

6 months ago

2.33.0

6 months ago

2.32.2

7 months ago

2.32.1

7 months ago

2.32.0

7 months ago

2.31.0

7 months ago

2.30.0

7 months ago

2.29.2

8 months ago

2.29.1

8 months ago

2.29.0

8 months ago

2.28.0

8 months ago

2.27.1

8 months ago

2.27.0

8 months ago

2.26.1

8 months ago

2.26.0

8 months ago

2.25.0

8 months ago

2.24.1

8 months ago

2.24.0

8 months ago

2.23.2

9 months ago

2.23.1

9 months ago

2.23.0

9 months ago

2.22.2

9 months ago

2.22.1

9 months ago

2.22.0

9 months ago

2.21.0

9 months ago

2.20.3

9 months ago

2.20.2

9 months ago

2.20.1

9 months ago

2.20.0

9 months ago

2.19.5

9 months ago

2.19.4

9 months ago

2.19.3

9 months ago

2.19.2

9 months ago

2.19.1

9 months ago

2.19.0

9 months ago

2.18.3

9 months ago

2.18.2

9 months ago

2.18.1

9 months ago

2.18.0

9 months ago

2.17.0

9 months ago

2.16.1

9 months ago

2.16.0

9 months ago

2.15.2

9 months ago

2.15.1

9 months ago

2.15.0

9 months ago

2.14.0

10 months ago

2.13.4

10 months ago

2.13.3

10 months ago

2.13.2

11 months ago

2.13.1

11 months ago

2.13.0

11 months ago

2.12.0

11 months ago