question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Populated records not updating in realtime

See original GitHub issue

Are records populated on the feathers server supposed to publish changes to clients in realtime?

Say we have a user with populated contacts as so:

// server (in schema)
User = {
    contacts: [ User ] // populated in hook
}

// client (in model)
setupInstance(data, { models, store }) {
    const { User } = models.api
    if (data.contacts) {
        data.contacts = data.contacts.map(contact => new User(contact))
    }
    return data
}

If one of the populated users were to change its name remotely from its own account, would it be reflected live to the others? Or does the model assignment in setupInstance only serves as a means to save the user back to its own service?

I didn’t manage to make the live connection work with populated records. It only works when get/finding the services directly. Please tell me if this is expected behavior.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:25 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
hybridmusecommented, Dec 27, 2019

Thanks for your input @J3m5. You’re right, getting the contacts directly from the store is a solution (although we still have to filter out the main user + other users that might have been independently loaded to the store).

Another workaround would be to fetch the relationships separately from the client, as shown in the guide’s example where a patient record first gets loaded, then its appointments.

Still, it seems having the populated records react directly through the nested relationships would be a more elegant and convenient way to do it.

1reaction
J3m5commented, Oct 6, 2020

I did something similar 6 months ago.

import _get from "lodash/get";

const getItem = (service, store, select = []) => (id) => {
  const basePath = ["state", service, "keyedById", id];
  const fullPath = basePath.concat(select);
  return _get(store, fullPath);
};

const getFromStore = ({ item, service, select, localField, store }) => {
  const ids = item[localField];
  const itemGetter = getItem(service, store, select);


  return Array.isArray(ids)
    ? ids.map(itemGetter)
    : itemGetter(ids);
};

const findFromStore = ({ item, service, store, find, onlyOne }) => {
  const query = find && (typeof find === "function" ? find(item) : find);
  const { data } = store.getters[`${service}/find`]({ query });
  return onlyOne ? data[0] : data;
};
const generateGetter = ({ from, localField, as, select, find, onlyOne }) => {
  if (!from || !localField || !as) {
    return {};
  }

  const storeGetter = find ? findFromStore : getFromStore;
  return {
    getter: {
      get() {
        return storeGetter({
          item: this,
          service: from,
          select,
          localField,
          store: this.constructor.store,
          find,
          onlyOne,
        });
      },
      enumerable: true,
      configurable: true,
    },
    property: as,
  };
};

export const generateGetters = function(Model) {
  if (Array.isArray(Model.populate)) {
    Model.populate.forEach((lookup) => {
      const { property, getter } = generateGetter(lookup);
      if (property && getter) {
        Object.defineProperty(Model.prototype, property, getter);
      }
    });
  }
};

And I define relttionships in he Model like that:

  static populate = [
    {
      from: "users",
      localField: "userId",
      // foreignField: "_id",
      as: "user",
      onlyOne: true,
    },
    {
      from: "employees",
      localField: "managerId",
      as: "manager",
    },
    {
      from: "companies",
      localField: "companyId",
      as: "company",
    },
    {
      from: "roles",
      localField: "roleIds",
      as: "roles",
      select: "name",
    },
  ];
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Update millions or records in a table - Ask TOM
With nologging, if the system aborts, you simply re-run the 'update' again, as you have the original data in the main table. When...
Read more >
Solved: Code not updating records - ServiceNow Community
Hello,. I have a background script that does not seem to be working. It is supposed to copy all field values from 1...
Read more >
Are Views automatically updated - sql - Stack Overflow
Yes, they are updated, every time you use them. I think Microsoft sums up what a View is quite clearly: A view can...
Read more >
UPDATE RECORD ELEMENT In Salesforce Flow - YouTube
In this video, I'm explaining the update record element to learn how we can update records in Salesforce Flow.
Read more >
Why is the DNS Server not updating with dynamic updates ...
Most often when I see this kind of DHCP-not-updating DNS behavior, it is because the credentials that DHCP uses to do the updates...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found