npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

normalized-get

v1.1.0

Published

Easily follow relationships in normalizr data

Downloads

19

Readme

Normalized Get

Helper functions for traversing normalizr relationships.

Who is this for?

Anyone that uses normalizr, or normalized data that conforms to it.

What does it do?

  1. NormalizedGet lets you access normalized data using a path as though it was denormalized.
  2. GraphGet lets you denormalize multiple paths at once, perfect for using in components.

NormalizedGet

Say you have a schema like this:

  const users = new schema.Entity('users');
  const comments = new schema.Entity('comments', {
    user: users
  });
  const articles = new schema.Entity('articles', {
    author: users,
    comments: [comments]
  });
  return { comments, articles, users };

Just setup bindNoramlizedGet like so:

const normalizedGet = bindNormalizedGet(schemas, data); 

Now you can access deep relationships with a single line of code:

// With normalizedGet
const commenters = normalizedGet('articles[123].comments.user');

It even follows Array entities. So the above example will map over the comments and return the authors.

For comparison, here's how you'd do it without normalizedGet:

// Without normlaizedGet:
const commenters = Object.keys(data.comments)
  .map(key => {
    const userId = data.comments[key].user
    return data.users[userId];
  });

The more relationships your data has have the more complex this manual fetching gets. And you have to remember which property maps to which model.

graphGet

normalizedGet is nice for getting a specific piece of data from your entity graph. What if you need more data? That's where graphGet comes in handy. It returns a deep structure containing the data you specify.

First we bind graphGet using the similar config method as before:

const graphGet = bindGraphGet(schemas, data); 

Now we can retrieve the commenters as before, but also the article and comments too.

const models = graphGet('articles[123].comments.user')

This will output nested data ready for use in a view:

{
  articles: {
    123: {
      title: '…',
      comments: [{
        comment: 'Looks good to me', 
        user: { name: 'Jane' } 
      }, {…}, {…}]
    }
  }
}

GraphGet can accept multiple paths and merge them together.

graphGet(
  `articles[${articleId}].comments.user`,
  `users[${currentUserId}]`
)

Differences between graphGet and normalizedGet

Both functions use the same underlying recursive getter. Both map over array results. normalizedGet only returns the "leaf" data, the last element of the path. graphGet returns both the leaf and the data leading to it. normalizedGet works only on a single path and has no merge strategy. graphGet merges paths into an identity map at the root.

Usage

First bind your schemas. This makes it easy to use the getters without passing schemas and data in each time. Probably best to do this somewhere central in your project and export the bound getter.

export const normalizedGet = bindNormalizedGet(schemas, data);
export const graphGet = bindGraphGet(schemas, data);

Now you have normalizedGet and graphGet functions ready for easy data denormalization.