Learn
What is Nucleoid?
Nucleoid is a low-code framework, which uses symbolic AI, 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 storing in the built-in data store, so that your application doesn't require external database or anything else.
..but why?
Even simple applications today require lots of coding, libraries, tuning etc., and the majority of them are technical requirements rather than business logic. The declarative programming approach used by Nucleoid abstracts away the technical details and allows developers to write code that expresses the desired behavior without needing to worry about low-level implementation details.
Nucleoid reduces the number of code lines required to build an application by providing a declarative runtime environment. The environment allows developers to declaratively describe the behavior of their program, rather than writing out all the technical instructions required to implement the behavior. This means that developers can focus on defining the desired behavior of their program, rather than writing low-level technical instructions, leading to more concise and manageable code, which also aligns with modern software development methodologies that focus on functional requirements and encapsulating technical details.
Hello World
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 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);
You run your business logic and successfully persisted your object only with this 👆
Just the reminder, you don't need external database,
const app = nucleoid()
will do the magic.
Setting up
Nucleoid runtime runs top of Node.js, it can be installed as a npm package:
> npm i nucleoidjs
Initialize the runtime with this:
const nucleoid = require("nucleoidjs");
const app = nucleoid();
💡 The runtime uses local files for internal data management, and stores every statement locally. This clears local data:
> npx nucleoidjs clear
Variables
As defined in JavaScript, variables of var
, let
and const
can be used, but only difference is var
is stored automatically,
in meanwhile, let
and const
are temporary to its block
app.post("/test", () => {
var a = 1;
return a;
});
app.get("/test", () => {
return a;
});
💡 Variable definitions without identifier like
a = 1
are upsert operation, also automatically stored.
Class
Classes require to be registered before used in Nucleoid:
class Order {
constructor(item, qty) {
this.item = item;
this.qty = qty;
}
}
nucleoid.register(Order);
Objects
The same thing for objects, once initiated and assigned to the var
variable as well as it stored.
app.post("/orders", () => {
var order = new Order("ITEM-123", 3);
return order;
});
and it can retrieve by its var
variable as mentioned earlier.
app.get("/orders", () => {
return order;
});
{
"id": "order0",
"item": "ITEM-123",
"qty": 3
}
if object initiated without assigning var
variable, the runtime automatically assign var
variable along with id
app.post("/test", () => new Order("ITEM-123", 3));
{
"id": "order0",
"item": "ITEM-123",
"qty": 3
}
💡
id
of object is always the same to its globalvar
so that either can be used to retrieve the object likeOrder["order0"]
andorder0
.
if object assigned to either let
or const
, the runtime will create var
variable the same as its id
app.post("/orders", () => {
const order = new Order("ITEM-123", 3);
return order;
});
{
"id": "order1",
"item": "ITEM-123",
"qty": 3
}
Now, it can use its id
as var
in order to retrieve the object
app.get("/test", () => order1);
Properties
Properties can be defined as part of its var
, and it will store along with object.
app.post("/test", () => {
var order = new Order("ITEM-123", 3);
order.details = "some details";
return order;
});
or it can be part of let
or const
app.post("/test", () => {
const order = new Order("ITEM-123", 3);
order.details = "some details";
return order;
});
They will be part of JSON when retrieved as in standard JavaScript
app.get("/test", () => order1);
{
"id": "order1",
"item": "ITEM-123",
"qty": 3,
"details": "some details"
}
or can be part of constructor as seen in previous examples
class Order {
constructor(item, qty) {
this.item = item;
this.qty = qty;
}
}
nucleoid.register(Order);
app.post("/test", () => new Order("ITEM-123", 3));
Query
Once a class registered, the runtime creates list of instance under its name. For example:
class User {
constructor(username) {
this.username = username;
this.active = true;
this.created = Date.now();
}
}
nucleoid.register(User);
// Retrieves active users 👇
app.get("/users", () => User.filter((u) => u.active == true));
// Retrieves the student 👇
app.get("/users", () => User.find((u) => u.username == "canmingir"));
or lodash functions can be used 🥳
app.get("/users", () => _.map(User, (u) => ({ x: u.username })));
Scope
Nucleoid can be used without built-in API routes as well, but in order to do that, outside data needs to pass in as a
scope in your project an call nucleoid.run( fn )
and it will return the value.
const scope = { userId: "USER-1" };
const user = nucleoid.run((scope) => new User(scope.userId), scope);
console.log(user);
Terminal
Nucleoid also opens terminal channel at 8448
port for queries like in SQL, so that you can write code snippet for data operations