@normalized-db/normalizer
TypeScript icon, indicating that this package has built-in type declarations

2.5.0-beta.4 • Public • Published

@normalized-db/normalizer

Normalize JavaScript objects based on a simple schema (implemented with TypeScript).

Versioning

To ease versioning equal major and minor version numbers are used for all modules.

Installation

Install using NPM:

npm install --save @normalized-db/normalizer

Usage

Use the NormalizerBuilder to create a Normalizer. Use either schema(…) or schemaConfig(…) to apply a schema configuration. This is the only required parameter.

Due to references between objects the normalizer needs them to have keys, this is why you should define a UniqueKeyCallback which generates such keys. Note that these will also be used for persisting the normalized data by @normalized-db/data-store.

Using reverse references will generate a _refs-field on objects. E.g. normalizing an object like

const schema: ISchemaConfig = {
  _defaults: { key: 'id' },
  parentType: {
    targets: {
      foo: 'childType'
    }
  },
  childType: true
}

const parent: ParentType = { 
  id: 'parent',
  foo: { 
    id: 'child', 
    bar: 123
  }
}

would result in

const normalizedChild: ChildType = { 
  id: 'child', 
  bar: 123, 
  _refs: { 
    parentType: Set<string>(['parent'])
  }
}

Examples

A more detailed example for a ISchemaConfig and normalization input/output can be found in the core-module's README.

See the examples-project for detailed examples:

Package Sidebar

Install

npm i @normalized-db/normalizer

Weekly Downloads

0

Version

2.5.0-beta.4

License

MIT

Unpacked Size

143 kB

Total Files

18

Last publish

Collaborators

  • atroy
  • sschmid