2.48.0 • Published 4 months ago

@memberjunction/core v2.48.0

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

@memberjunction/core

The @memberjunction/core library provides a comprehensive interface for accessing and managing metadata within MemberJunction, along with facilities for working with entities, applications, and various other aspects central to the MemberJunction ecosystem. This library serves as the foundation for all MemberJunction applications and provides essential functionality for data access, manipulation, and metadata management.

Installation

npm install @memberjunction/core

Overview

The @memberjunction/core library is the central package in the MemberJunction ecosystem, providing:

  • Metadata Management: Complete access to MemberJunction metadata including entities, fields, relationships, permissions, and more
  • Entity Data Access: Base classes and utilities for loading, saving, and manipulating entity records
  • View Execution: Powerful view running capabilities for both stored and dynamic views
  • Query & Report Execution: Tools for running queries and reports
  • Security & Authorization: User authentication, role management, and permission handling
  • Transaction Management: Support for grouped database transactions
  • Provider Architecture: Flexible provider model supporting different execution environments

Core Components

Metadata Class

The Metadata class is the primary entry point for accessing MemberJunction metadata and instantiating entity objects.

import { Metadata } from '@memberjunction/core';

// Create metadata instance
const md = new Metadata();

// Refresh cached metadata
await md.Refresh();

// Access various metadata collections
const applications = md.Applications;
const entities = md.Entities;
const currentUser = md.CurrentUser;
const roles = md.Roles;
const authorizations = md.Authorizations;

Key Metadata Properties

  • Applications: Array of all applications in the system
  • Entities: Array of all entity definitions
  • CurrentUser: Current authenticated user (when available)
  • Roles: System roles
  • AuditLogTypes: Available audit log types
  • Authorizations: Authorization definitions
  • Libraries: Registered libraries
  • Queries: Query definitions
  • QueryFields: Query field metadata
  • QueryCategories: Query categorization
  • QueryPermissions: Query-level permissions
  • VisibleExplorerNavigationItems: Navigation items visible to current user
  • AllExplorerNavigationItems: All navigation items (including hidden)

Helper Methods

// Get entity ID from name
const entityId = md.EntityIDFromName('Users');

// Get entity name from ID
const entityName = md.EntityNameFromID('12345');

// Find entity by name (case-insensitive)
const userEntity = md.EntityByName('users');

// Find entity by ID
const entity = md.EntityByID('12345');

// Get entity object instance (IMPORTANT: Always use this pattern)
const user = await md.GetEntityObject<UserEntity>('Users');

BaseEntity Class

The BaseEntity class is the foundation for all entity record manipulation in MemberJunction. All entity classes generated by the MemberJunction code generator extend this class.

import { Metadata } from '@memberjunction/core';

// IMPORTANT: Never instantiate entity classes directly
// Always use Metadata.GetEntityObject() to ensure proper class registration

// ✅ Correct way to create entity instances
const md = new Metadata();
const user = await md.GetEntityObject<UserEntity>('Users');

// Load a record by ID
await user.Load(123);

// Create a new record
user.NewRecord();
user.FirstName = 'John';
user.LastName = 'Doe';
user.Email = 'john.doe@example.com';

// Save the record (creates new or updates existing)
const saveResult = await user.Save();
if (saveResult) {
    console.log('User saved successfully:', user.ID);
} else {
    console.log('Save failed:', user.LatestResult.Message);
}

// Delete a record
const deleteResult = await user.Delete();

Entity Fields

Each entity field is represented by an EntityField object that tracks value, dirty state, and metadata:

// Access field value
const firstName = user.Get('FirstName');

// Set field value
user.Set('FirstName', 'Jane');

// Check if field is dirty
const isDirty = user.Fields.find(f => f.Name === 'FirstName').Dirty;

// Access field metadata
const field = user.Fields.find(f => f.Name === 'Email');
console.log(field.IsUnique); // true/false
console.log(field.IsPrimaryKey); // true/false
console.log(field.ReadOnly); // true/false

Save Options

import { EntitySaveOptions } from '@memberjunction/core';

const options = new EntitySaveOptions();
options.IgnoreDirtyState = true; // Force save even if no changes detected
options.SkipEntityAIActions = true; // Skip AI-related actions
options.SkipEntityActions = true; // Skip entity actions
options.SkipOldValuesCheck = true; // Skip concurrency check (client-side only)

await entity.Save(options);

RunView Class

The RunView class provides powerful view execution capabilities for both stored views and dynamic queries.

import { RunView, RunViewParams } from '@memberjunction/core';

const rv = new RunView();

// Run a stored view by name
const params: RunViewParams = {
    ViewName: 'Active Users',
    ExtraFilter: 'CreatedDate > \'2024-01-01\'',
    UserSearchString: 'john'
};

const results = await rv.RunView(params);

// Run a dynamic view with entity objects returned
const dynamicResults = await rv.RunView<UserEntity>({
    EntityName: 'Users',
    ExtraFilter: 'IsActive = 1',
    OrderBy: 'LastName ASC, FirstName ASC',
    Fields: ['ID', 'FirstName', 'LastName', 'Email'],
    ResultType: 'entity_object' // Returns actual entity objects
});

// Access typed results
const users = dynamicResults.Results; // Properly typed as UserEntity[]

RunView Parameters

  • ViewID: ID of stored view to run
  • ViewName: Name of stored view to run
  • ViewEntity: Pre-loaded view entity object (optimal for performance)
  • EntityName: Entity name for dynamic views
  • ExtraFilter: Additional SQL WHERE clause
  • OrderBy: SQL ORDER BY clause (overrides stored view sorting)
  • Fields: Array of field names to return
  • UserSearchString: User search term
  • ExcludeUserViewRunID: Exclude records from specific prior run
  • ExcludeDataFromAllPriorViewRuns: Exclude all previously returned records
  • SaveViewResults: Store run results for future exclusion
  • IgnoreMaxRows: Bypass entity MaxRows setting
  • MaxRows: Maximum rows to return
  • StartRow: Row offset for pagination
  • ResultType: 'simple' (default) or 'entity_object'

RunQuery Class

Execute stored queries with parameters:

import { RunQuery, RunQueryParams } from '@memberjunction/core';

const rq = new RunQuery();

const params: RunQueryParams = {
    QueryID: '12345',
    Parameters: {
        StartDate: '2024-01-01',
        EndDate: '2024-12-31',
        Status: 'Active'
    }
};

const results = await rq.RunQuery(params);

RunReport Class

Execute reports with various output formats:

import { RunReport, RunReportParams } from '@memberjunction/core';

const rr = new RunReport();

const params: RunReportParams = {
    ReportID: '12345',
    Parameters: {
        Year: 2024,
        Department: 'Sales'
    }
};

const results = await rr.RunReport(params);

Transaction Management

Group multiple operations in a transaction:

import { TransactionGroupBase } from '@memberjunction/core';

// Transaction management is provider-specific
// Consult your provider documentation for implementation details

Entity Relationships

MemberJunction automatically handles entity relationships through the metadata system:

// Load an entity with related data
const order = await md.GetEntityObject<OrderEntity>('Orders');
await order.Load(123, ['OrderDetails', 'Customer']);

// Access related entities
const orderDetails = order.OrderDetails; // Array of OrderDetailEntity
const customer = order.Customer; // CustomerEntity

Security & Permissions

The library provides comprehensive security features:

const md = new Metadata();

// Check current user
const user = md.CurrentUser;
console.log('Current user:', user.Email);

// Check user roles
const isAdmin = user.RoleName.includes('Admin');

// Entity permissions
const entity = md.EntityByName('Orders');
const canCreate = entity.CanCreate;
const canUpdate = entity.CanUpdate;
const canDelete = entity.CanDelete;

Error Handling

All operations return detailed error information:

const entity = await md.GetEntityObject('Users');
const result = await entity.Save();

if (!result) {
    // Access detailed error information
    const error = entity.LatestResult;
    console.error('Error:', error.Message);
    console.error('Details:', error.Details);
    
    // Check validation errors
    if (error.ValidationErrors && error.ValidationErrors.length > 0) {
        error.ValidationErrors.forEach(ve => {
            console.error(`Field ${ve.FieldName}: ${ve.Message}`);
        });
    }
}

Provider Architecture

MemberJunction uses a provider model to support different execution environments:

import { SetProvider } from '@memberjunction/core';

// Provider setup is typically handled by your application initialization
// The provider determines how data is accessed (direct database, API, etc.)
SetProvider(myProvider);

Best Practices

  1. Always use Metadata.GetEntityObject() to create entity instances - never use new
  2. Use generic types with RunView for type-safe results
  3. Handle errors properly - check return values and LatestResult
  4. Use transactions for related operations that must succeed/fail together
  5. Leverage metadata for dynamic UI generation and validation
  6. Respect permissions - always check CanCreate/Update/Delete before operations
  7. Use ExtraFilter carefully - ensure SQL injection protection
  8. Cache metadata instances when possible to improve performance

Integration with Other MemberJunction Packages

  • @memberjunction/global: Global utilities and constants
  • @memberjunction/server: Server-side provider implementation
  • @memberjunction/client: Client-side provider implementation
  • @memberjunction/angular: Angular-specific components and services
  • @memberjunction/react: React-specific components and hooks
  • @memberjunction/ai: AI integration features
  • @memberjunction/communication: Communication and messaging features

Dependencies

  • @memberjunction/global: Core global utilities
  • rxjs: Reactive programming support
  • zod: Schema validation
  • debug: Debug logging

TypeScript Support

This library is written in TypeScript and provides full type definitions. All generated entity classes include proper typing for IntelliSense support.

License

ISC License - see LICENSE file for details

Support

For support, documentation, and examples, visit MemberJunction.com

@everything-registry/sub-chunk-594mj_generatedentities@memberjunction/ai-vectors-entity-sync@memberjunction/graphql-dataprovider@memberjunction/metadata-sync@memberjunction/ng-ask-skip@memberjunction/ng-auth-services@memberjunction/ng-base-forms@memberjunction/ng-base-types@memberjunction/ng-chat@memberjunction/ng-code-editor@memberjunction/ng-compare-records@memberjunction/ng-treelist@memberjunction/ng-user-view-grid@memberjunction/ng-user-view-properties@memberjunction/queue@memberjunction/scheduled-actions@memberjunction/scheduled-actions-server@memberjunction/server@memberjunction/sqlserver-dataprovider@memberjunction/storage@memberjunction/templates@memberjunction/ng-container-directives@memberjunction/ng-core-entity-forms@memberjunction/ng-dashboards@memberjunction/ng-data-context@memberjunction/ng-entity-communications@memberjunction/ng-entity-form-dialog@memberjunction/ng-entity-permissions@memberjunction/ng-explorer-core@memberjunction/ng-explorer-settings@memberjunction/ng-file-storage@memberjunction/ng-find-record@memberjunction/ng-form-toolbar@memberjunction/ng-join-grid@memberjunction/ng-link-directives@memberjunction/ng-list-detail-grid@memberjunction/ng-notifications@memberjunction/ng-query-grid@memberjunction/ng-record-changes@memberjunction/ng-record-selector@memberjunction/ng-resource-permissions@memberjunction/ng-shared@memberjunction/ng-simple-record-list@memberjunction/ng-skip-chat@memberjunction/ng-timeline@memberjunction/ai-vector-dupe@memberjunction/ai-vector-sync@memberjunction/ai-vectordb@memberjunction/ai-vectors@memberjunction/ai-vectors-pinecone@memberjunction/aiengine@memberjunction/codegen-lib@memberjunction/communication-engine@memberjunction/communication-gmail@memberjunction/communication-ms-graph@memberjunction/communication-sendgrid@memberjunction/communication-twilio@memberjunction/communication-types@memberjunction/content-autotagging@memberjunction/core-actions@memberjunction/core-entities@memberjunction/core-entities-server@memberjunction/data-context@memberjunction/doc-utils@memberjunction/entity-communications-base@memberjunction/entity-communications-client@memberjunction/entity-communications-server@memberjunction/external-change-detection@memberjunction/templates-base-types@memberjunction/ai-engine-base@memberjunction/ai-mcp-server@memberjunction/ai-prompts@memberjunction/ai-recommendations@memberjunction/a2aserver@memberjunction/actions@memberjunction/actions-apollo@memberjunction/actions-base@memberjunction/actions-content-autotag@memberjunction/ai-agents
2.23.2

8 months ago

2.46.0

5 months ago

2.23.1

8 months ago

2.34.0

6 months ago

2.19.4

9 months ago

2.19.5

9 months ago

2.19.2

9 months ago

2.19.3

9 months ago

2.19.0

9 months ago

2.19.1

9 months ago

2.34.2

6 months ago

2.34.1

6 months ago

2.45.0

5 months ago

2.22.1

8 months ago

2.22.0

9 months ago

2.22.2

8 months ago

2.33.0

6 months ago

2.18.3

9 months ago

2.18.1

9 months ago

2.18.2

9 months ago

2.18.0

9 months ago

2.21.0

9 months ago

2.44.0

5 months ago

2.29.0

7 months ago

2.29.2

7 months ago

2.29.1

7 months ago

2.32.0

7 months ago

2.32.2

7 months ago

2.32.1

7 months ago

2.17.0

9 months ago

2.43.0

5 months ago

2.20.2

9 months ago

2.20.3

9 months ago

2.20.0

9 months ago

2.20.1

9 months ago

2.28.0

8 months ago

2.31.0

7 months ago

2.39.0

5 months ago

2.16.1

9 months ago

2.16.0

9 months ago

2.42.1

5 months ago

2.42.0

5 months ago

2.27.1

8 months ago

2.27.0

8 months ago

2.30.0

7 months ago

2.15.2

9 months ago

2.15.0

9 months ago

2.15.1

9 months ago

2.38.0

5 months ago

2.41.0

5 months ago

2.26.1

8 months ago

2.26.0

8 months ago

2.37.1

5 months ago

2.37.0

6 months ago

2.14.0

10 months ago

2.40.0

5 months ago

2.25.0

8 months ago

2.48.0

4 months ago

2.13.4

10 months ago

2.36.0

6 months ago

2.13.2

11 months ago

2.13.3

10 months ago

2.13.0

11 months ago

2.36.1

6 months ago

2.13.1

11 months ago

2.47.0

4 months ago

2.24.1

8 months ago

2.24.0

8 months ago

2.12.0

12 months ago

2.35.1

6 months ago

2.35.0

6 months ago

2.23.0

8 months ago

2.11.0

12 months ago

2.10.0

12 months ago

2.9.0

12 months ago

2.8.0

1 year ago

2.7.0

1 year ago

2.7.1

1 year ago

2.6.1

1 year ago

2.6.0

1 year ago

2.5.2

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

2.4.1

1 year ago

2.4.0

1 year ago

1.5.3

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

2.3.2

1 year ago

2.3.1

1 year ago

2.3.3

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

2.2.1

1 year ago

2.2.0

1 year ago

2.2.2

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.1.5

1 year ago

2.1.0

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

2.0.0

1 year ago

1.8.1

1 year ago

1.8.0

1 year ago

1.7.1

1 year ago

1.7.0

1 year ago

2.5.0

1 year ago

2.5.1

1 year ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.11

1 year ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.8-next.6

2 years ago

1.0.8-next.5

2 years ago

1.0.8-next.4

2 years ago

1.0.8-next.3

2 years ago

1.0.8-next.2

2 years ago

1.0.8-next.1

2 years ago

1.0.8-next.0

2 years ago

1.0.7-next.0

2 years ago

1.0.8-beta.0

2 years ago

1.0.2

2 years ago

1.0.6

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.9.187

2 years ago

0.9.186

2 years ago

0.9.188

2 years ago

0.9.185

2 years ago

0.9.184

2 years ago

0.9.183

2 years ago

0.9.178

2 years ago

0.9.182

2 years ago

0.9.181

2 years ago

0.9.180

2 years ago

0.9.176

2 years ago

0.9.175

2 years ago

0.9.174

2 years ago

0.9.177

2 years ago

0.9.172

2 years ago

0.9.171

2 years ago

0.9.169

2 years ago

0.9.168

2 years ago

0.9.167

2 years ago

0.9.166

2 years ago

0.9.165

2 years ago

0.9.154

2 years ago

0.9.155

2 years ago

0.9.164

2 years ago

0.9.163

2 years ago

0.9.162

2 years ago

0.9.158

2 years ago

0.9.157

2 years ago

0.9.159

2 years ago

0.9.153

2 years ago

0.9.152

2 years ago

0.9.150

2 years ago

0.9.151

2 years ago

0.9.149

2 years ago

0.9.148

2 years ago

0.9.147

2 years ago

0.9.146

2 years ago

0.9.145

2 years ago

0.9.143

2 years ago

0.9.144

2 years ago

0.9.142

2 years ago

0.9.141

2 years ago

0.9.134

2 years ago

0.9.128

2 years ago

0.9.114

2 years ago

0.9.116

2 years ago

0.9.115

2 years ago

0.9.110

2 years ago

0.9.112

2 years ago

0.9.111

2 years ago

0.9.107

2 years ago

0.9.106

2 years ago

0.9.109

2 years ago

0.9.108

2 years ago

0.9.103

2 years ago

0.9.105

2 years ago

0.9.104

2 years ago

0.9.113

2 years ago

0.9.96

2 years ago

0.9.97

2 years ago

0.9.98

2 years ago

0.9.99

2 years ago

0.9.92

2 years ago

0.9.93

2 years ago

0.9.94

2 years ago

0.9.95

2 years ago

0.9.90

2 years ago

0.9.91

2 years ago

0.9.89

2 years ago

0.9.88

2 years ago

0.9.87

2 years ago

0.9.85

2 years ago

0.9.86

2 years ago

0.9.81

2 years ago

0.9.82

2 years ago

0.9.83

2 years ago

0.9.84

2 years ago

0.9.80

2 years ago

0.9.79

2 years ago

0.9.78

2 years ago

0.9.77

2 years ago

0.9.75

2 years ago

0.9.76

2 years ago

0.9.74

2 years ago

0.9.73

2 years ago

0.9.72

2 years ago

0.9.71

2 years ago

0.9.70

2 years ago

0.9.69

2 years ago

0.9.68

2 years ago

0.9.67

2 years ago

0.9.65

2 years ago

0.9.64

2 years ago

0.9.63

2 years ago

0.9.62

2 years ago

0.9.61

2 years ago

0.9.60

2 years ago

0.9.59

2 years ago

0.9.58

2 years ago

0.9.57

2 years ago

0.9.56

2 years ago

0.9.55

2 years ago

0.9.54

2 years ago

0.9.53

2 years ago

0.9.52

2 years ago

0.9.50

2 years ago

0.9.49

2 years ago

0.9.48

2 years ago

0.9.47

2 years ago

0.9.46

2 years ago

0.9.45

2 years ago

0.9.44

2 years ago

0.9.43

2 years ago

0.9.42

2 years ago

0.9.41

2 years ago

0.9.40

2 years ago

0.9.39

2 years ago

0.9.38

2 years ago

0.9.37

2 years ago

0.9.36

2 years ago

0.9.35

2 years ago

0.9.34

2 years ago

0.9.33

2 years ago

0.9.32

2 years ago

0.9.31

2 years ago

0.9.30

2 years ago

0.9.29

2 years ago

0.9.28

2 years ago

0.9.27

2 years ago

0.9.26

2 years ago

0.9.25

2 years ago

0.9.24

2 years ago

0.9.23

2 years ago

0.9.22

2 years ago

0.9.21

2 years ago

0.9.20

2 years ago

0.9.19

2 years ago

0.9.18

2 years ago

0.9.17

2 years ago

0.9.16

2 years ago

0.9.15

2 years ago

0.9.14

2 years ago

0.9.13

2 years ago

0.9.12

2 years ago

0.9.10

2 years ago

0.9.9

2 years ago

0.9.8

2 years ago

0.9.7

2 years ago

0.9.6

2 years ago

0.9.5

2 years ago

0.9.4

2 years ago

0.9.3

2 years ago

0.9.1

2 years ago

0.9.0

2 years ago