2.48.0 • Published 4 months ago

@memberjunction/data-context-server v2.48.0

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

@memberjunction/data-context-server

This library provides a server-side implementation of the DataContextItem class from @memberjunction/data-context that can handle the server-side only use case of loading data into a context using raw SQL statements.

Overview

The @memberjunction/data-context-server package extends the base DataContextItem class to provide server-side functionality for executing SQL queries directly against a database. This is particularly useful when you need to load data contexts that include custom SQL statements, which cannot be executed on the client side.

Installation

npm install @memberjunction/data-context-server

Purpose and Functionality

This package serves a critical role in the MemberJunction ecosystem by:

  1. Enabling SQL Execution: Provides the ability to execute raw SQL statements through data contexts on the server side
  2. TypeORM Integration: Uses TypeORM's DataSource for database operations
  3. Automatic Registration: Registers itself with higher priority (2) to override the base implementation when running on the server
  4. Tree-Shaking Prevention: Includes a utility function to ensure the class isn't removed during build optimization

Usage

Basic Setup

First, ensure the server-side implementation is loaded to prevent tree-shaking:

import { LoadDataContextItemsServer } from '@memberjunction/data-context-server';

// Call this once in your server initialization code
LoadDataContextItemsServer();

Loading Data Contexts with SQL Items

import { DataContext } from '@memberjunction/data-context';
import { DataSource } from 'typeorm';
import { UserInfo } from '@memberjunction/core';

// Assume you have a TypeORM DataSource configured
const dataSource: DataSource = /* your configured data source */;

// Load a data context that includes SQL-type items
const context = new DataContext();
const user = /* current user context */;

// Load metadata and data in one operation
const success = await context.Load(
  dataContextId,
  dataSource,  // Pass the TypeORM DataSource
  false,       // forceRefresh
  false,       // loadRelatedDataOnSingleRecords
  0,           // maxRecordsPerRelationship
  user         // contextUser
);

if (success) {
  // Access the loaded data
  context.Items.forEach(item => {
    if (item.Type === 'sql' && item.DataLoaded) {
      console.log(`SQL Item Data:`, item.Data);
    }
  });
}

Creating SQL Data Context Items

import { DataContext } from '@memberjunction/data-context';

const context = new DataContext();
const sqlItem = context.AddDataContextItem();

// Configure as SQL type
sqlItem.Type = 'sql';
sqlItem.SQL = 'SELECT * FROM Customers WHERE Country = @country';
sqlItem.RecordName = 'US Customers';
sqlItem.AdditionalDescription = 'All customers from the United States';

// Load the data
const dataSource = /* your TypeORM DataSource */;
const loaded = await sqlItem.LoadData(dataSource);

if (loaded) {
  console.log('SQL Results:', sqlItem.Data);
} else {
  console.error('Loading failed:', sqlItem.DataLoadingError);
}

API Documentation

DataContextItemServer Class

The DataContextItemServer class extends DataContextItem and overrides the LoadFromSQL method.

Protected Methods

LoadFromSQL(dataSource: any, contextUser?: UserInfo): Promise<boolean>

Executes a SQL statement and loads the results into the DataContextItem.

Parameters:

  • dataSource (any): The TypeORM DataSource object used to execute queries
  • contextUser (UserInfo, optional): The user context for the operation

Returns:

  • Promise<boolean>: Returns true if successful, false if an error occurs

Error Handling:

  • Catches and logs any SQL execution errors
  • Sets the DataLoadingError property with error details
  • Returns false on failure

Utility Functions

LoadDataContextItemsServer(): void

Prevents tree-shaking from removing the DataContextItemServer class during build optimization.

Usage: Call this function after importing the package in your server application to ensure the class registration takes effect.

Integration with MemberJunction Packages

This package integrates seamlessly with:

  • @memberjunction/data-context: Provides the base DataContextItem class and DataContext functionality
  • @memberjunction/global: Uses the class registration system for runtime polymorphism
  • @memberjunction/core: Leverages logging utilities and user context
  • typeorm: Utilizes TypeORM's DataSource for database operations

Dependencies

{
  "@memberjunction/data-context": "2.43.0",
  "@memberjunction/global": "2.43.0",
  "typeorm": "^0.3.20"
}

Configuration

No special configuration is required. The package automatically registers itself with the MemberJunction class factory system when imported.

Build and Development

Scripts

  • npm run build: Compiles TypeScript to JavaScript
  • npm start: Runs the TypeScript code directly using ts-node-dev

TypeScript Configuration

The package uses a standard TypeScript configuration that compiles to ES modules and includes type definitions.

Important Notes

  1. Server-Side Only: This package is designed for server-side use only. Client-side applications should not include this package.

  2. Class Registration Priority: The DataContextItemServer class registers with priority 2, ensuring it overrides the base implementation when present.

  3. Error Handling: SQL execution errors are caught and stored in the DataLoadingError property rather than throwing exceptions.

  4. Data Loading: The LoadFromSQL method stores query results directly in the Data property as an array of objects.

Example: Complete Server Application

import { DataContext } from '@memberjunction/data-context';
import { LoadDataContextItemsServer } from '@memberjunction/data-context-server';
import { createConnection, DataSource } from 'typeorm';
import { Metadata } from '@memberjunction/core';

// Initialize the server-side data context support
LoadDataContextItemsServer();

// Configure your database connection
const dataSource = new DataSource({
  type: 'mssql',
  host: 'localhost',
  username: 'your_username',
  password: 'your_password',
  database: 'your_database',
  // ... other TypeORM configuration
});

async function loadDataContextWithSQL() {
  await dataSource.initialize();
  
  const context = new DataContext();
  const user = await Metadata.Provider.GetCurrentUser();
  
  // Create a SQL-based data context item
  const item = context.AddDataContextItem();
  item.Type = 'sql';
  item.SQL = `
    SELECT 
      c.CustomerID,
      c.CompanyName,
      COUNT(o.OrderID) as OrderCount,
      SUM(od.Quantity * od.UnitPrice) as TotalRevenue
    FROM Customers c
    LEFT JOIN Orders o ON c.CustomerID = o.CustomerID
    LEFT JOIN [Order Details] od ON o.OrderID = od.OrderID
    GROUP BY c.CustomerID, c.CompanyName
    ORDER BY TotalRevenue DESC
  `;
  item.RecordName = 'Customer Revenue Summary';
  
  // Load the data
  const success = await item.LoadData(dataSource, false, false, 0, user);
  
  if (success) {
    console.log('Customer revenue data:', item.Data);
    
    // Save the context if needed
    await context.SaveItems(user, true); // true to persist the data
  }
  
  await dataSource.destroy();
}

License

This package is part of the MemberJunction framework and follows the same licensing terms.

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

5 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

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

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

2 years ago

0.9.68

2 years ago

0.9.64

2 years ago

0.9.66

2 years ago

0.9.63

2 years ago

0.9.62

2 years ago

0.9.61

2 years ago

0.9.57

2 years ago

0.9.58

2 years ago

0.9.59

2 years ago

0.9.60

2 years ago

0.9.56

2 years ago

0.9.54

2 years ago

0.9.55

2 years ago

0.9.53

2 years ago

0.9.51

2 years ago

0.9.50

2 years ago

0.9.49

2 years ago

0.9.46

2 years ago

0.9.47

2 years ago

0.9.48

2 years ago

0.9.43

2 years ago

0.9.44

2 years ago

0.9.45

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

2 years ago

0.9.37

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

2 years ago

0.9.24

2 years ago

0.9.26

2 years ago

0.9.27

2 years ago

0.9.28

2 years ago

0.9.29

2 years ago

0.9.21

2 years ago

0.9.22

2 years ago

0.9.14

2 years ago

0.9.15

2 years ago

0.9.20

2 years ago

0.9.16

2 years ago

0.9.17

2 years ago

0.9.18

2 years ago

0.9.19

2 years ago

0.9.13

2 years ago

0.9.12

2 years ago

0.9.10

2 years ago

0.9.11

2 years ago

0.9.8

2 years ago

0.9.7

2 years ago

0.9.9

2 years ago

0.9.4

2 years ago

0.9.3

2 years ago

0.9.6

2 years ago

0.9.5

2 years ago

0.9.2

2 years ago