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

postflight

v1.0.2

Published

Database to Javascript object mapper

Downloads

5

Readme

Postflight

CircleCI codecov Generic badge MIT license npm

Postflight is a javascript library for turning an array of database objects into business objects.

Usage

Add a model mapping

const propertyMap = new Map([
    ['id', 'id'],
    ['name', 'name'],
    ['weight', 'weight'],
    ['packageSize', 'package_size']
]);

const widgetSpecConfig = {
    modelClass: Widget, // optional
    propertyMap,
};

const postflight = require('postflight');
postflight.addSpec('Widget', widgetSpecConfig);

Map database rows to models

const rows =  [
    {
        id: 1,
        name: 'Factory widget',
        weight: 150,
        package_size: 5
    }
];

const model = postflight.getSpec('Widget').getModels(rows)[0];

console.log(model.id) // 1
console.log(model.name) // Factory widget
console.log(model.weight) // 150
console.log(model.packageSize) // 5
console.log(model instanceof Widget) // true

Tests

These tests use bash scripting.

Unit and integration tests

$ npm install
$ npm test

End-to-end tests

Test using a local package installation.

$ npm install
$ npm run test-e2e

API

Postflight

addSpec(specConfig)

Add a model mapping to the Postflight object. The argument is a config object:

{
    propertyMap,
    modelClass // optional
}

propertyMap is a Map object, with keys for model property names and values for database column names. modelClass is an optional property to instantiate model objects as an instance of a class.

getSpec(modelName)

Get the ModelSpec with a key matching modelName.

ModelSpec

The ModelSpec class maps model object property names to database column names using a Map object, and creates objects based on this mapping.

constructor(propertyMap, modelClass)

Create a ModelSpec object using a Map object where keys represent model property names, and values represent the matching database column name. It also takes an optional modelClass argument, which is the business object class for instantiation.

getModels(rows)

Gets Model objects from an array of database objects. The database objects have property names matching the database column names, where the property's value is equal to the value of the database column.

Install

$ npm install postflight

Build

The package needs to be packed/published from the dist folder, after running the build script.

$ npm run build
$ cd dist
$ npm pack

License

This package uses the MIT license.

Up next

Automatically generate mappings

  • Inspect database to generate model property to database column name mappings
  • Column name prefixes
  • Built-in naming conventions
  • Provide custom naming convention