HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Modeling complex JavaScript object advanced relationships

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptobjectmodelingadvancedrelationshipscomplex

Problem

> [!IMPORTANT]
>
> This article is part of a series, picking up where Modeling complex JavaScript object autoloading and console left off. If you haven't read the previous installments yet, I strongly advise you to do so to get the full context. This series is more of a show & tell hoping to <strong class="sparkles">inspire</strong> you to build your own advanced JavaScript projects.
We've already come a long way implementing an ActiveRecord-like JavaScript object system. While we've already covered models, records and queries extensively, I want to revisit the relationships between objects. The current implementation is fairly basic and I believe we can do plenty more to make it more powerful.
This time, we'll simply add a new model under our models directory. We'll also implement the relevant factory for it to match the rest of the implementation. And we'll make a few updates in the Model class and our models to support the new features.

Solution

src/
├── config/
│   └── settings.js
├── core/
│   ├── model.js
│   ├── recordSet.js
│   ├── serializer.js
│   └── factory.js
├── models/
│   ├── author.js
│   ├── category.js
│   └── post.js
├── scripts/
│   ├── autoload.js
│   └── console.js
└── serializers/
    ├── postSerializer.js
    └── postPreviewSerializer.js
spec/
└── factories/
    ├── authorFactory.js
    ├── categoryFactory.js
    └── postFactory.js


> This article is part of a series, picking up where Modeling complex JavaScript object autoloading and console left off. If you haven't read the previous installments yet, I strongly advise you to do so to get the full context. This series is more of a show & tell hoping to <strong class="sparkles">inspire</strong> you to build your own advanced JavaScript projects.
We've already come a long way implementing an ActiveRecord-like JavaScript object system. While we've already covered models, records and queries extensively, I want to revisit the relationships between objects. The current implementation is fairly basic and I believe we can do plenty more to make it more powerful.
This time, we'll simply add a new model under our models directory. We'll also implement the relevant factory for it to match the rest of the implementation. And we'll make a few updates in the Model class and our models to support the new features.
> [!TIP]
>
> You may need a code refresher before you begin and I've got you covered. The entire implementation thus far is available in the code summary of the previous article.

Code Snippets

src/
├── config/
│   └── settings.js
├── core/
│   ├── model.js
│   ├── recordSet.js
│   ├── serializer.js
│   └── factory.js
├── models/
│   ├── author.js
│   ├── category.js
│   └── post.js
├── scripts/
│   ├── autoload.js
│   └── console.js
└── serializers/
    ├── postSerializer.js
    └── postPreviewSerializer.js
spec/
└── factories/
    ├── authorFactory.js
    ├── categoryFactory.js
    └── postFactory.js
This small change will break everything, so we need to **update the models** accordingly.
### Adding the `Category` model

We'll now create a `Category` model and a relevant `CategoryFactory` to match the rest of the implementation. We'll use this model as the main example for some of our relationships later in the article.

Context

From 30-seconds-of-code: complex-object-advanced-relationships

Revisions (0)

No revisions yet.