JavaScript
This section is intended to help JavaScript developers get started using the Fauna JavaScript driver for application development.
Supported runtimes
This driver supports and is tested on:
-
Node.js
-
LTS
-
Stable
-
-
Chrome
-
Firefox
-
Safari
-
Internet Explorer 11
Example application
The following example code runs a bare-bones Node.js application which creates
a new document in a collection called
scores
.
Prerequisites
-
A Fauna account.
-
Node.js and the Fauna JavaScript driver. See Installation for driver installation help.
Procedure
-
Navigate to the Fauna v4 Dashboard
Log in to your Fauna account at v10 Fauna Dashboard if you’re not already logged in.
-
Create a new database
Create a new database and select your Region Group.
-
Access the v4 Dashboard
Access the Fauna v4 Dashboard by clicking the v4 Dashboard tab for the database.
-
Create a new collection
Under Collections, click NEW COLLECTION. Name your new collection
People
and save it. -
Create an access key
Click SECURITY in the left-side navigation menu. Create a new key for your database. Be sure to save the key’s secret in a safe place, as it is only displayed once.
-
Create a local environment variable with your access key’s secret
On MacOS and Linux systems, enter the following in a terminal window:
export FAUNADB_SECRET=<your-secret>
For Windows systems, enter the following in a terminal window:
set FAUNADB_SECRET=<your secret>
For either example, replace
<your secret>
with the secret for the access key that you created. -
Create a local application file
With your preferred editor, create a file called
test.js
with the following contents:The above example app uses the
Create
function to create a new document in thePeople
collection. -
Run your application
In a terminal window, run the application:
node test.js
You should see results similar to this:
{ ref: Ref(Collection("People"), "324879521056555520"), ts: 1646088105160000, data: { first: 'Linus', last: 'Torvalds', age: 52 } }
If you get an error, check to make sure your access key is correct.