Client for calling Fauna.

Constructors

  • Constructs a new Client.

    Parameters

    Returns Client

     const myClient = new Client(
    {
    endpoint: endpoints.cloud,
    secret: "foo",
    query_timeout_ms: 60_000,
    }
    );

Accessors

  • get lastTxnTs(): undefined | number
  • Returns undefined | number

    the last transaction time seen by this client, or undefined if this client has not seen a transaction time.

  • set lastTxnTs(ts): void
  • Sets the last transaction time of this client.

    Parameters

    • ts: undefined | number

      the last transaction timestamp to set, as microseconds since the epoch. If ts is less than the existing #lastTxnTs value or is undefined , then no change is made.

    Returns void

Methods

  • Closes the underlying HTTP client. Subsquent query or close calls will fail.

    Returns void

  • Creates an iterator to yield pages of data. If additional pages exist, the iterator will lazily fetch addition pages on each iteration. Pages will be retried in the event of a ThrottlingError up to the client's configured max_attempts, inclusive of the initial call.

    Type Parameters

    • T extends QueryValue

      The expected type of the items returned from Fauna on each iteration. T can be inferred if the provided query used a type parameter.

    Parameters

    Returns SetIterator<T>

    A SetIterator that lazily fetches new pages of data on each iteration

     const userIterator = await client.paginate(fql`
    Users.all()
    `);

    for await (const users of userIterator) {
    for (const user of users) {
    // do something with each user
    }
    }

    The SetIterator.flatten method can be used so the iterator yields items directly. Each item is fetched asynchronously and hides when additional pages are fetched.

     const userIterator = await client.paginate(fql`
    Users.all()
    `);

    for await (const user of userIterator.flatten()) {
    // do something with each user
    }
  • Queries Fauna. Queries will be retried in the event of a ThrottlingError up to the client's configured max_attempts, inclusive of the initial call.

    Type Parameters

    • T extends QueryValue

      The expected type of the response from Fauna. T can be inferred if the provided query used a type parameter.

    Parameters

    • query: Query<T>

      a Query to execute in Fauna. Note, you can embed header fields in this object; if you do that there's no need to pass the headers parameter.

    • Optionaloptions: QueryOptions

      optional QueryOptions to apply on top of the request input. Values in this headers parameter take precedence over the same values in the ClientConfiguration.

    Returns Promise<QuerySuccess<T>>

    Promise<QuerySuccess>.

    ServiceError Fauna emitted an error. The ServiceError will be one of ServiceError's child classes if the error can be further categorized, or a concrete ServiceError if it cannot. You can use either the type, or the underlying httpStatus + code to determine the root cause.

    ProtocolError the client a HTTP error not sent by Fauna.

    NetworkError the client encountered a network issue connecting to Fauna.

    A ClientError the client fails to submit the request

    ClientClosedError if a query is issued after the client is closed. due to an internal error.

  • Initialize a streaming request to Fauna

    Type Parameters

    • T extends QueryValue

      The expected type of the response from Fauna. T can be inferred if the provided query used a type parameter.

    Parameters

    Returns StreamClient<T>

    A StreamClient that which can be used to listen to a stream of events

     const stream = client.stream(fql`MyCollection.all().toStream()`)

    try {
    for await (const event of stream) {
    switch (event.type) {
    case "update":
    case "add":
    case "remove":
    console.log("Stream update:", event);
    // ...
    break;
    }
    }
    } catch (error) {
    // An error will be handled here if Fauna returns a terminal, "error" event, or
    // if Fauna returns a non-200 response when trying to connect, or
    // if the max number of retries on network errors is reached.

    // ... handle fatal error
    };
     const stream = client.stream(fql`MyCollection.all().toStream()`)

    stream.start(
    function onEvent(event) {
    switch (event.type) {
    case "update":
    case "add":
    case "remove":
    console.log("Stream update:", event);
    // ...
    break;
    }
    },
    function onError(error) {
    // An error will be handled here if Fauna returns a terminal, "error" event, or
    // if Fauna returns a non-200 response when trying to connect, or
    // if the max number of retries on network errors is reached.

    // ... handle fatal error
    }
    );