2.48.0 • Published 4 months ago

@memberjunction/data-context v2.48.0

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

@memberjunction/data-context

The @memberjunction/data-context library provides a comprehensive framework for managing data contexts in MemberJunction applications. It enables developers to define, load, and manipulate collections of related data items across different tiers of an application.

Overview

A Data Context in MemberJunction represents a collection of data items that can include:

  • Views: Filtered and customized views of entity data
  • Queries: Pre-defined SQL queries registered in the system
  • Full Entities: Complete data sets from an entity
  • Single Records: Individual entity records with optional related data
  • SQL Statements: Direct SQL queries (server-side only)

Installation

npm install @memberjunction/data-context

Key Features

  • Type-safe data context management: Full TypeScript support with proper typing
  • Flexible data loading: Support for various data sources including views, queries, entities, and SQL
  • Metadata-driven: Automatic loading of metadata from the MemberJunction system
  • Transaction support: Save multiple data context items in a single transaction
  • Cross-tier compatibility: Works across server and client tiers with appropriate implementations
  • Data persistence: Optional saving of loaded data to the database

Usage

Basic Example

import { DataContext, DataContextItem } from '@memberjunction/data-context';
import { Metadata } from '@memberjunction/core';

// Create a new data context
const context = new DataContext();

// Load metadata and data for an existing data context
const dataContextID = 'your-data-context-id';
const loaded = await context.Load(
  dataContextID,
  dataSource, // Required for SQL type items (server-side only)
  false,      // forceRefresh
  true,       // loadRelatedDataOnSingleRecords
  10,         // maxRecordsPerRelationship
  userInfo    // contextUser
);

if (loaded) {
  // Access the loaded data
  context.Items.forEach(item => {
    console.log(`Item: ${item.Description}`);
    console.log(`Data rows: ${item.Data?.length || 0}`);
  });
}

Creating Data Context Items

From a View

import { UserViewEntityExtended } from '@memberjunction/core-entities';

// Assuming you have a view entity loaded
const viewEntity: UserViewEntityExtended = await md.GetEntityObject<UserViewEntityExtended>('User Views');
await viewEntity.Load(viewID);

const viewItem = DataContextItem.FromViewEntity(viewEntity);
context.Items.push(viewItem);

From a Single Record

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

// Assuming you have an entity record loaded
const record: BaseEntity = await md.GetEntityObject('Customers');
await record.Load(recordID);

const recordItem = DataContextItem.FromSingleRecord(record);
context.Items.push(recordItem);

From a Query

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

// Get query info from metadata
const queryInfo = md.Queries.find(q => q.Name === 'My Query');
if (queryInfo) {
  const queryItem = DataContextItem.FromQuery(queryInfo);
  context.Items.push(queryItem);
}

From a Full Entity

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

// Get entity info from metadata
const entityInfo = md.Entities.find(e => e.Name === 'Products');
if (entityInfo) {
  const entityItem = DataContextItem.FromFullEntity(entityInfo);
  context.Items.push(entityItem);
}

Loading Data for Items

// Load data for all items in the context
const dataLoaded = await context.LoadData(
  dataSource,  // Required for SQL type items
  false,       // forceRefresh
  true,        // loadRelatedDataOnSingleRecords
  10          // maxRecordsPerRelationship
);

// Or load data for a specific item
const itemLoaded = await context.Items[0].LoadData(
  dataSource,
  false,
  true,
  10
);

Saving Data Context Items

// Save all items in the context to the database
const saved = await context.SaveItems(
  userInfo,  // contextUser
  true       // persistItemData - saves actual data, not just metadata
);

if (saved) {
  console.log('Data context items saved successfully');
  // Each item now has a DataContextItemID populated
}

Working with Data

// Validate that all items have data loaded
if (context.ValidateDataExists()) {
  // Convert to a simple object for easier manipulation
  const simpleData = context.ConvertToSimpleObject('item_', false);
  // Result: { item_0: [...], item_1: [...], ... }
  
  // Get type definition for the data structure
  const typeDef = context.CreateSimpleObjectTypeDefinition('item_');
  console.log(typeDef);
  // Output: {item_0: []; // View: Customer List, From Entity: Customers\n...}
}

// Access individual item data
context.Items.forEach(item => {
  if (item.DataLoaded && item.Data) {
    console.log(`${item.Description}: ${item.Data.length} rows`);
    
    // Process the data
    item.Data.forEach(row => {
      // Work with row data
    });
  } else if (item.DataLoadingError) {
    console.error(`Error loading ${item.Description}: ${item.DataLoadingError}`);
  }
});

Cloning Data Contexts

// Clone an existing data context
const clonedContext = await DataContext.Clone(
  originalContext,
  true,      // includeData - copies the data along with metadata
  userInfo   // contextUser
);

if (clonedContext) {
  console.log(`Cloned context ID: ${clonedContext.ID}`);
}

API Reference

DataContext Class

Properties

  • ID: string - The unique identifier of the data context
  • DataContextEntity: DataContextEntity - The metadata entity for the data context
  • Items: DataContextItem[] - Array of data context items

Methods

  • async LoadMetadata(DataContextID: string, contextUser?: UserInfo, provider?: IMetadataProvider): Promise<boolean>

    • Loads only the metadata for the data context and its items
  • async LoadData(dataSource: any, forceRefresh?: boolean, loadRelatedDataOnSingleRecords?: boolean, maxRecordsPerRelationship?: number, contextUser?: UserInfo): Promise<boolean>

    • Loads data for all items in the context
  • async Load(DataContextID: string, dataSource: any, forceRefresh?: boolean, loadRelatedDataOnSingleRecords?: boolean, maxRecordsPerRelationship?: number, contextUser?: UserInfo): Promise<boolean>

    • Loads both metadata and data in one operation
  • async SaveItems(contextUser?: UserInfo, persistItemData?: boolean): Promise<boolean>

    • Saves all data context items to the database
  • AddDataContextItem(): DataContextItem

    • Creates and adds a new item to the context
  • ValidateDataExists(ignoreFailedLoadItems?: boolean): boolean

    • Checks if all items have data loaded
  • ConvertToSimpleObject(itemPrefix?: string, includeFailedLoadItems?: boolean): any

    • Converts the context to a simple object structure
  • CreateSimpleObjectTypeDefinition(itemPrefix?: string, includeFailedLoadItems?: boolean): string

    • Generates TypeScript type definition for the data structure
  • LoadDataFromObject(data: any[][]): boolean

    • Loads pre-fetched data into the context
  • static async Clone(context: DataContext, includeData?: boolean, contextUser?: UserInfo): Promise<DataContext>

    • Creates a deep copy of a data context
  • static async FromRawData(rawData: any): Promise<DataContext>

    • Creates a context from raw data object

DataContextItem Class

Properties

  • Type: 'view' | 'query' | 'full_entity' | 'sql' | 'single_record' - The type of data item
  • RecordID: string - Primary key for single_record types
  • EntityID?: string - Entity identifier
  • ViewID?: string - View identifier
  • QueryID?: string - Query identifier
  • RecordName: string - Name of the view, query, or entity
  • SQL?: string - SQL statement for 'sql' type
  • EntityName?: string - Name of the entity
  • Fields: DataContextFieldInfo[] - Field metadata
  • Data: any[] - The loaded data
  • DataLoaded: boolean - Indicates if data has been loaded
  • DataLoadingError?: string - Error message if loading failed
  • Description: string - Auto-generated description
  • AdditionalDescription?: string - Optional custom description

Methods

  • async LoadData(dataSource: any, forceRefresh?: boolean, loadRelatedDataOnSingleRecords?: boolean, maxRecordsPerRelationship?: number, contextUser?: UserInfo): Promise<boolean>

    • Loads data for this specific item
  • LoadDataFromObject(data: any[]): boolean

    • Loads pre-fetched data into the item
  • ValidateDataExists(ignoreFailedLoad?: boolean): boolean

    • Validates that data has been loaded
  • static FromViewEntity(viewEntity: UserViewEntityExtended): DataContextItem

  • static FromSingleRecord(singleRecord: BaseEntity): DataContextItem
  • static FromQuery(query: QueryInfo): DataContextItem
  • static FromFullEntity(entity: EntityInfo): DataContextItem
  • static FromRawItem(rawItem: any): DataContextItem

DataContextFieldInfo Class

class DataContextFieldInfo {
  Name: string;
  Type: string;
  Description?: string;
}

Server-Side Considerations

For SQL type data context items, you'll need to use the server-side implementation from @memberjunction/data-context-server which properly handles SQL execution with appropriate security and data source management.

Dependencies

  • @memberjunction/global: Core global utilities and class factory
  • @memberjunction/core-entities: Entity definitions for MemberJunction
  • @memberjunction/core: Core MemberJunction functionality

Best Practices

  1. Always load metadata before data: Use LoadMetadata() before LoadData() or simply use Load() which does both
  2. Handle loading errors: Check DataLoaded and DataLoadingError properties on items
  3. Use appropriate data sources: SQL type items require server-side data sources
  4. Consider performance: Use maxRecordsPerRelationship to limit related data loading
  5. Validate before use: Call ValidateDataExists() before processing data
  6. Use transactions: When saving multiple items, they're automatically wrapped in a transaction

License

ISC

2.23.2

8 months ago

2.46.0

4 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

4 months ago

2.22.1

8 months ago

2.22.0

8 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

8 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

8 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

5 months ago

2.14.0

9 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

2.0.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.0

1 year ago

2.3.2

1 year ago

2.3.1

1 year ago

2.3.3

1 year ago

1.8.1

1 year ago

1.8.0

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

1.7.1

1 year ago

1.7.0

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.5.0

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.5.1

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

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.70

2 years ago

0.9.71

2 years ago

0.9.72

2 years ago

0.9.68

2 years ago

0.9.67

2 years ago

0.9.66

2 years ago

0.9.65

2 years ago

0.9.63

2 years ago

0.9.64

2 years ago

0.9.61

2 years ago

0.9.62

2 years ago

0.9.58

2 years ago

0.9.59

2 years ago

0.9.60

2 years ago

0.9.57

2 years ago

0.9.55

2 years ago

0.9.53

2 years ago

0.9.54

2 years ago

0.9.52

2 years ago

0.9.50

2 years ago

0.9.51

2 years ago

0.9.47

2 years ago

0.9.48

2 years ago

0.9.49

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.37

2 years ago

0.9.38

2 years ago

0.9.36

2 years ago

0.9.35

2 years ago

0.9.34

2 years ago

0.9.30

2 years ago

0.9.31

2 years ago

0.9.32

2 years ago

0.9.33

2 years ago

0.9.27

2 years ago

0.9.28

2 years ago

0.9.25

2 years ago

0.9.26

2 years ago

0.9.23

2 years ago

0.9.24

2 years ago

0.9.20

2 years ago

0.9.21

2 years ago

0.9.22

2 years ago

0.9.18

2 years ago

0.9.19

2 years ago

0.9.17

2 years ago

0.9.14

2 years ago

0.9.15

2 years ago

0.9.16

2 years ago

0.9.12

2 years ago

0.9.8

2 years ago

0.9.13

2 years ago

0.9.7

2 years ago

0.9.6

2 years ago

0.9.11

2 years ago

0.9.5

2 years ago

0.9.4

2 years ago

0.9.3

2 years ago

0.9.2

2 years ago