{"library":"pgmock2","title":"PostgreSQL Mocking Library","description":"pgmock2 is a JavaScript and TypeScript library designed for mocking PostgreSQL database connections, primarily for testing applications that rely on the popular `pg` npm package. It provides a mechanism to simulate `pg.Client` and `pg.Pool` instances by allowing developers to pre-define SQL queries and their expected responses, including `rowCount` and `rows` data. The library supports both basic type validation for query parameters and more complex validation logic using custom functions. Currently at version 2.1.7, it appears to be actively maintained, though a specific release cadence isn't explicitly stated. Its core differentiation lies in its direct integration with the `pg` interface, ensuring that mocked connections behave nearly identically to real `pg` connections, thereby minimizing changes needed in application code during testing.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install pgmock2"],"cli":null},"imports":["import PgMock2 from 'pgmock2';","import PgMock2, { PgMock2Client } from 'pgmock2';\n// Or for the instance:\nconst client = new PgMock2();","import type { QueryConfig } from 'pgmock2';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import PgMock2 from 'pgmock2';\n\nconst pg = new PgMock2();\n\n// Add a query with a number validation for the first parameter ($1)\npg.add('SELECT * FROM employees WHERE id=$1', ['number'], {\n    rowCount: 1,\n    rows: [\n        { id: 1, name: 'John Smith', position: 'application developer' }\n    ]\n});\n\n// Add a query without parameters\npg.add('SELECT * FROM products', [], {\n    rowCount: 2,\n    rows: [\n        { id: 101, name: 'Laptop', price: 1200 },\n        { id: 102, name: 'Mouse', price: 25 }\n    ]\n});\n\n(async function() {\n    try {\n        // Connect to the mock database\n        const client = await pg.connect();\n\n        // Query with a valid parameter\n        const employeeData = await client.query('SELECT * FROM employees WHERE id=$1;', [1]);\n        console.log('Employee Query Result:', employeeData.rows);\n\n        // Query without parameters\n        const productData = await client.query('SELECT * FROM products;');\n        console.log('Product Query Result:', productData.rows);\n\n        // Attempt a query with an invalid parameter type (will likely throw)\n        await client.query('SELECT * FROM employees WHERE id=$1;', ['invalid']);\n    } catch (err: any) {\n        console.error('Error during quickstart:', err.message);\n    }\n})();","lang":"typescript","description":"This quickstart demonstrates how to set up mock queries with `pgmock2`, including parameter validation, and then execute those queries against a mock client.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}