flamelink / flamelink-js-sdk

🦊 Official Flamelink JavaScript SDK for both the Firebase Realtime database and Cloud Firestore
https://flamelink.github.io/flamelink-js-sdk
MIT License
43 stars 5 forks source link

get content question #100

Closed Bickson closed 3 years ago

Bickson commented 5 years ago

Hi, is it possible to get content based on attribute in referenced schema? say I want all my products based on category and my product schema has reference to category schema. Sorry if i explain poorly!

Thanks!

jperasmus commented 5 years ago

I haven't checked now, but it depends on the order in which we populate the references and applying the filters.

You can try to write your query like this:

const result = await app.content.get({
    schemaKey: 'yourSchema',
    populate: true, // to expand the references
    filters: [
        ['_fl_meta_.schema.category', '==', 'someValue'] // assuming the field is called "category"
    ]
})
Bickson commented 5 years ago

hmm... doesn't seem to work. products have a field called category which is a reference to schema category that have a name. can I get product dependent on category name?

gitdubz commented 4 years ago

Hi @Bickson I am not sure if you have managed to resolve the issue yet but you can try this

const result = await app.content.get({ schemaKey: 'yourSchema', populate: true, // to expand the references filters: [ ['category', '==', 'someValue'] // assuming the field is called "category" ] })

erik-slack commented 4 years ago

@gitdubz that was very helpful! I don't see it in the documentation though. Can it be updated?

https://flamelink.github.io/flamelink-js-sdk/#/content?id=subscribe

gitdubz commented 3 years ago

@erik-slack @Bickson

Documentation for using filters have been added for both relational and non relational values. Please see (sections including filters)

Regarding the issue in questions the following can be done for reference/relational data

// get the category by name
app.content
  .get({
    schemaKey: 'category',
    filters: [['name', '==', 'Super Awesome']]
  })
  .then(category => {
    // get the category reference
    for (let id in category) {
      app.content
        .ref(['category', id]) // [schemaKey, entryId]
        .get()
        .then(categoryRef => {
          // get the blog posts filtered on the category reference
          app.content
            .get({
              schemaKey: 'blogPosts',
              populate: true,
              filters: [['category', '==', categoryRef.docs[0].ref]]
            })
            .then(blogPosts => console.log('All the blog posts:', blogPosts))
            .catch(error =>
              console.error(
                'Something went wrong retrieving posts for the given category',
                error
              )
            )
        })
        .catch(error =>
          console.error(
            'Something went wrong retrieving the category ref',
            error
          )
        )
      break
    }
  })
  .catch(error =>
    console.error('Something went wrong retrieving a category', error)
  ) 

TLDR;

const category = await app.content.get({
  schemaKey: "category",
  filters: [["name", "==", "Super Awesome"]],
});

let categoryRef;
for (let id in category) {
  categoryRef = await app.content.ref(["category", id]).get();
  break;
}

const posts = await app.content.get({
  schemaKey: "blogPosts",
  populate: true,
  filters: [["category", "==", categoryRef.docs[0].ref]],
});