Class Client

The RxJS wrapper for pg.Client.

Example

// Query a database with client
new Client()
.connect()
.pipe(
concatMap((client) =>
client.query(`SELECT * FROM my_table`).pipe(concatMap(client.end))
)
)
.subscribe({
next: (result) => {
console.log(
`Query succeeded. Returned ${result.rows.length} rows.`
);
console.table(result.rows);
},
error: (err) => {
console.error('Query failed:');
console.error(err);
}
});

Example

// Stream rows from a client query
new Client(testDbParams)
.connect()
.pipe(
concatMap((client) =>
client
.stream(sql`SELECT * FROM get_animals()`)
// Use finalize to end the connection after all rows are returned
.pipe(finalize(() => client.end().subscribe()))
)
)
.subscribe({
next: (val) => {
// Emits individual rows
},
complete: () => {
// Complete is called when all rows have been read
}
});

Public Api

Hierarchy (view full)

Constructors

Properties

_clientNative: Client | PoolClient

Accessors

  • get onDrain(): Subject<void>
  • A Subject that emits drain events.

    Returns Subject<void>

  • get onEnd(): Subject<void>
  • A Subject that emits end events.

    Returns Subject<void>

  • get onError(): Subject<Error>
  • A Subject that emits error events.

    Returns Subject<Error>

  • get onNotice(): Subject<NoticeMessage>
  • A Subject that emits notice events.

    Returns Subject<NoticeMessage>

  • get onNotification(): Subject<Notification>
  • A Subject that emits notification events.

    Returns Subject<Notification>

Methods

  • Queries a database, returning query results after all results have been returned.

    Type Parameters

    • T extends QueryResultRow = any
    • I = any[]

    Parameters

    • config: QueryConfig<I> & {
          returnsCursors?: false;
          stream?: false;
      }

      Defines query parameters

    Returns Observable<QueryResult<T>>

  • Queries a database, streaming query results as they are read.

    Type Parameters

    • T = any
    • I = any[]

    Parameters

    Returns Observable<T>

  • Queries a database, returning query results by cursor after cursor has read all rows.

    Type Parameters

    • I = any[]

    Parameters

    • config: QueryConfig<I> & {
          returnsCursors: true;
          stream?: false;
      }

      Defines query parameters

    Returns Observable<QueryCursorResult>

  • Queries a database, streaming query results as cursor reads rows.

    Type Parameters

    • I = any[]

    Parameters

    Returns Observable<QueryCursorRow>

  • Completes after one drain event has been observed.

    Parameters

    • eventName: "drain"

    Returns Observable<void>

  • Completes after one end event has been observed.

    Parameters

    • eventName: "end"

    Returns Observable<void>

  • Completes after one error has been observed.

    Parameters

    • eventName: "error"

    Returns Observable<Error>

  • Completes after one NoticeMessage has been observed.

    Parameters

    • eventName: "notice"

    Returns Observable<NoticeMessage>

  • Completes after one Notification has been observed.

    Parameters

    • eventName: "notification"

    Returns Observable<Notification>

  • Unsubscribes from an event subject or all event subjects.

    Parameters

    • Optional eventName: "error" | "end" | "drain" | "notice" | "notification"

      A Client event name

    Returns this

  • Closes a connection opened in connect() and forwards the result of a successful query.

    Type Parameters

    • T

    Parameters

    • result: T

      The result of a successful query, useful for RxJS maps.

    Returns Observable<T>

  • Closes a connection opened in connect().

    Returns Observable<Client>