Giter VIP home page Giter VIP logo

elm-firebase's Introduction

Disclaimer: This package contains a lot of native/kernel code. This makes it unsafe, opening up users to the very real possibility of runtime errors and difficult-to-debug situations. Given that, this package cannot be published to package.elm-lang.org (in its current form, anyway). Still, it's something that I wanted personally and it has been fun to work on. I hope that others find it useful in some capacity.

Firebase for Elm

Easy access to the Firebase platform from Elm

import Firebase
import Firebase.Database as Database exposing ( Query(..), Filter(..), Limit(..) )
import Json.Encode as Encode

-- SETUP AN APP

firebase : Firebase.App
firebase =
    Firebase.getApp
        { apiKey = ""
        , authDomain = ""
        , databaseUrl = ""
        , storageBucket = ""
        , messagingSenderId = ""
        }

-- FETCH DATA

type Msg
  = UsersLoaded (Result Database.Error (List User))

type alias User = { ... }

decodeUser : String -> Encode.Value -> User
decodeUser key data =
  ...

fetchUsersOnce : Cmd Msg
fetchUsersOnce =
  Database.getList "users" (OrderByKey NoFilter NoLimit) decodeUser
    |> Database.attempt firebase UsersLoaded

subscribeToUsers : Sub Msg
subscribeToUsers =
  Database.listChanges "users" (OrderByKey NoFilter NoLimit) decodeUser
    |> Database.subscribe firebase UsersLoaded

Why?

The goal of this package is to offer a simple, usable Elm API to Firebase. The goal of this package is not to mirror the Firebase API in Elm, or to include Elm bindings for every Firebase feature (there are other projects for that). This package adds, changes, or omits features when it better serves the stated goals of simplicity and usability. For details on specific differences between this API and the original Firebase API, see the "Notable differences" section at the bottom of this README.

Installation

I have not yet been able to find a good way to bundle the Firebase JavaScript client with this package. The main issue is that the client breaks when run in "strict mode", but all Elm output is included in an IIFE that invokes "strict mode". Because of this the Firebase client must be included separately (e.g. via a script tag on the host page).

...more here...

Examples

Examples exist in the examples folder. In the future they should be linked to from here (along with live demos).

Notable differences

This API diverges from the original Firebase API in some areas:

  • The elm-firebase API does not surface Firebase's Reference or Snapshot objects. Instead, data paths are specified using plain strings and data is returned as Json.Encode.Value structures that are decoded into whatever shape is needed by the user. This simplifies the database API a good deal while only sacrificing a few convenience functions such as Reference.parent or Snapshot.key.
  • elm-firebase offers separate database APIs for querying list data and non-list data. This removes the ambiguity that exists in the original Firebase API around querying lists (e.g. the issue where list data can be accessed via the generic Snapshot.val method, which does not respect the sort parameter).
  • The database Priority feature is omitted from elm-firebase. This feature exists in Firebase primarily for backwards compatibility, and can be replaced with the OrderByChild list query functionality.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.