Hapi PostgreSQL People Plugin

2.3.0 · maintenance · verified Wed Apr 22

pg-people is a Hapi plugin designed to manage 'people' and 'organisations' data within a PostgreSQL database. As of version 2.3.0, it provides a structured approach for user management, including adding users, updating passwords, retrieving user and organization details, and toggling account activity. It integrates with Hapi by exposing its functions via the `request.server.pg.people` and `request.server.pg.organisations` objects after registration. The plugin automatically creates necessary tables (`people`, `organisations`, `tags_organisations`) if they don't exist, and offers an option to reset tables with initial data. Its release cadence appears tied to the Hapi ecosystem, targeting older Node.js environments (engine `^6.5.0`). A key differentiator is its seamless integration with Hapi's request lifecycle, extending `request.pg` which implies a reliance on another Hapi plugin to establish the underlying PostgreSQL connection.

Common errors

Warnings

Install

Imports

Quickstart

Sets up a basic Hapi server, registers a mock PostgreSQL connector, then registers `pg-people` and defines a route to demonstrate fetching all people from the database. This example highlights the typical Hapi plugin registration and function access pattern.

const Hapi = require('@hapi/hapi');
const PgPeople = require('pg-people');

const init = async () => {
    const server = Hapi.server({
        port: 3000,
        host: 'localhost'
    });

    // In a real application, you would register a Hapi plugin for PostgreSQL
    // connectivity, e.g., hapi-pg or hapi-pg-promise, to provide `request.pg`.
    // For this quickstart, we'll mock `request.pg` to simulate its presence.
    await server.register({
        plugin: {
            name: 'mock-pg-connector',
            version: '1.0.0',
            register: async function (serverInstance) {
                serverInstance.decorate('request', 'pg', {
                    query: async (sql, params) => {
                        console.log('Mock PG query:', sql, params);
                        if (sql.includes('SELECT') && sql.includes('people')) {
                            return { rows: [{ id: 1, first_name: 'Mock', last_name: 'Person', email: 'mock@example.com' }] };
                        }
                        return { rows: [] };
                    }
                });
            }
        }
    });

    await server.register({
        plugin: PgPeople,
        options: {
            // WARNING: `reset: true` will clear existing data in `people` and `organisations` tables.
            // Use with extreme caution, especially in production environments.
            reset: false, 
            people: []
        }
    });

    server.route({
        method: 'GET',
        path: '/people',
        handler: async (request, h) => {
            try {
                // Accessing functions via request.server.pg.people as shown in README
                const people = await request.server.pg.people.getAllPeople();
                return h.response(people).code(200);
            } catch (error) {
                console.error('Error fetching people:', error.message);
                return h.response({ message: 'Failed to fetch people' }).code(500);
            }
        }
    });

    await server.start();
    console.log(`Server running on ${server.info.uri}`);
};

process.on('unhandledRejection', (err) => {
    console.error(err);
    process.exit(1);
});

init();

view raw JSON →