DEV Community

Cover image for Writing Simple Application with Nucleoid
Can Mingir for Nucleoid

Posted on

Writing Simple Application with Nucleoid

Nucleoid is open source (Apache 2.0), a declarative runtime that learns from declarative statements and runs as building own control flow. This approach lower number of code lines required without need of compile, and at the same time, it stores statements so that it doesn’t require external database.

Reminder: There are no files in Nucleoid, just statements in JavaScript inserted in the system like SQL statements for database.

Let’s say we are targeting those features:


  • Question must be between 5 and 50 character long.
  • Question initializes its count and rate as 0.


  • Review must have a question assigned.
  • Review’a rate must be between 1 and 5
  • Review’s date is when time created.
  • Each review recalculates its question rate and increases its question’s count by 1.

You can download full example here:

As a first step:

We need to define domains as well as their class level declarations based on requirements:


> class Question {
    constructor(text) {
      this.text = text;

> if( !/.{5,50}/.test(Question.text) ){

> Question.rate = 0;

> Question.count = 0;
Enter fullscreen mode Exit fullscreen mode

if(!/.{5,50}/.test(Question.text) { .. } block runs regular expression to test the text of Question.


> class Review {
   constructor(question, rate) {
     this.question = question;
     this.rate = rate

> if( Review.rate > 5 || Review.rate < 1 ) {
    throw "INVALID_REVIEW"

> Review.timestamp =;

> {
    let question = Review.question;
    question.rate = ( question.rate * question.count + Review.rate ) / ( question.count + 1 );
    question.count =  question.count + 1;
Enter fullscreen mode Exit fullscreen mode

Last block calculates the average rate of questions.

Let’s initialize some instance of Question and Review, those declarations above automatically applied by runtime.

> question1 = new Question("How was the service today?");
> question1
{"id":"question1", "text": "How was the service today?", rate:0, count:0 }
Enter fullscreen mode Exit fullscreen mode

At final step, we will rate questions as creating Review:

> new Review(q1, 3)
> new Review(q1, 4)
> new Review(q1, 5)
> question1
{"id":"question1", "text": "How was the service today?", rate:4, count:3 }
Enter fullscreen mode Exit fullscreen mode

question1 is recalculated after three reviews by the runtime, so at a developer standpoint, it doesn’t require to call any functions.


> new Question("Test")
Enter fullscreen mode Exit fullscreen mode

By its requirements, a text of a question must be bigger than 5 characters, and there is a declaration as if(!/.{5,50}/.test(Question.text) { .. } so that "INVALID_QUESTION" is thrown.

Same as for review, its rate cannot be 6:

In case of failure, the runtime rollbacks transaction with changing all variables/properties to original values.

API Gateway

So far, we have established statements to run behaviors of program, but in order to integrate with a browser or smart phone, we need an API Gateway.

Alt Text

In this example, we will use a service from AWS and Nucleoid is installed at that accepting POST requests.

Alt Text

In this configuration, REST request to API Gateway as

POST /questions


{ "text": "How was the service today?" }
Enter fullscreen mode Exit fullscreen mode

is translated into:


new Question("How was the service today?");
Enter fullscreen mode Exit fullscreen mode

This triggers steps mentioned above, and the same flow is followed for other REST resources. The bottom line, whatever we can do with terminal, it can easily port to API Gateways, so that, those can be used in production.

You may already realize, there are correlation between user stories and declarative statements in term of sequence and count because both followed formal logic.

In conclusion, using declarative statements in Nucleoid dramatically lowers number of code lines without need of database since the runtime is able to store data at the same time, so that, this approach simplifies system architecture as eliminating database and cache server, and let developers focus on functionalities.

Alt Text

Nucleoid is open source (Apache 2.0), a runtime environment that allows declarative programming written in ES6 (JavaScript) syntax. Since statements are declarative, the runtime provides logical integrity and persistency as hiding technical details.

Learn more at

Top comments (0)