Options
All
  • Public
  • Public/Protected
  • All
Menu

Class default

An NPM module for mocking a connection to a PostgreSQL database.

author

Jason Favrod mail@jasonfavrod.com

example
const PGMock2 = require('pgmock2'),
const pgmock = new PGMock2();

Hierarchy

  • default

Index

Constructors

Properties

data: IPGMockData = ...
latency: number = 20

Methods

  • add(query: string, valueDefs: any[], response: MockQueryResult): void
  • Add a query, it's value definitions, and response to the mock database.

    example
    pgmock.add("SELECT * FROM employees WHERE id = $1", ['number'], {
    rowCount: 1,
    rows: [
    { id: 1, name: 'John Smith', position: 'application developer' }
    ]
    });

    Parameters

    • query: string

      An SQL query statement.

    • valueDefs: any[]

      Contains the types of each value used in the query.

    • response: MockQueryResult

      The simulated expected response of the given query.

    Returns void

  • connect(): Promise<IPGClient>
  • Get a simulated pg.Client or pg.Pool connection.

    namespace

    connect

    example

    const conn = pgmock.connect();

    Returns Promise<IPGClient>

  • drop(query: string): boolean
  • Remove a query from the mock database.

    Parameters

    • query: string

      An SQL statement added with the add method.

    Returns boolean

    true if removal successful, false otherwise.

  • dropAll(): void
  • end(): Promise<unknown>
  • normalize(rawQuery: string): string
  • performQuery(sql: string, values?: unknown[]): Promise<QueryResult<any>>
  • Parameters

    • sql: string
    • values: unknown[] = []

    Returns Promise<QueryResult<any>>

  • query(queryTextOrConfig: string | QueryConfig<any[]>, values?: any[]): Promise<QueryResult<any>>
  • Parameters

    • queryTextOrConfig: string | QueryConfig<any[]>
    • Optional values: any[]

    Returns Promise<QueryResult<any>>

  • setLatency(latency: number): void
  • Set the simulated network latency (default 20 ms).

    Parameters

    • latency: number

    Returns void

  • toString(): string
  • Return a string representation of the mock database.

    example
    {
    "3141ffa79e40392187830c52d0588f33": {
    "query": "SELECT * FROM it.projects",
    "valDefs": [],
    "response": {
    "rowCount": 3,
    "rows": [
    {
    "title": "Test Project 0",
    "status": "pending",
    "priority": "low",
    "owner": "Favrod, Jason"
    },
    {
    "title": "Test Project 1",
    "status": "pending",
    "priority": "low",
    "owner": "Favrod, Jason"
    },
    ]
    }
    },
    "81c4b35dfd07db7dff2cb0e91228e833": {
    "query": "SELECT * FROM it.projects WHERE title = $1",
    "valDefs": ["string"],
    "response": {
    "rowCount": 1,
    "rows": [
    {
    "title": "Test Project 0",
    "status": "pending",
    "priority": "low",
    "owner": "Favrod, Jason"
    }
    ]
    }
    }
    }

    Returns string

  • validVals(values: unknown[], defs: unknown[]): boolean
  • Parameters

    • values: unknown[]
    • defs: unknown[]

    Returns boolean

Generated using TypeDoc