Writing Simple Application with Nucleoid
Tags: programming, database, javascript, node
Published: 2020-07-07 12:51:27

---
description: Nucleoid is an open source (Apache 2.0), a declarative runtime that learns from declarative statements and runs as building own control flow without need of database.
---
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:
[https://gitlab.com/canmingir/OneQuestion.app/-/tree/master/data](https://gitlab.com/canmingir/OneQuestion.app/-/tree/master/data)

As a first step:

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

Question:

```javascript
> 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:

```javascript
> 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.

```javascript
> 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:

```javascript
> 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

```javascript
> 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`:

> 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](https://dev-to-uploads.s3.amazonaws.com/i/xm36fq9rw4tgckbh4kdw.PNG)

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

{% link https://dev.to/nucleoid/how-to-install-nucleoid-on-ubuntu-18-04-15m4 %}

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8tyrkfncgl7mbjjzo8fd.png)

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.

---

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/olwtfbfdiqz2jksoxmim.PNG)

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](https://nucleoid.org/)