Coded Geekery

BlogAboutContact
BlogAboutContact

Flybase

Read data from Realtime Database.

Flybase
import loadDB from '../../lib/db'; export default (req, res) => { const flybaseRef = loadDB("table"); if (!req.query.id) { return res.status(400).json({ error: 'Missing "id" query parameter' }); } try { const snapshot = await flybaseRef.where({"slug": req.query.id); const val = await snapshot.first().value(); res.status(200).json({ total: val }); } catch(e){ return res.status(400).json({ error: 'Missing "id" query parameter' }); } };
// lib/db.js const Flybase = require('flybase') export default function loadDB( table ) { return Flybase.init( process.env.FLYBASE_APP, table, process.env.FLYBASE_APIKEY); }

Usage

  • Create Flybase Account

  • First, create a Flybase account.

  • Create your First app

    1. If you do not have a Flybase account, create one first.
    2. Create a new app.
    3. Copy your API Key

    You have successfully set up a real-time database, as well as generated credentials for your serverless function to connect to Flybase.

  • Add Environment Variables

  • To securely access the API, we need to include the secret with each request. We also do not want to commit secrets to git. Thus, we should use an environment variable. Learn how to add environment variables in Vercel.

    Coded Geekery

    © 2021 Roger Stringer. All rights reserved.

    TwitterGitHubInstagram

    Stuff

    HomeAbout MeContact Me

    Not Playing

    Spotify