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

Method to get document only publish status #171

Closed maztak closed 3 years ago

maztak commented 3 years ago

By flamelink JavaScript SDK, we can't get documents that only published. I discussed with flamelink support team, and I try this code but I got null.

export async function getCategories() {
  const _categories = await app.content
    .get({
      schemaKey: "categories",
      fields: ["id", "slug", "name", 'imageUrl', '_fl_meta_.status'],
      // filters: [ ['_fl_meta_.status', '==', 'publish'] ],
      filters: [ ['_fl_meta_.status', 'in', ['publish', undefined]] ],
    })
  console.log('_categories: ', _categories)
  return Object.values(_categories)
}

so I use this code

export async function getCategories() {
  const _categories = await app.content
    .get({
      schemaKey: "categories",
      fields: ["id", "slug", "name", 'imageUrl', '_fl_meta_.status'],      
    })
  // console.log('_categories: ', _categories)

  const publishedValues = [];
  Object.keys(_categories).map(key => {
    const value = _categories[key]
    // console.log('value._fl_meta_: ', value._fl_meta_)
    if ( (value._fl_meta_ === undefined) || (value._fl_meta_.status == 'publish') ) {
      publishedValues.push(value);
    }
  })
  // console.log('publishedValues: ', publishedValues);
  return publishedValues
}

Note

To use the undefined filter, we have to set ignoreUndefinedProperties to true.

import * as admin from "firebase-admin"
import firebase from 'firebase/app'
import 'firebase/firestore'
import 'firebase/storage'
import flamelink from "flamelink/app"
import "flamelink/cf/content"
import "flamelink/cf/storage"

let firebaseApp

if (typeof window === "undefined") {
  console.log("Running on server-side..")
  if (!admin.apps.length) {
    firebaseApp = admin.initializeApp({
      credential: admin.credential.cert({
        projectId: process.env.FSA_PROJECT_ID,
        privateKey: process.env.FSA_PRIVATE_KEY.replace(/\\n/g, "\n"),
        clientEmail: process.env.FSA_CLIENT_EMAIL,
      }),
      databaseURL: process.env.FIREBASE_DATABASE_URL,
      storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
    })
    firebaseApp.firestore().settings({
      ignoreUndefinedProperties: true,
    })
  } else {
    firebaseApp = admin.app()
  }
} else {
  console.log("Running on client-side..")
  if (!firebase.apps.length) {
    firebaseApp = firebase.initializeApp({
      apiKey: process.env.FIREBASE_API_KEY,
      authDomain: process.env.FIREBASE_AUTH_DOMAIN,
      databaseURL: process.env.FIREBASE_DATABASE_URL,
      projectId: process.env.FIREBASE_PROJECT_ID,
      storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
      messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
    })
  } else {
    firebaseApp = firebase.app()
  }
}

export const app = flamelink({ firebaseApp, dbType: "cf" })
gitdubz commented 3 years ago

Hi @maztak

You are not able to query on values that do not exist unfortunately for Firestore. If the value does not exist it is not indexed and can not be queried.

More here https://stackoverflow.com/questions/59483600/how-to-form-firestore-query-matching-with-undefined-elements

You can use the bulk update feature to set the relevant status on your entries within Flamelink.

image

The ignoreUndefinedProperties only removes undefined values from your queries as opposed to failing the API request with an error. So it basically sanitizes the queries for you.

maztak commented 3 years ago

Yeah. Thanks reply. So anyway we have to bulk update each time when we added contents,right?

I think if flamelink add stasu field as publish, we can get data with filter property smooth.

I’m glad to you to consider. Best.

iPhoneから送信

2021/06/15 18:11、De Wet van der Merwe @.***>のメール:

 Hi @maztak

You are not able to query on values that do not exist unfortunately for Firestore. If the value does not exist it is not indexed and can not be queried.

More here https://stackoverflow.com/questions/59483600/how-to-form-firestore-query-matching-with-undefined-elements

You can use the bulk update feature to set the relevant status on your entries within Flamelink.

The ignoreUndefinedProperties only removes undefined values from your queries as opposed to failing the API request with an error. So it basically sanitizes the queries for you.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

gitdubz commented 3 years ago

If you are adding the content via the CMS you can simply choose the status when adding it.

image