Coded Geekery

BlogWritingAboutContact
BlogWritingAboutContact

Flybase

Read data from Flybase in real-time.

lib/flybase.js
const Flybase = require('flybase') export default function loadDB( table ) { return Flybase.init( process.env.FLYBASE_APP, table, process.env.FLYBASE_APIKEY); }
pages/api/read/[id].js
import loadDB from '../../lib/flybase'; export default (req, res) => { const flybaseRef = loadDB("posts"); 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' }); } };

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.

    Remember: never commit secrets to git. Thus, we should use an environment variable.

    Not Playing

    Spotify

    Coded Geekery

    TwitterGitHubInstagramgumroadbuymeacoffee

    Stuff

    HomeAbout MeContact Me

    © 2022 Roger Stringer. All rights reserved.