Skip to content

Instantly share code, notes, and snippets.

@mfpiccolo
Created December 16, 2018 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfpiccolo/ae395223f57bd01ad2efd224acb276f7 to your computer and use it in GitHub Desktop.
Save mfpiccolo/ae395223f57bd01ad2efd224acb276f7 to your computer and use it in GitHub Desktop.
// I am trying to decide on an API for blue-chip's virtual models.
class Owner extends BaseModel {
static get hasMany() {return [Dog]}
}
class Dog extends BaseModel {
static get belongsTo() {return [Owner]}
}
class VirtualOwner extends BaseModel {
static get hasMany() {return [VirtualDog]}
}
class VirtualDog extends BaseModel {
static get belongsTo() {return [VirtualOwner]}
}
const payload = {
data: [
{
type: "owner",
id: 1,
attributes: {
name: "John"
},
relationships: {
dogs: {
data: [{type: "dogs", id: 1}]
}
}
}
],
included: [
{
type: "dogs",
id: 1,
attributes: {
name: "Fido"
}
},
{
type: "dogs",
id: 2,
attributes: {
name: "Max"
}
}
]
};
const virtuals = {virtualDogs: 'dogs', virtualOwners: 'owners'}
// payload you want to store all instances of dogs as dogs
actions.updateResources(payload)
// payload you want to store all instances of dogs as virtualDogs
actions.updateResources(payload, {virtuals})
// Querying would stay the same
Dog.where({name: 'Fido'}).includes(['owners']).toObjects()
const vDogs = VirtualDog.where({name: 'Fido'}).includes(['virtualOwner']).toObjects();
vDogs == {
name: 'Fido',
virtualOwner: {
name: 'John'
}
}
// Do we need to support virutal relationships to real models?
const virtuals = {virtualDogs: 'dogs'}
actions.updateResources(payload, {virtuals})
cosnt vDogs = VirtualDog.where({name: 'Fido'}).includes(['owners']).toObjects();
vDogs == {
name: 'Fido',
owner: {
name: 'John'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment