DEV Community

SudoJunior
SudoJunior

Posted on • Updated on

Class-based Data Model in JavaScript

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:

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

GitHub logo sudojunior / class-based-programming

Data model construction on class abstractions (for languages that have them).

class-based-programming

Data model construction on class abstractions (for languages that have them).

For the purpose of demonstration, the files contained in this project will not have anything resembling solution metadata. Namely languages including, but not limited to, JavaScript (package.json, package-lock.json, yarn.lock); C# / F# / VB; Rust and Ruby.

Primary Ideals

Since there is more than one way to do this, each of the possibilities opens up both good and bad features of each language found here. Not all of these are possible in certain languages, given that they not have access to features used in sibling languages.

  • Independant
    • [-] Data caching unlikely
  • One File
    • [~] Depends on module used
    • [-] Asset access may be limited, dependant on export method.
  • Recall Parent
    • [+] Automatic garbage collection on unused assets
    • [-] Higher rate of class instancing
  • Recall Parent (Instance)
    • [-] Likely to cause memory leaks
    • [-] Data loss possible



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

Top comments (0)