Categorygithub.com/Nv7-Github/firebase
modulepackage
0.0.0-20201012033747-47c56e1d869d
Repository: https://github.com/nv7-github/firebase.git
Documentation: pkg.go.dev

# README

Firebase

The firebase library supplies lightweight, easy-to-use bindings to the Firebase REST HTTP API. It was created because of a lack of all-in-one packages that allowed Authentication, and Database management.

Installation

You can install the latest version of this library with

go get -v -u github.com/Nv7-Github/firebase

Usage

The first thing to do is to create a Firebase object. This has all the dat required for using the Firebase project. You do this with the CreateApp and CreateAppWithServiceAccount methods. This is how to use the CreateApp method:

package main

import "github.com/Nv7-Github/firebase"

func main() {
    app := firebase.CreateApp("https://[PROJECT_ID].firebaseio.com", "[API_KEY]")
}

And this is how the CreateAppWithServiceAccount method is used:

package main

import "github.com/Nv7-Github/firebase"

var serviceAccount []byte = []byte(`
{
	// ServiceAccount here, get rid of this comment
}
`)

func main() {
    app, err := firebase.CreateAppWithServiceAccount("https://[PROJECT_ID].firebaseio.com", "[API_KEY]", serviceAccount)
    if err != nil {
        panic(err)
    }
}

Realtime Database

The realtime database is the only one currently supported. It supports writing data to a path, reading data from a path, and streaming data from a path. To do this, first you need to create a Database object. In this example I am using the basic app, but an app with a service account works the same.

package main

import (
    "github.com/Nv7-Github/firebase"
    "github.com/Nv7-Github/firebase/db"
)

func main() {
    app := firebase.CreateApp("https://[PROJECT_ID].firebaseio.com", "[API_KEY]")
    database := db.CreateDatabase(app)
}

Reading

To read from the database, you use the Get and GetData methods. To read the data from path /learning, you could use the Get method so that, if you know the type you can Unmarshal the data into the right type.

var learningData map[string]bool
data, err := database.Get("/learning")
if err != nil {
    panic(err)
}
err := json.Unmarshal(data, &learningData)
if err != nil {
    panic(err)
}
// Now you can have a map[string]bool

Or, if you want to typecast it, you can do this:

data, err := database.GetData("/learning")
if err != nil {
    panic(err)
}
learningData := make(map[string]bool, 0)
learningData["learning"] = data.(map[string]interface{})["learning"].(bool)

Writing

To write to the database, you use the Set and SetData methods. To change the data at path /learning, to {"learning": true} you would marshal data and then set it:

database.Set("/learning", []byte(`{"learning": true}`))

Or you could use SetData:

database.SetData("/learning", map[string]bool{"learning": true})

Streaming

To stream, you need a handler. This handler is called every time a change is made to the database. Here is a streaming example:

database.Stream("/learning", func(ev db.StreamEvent, err error) {
    if err != nil {
        panic(err)
    }
    fmt.Println("Data at " + ev.AbsPath + " was changed to " + ev.Data)
})

The event has three values: Data, which is the Data at the path, Path, which is the path where the data was changed, relative to the input path that is sent with the request, and AbsPath, which is the absolute path, generated by the firebase package.

Authentication

This is still not done, maybe check back later? Check out the godoc for the time being.

Q & A

Why not use the firebase-admin-sdk?

The Firebase admin SDK, as said in the name, is an Admin SDK. It doesn't allow authenticating with passwords, only changing accounts. In addition, it is not very lightweight. On my laptop (Mac OS), as of when this was written, programs using Only the Firebase SDK were around 21.6 MB. On the other hand, programs written using this library are around 4.3 MB.

Why use this one, over all the other ones?

This package provides the most complete access to firebase. It supports all of Realtime Database's functions and all of the authentication functions that involve email and password.

# Packages

No description provided by the author
No description provided by the author

# Functions

CreateApp creates a firebase app without a service account.
CreateAppWithServiceAccount creates a firebase app with a service account.

# Structs

Firebase contains the data required for the web API.