All Projects β†’ fauna-labs β†’ fauna-workers

fauna-labs / fauna-workers

Licence: MIT-0 License
A template for building fast, globally distributed applications using Cloudflare Workers and Fauna, the data API for modern applications.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to fauna-workers

fauna-gatsby-comments
Roll your own comments with Gatsby and FaunaDB πŸ—žοΈ
Stars: ✭ 29 (-17.14%)
Mutual labels:  faunadb, fauna
use-fauna
React hooks for interacting with Fauna databases
Stars: ✭ 44 (+25.71%)
Mutual labels:  faunadb, fauna
coderplex-org
Official Website for Coderplex Community. Built with Next.js and deployed on Vercel.
Stars: ✭ 32 (-8.57%)
Mutual labels:  faunadb, fauna
shopnote
shopnote is a JAMstack application that helps in creating notes with shopping items. This application is built to showcase the JAMstack concept using Fauna, Netlify Serverless Functions and GatsbyJS.
Stars: ✭ 15 (-57.14%)
Mutual labels:  faunadb, fauna
faunadb-ruby
Ruby driver for FaunaDB
Stars: ✭ 37 (+5.71%)
Mutual labels:  faunadb, fauna
faunadb-csharp
C# driver for FaunaDB
Stars: ✭ 55 (+57.14%)
Mutual labels:  faunadb, fauna
fauna-gql-upload
A tool for managing your FaunaDB database using files. Create resources such as functions by simply creating a new file.
Stars: ✭ 45 (+28.57%)
Mutual labels:  faunadb, fauna
miniflare
πŸ”₯ Fully-local simulator for Cloudflare Workers
Stars: ✭ 2,811 (+7931.43%)
Mutual labels:  cloudflare-workers
web-extension-boilerplate
The web extension boilerplate help to set up project quickly using typescript, jest, webpack, githook, prettier and github actions
Stars: ✭ 35 (+0%)
Mutual labels:  edge
Tree-Style-History
Not only show browser history in tree style. δΈζ­’η”¨ζ ‘ηŠΆε½’εΌε±•η€Ίζ΅θ§ˆε™¨εŽ†ε² (For Edge / Chromium / Chrome)
Stars: ✭ 124 (+254.29%)
Mutual labels:  edge
safety-gear-detector-python
Observe workers as they pass in front of a camera to determine if they have adequate safety protection.
Stars: ✭ 54 (+54.29%)
Mutual labels:  edge
edgekit
Open source, privacy focused client side library for the creation and monetisation of online audiences.
Stars: ✭ 42 (+20%)
Mutual labels:  edge
relay-starter-kit
πŸ’₯ Monorepo template (seed project) pre-configured with GraphQL API, PostgreSQL, React, Relay, Material UI.
Stars: ✭ 3,513 (+9937.14%)
Mutual labels:  cloudflare-workers
workers-jwt
Generate JWTs on Cloudflare Workers using the WebCrypto API
Stars: ✭ 67 (+91.43%)
Mutual labels:  cloudflare-workers
libDrive
libDrive is a Google Drive media library manager and indexer, similar to Plex, that organizes Google Drive media to offer an intuitive and user-friendly experience.
Stars: ✭ 14 (-60%)
Mutual labels:  cloudflare-workers
terraform-cloudflare-maintenance
Terraform module to create a responsive Maintenance Page using Cloudflare Workers.
Stars: ✭ 111 (+217.14%)
Mutual labels:  cloudflare-workers
iris
Distributed streaming key-value storage
Stars: ✭ 55 (+57.14%)
Mutual labels:  edge
flutter-webview-windows
A WebView2-powered Flutter WebView implementation for the Windows platform.
Stars: ✭ 83 (+137.14%)
Mutual labels:  edge
MSEdgeRedirect
A Tool to Redirect News, Search, Widgets, Weather and More to Your Default Browser
Stars: ✭ 1,381 (+3845.71%)
Mutual labels:  edge
virtual-lolly
JAMstack demo site - prerendered with serverless API fallbacks
Stars: ✭ 110 (+214.29%)
Mutual labels:  faunadb

This repository contains unofficial patterns, sample code, or tools to help developers build more effectively with Fauna. All Fauna Labs repositories are provided β€œas-is” and without support. By using this repository or its contents, you agree that this repository may never be officially supported and moved to the Fauna organization.


Fauna template for Cloudflare Workers

This template helps you build a fast, globally distributed REST API using Cloudflare Workers and Fauna, the data API for modern applications.

The template uses Fauna Schema Migrate, an infrastructure-as-code (IaC) tool, to deploy resources to your Fauna database. This template implements recommended practices for building with Fauna, including restricting access to the database through user-defined functions (UDFs) and least-privilege roles.

How to use this template

First, install Wrangler, the Workers command-line tool, and generate a new project:

$ wrangler generate my-fauna-api https://github.com/fauna-labs/fauna-workers

Change into the my-fauna-api directory, install the project dependencies, and publish your worker:

$ cd my-fauna-api
$ wrangler publish

At this point your worker is published but is not yet configured to connect to your Fauna database. You can test this by sending an HTTP GET request to your worker's endpoint.

$ curl https://my-fauna-api.<your-account-alias>.workers.dev

Hello, Fauna Workers!

Creating your Fauna database

Open the Fauna dashboard in your browser and login to your Fauna account.

Note: If you do not have a Fauna account, you can sign up for one and deploy this template using the free tier!

Choose Create database, provide a valid name, choose a Region Group for your database, and choose Create.

Note: If you choose either the EU or US Region Group, you must set the FAUNADB_DOMAIN environment variable to the correct endpoint before you deploy your Fauna resources with Fauna Schema Migrate.

EU Region Group:

$ export FAUNADB_DOMAIN=db.eu.fauna.com

US Region Group:

$ export FAUNADB_DOMAIN=db.fauna.com

This step is not necessary if you create your database in the Classic Region Group.

Deploying your Fauna resources

Navigate to the Security tab and choose New Key. Ensure the Role is set to Admin and choose Save to create your new key. Copy and save this admin key to use in the next step.

Note: You create an admin key only to create your Fauna infrastructure, such as Collections, Roles, and UDFs. Never use an admin key in your application!

From your project directory, run Fauna Schema Migrate in interactive mode:

$ npx fauna-schema-migrate run

Choose Generate to generate the queries that will run in your migration. Review the queries in the fauna/migrations directory to see what is generated, then choose Apply to create the resources. When prompted, paste the admin key you copied in a previous step.

Publishing your connection information to Cloudflare

The final step is creating a client key for accessing your database and storing that key in Cloudflare as a secret.

Return to your browser and again choose New Key from the Security tab in the Fauna dashboard. Be sure to select Worker as the role and choose Save to create your client key. Copy and save this client key to use in the next step.

Note: This client key is restricted to the actions you explicitly allow. In this application, it is only allowed to call the CreateProduct, GetProductByID, AddProductQuantity*, and DeleteProduct UDFs. It cannot write to or read from any collections or indexes directly.

Add your key to your workers project by running the following command. Paste the client key you just created when prompted:

$ wrangler secret put FAUNA_SECRET

If you created your database in a Region Group other than Classic, proceed to the next section to configure your connection.

If you created your database in the Classic Region Group, you can skip to Testing your API.

Configuring the connection for your Region Group

When you created your database, you specified a Region Group. Connections to the database need to be configured with the corresponding Region Group endpoint. If this is a child database, the Region Group will already be set to match the parent database. If your database is in the Classic region group, then the default configuration is already set. Otherwise, configure your project for the correct Region Group as follows:

  1. Refer to the Region Groups documentation to obtain the correct endpoint for your Region Group.

  2. Set the FAUNA_DOMAIN environment variable to the appropriate endpoint.

    EU Region Group:

    $ wrangler secret put FAUNA_DOMAIN
    Enter the secret text you'd like assigned to the variable FAUNA_DOMAIN on the script named my-fauna-api:
    db.eu.fauna.com
    πŸŒ€  Creating the secret for script name my-fauna-api
    ✨  Success! Uploaded secret FAUNA_DOMAIN.

    US Region Group:

    $ wrangler secret put FAUNA_DOMAIN
    Enter the secret text you'd like assigned to the variable FAUNA_DOMAIN on the script named my-fauna-api:
    db.us.fauna.com
    πŸŒ€  Creating the secret for script name my-fauna-api
    ✨  Success! Uploaded secret FAUNA_DOMAIN.

Testing your API

You can test your API using the endpoint that Wrangler provides, or you can test it locally using:

$ wrangler dev
Create a product
$ curl \
    --data '{"serialNumber": "H56N33834", "title": "Bluetooth Headphones", "weightLbs": 0.5}' \
    --header 'Content-Type: application/json' \
    --request POST \
    http://127.0.0.1:8787/products

You should receive a response similar to the following:

{ 
  "productId": "<document_id>"
}

Copy and save the productId for use in the following queries.

Find a product by ID
$ curl \
    --header 'Content-Type: application/json' \
    --request GET \
    http://127.0.0.1:8787/products/<document_id>

You should receive a response similar to the following:

{
  "id": "<document_id>",
  "serialNumber": "H56N33834",
  "title": "Bluetooth Headphones",
  "weightLbs": 0.5,
  "quantity": 0
}
Update a product
$ curl \
    --data '{"quantity": 5}' \
    --header 'Content-Type: application/json' \
    --request PATCH \
    http://127.0.0.1:8787/products/<document_id>/add-quantity

You should receive a response similar to the following:

{
  "id": "<document_id>",
  "serialNumber": "H56N33834",
  "title": "Bluetooth Headphones",
  "weightLbs": 0.5,
  "quantity": 5
}
Delete a product
$ curl \
    --header 'Content-Type: application/json' \
    --request DELETE \
    http://127.0.0.1:8787/products/<document_id>

You should receive a response similar to the following:

{
  "id": "<document_id>",
  "serialNumber": "H56N33834",
  "title": "Bluetooth Headphones",
  "weightLbs": 0.5,
  "quantity": 0
}

Additional resources

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].