All Projects → fauna → Faunadb Js

fauna / Faunadb Js

Licence: other
Javascript driver for FaunaDB

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Faunadb Js

Faunadb Go
Go driver for FaunaDB
Stars: ✭ 140 (-71.89%)
Mutual labels:  database, driver, drivers, client
Faunadb Python
Python driver for FaunaDB
Stars: ✭ 75 (-84.94%)
Mutual labels:  database, driver, drivers, client
Faunadb Jvm
Scala and Java driver for FaunaDB
Stars: ✭ 50 (-89.96%)
Mutual labels:  database, driver, drivers, client
Gocql
Package gocql implements a fast and robust Cassandra client for the Go programming language.
Stars: ✭ 2,182 (+338.15%)
Mutual labels:  database, driver, client
Biota
A simple database framework for Fauna
Stars: ✭ 54 (-89.16%)
Mutual labels:  database, driver, client
Cdrs
Cassandra DB native client written in Rust language. Find 1.x versions on https://github.com/AlexPikalov/cdrs/tree/v.1.x Looking for an async version? - Check WIP https://github.com/AlexPikalov/cdrs-async
Stars: ✭ 314 (-36.95%)
Mutual labels:  database, driver, client
Nodejs Driver
DataStax Node.js Driver for Apache Cassandra
Stars: ✭ 1,074 (+115.66%)
Mutual labels:  database, driver, client
Postgres
Postgres.js - The Fastest full featured PostgreSQL client for Node.js
Stars: ✭ 2,193 (+340.36%)
Mutual labels:  database, driver, client
Csharp Driver
DataStax C# Driver for Apache Cassandra
Stars: ✭ 477 (-4.22%)
Mutual labels:  database, driver, client
Driver.NET
Lightweight and flexible library to load and communicate with kernel drivers on Windows.
Stars: ✭ 59 (-88.15%)
Mutual labels:  driver, drivers
rtl88x2BU WiFi linux v5.2.4.1 22719 COEX20170518-4444.20170613
rtl88x2bu driver updated for modern kernels.
Stars: ✭ 26 (-94.78%)
Mutual labels:  driver, drivers
Psycopg3
New generation PostgreSQL database adapter for the Python programming language
Stars: ✭ 278 (-44.18%)
Mutual labels:  database, driver
faunadb-csharp
C# driver for FaunaDB
Stars: ✭ 55 (-88.96%)
Mutual labels:  driver, drivers
node-drivers
Industrial protocol drivers in node.js
Stars: ✭ 20 (-95.98%)
Mutual labels:  driver, drivers
Android Ddp
[UNMAINTAINED] Meteor's Distributed Data Protocol (DDP) for clients on Android
Stars: ✭ 271 (-45.58%)
Mutual labels:  database, client
faunadb-ruby
Ruby driver for FaunaDB
Stars: ✭ 37 (-92.57%)
Mutual labels:  driver, drivers
Vertica Python
Official native Python client for the Vertica Analytics Database.
Stars: ✭ 301 (-39.56%)
Mutual labels:  database, driver
Neo4j Java Driver
Neo4j Bolt driver for Java
Stars: ✭ 241 (-51.61%)
Mutual labels:  database, driver
Study
A simple, progressive, client/server AB testing library 📚
Stars: ✭ 293 (-41.16%)
Mutual labels:  drivers, client
Rdbc
Rust DataBase Connectivity (RDBC) :: Common Rust API for database drivers
Stars: ✭ 328 (-34.14%)
Mutual labels:  database, driver

FaunaDB Javascript Driver

CircleCI Npm Version License

semantic-release

A Javascript driver for FaunaDB.

View reference JSDocs here.

See the FaunaDB Documentation and Tutorials for guides and a complete database API reference.

Supported Runtimes

This Driver supports and is tested on:

  • Node.js
    • LTS
    • Stable
  • Chrome
  • Firefox
  • Safari
  • Internet Explorer 11

Using the Client

Installation

Node.js

npm install --save faunadb

or

yarn add faunadb

Browsers

Via CDN:

<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/faunadb.js"></script>

The minified version of the driver can also be used via CDN:

<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/faunadb-min.js"></script>

Use

The tutorials in the FaunaDB documentation contain other driver-specific examples.

Connecting from the browser

To get up and running quickly, below is a full example for connecting from the browser. Replace <your_key_here> with a database secret. You can get that by visiting your FaunaDB Dashboard, creating a new database, clicking on "Security" in the sidebar on the left, and then clicking "New Key". To learn more about keys, see FaunaDB Key System.

<html>
  <head>
  </head>
<body>
  <h1>Test</h1>
</body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/faunadb.js"></script>
<script type="text/javascript">
  var faunadb = window.faunadb
  var q = faunadb.query
  var client = new faunadb.Client({
    secret: 'your_key_here',
    domain: 'db.fauna.com',
    scheme: 'https',
  })
  client.query(
    q.ToDate('2018-06-06')
  )
  .then(function (res) { console.log('Result:', res) })
  .catch(function (err) { console.log('Error:', err) })
</script>
</html>

Requiring the Driver

var faunadb = require('faunadb'),
  q = faunadb.query

This is the recommended require stanza. The faunadb.query module contains all of the functions to create FaunaDB Query expressions.

Instantiating a Client and Issuing Queries

var client = new faunadb.Client({ secret: 'YOUR_FAUNADB_SECRET' })

Once the client has been instantiated, it can be used to issue queries. For example, to create an document in an existing collection named test with the data: { testField: 'testValue' }:

var createP = client.query(
  q.Create(q.Collection('test'), { data: { testField: 'testValue' } })
)

All methods on faunadb.Client return ES6 Promises. So, if we wanted to handle the Promise to access the Ref of the newly created document:

createP.then(function(response) {
  console.log(response.ref) // Would log the ref to console.
})

response is a JSON object containing the FaunaDB response. See the JSDocs for faunadb.Client.

Pagination Helpers

This driver contains helpers to provide a simpler API for consuming paged responses from FaunaDB. See the Paginate function reference for a description of paged responses.

Using the helper to page over sets lets the driver handle cursoring and pagination state. For example, client.paginate:

var helper = client.paginate(q.Match(q.Index('test_index'), 'example-term'))

The return value, helper, is an instance of PageHelper. The each method will execute a callback function on each consumed page.

helper.each(function(page) {
  console.log(page) // Will log the page's contents, for example: [ Ref("collections/test/1234"), ... ]
})

Note that each returns a Promise<void> that is fulfilled on the completion of pagination.

The pagination can be transformed server-side via the FaunaDB query language via the map and filter functions.

For example, to retrieve the matched documents:

helper
  .map(function(ref) {
    return q.Get(ref)
  })
  .each(function(page) {
    console.log(page) // Will now log the retrieved documents.
  })

See the JSDocs for more information on the pagination helper.

Timeouts

The client can be configured to handle timeoutsy setting a queryTimeout on the client (or passing the value to the client's .query() method directly)

Using the client's queryTimeout dictates how long FaunaDB will process the request on the server before timing out if it hasn't finished running the operation. This can be done in two different ways:

// 1. Setting the value when instantiating a new client
const client = new faunadb.Client({
  queryTimeout: 2000,
  secret: 'YOUR_FAUNADB_SECRET',
})

// 2. Specifying the value per-query
var data = client.query(q.Paginate(q.Collections()), {
  queryTimeout: 100,
})

Note: When passing a queryTimeout value to client.query() as part of the options object, it will take precendence over any value set on the client when instantiating it.

Per-query options

Some options (currently only secret and queryTimout) can be overriden on a per-query basis:

var createP = client.query(
  q.Create(q.Collection('test'), { data: { testField: 'testValue' } }),
  { secret: 'YOUR_FAUNADB_SECRET' }
)
var helper = client.paginate(
  q.Match(q.Index('test_index'), 'example-term'),
  null,
  {
    secret: 'YOUR_FAUNADB_SECRET',
  }
)
var data = client.query(q.Paginate(q.Collections()), {
  queryTimeout: 100,
})

Custom Fetch

To use a custom fetch() you just have to specify it in the configuration and make it compatible with the standard Web API Specification of the Fetch API.

const customFetch = require('./customFetch')
const client = new faunadb.Client({
  secret: 'YOUR_FAUNADB_SECRET',
  fetch: customFetch,
})

Known issues

Using with Cloudflare Workers

Cloudflare Workers have neither XMLHttpRequest nor fetch in the global scope. Therefore, the cross-fetch package is unable to inject its own fetch() function, and throws an error. The fetch() function is injected via a closure, so the workaround would be to pass the fetch objects when initiating the FaunaDB client config. Cloudflare Workers also doesn't support the use of an AbortController, which terminates requests as well as streams. Here is a workaround:

const c = new faunadb.Client({
  secret: 'your secret',
  fetch: (url, params) => {
    const signal = params.signal
    delete params.signal
    const abortPromise = new Promise(resolve => {
      if (signal) {
        signal.onabort = resolve
      }
    })
    return Promise.race([abortPromise, fetch(url, params)])
  },
})

Client Development

Run yarn to install dependencies.

Code

This project includes no polyfills. Support for Internet Explorer 11 requires a Promise polyfill.

Testing

The driver tests need to connect to a FaunaDB so we recommend you setup one locally. The fast way is running a docker image like docker run --rm --name faunadb -p 8443:8443 fauna/faunadb.

After have the faunadb working on local you have to setup a set of env variables before run the tests. You can set them manually or use a .env file for this.

FAUNA_DOMAIN=localhost
FAUNA_SCHEME=http
FAUNA_PORT=8443
FAUNA_ROOT_KEY=secret
AUTH_0_URI=https://{TENANT}.auth0.com/
AUTH_0_TOKEN=auth0 token

Guide for Auth0

  • yarn test: This will run tests against the current version of Node.js. nvm is useful for managing multiple versions of Node.js for testing.

Each test run will create a new database, and will attempt to clean it up when done. If the tests are cancelled, the test database will not get cleaned up. Therefore it is recommended to use a FaunaDB key scoped to an empty parent database created for this purpose, rather than your account's root key. This will make cleanup of test databases as easy as removing the parent database.

See the FaunaDB Multitenancy Tutorial for more information about nested databases.

Alternatively, tests can be run via a Docker container with FAUNA_ROOT_KEY="your-cloud-secret" make docker-test (an alternate Alpine-based NodeJS image can be provided via RUNTIME_IMAGE).

Documentation

  • yarn doc will generate JSDoc documentation for the project.

Previewing upcoming functionality

If you want to preview unreleased features in your project, you can do so by installing this driver using one of the following methods.

1. Using a git URL

Normally, you would install the latest release of this package using npm install --save faunadb or yarn add faunadb. To access our latest features, you will need to define this dependency by using a git URL.

  1. Open your package.json file

  2. If you have already installed this driver, you should see the following in your list of dependencies. If not, add it.

"faunadb": "^2.14.1"
  1. Instead of using a version from the npm registry, we'll want to point our package.json to the master branch of our GitHub repo. To do that, change the ^2.4.1 to fauna/faunadb-js#master.
"faunadb": "fauna/faunadb-js#master"
  1. Update your node_modules by running npm install or yarn

2. Using npm pack

  1. Clone this repo to your local system
git clone https://github.com/fauna/faunadb-js.git
  1. Navigate to the cloned repo and open the package.json
cd faunadb-js
code package.json
  1. Change the version to be semantic. For example, 3.0.0-beta.

  2. Run npm pack. This creates a tarball at the root of your project directory which represents the image sent to the NPM registry when publishing.

  3. In another project, you can now install the beta from the local image you just created by running:

npm install /path/to/tarball

License

Copyright 2021 Fauna, Inc.

Licensed under the Mozilla Public License, Version 2.0 (the "License"); you may not use this software except in compliance with the License. You may obtain a copy of the License at

http://mozilla.org/MPL/2.0/

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].