Background
I have an app for game collecting. In this context, the properties of the game itself are not necessarily important. Instead, we really care about the actual different releases of an individual game. We might have a PS4 version or an Xbox version, or we might have a European version vs. a Japanese version.
So, I have a couple of Sequelize models like this (truncated/modified for brevity):
const Game = sequelize.define('Game', {
title: { type: DataTypes.STRING },
});
const Release = sequelize.define('Release', {
region: { type: DataTypes.STRING },
platform: { type: DataTypes.STRING },
releaseDate: { type: DataTypes.Date },
});
And of course later I associate them…
Release.belongsTo(Game);
Game.hasMany(Releases);
This all works great! Everything in the app is going swimmingly.
The Problem
I wanted to a way to modify data in my app without directly accessing the database, but before I’d actually finished every single CRUD route in my app. So I went with AdminJS, hoping it would be similar to something like Django Admin.
When going to add a Release to another object, however (like a User Collection) the options are the IDs of the individual records. These are not helpful at all and don’t mean anything. Ok, great, it looks like AdminJS automatically looks for things like name
or title
to use here.
So, my first thought was to add a Sequelize Virtual field to cover this:
title: {
type: DataTypes.VIRTUAL,
get() {
// uh-oh
}
}
And here we run into a problem. None of the fields of this object uniquely identify it! The thing that uniquely identifies it is composite with the Game object.
What I Want
What I would like to happen, is that the title of a Release is something like: ${this.Game.title} ${this.platform} ${this.region}
and that I can choose that from a drop-down when creating new records.
However, as far as I know, it is not possible to have access to the associated record here. I can’t use a Promise to actually load the associated record in the get
function (not that I think I’d want to, that would cause mandatory n+1 issues or worse?) and so my expectation is that this won’t be something in Sequelize, but rather AdminJS.
I have looked through the documentation on Properties and Resources and don’t really see anything for changing what’s actually being displayed at all. There are options for sorting and visibility and so on, which are useful, but not what I’m trying to do.
I have also searched question after question here and elsewhere about getting related models in a Sequelize Model get
function, but most of them remain unanswered. (Or just simply list model.Model.property
as if it works?)
So my question is:
Is this possible? Can I have an AdminJS Title composed of another record’s property?