Skip to content

Experimental TypeScript normalizr decorators for normalization of class and property definitions

License

Notifications You must be signed in to change notification settings

mchlbrnd/normalizr-decorators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

a4b84cb · Aug 20, 2017

History

9 Commits
Aug 20, 2017
Apr 24, 2017
Aug 14, 2017
Aug 14, 2017
Apr 24, 2017
Apr 24, 2017
Apr 24, 2017
Aug 14, 2017
Aug 14, 2017
Apr 24, 2017
Aug 14, 2017
Aug 14, 2017
Aug 20, 2017

Repository files navigation

normalizr-decorators (highly experimental)

TypeScript normalizr decorators for normalization of class and property definitions

Which normalizr Schema types can be used as decorators?

  • Class
    • Entity via @Entity({key: string, options?: EntityOptions})
  • Property
    • Entity via @EntityProperty()
    • Array via @ArrayProperty(element)

Example decorating, normalizing and denormalizing

import { ArrayProperty, Entity, EntityProperty, normalize, denormalize } from './index';

@Entity({key: 'users'})
class User {
  readonly id: number;
}

@Entity({key: 'comments'})
class Comment {
  readonly id: number;

  @EntityProperty()
  commenter: User;
}

@Entity({key: 'articles'})
class Article {
  readonly id: number;

  @EntityProperty()
  author: User;

  @ArrayProperty(Comment)
  comments: Comment[];
}

const originalData = {
  id: 123,
  author: {
    id: 1,
    name: 'Paul'
  },
  title: 'My awesome blog post',
  comments: [
    {
      id: 324,
      commenter: {
        id: 2,
        name: 'Nicole'
      }
    }
  ]
};


const {entities, result}  = normalize(originalData, Article);
const denormalizedData = denormalize(result, Article, entities);

About

Experimental TypeScript normalizr decorators for normalization of class and property definitions

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published