ReferencesMScript Syntax
References

MScript Syntax Guide

What is MScript

MScript is a string-type field in Mindbricks patterns used to store JavaScript expressions. MScript values allow architects to inject native JavaScript expressions into the generated code in a definitive and controlled manner.

Scope

Mindbricks is an AI-powered, pattern-based backend microservice code generator. This document aims to guide both human and AI architects* in designing applications on Mindbricks using MScript.

*Architect refers to either AI agents or human users of Mindbricks, including product managers, business analysts, and software architects. Throughout this document, the term "architect" will be used interchangeably for both without further distinction.

First Rule

MScript values are injected directly into the generated code after basic JavaScript syntax validation. However, even if a script is syntactically correct in isolation, it may cause syntax errors depending on where it is injected.

A good rule of thumb is to ensure your script would be valid if combined with a simple JavaScript assignment operation, such as:

Your script in the project design:

...doWhen: "this.user.age > 18"

Mindbricks-generated code:

const condition = this.user.age > 18;
if (!condition) {
  throw new Error("User should be older than 18");
}

or:

if (!this.user.age > 18) {
  throw new Error("User should be older than 18");
}

You have an additional option for writing MScripts:

  • Using**module.exports**
    You can write any MScript using a module.exports statement to reduce mistakes. Mindbricks will inject the exported part into the generated code.
module.exports = (this.user.age > 18)

Valid MScript Expressions

Literals

You can use literal expressions in MScript fields, suitable for the context where they are expected.

  • Number
5

or

0.4
  • String
"premium"

Example from design JSON:

// snippet from design JSON, not generated code
userSettings.userType: "'premium'"
  • Boolean
true
  • Null identifier
null

Identifiers

You can use unreserved words as identifiers in JavaScript.

userId

this Expression

By default, MScript expressions are parsed into the methods of a class unless stated otherwise.
Using this refers to the API context class.

this

Member Expressions

You can access object properties using member expressions, provided the object is available in the scope.

Example:

user.roleId

Common usage with this:

this.projectId

or

this.organization.ownerId

Binary Expressions

You can use any binary expression that evaluates to a value.

Examples:

this.user.id == this.session.userId
5 - 2
5 + this.userAge
this.userAge ?? 18
// Not allowed
// this.userAge = 5

Conditional Expressions

Simple conditional expression:

isActive ? "yes" : "no"

More complex example:

this.user.isPremium ? this.user.paidRights > this.user.usedRights : true

Array Expressions

Array expressions are valid in MScript.
Ensure that the receiving code expects or supports an array.

Example:

["admin", "superAdmin"]

Call Expressions

You can call functions or methods that are accessible in the given scope.

Example using a method from the context:

this.getUserRole(userId)

Example using an imported function:

getUserAddressFromDb(this.userId)

A trick when you need some calculation logic before giving the expression in MScript, Define an Inline Arrow Function.

(
()=>{
  let a = null;
  // do something
  // more something
  // return the expression 
  return a;
}
)()

Object Expressions

Mindbricks expects object expressions in some MScript fields, such as in "where" clauses.

Simple object:

{
  admin: 0,
  user: 1,
  proUser: 2
}

Complex object (e.g., a where clause):

{
  "$or": [
    {
      userId: this.session.userId
    },
    {
      isProtected: false
    }
  ]
}

The Context: this

When the pattern-based architecture design in Mindbricks is converted into services, all MScript fields within the patterns are transformed into code inside the API Manager layer. This layer is implemented as a JavaScript class that manages the current API operation.

Thus, when we refer to the context, we are talking about the properties of this class. Since scripts defined in MScript fields are injected into methods of the class, you can always access the context within scripts using the this identifier.


📋 The Contents of the Context

🛤 Business API Parameters

You can directly access the Business API parameters as properties of this:

this.productId

👤 Session Data

Information about the currently logged-in user is accessible via the session object:

this.session.userId
this.session.roleId

📥 Action Results in Business API

If you have added any BusinessApiAction in your Business Logic design like FetchObjectAction, FetchStatsAction, CreateCrudAction, ApiCallAction, the result of the action call will be written to the contextPropertyName value define din the action settings.

Example for an object fetch has contextPropertyName as userCompany:

this.userCompany.name

Note: BusinessApiAction result data becomes available only after the action operation is executed.

📦 Main Operation Data

Depending on the crudType of the Business Api, the main resource data becomes available:

  • Create: Access data after creation with the name of the data object like this.customer

  • Get: Access data after retrieval with the name of the data object like this.customer

  • Delete: Access data after fetching the instance with the name of the data object like this.customer

  • Update: Access data after fetching the instance with the name of the data object like this.customer

  • List: Access data list after retrieval with the plural name of the data object like this.customers

Example:

this.customer.countryId

🛠 LIB Functions

Global library functions are accessible via the LIB global object. You do not need to use this.

LIB.getDayOfTheWeek()

🧩 Function Results

If you call a function inside your Business Api using the FunctionCallAction, the result will be available in the context using the action's contextPropertyName after the function is executed.

Example:

// assuming the contextPropertyName of a FunctionCallAction is userCount
this.userCount > 5

By consistently accessing the context with this, Mindbricks enables powerful, dynamic, and readable script logic inside your services.

📄 MScript Query Syntax Guide

Mindbricks provides a unified object syntax for writing filter queries that work across:

  • MongoDB

  • Elasticsearch

  • Sequelize (PostgreSQL, MySQL, etc.)

Using Mindbricks' MScript Query syntax, you can write one object structure, and Mindbricks services will automatically generate the platform-specific query for data fetching.


✨ Basic Structure

MScript Queries are JavaScript objects where:

  • Field names are keys.

  • Field conditions are values.

  • Operators start with a `## What is MScript

MScript is a string-type field in Mindbricks patterns used to store JavaScript expressions. MScript values allow architects to inject native JavaScript expressions into the generated code in a definitive and controlled manner.

Scope

Mindbricks is an AI-powered, pattern-based backend microservice code generator. This document aims to guide both human and AI architects* in designing applications on Mindbricks using MScript.

*Architect refers to either AI agents or human users of Mindbricks, including product managers, business analysts, and software architects. Throughout this document, the term "architect" will be used interchangeably for both without further distinction.

First Rule

MScript values are injected directly into the generated code after basic JavaScript syntax validation. However, even if a script is syntactically correct in isolation, it may cause syntax errors depending on where it is injected.

A good rule of thumb is to ensure your script would be valid if combined with a simple JavaScript assignment operation, such as:

Your script in the project design:

...doWhen: "this.user.age > 18"

Mindbricks-generated code:

const condition = this.user.age > 18;
if (!condition) {
  throw new Error("User should be older than 18");
}

or:

if (!this.user.age > 18) {
  throw new Error("User should be older than 18");
}

You have an additional option for writing MScripts:

  • Using**module.exports**
    You can write any MScript using a module.exports statement to reduce mistakes. Mindbricks will inject the exported part into the generated code.
module.exports = (this.user.age > 18)

Valid MScript Expressions

Literals

You can use literal expressions in MScript fields, suitable for the context where they are expected.

  • Number
5

or

0.4
  • String
"premium"

Example from design JSON:

// snippet from design JSON, not generated code
userSettings.userType: "'premium'"
  • Boolean
true
  • Null identifier
null

Identifiers

You can use unreserved words as identifiers in JavaScript.

userId

this Expression

By default, MScript expressions are parsed into the methods of a class unless stated otherwise.
Using this refers to the API context class.

this

Member Expressions

You can access object properties using member expressions, provided the object is available in the scope.

Example:

user.roleId

Common usage with this:

this.projectId

or

this.organization.ownerId

Binary Expressions

You can use any binary expression that evaluates to a value.

Examples:

this.user.id == this.session.userId
5 - 2
5 + this.userAge
this.userAge ?? 18
// Not allowed
// this.userAge = 5

Conditional Expressions

Simple conditional expression:

isActive ? "yes" : "no"

More complex example:

this.user.isPremium ? this.user.paidRights > this.user.usedRights : true

Array Expressions

Array expressions are valid in MScript.
Ensure that the receiving code expects or supports an array.

Example:

["admin", "superAdmin"]

Call Expressions

You can call functions or methods that are accessible in the given scope.

Example using a method from the context:

this.getUserRole(userId)

Example using an imported function:

getUserAddressFromDb(this.userId)

A trick when you need some calculation logic before giving the expression in MScript, Define an Inline Arrow Function.

(
()=>{
  let a = null;
  // do something
  // more something
  // return the expression 
  return a;
}
)()

Object Expressions

Mindbricks expects object expressions in some MScript fields, such as in "where" clauses.

Simple object:

{
  admin: 0,
  user: 1,
  proUser: 2
}

Complex object (e.g., a where clause):

{
  "$or": [
    {
      userId: this.session.userId
    },
    {
      isProtected: false
    }
  ]
}

The Context: this

When the pattern-based architecture design in Mindbricks is converted into services, all MScript fields within the patterns are transformed into code inside the API Manager layer. This layer is implemented as a JavaScript class that manages the current API operation.

Thus, when we refer to the context, we are talking about the properties of this class. Since scripts defined in MScript fields are injected into methods of the class, you can always access the context within scripts using the this identifier.


📋 The Contents of the Context

🛤 Business API Parameters

You can directly access the Business API parameters as properties of this:

this.productId

👤 Session Data

Information about the currently logged-in user is accessible via the session object:

this.session.userId
this.session.roleId

📥 Action Results in Business API

If you have added any BusinessApiAction in your Business Logic design like FetchObjectAction, FetchStatsAction, CreateCrudAction, ApiCallAction, the result of the action call will be written to the contextPropertyName value define din the action settings.

Example for an object fetch has contextPropertyName as userCompany:

this.userCompany.name

Note: BusinessApiAction result data becomes available only after the action operation is executed.

📦 Main Operation Data

Depending on the crudType of the Business Api, the main resource data becomes available:

  • Create: Access data after creation with the name of the data object like this.customer

  • Get: Access data after retrieval with the name of the data object like this.customer

  • Delete: Access data after fetching the instance with the name of the data object like this.customer

  • Update: Access data after fetching the instance with the name of the data object like this.customer

  • List: Access data list after retrieval with the plural name of the data object like this.customers

Example:

this.customer.countryId

🛠 LIB Functions

Global library functions are accessible via the LIB global object. You do not need to use this.

LIB.getDayOfTheWeek()

🧩 Function Results

If you call a function inside your Business Api using the FunctionCallAction, the result will be available in the context using the action's contextPropertyName after the function is executed.

Example:

// assuming the contextPropertyName of a FunctionCallAction is userCount
this.userCount > 5

By consistently accessing the context with this, Mindbricks enables powerful, dynamic, and readable script logic inside your services.

📄 MScript Query Syntax Guide

Mindbricks provides a unified object syntax for writing filter queries that work across:

  • MongoDB

  • Elasticsearch

  • Sequelize (PostgreSQL, MySQL, etc.)

Using Mindbricks' MScript Query syntax, you can write one object structure, and Mindbricks services will automatically generate the platform-specific query for data fetching.


✨ Basic Structure

MScript Queries are JavaScript objects where:

  • Field names are keys.

  • Field conditions are values.

  • Operators start with a (similar to MongoDB).

Example:

{
  "age": { "$gte": 18 },
  "status": { "$eq": "active" }
}

This represents:

  • age >= 18

  • status == "active"

Multiple fields are combined with an implicit**$and** logic.

When a pattern field in Mindbricks service design expects an MScript Query, any JavaScript statement that evaluates to an MScript Query object can be used as well as setting an object value in the field. You may construct your queries in library functions and return them for use in the MScript Query field.

For example, consider the following function defined in your service functions library:

const getSearchQueryForProduct = (pName) => {
    return { "name": { "$ilike": `%${pName}%` } };
};

module.exports = getSearchQueryForProduct;

You can use it where an MScript Query is expected:

{
  "searchClause": "getSearchQueryForProduct(this.product.name)"
}

📌 Mindbricks MScript Query Cheat Sheet

🧙 Basic Comparison Operators

OperatorMeaningExample
$eqEquals{ "status": { "$eq": "active" } }
$neNot equals{ "status": { "$ne": "inactive" } }
$gtGreater than{ "age": { "$gt": 18 } }
$gteGreater or equal{ "age": { "$gte": 18 } }
$ltLess than{ "age": { "$lt": 30 } }
$lteLess or equal{ "age": { "$lte": 30 } }
$inIn list{ "role": { "$in": ["admin", "user"] } }
$ninNot in list{ "role": { "$nin": ["guest"] } }

🔍 Pattern Matching

OperatorMeaningExample
$likeLike pattern (case-sensitive){ "name": { "$like": "%john%" } }
$ilikeLike pattern (case-insensitive){ "name": { "$ilike": "%john%" } }
$nlikeNot like pattern{ "name": { "$nlike": "%bot%" } }
$nilikeNot ilike pattern{ "name": { "$nilike": "%bot%" } }
$matchMatch regex{ "description": { "$match": "sale" } }
$nmatchNot match regex{ "description": { "$nmatch": "error" } }

📏 Range and Null Checks

OperatorMeaningExample
$betweenValue between range{ "price": { "$between": [10, 100] } }
$nbetweenValue not in range{ "price": { "$nbetween": [10, 100] } }
$isnullIs null{ "deletedAt": { "$isnull": true } }
$notnullIs not null{ "deletedAt": { "$notnull": true } }

🛠 Array and String Operations

OperatorMeaningExample
$containsArray contains value{ "tags": { "$contains": "news" } }
$ncontainsArray does not contain value{ "tags": { "$ncontains": "draft" } }
$overlapArrays have common elements{ "tags": { "$overlap": ["tech", "ai"] } }
$noverlapArrays have no common elements{ "tags": { "$noverlap": ["old"] } }
$startsStarts with string{ "name": { "$starts": "Mr." } }
$nstartsDoes not start with string{ "name": { "$nstarts": "Ms." } }
$endsEnds with string{ "email": { "$ends": "@gmail.com" } }
$nendsDoes not end with string{ "email": { "$nends": "@yahoo.com" } }

🔀 Logical Operators

OperatorMeaningExample
$andAll conditions must be true{ "$and": [ {a}, {b} ] }
$orAt least one condition is true{ "$or": [ {a}, {b} ] }
$notNegates a condition{ "$not": {a} }
$norNone of the conditions are true{ "$nor": [ {a}, {b} ] }

✨ How Mindbricks Converts Your MScript Query

Depending on the target platform:

  • For Elasticsearch, it generates Elastic query DSL.

  • For Sequelize, it generates Sequelize ORM conditions.

  • For MongoDB, it generates native MongoDB queries.

Mindbricks services use their own query language internally. However, when your business logic requires custom conditions for data fetching, you can design the corresponding whereClause or query fields using your specific MScript Query conditions.

You can also customize standard CRUD database operations by modifying their queries with MScript Query syntax.


💡 Pro Tips

  • No**$eq**** needed** for simple key-value: { "age": 18 } = { "age": { "$eq": 18 } }

  • Multiple fields are automatically combined with $and.

  • Null checks ($isnull, $notnull) are platform aware.

  • Pattern search in MongoDB/Elastic uses regex internally.

  • Prefer $ilike for case-insensitive matches when available.


🌟 Full Example

{
  "$and": [
    { "status": { "$eq": "published" } },
    {
      "$or": [
        { "views": { "$gte": 1000 } },
        { "likes": { "$gte": 500 } }
      ]
    },
    { "tags": { "$overlap": ["technology", "science"] } }
  ]
}

Meaning:

  • Status is published

  • AND (Views ≥ 1000 OR Likes ≥ 500)

  • AND Tags overlap with "technology" or "science"


Mindbricks MScript Query lets you:

✅ Write once, let Mindbricks apply it across MongoDB, ElasticSearch, and Sequelize

✅ Build complex queries easily

✅ Stay close to MongoDB-style syntax while being flexible for SQL/NoSQL

✅ Focus on business logic instead of database-specific query languages!


Was this page helpful?
Built with Documentation.AI

Last updated 1 day ago