Created
December 16, 2018 02:56
-
-
Save mfpiccolo/ae395223f57bd01ad2efd224acb276f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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