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

normer

v1.0.1

Published

Simple functions for generating entity relationship schema standard

Downloads

10

Readme

normer

Build Status

A normalizer that uses plain objects for schema definitions so that they can be reused with other libraries

Setup

npm i normer

Usage

import normalize, {validateSchema, relationshipTypes} from 'normer'

const users = {
  idFunc: (ent)=>ent.id,
  modifier: ({name, ...otherProps})=>({name})
  premodifier: (ent)=>({
    ...ent,
    closeFriends: ent.friends.filter(f=>f.name === 'Harry')
  }),
  relationships: [{
    entityName: 'users',
    name: 'friends',
    type: relationshipTypes.MANY,
  },{
    entityName: 'users',
    name: 'closeFriends',
    type: relationshipTypes.ONE,
  }]
}

const schema = {
  users,
}

const data = [{
  id: 1,
  name: 'Loyd',
  friends: [{
    id: 2,
    name: 'Harry'
  }]
}]

validateSchema(schema)

const {entities, relationships} = normalize(data, 'users', schema);
console.log(entities)
/*
{
  [1]: {
    name: 'Loyd'
  },
  [2]: {
    name: 'Harry'
  }
}
*/

console.log(relationships)
/*
{
 users: {
  friends: {
    [1]: [2],
  },
  closeFriends: {
    [1]: 2
  }
 }
}

Why Another Normalizer?

I really liked the normalizr project by Dan Abramov and Paul Armstrong, but I felt like if I was going to all the trouble of creating a schema for my data, I'd like to be able to do as much as possible with the schema. I also wanted to extract the relationships from the entities into a completely separate structure.

For example:

  • I could use the schema to easily construct the shape of the redux store
  • I could listen for delete actions for a certain entityType, and know to search specific relationships for that id and scrub them

Flowtype

type $ONE = 1; type $MANY = 2; type $id = string | number; type $relationships = Array<{ entityName: string, name?: string, alias?: string, type: $ONE | $MANY }>

type $schema = { modifier?: (ent)=>ent, premodifier?: (ent)=>ent, idFunc?: (ent)=>string | $id, nameFunc?: (ent)=>string, relationships: $relationships, }

type $schemaName = string;

type $schemas = {[key: $schemaName]: $schema}

premodifier

Runs right before a single entity is about to be processed.

  • If you wanted to split an array of related entities into two separate relationships, you could run the premodifier to do so

modifier

Runs right after an entity is processed

  • Good for cleaning up and removing unneccessary object properties

idFunc

Used to retrieve the id of an entity

  • default is (ent)=>ent.id

nameFunc

Used to change the entityName of the entity after its schema has been retrieved

  • default is (ent)=>ent.id

relationships

An array of relationshipSchemas used to build relationships

entityName

The entityName of the related entity. This is the only required field.

name

The name of the relationship. For example, friends is the name of the relationship above, but the entityName of the relationship is users

alias

When a relationship is stored as a different property.

type

Is the relationship ONE-TO-ONE or ONE-TO-MANY

In production

I currently use derivatives of this approach for a number of my professional and side projects. My own specific versions are quite sophisticated, usually involving actions, selectors, and reducers, and are heavily alloyed with immutablejs, but feel free to design your own system.