firecms icon indicating copy to clipboard operation
firecms copied to clipboard

Using a sub collection as collection in main menu

Open kongweiying2 opened this issue 4 years ago • 4 comments

Is it possible to use buildCollection with a schema that refers to a sub collection?

Basically I have something like this

type Match = {
    events: Event[]
}

type Event = {
   eventName: string
}

One collection is Matches, which is located at /matches Another is Events which is a sub collection at /matches/{matchId}/events

I'd like both to show up as main menu collections, despite Events being a sub collection.

I understand I have to create a schema for Event, and then pass it to buildCollection. But I'd like to clarify two questions:

  1. How do I reference another schema using the array datatype? Currently it looks like you have to manually define the object shape in the schema using a map, you can't use another schema as a datatype, unless I've missed something. Sanity also uses a schema and they support the "object" datatype, which essentially refers to another custom schema. So that you can compose different schemas you've previously defined.

Maybe something like this?

export const eventSchema = buildSchema<FirestoreEventT>({
  name: "Event",
  properties: {
    eventName: {
      title: "Event Name",
      dataType: "string",
    }
  }

})

export const matchSchema = buildSchema<FirestoreMatchT>({
  name: "Match",
  properties: {
    events: {
      title: "Events",
      dataType: "array",
      of: eventSchema
    },
  },
});
  1. How would one define the path for a sub collection in buildCollection? Normally you would just go something like "events", but if we're using a sub collection, I'm not sure what the format would be. Maybe something like "matches/{match_id}/events". I did see your response here but it seems like that referred to a specific document: https://github.com/Camberi/firecms/issues/57

As always, great library, love the new changes in the 1.0.0 beta!

kongweiying2 avatar Dec 22 '21 00:12 kongweiying2

  1. You can't use the schema as an array of prop, but you can isolate the schema properties using buildProperties and then reuse those:
const eventProperties = buildProperties({
    eventName: {
        title: "Event Name",
        dataType: "string",
    }
});

export const eventSchema = buildSchema({
    name: "Event",
    properties: eventProperties
})

export const matchSchema = buildSchema({
    name: "Match",
    properties: {
        events: {
            title: "Events",
            dataType: "array",
            of: { dataType: "map", properties: eventProperties }
        },
    },
});
  1. Regarding this one, I think what you are looking for is a collection group query? https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query We don't support them yet but may consider it for the future!

fgatti675 avatar Dec 28 '21 20:12 fgatti675

Perfect, thank you! Are there any particular blockers to getting collection group queries working? I may be able to take a look at them and open a PR.

kongweiying2 avatar Feb 02 '22 06:02 kongweiying2

Hi @fgatti675 any word on support for collection group queries in the future? Or maybe an explanation of the issue so that we can make a PR.

dhazeley avatar Apr 29 '22 03:04 dhazeley

Hi @dhazeley We don't have immediate plans to support this feature in the near future. If it gets requested enough we will give it priority over other features. There are no real blockers for implementing PRs! If you decide to go for it, I would start from the version 2.0.0 branch master

fgatti675 avatar Jun 14 '22 18:06 fgatti675

This is now possible as described here: https://firecms.co/docs/recipes/documents_as_subcollections

fgatti675 avatar Jan 24 '23 21:01 fgatti675