包详细信息

moddle

bpmn-io486.5kMIT7.2.0

A library for importing meta-model based file formats into JS

model, meta-model, xml, xsd

自述文件

moddle

CI

A utility library for working with meta-model based data structures.

What is it good for?

moddle offers you a concise way to define meta models in JavaScript. You can use these models to consume documents, create model elements, and perform model validation.

Define a schema

You start by creating a moddle schema. It is a JSON file which describes types, their properties, and relationships:

{
  "$schema": "https://unpkg.com/moddle/resources/schema/moddle.json",
  "name": "Cars",
  "uri": "http://cars",
  "prefix": "c",
  "types": [
    {
      "name": "Base",
      "properties": [
        { "name": "id", "type": "String", "isAttr": true }
      ]
    },
    {
      "name": "Root",
      "superClass": [ "Base" ],
      "properties": [
        { "name": "cars", "type": "Car", "isMany": true }
      ]
    },
    {
      "name": "Car",
      "superClass": [ "Base" ],
      "properties": [
        { "name": "name", "type": "String", "isAttr": true, "default": "No Name" },
        { "name": "power", "type": "Integer", "isAttr": true },
        { "name": "similar", "type": "Car", "isMany": true, "isReference": true },
        { "name": "trunk", "type": "Element", "isMany": true }
      ]
    }
  ]
}

You may attach the provided JSON schema to get your moddle descriptor validated by code editor.

Instantiate moddle

You can instantiate a moddle instance with a set of defined schemas:

import { Moddle } from 'moddle';

var cars = new Moddle([ carsJSON ]);

Create objects

Use a moddle instance to create objects of your defined types:

var taiga = cars.create('c:Car', { name: 'Taiga' });

console.log(taiga);
// { $type: 'c:Car', name: 'Taiga' };


var cheapCar = cars.create('c:Car');

console.log(cheapCar.name);
// "No Name"


// really?
cheapCar.get('similar').push(taiga);

Introspect things

Then again, given the knowledge moddle has, you can perform deep introspection:

var carDescriptor = cheapCar.$descriptor;

console.log(carDescriptor.properties);
// [ { name: 'id', type: 'String', ... }, { name: 'name', type: 'String', ...} ... ]

Access extensions

moddle is friendly towards extensions and keeps unknown any properties around:

taiga.set('specialProperty', 'not known to moddle');

console.log(taiga.get('specialProperty'));
// 'not known to moddle'

It also allows you to create any elements for namespaces that you did not explicitly define:

var screwdriver = cars.createAny('tools:Screwdriver', 'http://tools', {
  make: 'ScrewIt!'
});

car.trunk.push(screwdriver);

There is more

Have a look at our test coverage to learn about everything that is currently supported.

Resources

Related

  • moddle-xml: read xml documents based on moddle descriptors

License

MIT

更新日志

Changelog

All notable changes to moddle are documented here. We use semantic versioning for releases.

Unreleased

_Note: Yet to be released changes appear here._

7.2.0

  • FEAT: expose moddle schema via well-known folder (#64)
  • DOCS: simplify $schema related documentation

7.1.0

  • FEAT: provide a JSON schema to validate moddle schema (#62)

7.0.0

  • FEAT: add exports configuration
  • FEAT: add moddle descriptor JSON schema (#57)
  • FIX: remove broken main configuration
  • CHORE: drop UMD distribution
  • DEPS: update to min-dash@4.2.1

Breaking Changes

  • UMD distribution no longer bundled. The module is now available as an ES module.

6.2.3

  • FIX: mark accessors as non-enumerable (#55)

6.2.2

  • FIX: add accessors for elements created by createAny (#54)

6.2.1

  • FIX: allow self-extension using local name (#52)

6.2.0

  • FEAT: add ability to configure moddle (#48)
  • FEAT: add ability to explicitly mark property as global (#48)
  • FEAT: add strict mode / ability to log unknown property access (#48)

6.1.0

  • FEAT: improve error thrown on trait introspection (#38, #46)
  • FIX: correctly handle inherits flag with multiple parents (#47)

6.0.0

  • DEPS: bump to min-dash@4
  • CHORE: turn into ES module

5.0.4

  • FIX: guard against ModdleElement#set miss-use (#43)

5.0.3

  • FIX: use getters for read-only properties (#40)
  • CHORE: add prepare script (#33))

5.0.2

  • FIX: make Any type $instanceOf member non-enumerable

5.0.1

  • CHORE: cleanup pre-built distribution

5.0.0

  • CHORE: expose { Moddle } and utilities
  • CHORE: provide pre-packaged distribution

Breaking Changes

  • We expose Moddle as a named export now.
  • We do not publish lib folder anymore, destructure the provided default export.
  • No need for esm to consume the library anymore.

4.1.0

  • CHORE: bump utility toolbelt

4.0.0

Breaking Changes

  • FEAT: migrate to ES modules. Use esm or a ES module aware transpiler to consume this library.

3.0.0

  • FEAT: drop lodash in favor of min-dash

...

Check git log for earlier history.