Class-based Data Model in JavaScript
Tags: javascript
Published: 2019-06-21 20:25:40

# *Foreword*

This post is a direct descendant to another post I made around 6 months ago at the time of writing. It was poorly explained, and required direct class parents to be reinstated through a new instance of the target. I do hope this is an improvement.

# Concept

The base model *has the behavior* of a plugin handler that calls upon its child classes. Though it is not that, I'm sure someone would be more than willing to make it if their project needs it.

# Scenario

In the ‘source’ reference, there are 3 classes, 2 of which accept the first as a constructor parameter. They further contain get descriptors that reference to the parent where the primary data set is found.

As a result, you are given 2 paths to the data (from User context).

1) `this.data.admin`: raw unmutated data set connected to the instance.
2) `this.admin.data`: ‘child’ class descriptor still connected to primary reference

From a base class perspective there isn’t much difference between the two, but from the child class perspective if you were to access the dataset without descriptors you would be doing `this.user.data.admin` rather than `this.data`. A shortcut if anything else.

The subtle difference between them is of little importance at first, but as your app is likely to evolve, it can allow for data mutations without affecting the data itself. How you choose to mutate it and what you do with it, is for you to decide.

## UserInventory handling

There are several ways of handling inventory data.

- Your standard object where `{[itemID: string]: number}` is creating an ID map to the amount in the inventory, very simple item storage if you do it right.
- Same as the object above but reference to a secondary model instead of a number.
- Using an array where each of the item meta is independent from each other.
- Using a 2 dimensional array where the itemID is left-joined from another reference, the 2nd value is the amount and the 3rd could be additional item meta. *You can switch it round, or drop one; the other or both if you’d like and just have the itemID reference to another part of your app.*

## ~~Static~~ Instance get descriptors for default object model

The purpose of this is to prevent anyone from ‘accidentally’ changing the default model while the application is running. That’s all. *I also like it when my editor knows what I’m trying to do without the extra comments... and interpretation or guesses from others trying to use it for themselves.*

# References

**Original post:**

{% post junior/random-circular-class-recurrence-1jmn %}

{% gist https://gist.github.com/sudojunior/80599b8c3b200e41dd06a1da20552bb5 %}

EDIT (October 8th 2019): A GitHub repository has been created.

{% github PlayTheFallen/class-based-programming %}

*I am not claiming this as my idea, nor do I know of anyone who has done this successfully before me. I am simply using the resources provided to the community to create an application without the need for an ORM, but still provides the flexibility to move forward with features while allowing space to expand and deprecate.*

*That said, I do welcome comments to further improve this model of class-based programming in JavaScript.*

# Final words

Similar things can be done in Python, C#, Java / Kotlin, Rust, etc. I'll see what I can do to make them myself, but I welcome you to port them over or improve this further as a challenge... if you so wish to accept it.

I wish you the best of luck in that, whatever you choose to do,
Junior

![](https://cdn.discordapp.com/attachments/323921288974303233/561895750536003585/Rattmann.jpg)