SCSAI Native JavaScript Usage Guide

SCSAI Native JavaScript Usage Guide

1. Overview

This document describes how to use the existing SCSAI native JavaScript implementation in front-end PLM projects, avoiding redundant development of connection and AML operation functions.

Core file location: D:\n8nscsai\SCSAI\n8n\packages\nodes-base\nodes\SCSAI

2. Existing SCSAI Native JS Functions

2.1 Core API Connection (GenericFunctions.ts)

GenericFunctions.ts provides complete SCSAI API connection and AML operation functions:

#### Main Functions:

| Function Name | Description | Parameters |

|--------|----------|------|

| SCSAIApiRequest | Core API request function | method: GET/POSTendpoint: API endpointaml: AML string |

| executeAMLQuery | Execute AML query | context: Execution contextamlQuery: AML query string |

| batchWriteToSCSAI | Batch write data | context: Execution context items: Array of items to write |

| applyAML | Apply AML operation | itemType: Object type action: Operation type (add/edit/delete) data: Data id: Object ID |

#### Connection Configuration:

API connections are configured via SCSAI API credentials, including the following parameters:

  • URL: SCSAI server address
  • Username
  • Password
  • Database name
  • Time zone

2.2 Native JS Executor (SCSAINativeJSExecutor.ts)

SCSAINativeJSExecutor.ts provides the capability to execute JavaScript code in the SCSAI environment:

#### Main Functions:

| Function Name | Description |

|--------|----------|

| executeSCSAIJavaScript | Execute SCSAI native JavaScript code |

| executeAMLQuery | Execute AML query and return results |

| batchWriteToSCSAI | Batch Write to SCSAI Database |

| generateJSCodeExample | Generate JavaScript Code Example |

| validateJSCodeSafety | Validate JavaScript Code Safety |

3. Using in Frontend PLM Projects

3.1 Importing Existing Functions

In frontend PLM projects, existing functions can be imported via relative paths:

``javascript

// Import core API connection functions

import { SCSAIApiRequest, applyAML, queryItems } from '../../nodes-base/nodes/SCSAI/GenericFunctions';

// Import JavaScript executor functions

import { executeSCSAIJavaScript, executeAMLQuery } from '../../nodes-base/nodes/SCSAI/SCSAINativeJSExecutor';

`

3.2 Basic Usage Example

#### 3.2.1 Execute AML Query

`javascript

// Build AML query

const aml = '';

// Execute query

const result = await executeAMLQuery(context, aml);

// Process result

if (result.success) {

console.log('Query result:', result.result);

} else {

console.error('Query failed:', result.error);

}

`

#### 3.2.2 Create New Object

`javascript

// Create object data

const itemData = {

name: 'New Item',

description: 'Test description',

status: 'active'

};

// Apply AML to create object

const result = await applyAML(context, 'ItemType', 'add', itemData);

if (result) {

console.log('Object created successfully, ID:', result.id);

} else {

console.error('Object creation failed');

}

`

#### 3.2.3 Executing SCSAI Native JavaScript

`javascript

// JavaScript code

const jsCode =

// Create a new Item

var newItem = SCSAI.newItem('ItemType', 'create');

newItem.setProperty('name', 'value');

newItem.setProperty('description', 'Item Description');

newItem.save();

console.log('Item created: ' + newItem.getID());

;

// Execute the code

const result = await executeSCSAIJavaScript(context, jsCode);

if (result.success) {

console.log('JavaScript executed successfully:', result.result);

} else {

console.error('JavaScript execution failed:', result.error);

}

`

3.3 Permissions and Security

#### 3.3.1 Credential Management

Ensure that the SCSAIApi credentials are set in the n8n configuration, containing correct connection information.

#### 3.3.2 Code Security

Use the validateJSCodeSafety function to verify the security of the JavaScript code to be executed:

`javascript

const { safe, warnings } = validateJSCodeSafety(jsCode);

if (!safe) {

console.warn('Code contains security risks:', warnings);

// Handle unsafe code

}

`

4. Extended Features

4.1 Creating Custom Tool Functions

If you need to add custom functionality to the front-end PLM project, you can create new JS files, but ensure no duplication of existing features:

`javascript

// custom-SCSAI-utils.js

import { executeAMLQuery } from '../../nodes-base/nodes/SCSAI/SCSAINativeJSExecutor';

/**

  • Custom query function - Get active ItemType list

*/

export async function getActiveItemTypes(context) {

const aml = '1';

const result = await executeAMLQuery(context, aml);

if (result.success) {

return result.result;

} else {

throw new Error(result.error);

}

}

`

4.2 Error Handling

Ensure proper error handling when using the SCSAI API:

`javascript

try {

const result = await executeAMLQuery(context, aml);

return result;

} catch (error) {

console.error('SCSAI API Error:', error);

// Error handling logic

throw error;

}

`

5. Best Practices

  1. Avoid duplicate code: Always use existing SCSAI native JS implementations; do not create new connection or AML operation functions.
  2. Use type checking: If using TypeScript, ensure correct type definitions are imported.
  3. Security first: Always validate user-provided JavaScript code.
  4. Appropriate logging: Add necessary logging for debugging.
  5. Error handling: Implement proper error handling mechanisms.

6. Reference Documentation

  • SCSAI API Documentation
  • n8n node development documentation
  • Source code in the packages/nodes-base/nodes/SCSAI directory

7. Maintenance

If existing functionality requires updates or extensions, modify directly in the `packages/nodes-base/nodes/SCSAI` directory to ensure all projects benefit from the updates.

8. Contact Information

For any questions, please contact the system administrator or development team.

← 返回案例列表

Try 左帮右臂 Industrial Agent — your digital workforce

Available as a WeChat mini program, web app (ylxt.chat) and desktop client. Start from whichever fits your workflow.

WeChat mini program QR
Scan for the WeChat mini program
Share:
🤖 Try Now →
🤖
🎁