A Javascript driver for FaunaDB.
See the FaunaDB Documentation and Tutorials for guides and a complete database API reference.
This Driver supports and is tested on:
- Node.js
- LTS
- Stable
- Chrome
- Firefox
- Safari
- Internet Explorer 11
npm install --save faunadb
or
yarn add faunadb
Via CDN:
<script src="//cdn.jsdelivr.net/npm/faunadb@latest/dist/faunadb.js"></script>
The minified version of the driver can also be used via CDN:
<script src="//cdn.jsdelivr.net/npm/faunadb@latest/dist/faunadb-min.js"></script>
The tutorials in the FaunaDB documentation contain driver-specific examples.
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.
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
.
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.
Some options (currently only secret
) 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',
}
)
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,
})
Run yarn
to install dependencies.
This project includes no polyfills. Support for Internet Explorer 11 requires
a Promise
polyfill.
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
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
).
yarn doc
will generate JSDoc documentation for the project.
Copyright 2019 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
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.