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 🙏

© 2025 – Pkg Stats / Ryan Hefner

relmapper

v0.3.1

Published

Map relational data to JSON

Readme

relmapper

Map a relational model to JSON and back.

This library is a light-weight tool, that is only concerned with converting between a hierarchical representation and a flat column-like representation of the data. The idea behind this is, that a server is essentially an adapter transforming data from a database to a web based interface such as REST over HTTP and vice versa. relmapper facilitates this transformation while not interfering with your queries. This library is for all developers that have decided on a particular relational db technology and want to leverage its full potential without being limited by the abstraction of an ORM.

Support for transforming join results into arrays is planned but not yet implemented.

Getting started

npm install relmapper --save

Convert json to a db record:

var mapper = require('relmapper').defaultMapper;
var json = {
  myJsonProperty: 5,
  myNested: {
    jsonProperty: 'with text'
  }
};
mapper.apply(json);
/*
returns {
  my_json_property: 5,
  my_nested__json_property: 'with_text'
}
 */

Convert the result from a db query to nested json from a table looking like this:

CREATE TABLE(
  my_json_property: INTEGER,
  my_nested__json_property: VARCHAR(512)
);
var queryResult = query('SELECT * FROM mytable');
mapper.unapply(queryResult); 
/*
returns [{
  myJsonProperty: 5,
  myNested: {
   jsonProperty: 'with text'
  } 
},
...]
 */

API

The basic concept of this library is that of a mapper. A mapper is a plain javascript object with two methods: apply and unapply. The transformation between the hierarchical and the relation representation is performed by applying a pipeline of mappers:

json -> mapper1.apply -> mapper2.apply -> mapper3.apply -> db object
db object -> mapper3.unapply -> mapper2.unapply -> mapper1.unapply -> json

A mapper can either transform a single object or an array of objects. The module exposes the following mappers:

flatten(delimiter)

Transforms a hierarchical structure to a flat property where the path is indicated by the given delimiter.

Examples:

var relmapper = require('relmapper');
var json = { a: { b: { c: 1 } } };
relmapper.flatten('__').apply(json);
/*
returns { a__b__c: 1 }
 */

var result = { a__b__c: 1 };
relmapper.flatten('__').unapply(result);
/*
returns { a: { b: { c: 1 } } }
 */

camelCase

Transforms camel case to snake case and vice versa. This is needed since most relational database systems are case insensitive.

Examples:

var relmapper = require('relmapper');
var json = { aCamelCaseProperty: 1 };
relmapper.camelCase.apply(json);
/*
returns { a_camel_case_property: 1 }
 */

var result = { a_camel_case_property: 1 };
relmapper.camelCase.unapply(result);
/*
returns { aCamelCaseProperty: 1 }
 */

Mappers can be combined to form more complex mappers:

sequence(...mappers)

Creates a pipeline of mappers that are applied in sequence. The order is reversed when unapplying.

The library also publishes a defaultMapper which is defined as a sequence of flatten and camelCase:

var defaultMapper = relmapper.sequence(relmapper.flatten('__'), relmapper.camelCase);

See the getting started section for an example of this mapper.

fromObjectMapper(mapper)

Creates a mapper that can handle both arrays and objects from a simpler mapper that only processes objects.

Changelog

0.3.0

  • Reimplemented the existing functionality in typescript.
  • Changed the following names due to collisions with keywords:
    • default becomes defaultMapper
    • case becomes camelCase

0.2.0

  • Changed the mapper interface to also support arrays as arguments to apply and unapply.