strapi-connector-firestore icon indicating copy to clipboard operation
strapi-connector-firestore copied to clipboard

Feature: Populate designated fields when storing references

Open brettwillis opened this issue 4 years ago • 2 comments

Relations are stored as references in Firestore database. In some situations (when accessing the database natively, not via the Strapi API) it may be convenient to have certain values from the target document stored alongside the entry.

In such a way, key data from the relation would be immediately available without need to fetch the target document.

brettwillis avatar Jan 24 '21 21:01 brettwillis

Proposed configuration API, add a populate field to relation attributes:

{
  attributes: {
    owner: {
      model: "owner",
      autoPopulate: true, // Not to be confused with existing config for REST API
      populate: ["name"] // "name" refers to an attribute on the "owners" model
    }
  }
}

When an entity of such a model is stored, it may look like:

{
  owner: {
    ref: '/owners/abcd1234', // Original DocumentReference
    name: "John Doe" // Current value of the the "name" field on the target document
  }
}

Cannot populate attributes with reserved names: "ref", "id" (conflicts with DeepReference) or "filter" (conflicts with MorphReference).

Such functionality only impacts how the data is stored, and it has no impact on API responses.

brettwillis avatar Jan 24 '21 22:01 brettwillis

How to handle or support "deep" populating?

Consider if the owner model has a photo relation, and we want the photo's url populated on the original object, and customise the name of the field. E.g.

{
  attributes: {
    owner: {
      model: "owner",
      populate: {
        name: 'name',
        photoUrl: 'photo.url'
      }
    }
  }
}

When an entity of such a model is stored, it may look like:

{
  owner: {
    ref: '/owners/abcd1234', // Original DocumentReference
    name: "John Doe", // Current value of the the "name" field on the target document
    photoUrl: "https://..." // Current value of the "url" field on the target of the current value of the "photo" field of on the target document
  }
}

brettwillis avatar Jan 25 '21 21:01 brettwillis