{"library":"pg-people","title":"Hapi PostgreSQL People Plugin","description":"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.","language":"javascript","status":"maintenance","last_verified":"Wed Apr 22","install":{"commands":["npm install pg-people"],"cli":null},"imports":["const PgPeople = require('pg-people');\n// then register with Hapi server.register({ plugin: PgPeople, options: ... })","await request.server.pg.people.getAllPeople();","await request.server.pg.people.add(userObj, cb);"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const Hapi = require('@hapi/hapi');\nconst PgPeople = require('pg-people');\n\nconst init = async () => {\n    const server = Hapi.server({\n        port: 3000,\n        host: 'localhost'\n    });\n\n    // In a real application, you would register a Hapi plugin for PostgreSQL\n    // connectivity, e.g., hapi-pg or hapi-pg-promise, to provide `request.pg`.\n    // For this quickstart, we'll mock `request.pg` to simulate its presence.\n    await server.register({\n        plugin: {\n            name: 'mock-pg-connector',\n            version: '1.0.0',\n            register: async function (serverInstance) {\n                serverInstance.decorate('request', 'pg', {\n                    query: async (sql, params) => {\n                        console.log('Mock PG query:', sql, params);\n                        if (sql.includes('SELECT') && sql.includes('people')) {\n                            return { rows: [{ id: 1, first_name: 'Mock', last_name: 'Person', email: 'mock@example.com' }] };\n                        }\n                        return { rows: [] };\n                    }\n                });\n            }\n        }\n    });\n\n    await server.register({\n        plugin: PgPeople,\n        options: {\n            // WARNING: `reset: true` will clear existing data in `people` and `organisations` tables.\n            // Use with extreme caution, especially in production environments.\n            reset: false, \n            people: []\n        }\n    });\n\n    server.route({\n        method: 'GET',\n        path: '/people',\n        handler: async (request, h) => {\n            try {\n                // Accessing functions via request.server.pg.people as shown in README\n                const people = await request.server.pg.people.getAllPeople();\n                return h.response(people).code(200);\n            } catch (error) {\n                console.error('Error fetching people:', error.message);\n                return h.response({ message: 'Failed to fetch people' }).code(500);\n            }\n        }\n    });\n\n    await server.start();\n    console.log(`Server running on ${server.info.uri}`);\n};\n\nprocess.on('unhandledRejection', (err) => {\n    console.error(err);\n    process.exit(1);\n});\n\ninit();","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}