Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

transformalizer

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

transformalizer

a bare bones node module for building JSON API v1.0 compliant payloads

  • 0.1.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10
decreased by-98.57%
Maintainers
2
Weekly downloads
 
Created
Source

transformalizer

a bare bones node module for transforming raw data into JSON API v1.0 compliant payloads.

this module:

  • makes no assumption regarding the shape of your data or the datastores/sdks used.
  • supports the full JSON API v1.0 specification
  • supports dynamic transformations, links, and meta at all levels of a document

Installing

$ npm install --save transformalizer

Getting Started

Create a new transformalizer and register schemas

import createTransformalizer from 'transformalizer';

// create a new transformalizer
const transformalizer = createTransformalizer();

// register a schema
transformalizer.register({
  name: 'article',
  schema: { /* see below for schema details and examples */ },
});

// transform raw data into a valid JSON API v1.0 document
const document = transformalizer.transform({ name: 'article', source });
console.log(JSON.stringify(document));

Examples

See examples in the examples folder of this repository.

  • basic

API

createTransformalizer([options]) => transformalizer

Create a new transformalizer object

Parameters
NameTypeDescription
[options={}]Objectglobal options shared between all schemas
Examples
const createTransformalizer = require('transformalizer')

const transformalizer = createTransformalizer()

transformalizer.register(params)

Register a new document schema.

Parameters
NameTypeDescription
paramsObject
params.nameStringschema name
params.schemaObjectmappings for type, see Schema for more details
Examples
transformalizer.register({
  name: 'blog-post',
  schema: {
    // ..
  }
})

transformalizer.transform(params) => Object

Build a json api document using the schema with specified name and with the given source data.

Parameters
NameTypeDescription
paramsObject
params.nameStringthe name of the schema to use
params.sourceObjectObject[]
[params.options={}]Objectadditional data to be passed to transform functions, this will be merged with the global options
Examples
const blogPost = { title: 'Hello, World!', body: 'To be continued...', createdAt: new Date() }
const document = transformalizer.transform({ name: 'blog-post', source: blogPost })

Schema

A schema object defines a set of functions used to transform your raw data into a valid JSON API document. It has the following basic structure (that closely resembles a json api document), which is described in more detail below

{
  links({ source, options, document }) {
    return { /* top level links */ };
  },
  meta({ source, options, document }) {
    return { /* top level meta */ };
  },
  data: {
    dataSchema({ source, options, data }) {
      return 'other-schema-name'
    },
    type({ source, options, data }) {
      return 'my-type';
    },
    id({ source, options, data, type }) {
      return data.id.toString();
    },
    attributes({ source, options, data, type, id }) {
      return { /* resource attributes */ }
    },
    relationships: {
      // ..
      [key]({ source, options, data, type, id, attributes }) {
        return {
          data: {
            name: 'related-schema',
            data: { /* relationship data to be passed to other schema */ },
            included: true,
          },
          links: { /* relationship links if available */ },
          meta: { /* relationship meta if available */ }
        }
      },
      // ..
    },
    links({ source, options, data, type, id, attributes, relationships }) {
      return { /* resource links if available */ }
    },
    meta({ source, options, data, type, id, attributes, relationships }) {
      return { /* resource meta if available */ }
    }
  }
}

links(params) => Object optional

A function that should return the top level links object.

Parameters
NameTypeDescription
paramsObject
params.sourceObject[],Objectthe source data passed to the #transform function
params.optionsObjectany options passed to the #transform function, merged with the global options object
params.dataObjectthe json api document data after transform
params.includedObject[]the json api document included data after transform

meta(params) => Object optional

A function that should return the top level meta object.

Parameters
NameTypeDescription
paramsObject
params.sourceObject[],Objectthe source data passed to the #transform function
params.optionsObjectany options passed to the #transform function, merged with the global options object
params.dataObjectthe json api document data after transform
params.includedObject[]the json api document included data after transform

data.type(params) => String optional

A function that should return the type of the resource being processed. If this is not provided, the name of the schema will be used as the resource type.

Parameters
NameTypeDescription
paramsObject
params.sourceObject[],Objectthe source data passed to the #transform function
params.optionsObjectany options passed to the #transform function
params.dataObjectthe current item being processed when source is an array, or the source itself if not an array

data.id(params) => String optional

A function that should return the id of the resource being processed. If this is not provided, it is assumed that the "id" of the resource is simply the "id" property of the source object.

Parameters
NameTypeDescription
paramsObject
params.sourceObject[],Objectthe source data passed to the #transform function
params.optionsObjectany options passed to the #transform function
params.dataObjectthe current item being processed when source is an array, or the source itself if not an array
params.typeStringthe resource type determined in the data.type step

data.attributes(params) => Object optional

A function that should return the attributes portion of the resource being processed. If a null or undefined value is returned, no attributes will be included on the resource.

Parameters
NameTypeDescription
paramsObject
params.sourceObject[],Objectthe source data passed to the #transform function
params.optionsObjectany options passed to the #transform function
params.dataObjectthe current item being processed when source is an array, or the source itself if not an array
params.typeStringthe resource type determined in the data.type step
params.idStringthe id of the current resource, determined in the data.id step

data.relationships.key(params) => Object optional

A map of relationship keys to functions that should return a valid relationship object with one caveat outlined below. If a null or undefined value is returned, that relationship will be excluded from the relationships object.

Caveat: The data property of the relationship object should either be a single object or an array of objects in the form shown below

{
  name: 'schemaName', // the name of the related schema to use to transform the related item
  data: { /* the "data" param to be passed to the related schema's functions */ },
  included: true, // optional, required if the related item should be included
  meta: { /* a meta object to be included on the resource identifier object */ }
}
Parameters
NameTypeDescription
paramsObject
params.sourceObject[],Objectthe source data passed to the #transform function
params.optionsObjectany options passed to the #transform function
params.dataObjectthe current item being processed when source is an array, or the source itself if not an array
params.typeStringthe resource type determined in the data.type step
params.idStringthe id of the current resource, determined in the data.id step
params.attributesObjectthe attributes object of the current resource, determined in the data.attributes step

data.links(params) => Object optional

A function that should return the links object for the current resource. If a null or undefined value is returned, no links will be included on the resource.

Parameters
NameTypeDescription
paramsObject
params.sourceObject[],Objectthe source data passed to the #transform function
params.optionsObjectany options passed to the #transform function
params.dataObjectthe current item being processed when source is an array, or the source itself if not an array
params.typeStringthe resource type determined in the data.type step
params.idStringthe id of the current resource, determined in the data.id step
params.attributesObjectthe attributes object of the current resource, determined in the data.attributes step
params.relationshipsObjectthe relationships object of the current resource, determined in the data.relationships step

data.meta(params) => Object optional

A function that should return the meta object for the current resource. If a null or undefined value is returned, no attributes will be included on the resource.

Parameters
NameTypeDescription
paramsObject
params.sourceObject[],Objectthe source data passed to the #transform function
params.optionsObjectany options passed to the #transform function
params.dataObjectthe current item being processed when source is an array, or the source itself if not an array
params.typeStringthe resource type determined in the data.type step
params.idStringthe id of the current resource, determined in the data.id step
params.attributesObjectthe attributes object of the current resource, determined in the data.attributes step
params.relationshipsObjectthe relationships object of the current resource, determined in the data.relationships step
params.linksObjectthe links object of the current resource, determined in the data.links step

data.dataSchema(params) => String optional

A function that should return the name of a schema to use to transform the current source object. Useful for building documents who's primary data is a collection of multiple types.

Parameters
NameTypeDescription
paramsObject
params.sourceObject[],Objectthe source data passed to the #transform function
params.optionsObjectany options passed to the #transform function
params.dataObjectthe current item being processed when source is an array, or the source itself if not an array

Test

Run the test suite

$ npm test

Run coverage

$ npm run coverage

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Semver

Until transformalizer reaches a 1.0 release, breaking changes will be released with a new minor version. For example 0.5.1, and 0.5.4 will have the same API, but 0.6.0 will have breaking changes.

License

Copyright (c) 2017 Gaia.
Licensed under the MIT license.

Keywords

FAQs

Package last updated on 04 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc