DEV Community

Can Mingir for Nucleoid

Posted on

5 2

Storing Classes and Objects

Class

First thing first, classes require to be registered before used in Nucleoid:

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

nucleoid.register(Order);
Enter fullscreen mode Exit fullscreen mode

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;
});
Enter fullscreen mode Exit fullscreen mode

and it can retrieve by its var variable as mentioned earlier.

app.get("/orders", () => {
  return order;
});
Enter fullscreen mode Exit fullscreen mode
{
  "id": "order0",
  "item": "ITEM-123",
  "qty": 1
}
Enter fullscreen mode Exit fullscreen mode

if object initiated without assigning var variable, the runtime automatically assign var variable along with id

app.post("/test", () => new Order("ITEM-123", 3));
Enter fullscreen mode Exit fullscreen mode
{
  "id": "order0",
  "item": "ITEM-123",
  "qty": 1
}
Enter fullscreen mode Exit fullscreen mode

đź’ˇ id of object is always the same to its global var so that either can be used to retrieve the object like
Order["order0"] and order0.

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;
});
Enter fullscreen mode Exit fullscreen mode
{
  "id": "order1",
  "item": "ITEM-123",
  "qty": 1
}
Enter fullscreen mode Exit fullscreen mode

Now, it can use its id as var in order to retrieve the object

app.get("/test", () => order1);
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay