Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fauna

Package Overview
Dependencies
Maintainers
2
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fauna

A driver to query Fauna databases in browsers, Node.js, and other Javascript runtimes

  • 0.5.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.9K
decreased by-49.89%
Maintainers
2
Weekly downloads
 
Created
Source

WARNING This driver is in beta release and not recommended for production use. It operates with the Fauna database service via an API which is also in beta release, and is not recommended for production use. This driver is not compatible with v4 or earlier versions of Fauna. If you would like to participate in the private beta program please contact product@fauna.com.

A JavaScript driver for Fauna.

Npm Version License

See the Fauna Documentation for additional information how to configure and query your databases.

This driver can only be used with FQL X, and is not compatible with earlier versions of FQL. To query your databases with earlier API versions, see the faunadb package.

Table of Contents

Quick-Start

import { Client, fql } from "fauna";

// configure your client
const client = new Client({
  secret: YOUR_FAUNA_SECRET,
});

try {
  // create a Collection
  const collection_query = fql`Collection.create({ name: "Dogs" })`;
  const collection_result = await client.query(collection_query);

  // define some data
  const dog = { name: "Scout" };

  // create a Document
  const document_query = fql`
    Dogs.create(${dog}) {
      id,
      ts,
      name
    }
  `;
  const document_result = await client.query(document_query);
} catch (error) {
  if (error instanceof fauna.FaunaError) {
    // handle errors
  }
}

Supported Runtimes

This Driver supports and is tested on:

  • Browsers - Stable versions of
    • Chrome
    • Firefox
    • Safari
    • Edge
  • Node.js - Current and Active LTS
    • Current - v19
    • Active LTS - v18
  • Cloudflare Workers
  • AWS Lambda
  • Netlify
  • Vercel

Installing

Package Manager

The fauna-js driver is available on npm. You can install with your package manager of choice:

npm install fauna

or

yarn add fauna

Via CDN

The driver is additionally made available to browsers via CDN:

<script type="module">
  import * as fauna from "https://cdn.jsdelivr.net/npm/fauna@latest/dist/browser/index.js";
</script>

Usage

Connecting from the browser

<html>
  <head></head>
  <body>
    <h1>Test</h1>
  </body>
  <script type="module">
    import * as fauna from "https://cdn.jsdelivr.net/npm/fauna@latest/dist/browser/index.js";

    /* ... */
  </script>
</html>

Importing into a bundled project

import * as fauna from "fauna";

or using require for CommonJS files

const fauna = require("fauna");

Typescript Support

With TypeScript, you can apply a type parameter to your result.

import { Document, type DocumentT } from "fauna";

type User = {
  name: string;
  email: string;
};

const query = fql`User.create({
  name: "Alice",
  email: "alice@site.example",
})`;

const result = await client.query<DocumentT<User>>(query);
const user_doc = result.data;

// you have typesafe access to `Document` and `User` fields
console.assert(user_doc instanceof Document);
console.assert(user_doc.id);
console.assert(user_doc.ts);
console.assert(user_doc.name === "Alice");
console.assert(user_doc.email === "alice@site.example");

Query Options

Options are available to configure queries on each request.

import { Client, type QueryRequestHeaders } from "fauna";

const client = new Client();

const options: QueryRequestHeaders = {
  format: "tagged",
  linearized: false,
  query_timeout_ms: 60_000,
  max_contention_retries: 5,
  query_tags: { name: "readme query" },
  traceparent: "00-750efa5fb6a131eb2cf4db39f28366cb-000000000000000b-00",
  typecheck: true,
};

const result = await client.query(SOME_QUERY, options);

Client Configuration

The client can be configured for your specific environment. You can also provide query options that will be sent by default with every request

import { Client, endpoints, type ClientConfiguration } from "fauna";

const config: ClientConfiguration = {
  // configure client
  secret: YOUR_FAUNA_SECRET,
  endpoint: endpoints.default,
  max_conns: 10,

  // set default query options
  format: "tagged",
  linearized: false,
  query_timeout_ms: 60_000,
  max_contention_retries: 5,
  query_tags: { name: "readme query" },
  traceparent: "00-750efa5fb6a131eb2cf4db39f28366cb-000000000000000b-00",
  typecheck: true,
};

const client = new Client(config);

Using environment variables

The driver will default to configuring your client with the values of the FAUNA_SECRET and FAUNA_ENDPOINT environment variable.

For example, if you set the following environment variables:

export FAUNA_SECRET=YOUR_FAUNA_SECRET
export FAUNA_ENDPOINT=https://db.fauna.com/

you can create a client without additional options

const client = new Client()

Query Statistics

Query statistics are returned with successful query responses and ServiceErrors.

import {
  ServiceError,
  type QueryInfo,
  type QueryStats,
  type QuerySuccess,
} from "fauna";

try {
  const result: QuerySuccess<string> = await client.query(fql`"Hello world"`);
  const stats: QueryStats | undefined = result.stats;
} catch (error: any) {
  if (error instanceof ServiceError) {
    const info: QueryInfo = error.queryInfo;
    const stats: QueryStats | undefined = info.stats;
  }
}

console.log(stats);
/* example output
 * ```
 *  {
 *    compute_ops: 1,
 *    read_ops: 0,
 *    write_ops: 0,
 *    query_time_ms: 15,
 *    storage_bytes_read: 0,
 *    storage_bytes_write: 0,
 *    contention_retries: 0
 *  }
 * ```
 */

Advanced example

Querying with FQL X

This driver uses a template-based approach to composing queries and operations. The advantage of this design is that you can prototype and test your queries in the Fauna dashboard shell, and then cut-and-paste those queries as templates in your client, which are executed in Fauna via this driver. You can parameterize your query by adding placeholders to the template, and then pass a set of arguments to the query() method, or resolve the placeholders with string interpolation.

You can write a query in pure FQL X using the driver's fql tag template function. Each FQL X language driver exposes the raw FQL X language - there is no need to learn a new framework for each language.

Here's an example in pure FQL X:

const result = await client.query(fql`
  let create_user = (params) => if (params.email != null) {
    User.create(params)
  } else {
    null
  }

  let u = create_user({
    name: "Alice",
    email: "alice@site.example",
  })

  u {
    id,
    ts,
    name,
    email
  }
`);

Composition Using the Driver

Template literals in Javascript make it easy to pass in variables to your query and create reusable pieces of FQL.

// a reusable anonymous function to create Users with validated parameters
const create_user = fql`
  (params) => if (params.email != null) {
    User.create(params)
  } else {
    null
  }
`;

// a reusable projection to format User documents
const user_projection = fql`{ id, ts, name, email }`;

// an object to pass to the query
const user_params = {
  name: "Alice",
  email: "alice@site.example",
};

// put everything together
const composed_query = fql`
  let create_user = ${create_user}
  
  let u = create_user(${user_params})

  u ${user_projection}
`;
const result2 = await client.query(composed_query);

Contributing

Any contributions are from the community are greatly appreciated!

If you have a suggestion that would make this better, please fork the repo and create a pull request. You may also simply open an issue. We provide templates, so please complete those to the best of your ability.

Don't forget to give the project a star! Thanks again!

Setting up this Repo

  1. Clone the repository; e.g. gh repo clone fauna/fauna-js if you use the GitHub CLI
  2. Install dependencies via yarn install

Running tests

  1. Start a docker desktop or other docker platform.
  2. Run yarn test. This will start local fauna containers, verify they're up and run all tests.

Linting your code

Linting runs automatically on each commit.

If you wish to run on-demand run yarn lint.

License

Distributed under the MPL 2.0 License. See LICENSE for more information.

FAQs

Package last updated on 24 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc