Skip to content

Instantly share code, notes, and snippets.

View jamesplease's full-sized avatar
🍕
yum

James Please jamesplease

🍕
yum
  • USA
View GitHub Profile
update('lists', {
favoriteBooks: [2, 50, 22]
}, {
resourceType: 'books'
});
update('resources', [2, 50, 10], {
defaultData: {
resourceType: 'books',
meta: {
// Originally, it was like this
{
resources: {
books: {
// This shit is secret, and should not be exposed to the users.
__internalInfoAboutBooks: {},
resources: {
24: { ... },
getResources('books', {
filter: {
firstName: 'javi'
}
});
getResources('books', {
filter: resource => resource.meta.isSelected
});
// Deep merge
update('resources.books', {
24: {
attributes: {
firstName: 'james'
}
}
});
// Do some shit
// The "metadata" layer – the first layer after the resourceType or
// list name, is "skipped" in the pathing syntax, as users are not able
// to modify the store at that layer.
{
resources: {
books: {
resources: {
24: { ... },
50: { ... }
{
id,
resourceType,
attributes,
meta,
computedAttributes
}
// Given a flat resource and its schema, returns a full resource.
export default function fullFromFlat({ resource, schema }) {
const attributes = Object.values(schema.attributes);
const metas = Object.values(schema.meta);
const newAttributes = {};
attributes.map(attribute => {
if (resource[attribute]) {
newAttributes[attribute] = resource[attribute];
}
{
// These are the resource types in this relationship.
// In this case, both sides of the relationship are people.
resourceTypes: ['people'],
relationshipSchema: {
fields: {
father: {
resourceType: 'people',
},
@jamesplease
jamesplease / is-equal.js
Last active April 28, 2018 17:40
Serializable is-equal
export default function isEqual(x, y) {
// Handles primitives and exact object matches
if (x === y) {
return true;
}
// We can only handle comparing "regular" objects and
// arrays; everything else is considered not equal.
else if (
((x && x.constructor === Object) || Array.isArray(x)) &&
@jamesplease
jamesplease / update-api.js
Last active April 28, 2018 00:10
This API lets you "drill" into what you are trying to update.
update({
books: {
24: {
name: 'Lord of the Rings'
}
}
});
update('books', {
24: {