Writing Simple Application with Nucleoid

Can Mingir
Jul 7 · 4 min read
Image for post
Image for post

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:

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

Review:

  • 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:

Question:

> class Question {
constructor(text) {
this.text = text;
}
};
> if( !/.{5,50}/.test(Question.text) ){
throw "INVALID_QUESTION";
};
> Question.rate = 0;> Question.count = 0;

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

Review:

> class Review {
constructor(question, rate) {
this.question = question;
this.rate = rate
}
};
> if( Review.rate > 5 || Review.rate < 1 ) {
throw "INVALID_REVIEW"
}
> Review.timestamp = Date.now();> {
let question = Review.question;
question.rate = ( question.rate * question.count + Review.rate ) / ( question.count + 1 );
question.count = question.count + 1;
};

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 }

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 }

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

Exceptions

> new Question("Test")
"INVALID_QUESTION"

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:

> new Review(q1, 6)
"INVALID_REVIEW"

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.

Image for post
Image for post

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

Image for post
Image for post

In this configuration, REST request to API Gateway as

POST /questions
host: api-gateway.amazonaws.com
{ "text": "How was the service today?" }

is translated into:

POST /
host: onequestion.app
new Question("How was the service today?");

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.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

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 nucleoid.org

Nucleoid

Declarative Runtime System

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store