Nucleoid: Low-code Framework for Node.js
Tags: node, javascript, lowcode, ai
Published: 2022-03-02 17:55:00

---
series: Tutorial
description: Nucleoid is low-code framework for Node.js, lets you build your APIs with the help of AI and built-in datastore in declarative runtime environment
---

## What is Nucleoid?

[Nucleoid](https://github.com/NucleoidJS/Nucleoid) is an AI-managed low-code framework, which uses symbolic (logic-based) AI model, tracks given statements in JavaScript and creates relationships between variables, objects, and functions etc. in the graph. So, as writing just like any other codes in Node.js, the runtime translates your business logic to fully working application by managing the JS state as well as stores in the built-in data store, so that your application doesn't require external database or anything else.


![Nucleoid Graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acmahmr7igx4et2ww8h4.png)

### How it works

I. Write your business logic in JavaScript

II. Nucleoid runtime renders your codes

III. Creates APIs with the built-in datastore

## Hello World

```shell
> npm i nucleoidjs
```

Once installed, you can simply run with Express.js

```javascript
const nucleoid = require("nucleoidjs");
const app = nucleoid();

class Item {
  constructor(name, barcode) {
    this.name = name;
    this.barcode = barcode;
  }
}
nucleoid.register(Item);

// 👍 Only needed a business logic and 💖
// "Create an item with given name and barcode,
// but the barcode must be unique"
app.post("/items", (req) => {
  const name = req.body.name;
  const barcode = req.body.barcode;

  const check = Item.find((i) => i.barcode === barcode);

  if (check) {
    throw "DUPLICATE_BARCODE";
  }

  return new Item(name, barcode);
});

app.listen(3000);
```

This is pretty much it, thanks to the Nucleoid runtime, only with this :point_up_2:, you successfully persisted your first object with the business logic :sunglasses:

### OpenAPI Integration with Nucleoid IDE

Nucleoid IDE is a web interface that helps to run very same npm package with OpenAPI.

[![Nuceoid IDE](https://img.shields.io/badge/Nucleoid-IDE-green?style=for-the-badge&logo=apache)](https://nucleoid.com/ide/)

![Nucleoid IDE 1](https://cdn.nucleoid.com/media/screenshot-1.png)

![Nucleoid IDE 2](https://cdn.nucleoid.com/media/screenshot-2.png)

let's breaking it down:

### Defining a class

Just like classes in JS but requires to be registered before used inside Nucleoid:

```javascript
class Order {
  constructor(item, qty) {
    this.item = item;
    this.qty = qty;
  }
}

nucleoid.register(Order);
```

### API & Business Logic

```javascript
app.post("/orders", () => new Order("ITEM-123", 3));
```
```json
{
  "id": "order0",
  "item": "ITEM-123",
  "qty": 3
}
```

Once REST called, there are couple things happen. First of all, `new Order("ITEM-123", 3)` generates unique `id` becomes part of the object as well as JSON, and that `id` can be used to retrieve the object later on. In addition, <u>the order instance is stored automatically by the runtime</u> without requiring external database.

```javascript
// Retrieve order object with "order0" id
app.post("/orders", () => Order["order0"]);
```

Another thing Nucleoid does when defining a class, it creates list under the class name and when an object initiated, it also stores inside the list.

### Query

Queries also can be done like SQL, but in JS :sunglasses:

```javascript
app.get("/orders", () => Order.filter((o) => o.item == "ITEM-123"));

app.get("/orders", () => Order.find((o) => o.id == "order0"));
```

or you can lodash it :smile:

```javascript
app.get("/orders", () => _.map(Order, (o) => ({ x: o.id, y: o.qty})));
```

## Passing HTTP info

Let's add little bit coloring with HTTP info, and make it more real :fire:

Nucleoid uses Express.js and passes `req` as `{ params, query, body }`

```javascript
app.post("/orders", (req) => new Order(req.body.item, req.body.qty));

app.get("/users/:user", (req) => User[req.params.user]);
```

> :bulb: The return value of the function automatically converted
> to JSON as shorted of `res.send(object)` in Express.js

and let's add some business logic:

```javascript
app.post("/orders", (req) => {
  const qty = req.body.qty;
  const item = req.body.item;

  if(qty < 0) {
    throw "INVALID_QTY"
  }

  if(item == "ITEM-123" && qty > 3) {
    throw "QTY_LIMITATION"
  }

  return new Order(item, qty);
});

```

---

Thanks to declarative programming, we have a brand-new approach to data and logic. As we are still discovering what we can do with this powerful programming model, please join us with any types of contribution!

<center>
  Learn more at <a href="https://github.com/NucleoidJS/Nucleoid">https://github.com/NucleoidJS/Nucleoid</a>
</center>

[![Nucleoid Logo](https://cdn.nucleoid.com/media/icon.png)](https://nucleoid.com)