Skip to main content

Intro

With JavaScript schemas, you can do everything you can do with JSON schemas and more.

With JavaScript schemas, you can build your schemas in a standard language most developers are already familiar with. This means that you have all the power and flexibility from JavaScript available when you are creating your schemas.

JavaScript schema example
/** @type {Enterspeed.FullSchema} */
export default {
triggers: function(context) {
context.triggers('cms', ['page']);
},
routes: function(sourceEntity, context) {
context.url(sourceEntity.url);
},
properties: function (sourceEntity, context) {
const p = sourceEntity.properties;
return {
title: p.title,
blocks: context.partial("blocks", p.blocks),
aboutUsPage: context.reference("page").byOriginId(p.aboutUsPage.id),
};
}
}

The concepts in JavaScript schemas are similar to the concepts from JSON schemas, with triggers, route, properties and so on, and since the source entity and a context object (used for making references, partials, etc.) are passed in as parameters you can even create unit tests of your schemas if you want to.

Destructuring

You can destruct parameters, so the routes and properties method in the above example can be simplified to:

JavaScript destruct schema example
/** @type {Enterspeed.FullSchema} */
export default {
triggers: function(context) {
context.triggers('cms', ['page']);
},
routes: function({url}, context) {
context.url(url);
},
properties: function ({properties: p}, context) {
return {
title: p.title,
blocks: context.partial('blocks', p.blocks),
aboutUsPage: context.reference('page').byOriginId(p.aboutUsPage.id)
};
}
}

Arrow function expression

You can use arrow function expression to simplify or make your schema even more compact:

JavaScript arrow function expression schema example
/** @type {Enterspeed.FullSchema} */
export default {
triggers: (context) => context.triggers('cms', ['page']),
routes: ({url}, context) => context.url(url),
properties: ({properties: p}, context) =>({
title: p.title,
blocks: context.partial('blocks', p.blocks),
aboutUsPage: context.reference('page').byOriginId(p.aboutUsPage.id)
})
}

Console object

When debugging JavaScript it's often useful to use the console object to print out values to the console.

When using the Test schema feature in the Enterspeed Management App, the following methods are supported:

  • debug
  • error
  • info
  • log
  • trace
  • warn
JavaScript schema with conole usage
/** @type {Enterspeed.FullSchema} */
export default {
triggers: function(context) {
context.triggers('cms', ['page']);
},
routes: function({url}, context) {
context.url(url);
},
properties: function ({properties: p}, context) {
console.log('block type', p.block.type)
return {
title: p.title,
block: context.partial(`block-${p.block.type}`, p.block),
aboutUsPage: context.reference('page').byOriginId(p.aboutUsPage.id)
};
}
}

If you use any of the other native console methods, the schema is still working but the methods will just not output anything.

Limitations

Now, we said that you have all the power of the JavaScript language available for you in your JavaScript schemas, but we have added some limitations because of security.

This means that the following areas has been restricted.

  • No access to the filesystem
  • No network traffic
  • Maximum processing time of 60 sec pr schema