Skip to content

Instantly share code, notes, and snippets.

@hpneo
Created October 7, 2020 18:27
Show Gist options
  • Save hpneo/4f749d2cff2968728f94d13041ab0c1d to your computer and use it in GitHub Desktop.
Save hpneo/4f749d2cff2968728f94d13041ab0c1d to your computer and use it in GitHub Desktop.
Many to Many in Overpass
const resources = defineResources(({ resource, belongsTo, hasMany }) => {
resource("users", () => hasMany("savedProperties"));
resource("properties"); // Could be `resource("properties", () => hasMany("savedProperties"));`
resource("savedProperties", () => belongsTo("property"));
});
return (
<OverpassProvider
api={{
apiHost: location.host,
apiPrefix: "/api",
additionalHeaders: {
"X-CSRF-Token": csrfTag?.content,
},
}}
resources={resources}
/>
);
const fetchSavedPropertiesCollection = useFetchCollection(
"savedProperties",
{
include: ["property"]
}
);
const savedProperties = useNestedCollection(
"savedProperties",
{
property: {}
}
);
const properties = savedProperties
.filter((savedProperty) => savedProperty.property)
.map((savedProperty) => savedProperty.property);
class API::PropertyResource < JSONAPI::Resource
has_many :saved_properties
end
class API::SavedPropertyResource < JSONAPI::Resource
has_one :user
has_one :property
end
class API::UserResource < JSONAPI::Resource
has_many :saved_properties
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment