Skip to content

Instantly share code, notes, and snippets.

@n400
Created May 8, 2020 14:24
Show Gist options
  • Save n400/76530c146e45e19f4bd95efefd04aae4 to your computer and use it in GitHub Desktop.
Save n400/76530c146e45e19f4bd95efefd04aae4 to your computer and use it in GitHub Desktop.
Example observers to expose response headers from FaunaDB JS driver

When the FaunaDB JavaScript driver returns query results, it's exposing the the response payload, but not the response headers. However, the response headers contain query metrics you might want (e.g., read ops, write ops, transaction retires, query time, bytes in/out, storage bytes read/written, read ops).

The web shell/REPL at dashboard.fauna.com also presents this information to you through the GUI (click the "i" on left).

Here are some examples to help you get the repsonse headers from the driver:

var faunadb = require('./index')
var q = faunadb.query
function account (res) {
  if (!res || !res.responseHeaders) return
  h = res.responseHeaders
  console.log(h)
}
var client = new faunadb.Client({
  secret: 'secret',
  domain: 'localhost',
  port: 8443,
  scheme: 'http',
  observer: account
})

The FaunaDB dashboard does it by hooking it into API.selectDatabase:

type ApiConfig = {
  urlString: string
  secret: string
  opts?: ClientConfig
}
export const selectDatabase = (path: string | null | undefined, opts: ClientConfig): void => {
  const secret = path ? createDatabaseSecret(path) : rootSecret()
  connect({
    ...apiConfig,
    secret,
    opts
  })
}
@hichana
Copy link

hichana commented Aug 21, 2020

Thank you @n400, on the top code block I believe you need a 'let' in front of 'h = res.responseHeader'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment