A moddle wrapper for BPMN 2.0 that provides reading and writing of BPMN diagram files
npx @tessl/cli install tessl/npm-bpmn-moddle@9.0.0BPMN Moddle is a JavaScript library for reading and writing BPMN 2.0 diagram files in Node.js and browsers. It provides a programmatic interface to create, modify, and parse BPMN process definitions using the BPMN 2.0 meta-model to validate input and produce correct BPMN 2.0 XML.
npm install bpmn-moddleimport BpmnModdle from 'bpmn-moddle';For CommonJS:
const BpmnModdle = require('bpmn-moddle');import BpmnModdle from 'bpmn-moddle';
const moddle = new BpmnModdle();
const xmlStr =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<bpmn2:definitions xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" ' +
'id="empty-definitions" ' +
'targetNamespace="http://bpmn.io/schema/bpmn">' +
'</bpmn2:definitions>';
// Parse XML to object model
const { rootElement: definitions } = await moddle.fromXML(xmlStr);
// Modify the model
definitions.set('id', 'NEW_ID');
// Create new BPMN elements
const bpmnProcess = moddle.create('bpmn:Process', { id: 'MyProcess_1' });
definitions.get('rootElements').push(bpmnProcess);
// Serialize back to XML
const { xml: xmlStrUpdated } = await moddle.toXML(definitions);BPMN Moddle is built on top of the moddle and moddle-xml frameworks, providing:
Core functionality for parsing BPMN XML files into JavaScript objects and serializing objects back to XML.
// Note: The default export is actually SimpleBpmnModdle function, used like a constructor
function BpmnModdle(packages?: Object, options?: Object): BpmnModdle;
class BpmnModdle {
fromXML(xmlStr: string, typeName?: string, options?: Object): Promise<ParseResult>;
toXML(element: ModdleElement, options?: Object): Promise<SerializationResult>;
create(type: string, properties?: Object): ModdleElement;
getType(name: string): ModdleType;
}
interface ParseResult {
rootElement: ModdleElement;
references: Array<Object>;
warnings: Array<Error>;
elementsById: Object;
}
interface SerializationResult {
xml: string;
}Object model functionality for creating BPMN elements and manipulating their properties.
// Element creation
create(type: string, properties?: Object): ModdleElement;
// Create elements in undefined namespaces for custom extensions
createAny(type: string, namespace: string, properties?: Object): ModdleElement;
// Type system access
getType(name: string): ModdleType;
// Element property access
interface ModdleElement {
get(propertyName: string): any;
set(propertyName: string, value: any): void;
$instanceOf(type: string): boolean;
$type: string;
}Support for extending BPMN with custom element types and properties through package definitions.
// Factory function with built-in packages
function SimpleBpmnModdle(additionalPackages?: Object, options?: Object): BpmnModdle;
// Built-in package schemas
interface BuiltInPackages {
bpmn: Object; // Core BPMN 2.0 elements
bpmndi: Object; // BPMN Diagram Interchange
dc: Object; // Drawing Canvas elements
di: Object; // Diagram Interchange
bioc: Object; // BPMN.io custom elements
color: Object; // BPMN in Color extension
}interface ModdleElement {
$type: string;
$instanceOf(type: string): boolean;
get(propertyName: string): any;
set(propertyName: string, value: any): void;
}
interface ModdleType {
$descriptor: TypeDescriptor;
}
interface TypeDescriptor {
name: string;
properties: PropertyDefinition[];
propertiesByName: Object;
allProperties: PropertyDefinition[];
superClass?: string[];
}
interface PropertyDefinition {
name: string;
type: string;
isMany?: boolean;
isReference?: boolean;
}
interface ParseError extends Error {
warnings: Array<Error>;
}