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-actions

v1.0.4

Published

Simple functions for generating entity relationship schema standard

Downloads

8

Readme

normer-actions

Build Status

A plugin that generates entity and relationship actions based on the normalized output of the normer plugin.

Setup

npm install normer normer-actions --save

Usage

import { relationshipTypes } from 'normer'
import createNormerActionsCreator from 'normer-actions'

const entityActionsCreatorGenerator = (entityName: string, id: string | number)=>{
  get: (entity)=>({
    type: `${entityName}/get`,
    payload: entity,
  }),
  ...
}

const relationshipActionsCreatorGenerator = (entityName: string, relationshipName: string, id: string | number)=>{
  create: (value)=>({
    type: `${entityName}/${relationshipName}/create`,
    payload: {id, value},
  }),
  ...
}

const normerSchema = {
  users: {
    modifier: ({friends, ...props})=>props,
    relationships: [{
      entityName: 'users',
      name: 'friends',
      type: relationshipTypes.MANY,
    }]
  }
}

const normerActionsCreator = createNormerActionsCreator(
  entityActionsCreatorGenerator,
  relationshipActionsCreatorGenerator,
  normerSchema
);

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

const normerActions = normerActionsCreator(input, 'users')

console.log(normerActions)
/*
[
  {
    type: 'users/get',
    payload: {
      id: 1,
      name: 'Loyd',
    }
  },
  {
    type: 'users/get',
    payload: {
      id: 2,
      name: 'Harry',
    }
  },
  {
    type: 'users/friends/create',
    payload: {
      id: 1,
      value: [2],
    }
  },
]
*/

Why is this necessary

There are a lot of normalizers on npm, and they all normalize very well, most particularly, normalizr. The trick is how to use that normalized data. For many redux users, as far as I know, this requires a custom approach. I wanted to abstract away some of that customization, but at the same time, leave the plugin open to almost any configuration.

How it works

The basic idea is that normer-actions process a deeply nested object using the normer library's normalizing capabilities, and then parse the results into easy to use actions. The normer-actions library creates these actions through separate entity and relationship action creators. Action creators generator => action creators => actions. An action creator generator will return an object of action creator functions.

Entity action creator object

{
  get: (ent)=>({...}),
  ...
}

Relationship action creator object

{
  create: ({id, value})=>({...}),
  ...
}

The get action creator and create action creator are the default creators for the respective entity and relationship action creators.

Advanced Features: Options

The third argument of the normerActionsCreator (ie. the resulting function when calling the createNormerActionsCreator) is an options object with the following properties:

entityActionsCreator(input, entityName, options)

startingSchema

const options = {
  startingSchema: {
    idFunc: ()=>'pages'
  }
}

The starting schema is a normer concept, and it allows you to pass in the exact starting schema to be used when parsing a deeply nested object. Once the object goes through the first parse, it reverts back to using the regular schema for the remainder parsings.

entity

const options = {
  entity: (entityName, id)=>{
    actionName: 'create'
  }
}

A function that takes the current entityName and id and returns an object. Currently, there is only one option, actionName, which allows you to change which actionCreator is called.

relationship

const options = {
  relationship: (entityName, relationshipName, id)=>{
    actionName: 'create'
  }
}

A function that takes the current entityName, relationshipName and id and returns an object. Currently, there is only one option, actionName, which allows you to change which actionCreator is called.