Coder Social home page Coder Social logo

Comments (12)

acupofjose avatar acupofjose commented on August 15, 2024 1

Ah, one more modification then! You'll have to await the calls in FirestoreHandler.ts

So anywhere there is:

body = this.reference.transform.call(/*...*/)

it should be changed to:

body = await this.reference.transform.call(/* .... */)

I think that oughtta do it

from elasticstore.

acupofjose avatar acupofjose commented on August 15, 2024

Hey @butaminas - I haven't tried it myself, but you should be able to do this via the transform property.

Something like:

{
// ...
transform: async (data, parent) => {
      var user = await parent.ref.firestore.collection("users").doc(data.user_id).get()
      if (user.exists) {
        return { ...data, ...user.data() }
      } else {
        return { ...data }
      }
    }
// ...
}

Be aware that this will require a db lookup for every comment that's inserted.

Anyway, that should work? At least, it should give you an idea of where to go with it.

from elasticstore.

butaminas avatar butaminas commented on August 15, 2024

@acupofjose this does make sense and I think it should work.
However, parent is undefined in my case. Not really sure why, just started with this library.

from elasticstore.

acupofjose avatar acupofjose commented on August 15, 2024

Ah whoops, parent will only be set from a subcollection query. You'll have to import firestore.

Something like:

import firebase from "firebase/app"
import "firebase/firestore"

{
// ...
transform: async (data, parent) => {
      var user = await firestore.collection("users").doc(data.user_id).get()
      if (user.exists) {
        return { ...data, ...user.data() }
      } else {
        return { ...data }
      }
    }
// ...
}

from elasticstore.

butaminas avatar butaminas commented on August 15, 2024

@acupofjose This almost works. Only had to import firebase-admin like this import * as admin from "firebase-admin" and then access firestore like this admin.firestore().

However, whenever I try to run transform with async I always get empty hits no matter what. 
Even if I use transform like this:

transform: async (data, parent) => {
      return { ...data }
}

from elasticstore.

butaminas avatar butaminas commented on August 15, 2024

@acupofjose that was exactly it! Thanks!

from elasticstore.

butaminas avatar butaminas commented on August 15, 2024

@acupofjose I just realized that my index won't be updated whenever user data is updated since I include user data via transform.

Is there any way I could link this data together so that the index would stay in sync even when the data from linked collection is updated?

from elasticstore.

acupofjose avatar acupofjose commented on August 15, 2024

Yeah just add another item into references for your user collection, make sure it points to your existing index, and be sure that you transform the data to match the structure in elasticsearch

from elasticstore.

butaminas avatar butaminas commented on August 15, 2024

@acupofjose how do I point the user_id field of comments index to users index?

from elasticstore.

acupofjose avatar acupofjose commented on August 15, 2024

@butaminas I don't know what you're asking - could you clarify? Thanks!

from elasticstore.

butaminas avatar butaminas commented on August 15, 2024

@acupofjose I understand now how to transform the data when it is being indexed. 

What I don't fully understand is how do I do the linking so that transformed data would be updated whenever users table is updated.

From your previous comment I understand that this is how I should link users to comments:

{
  collection: "comments",
  index: "comments",
  include: ["text", "user_id"],
  transform: async (data, parent) => {
    // Transformation from Firestore happens here
  }
}
{
  collection: "users",
  index: "comments",
  include: ["name"]
}

But it is not linking to the transformed data in comments index.
What I would like to happen is when the users collection is updated so that the data in comments -> user_id index would also be updated.

from elasticstore.

acupofjose avatar acupofjose commented on August 15, 2024

Sorry for the delay on this!

It seems like a better idea would be to use the onItemUpserted property in references and the add an update_by_query from elasticsearch. (You'll have to pull the latest commit on the repo to have access to that)

Something resembling (I'm not sure what your actual structure is):

{
  collection: "comments",
  index: "comments",
  include: ["text", "user_id"],
  transform: async (data, parent) => {
    // Transformation from Firestore happens here
  },
  onItemUpserted: async (data, parent, esClient) => {
     await esClient.updateByQuery({
       index: 'comments',
       refresh: true,
       body: {
       script: {
          lang: 'painless',
          source: 'ctx._source["user_name"] = data.user.name
        },
        query: {
          match: {
            user_id: data.user_id
          }
        }
      }
    })
  }
}

from elasticstore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.