JoinJS

1.1.2 · abandoned · verified Wed Apr 22

JoinJS is a JavaScript library designed to transform flat result sets from database queries, typically involving complex joins, into deeply nested JavaScript objects. It functions as a lightweight alternative to full-blown Object-Relational Mappers (ORMs), giving developers precise control over their SQL queries while simplifying the object mapping layer. The current stable version is 1.1.2, last updated in 2019, suggesting an irregular or halted release cadence. Key differentiators include its "no-nonsense" approach to database interaction, inspired by the Java MyBatis framework, which encourages writing raw SQL or using existing query builders like Knex.js, and then leveraging JoinJS solely for the post-query data structuring. It ships with TypeScript definitions, enhancing developer experience for type-safe applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates mapping a flat database result set into nested 'Team' and 'Player' objects using `joinjs.map` and `joinjs.mapOne`, including error handling for `NotFoundError`.

import joinjs from 'join-js';

const resultSet = [
    { team_id: 1, team_name: 'New England Patriots', player_id: 1, player_name: 'Tom Brady'      },
    { team_id: 1, team_name: 'New England Patriots', player_id: 2, player_name: 'Rob Gronkowski' },
    { team_id: 2, team_name: 'New York Jets',        player_id: 3, player_name: 'Geno Smith'     },
    { team_id: 2, team_name: 'New York Jets',        player_id: 4, player_name: 'Darrelle Revis' }
];

const resultMaps = [
    {
        mapId: 'teamMap',
        idProperty: 'id',
        properties: ['name'],
        collections: [
            {name: 'players', mapId: 'playerMap', columnPrefix: 'player_'}
        ]
    },
    {
        mapId: 'playerMap',
        idProperty: 'id',
        properties: ['name']
    }
];

try {
    const mappedResult = joinjs.map(resultSet, resultMaps, 'teamMap', 'team_');
    console.log('Mapped Result:', JSON.stringify(mappedResult, null, 2));

    const mappedOneTeam = joinjs.mapOne(resultSet, resultMaps, 'teamMap', 'team_');
    console.log('\nMapped One Team:', JSON.stringify(mappedOneTeam, null, 2));

    // Demonstrate NotFoundError for mapOne with an empty set
    const emptyResultSet = [];
    try {
        joinjs.mapOne(emptyResultSet, resultMaps, 'teamMap', 'team_');
    } catch (e) {
        if (e.name === 'NotFoundError') {
            console.log('\nCaught expected NotFoundError for mapOne with empty result set.');
        } else {
            throw e;
        }
    }
} catch (error) {
    console.error('An error occurred:', error);
}

view raw JSON →