Firebase4s aims to provide a Scala-friendly alternative to the Firebase Java SDK. By providing more idiomatic API interfaces and automatically handling Scala-to-Java data conversions, Firebase4s will eliminate much of the boilerplate required to use the Java SDK, while providing all of the same functionality.
Firebase4s is hosted on Maven Central. To add the SDK to your project, add the following to your build.sbt
:
libraryDependencies += "com.github.firebase4s" %% "firebase4s" % "0.0.1"
Initialize the App
with your Firebase service account credentials and database url, which may be obtained from the Firebase console.
import com.firebase4s.App
val serviceAccount = getClass.getResourceAsStream("/serviceAccountCredentials.json")
App.initialize(serviceAccount, "https://<MY_INSTANCE>.firebaseio.com")
The above code snippet assumes that your Firebase service account credentials exist in your project's resources
directory.
To get an instance of a DatabaseReference
:
import com.firebase4s.database.Database
val db: Database = Database.getInstance()
val fooRef: DatabaseReference = db.ref("foo")
There are four categories of supported data types:
Currently, only JavaBean
classes are supported. Future versions of Firebase4s will support the use of case classes
through the use of Scala Macro Annotations.
Set and retrieve a List[Int]
:
/** Get a database reference */
val listRef: DatabaseReference = db.ref("myList")
/** Set value at reference location */
val result: Future[List[Int]] = liftRef.set(List(1, 2, 3))
/** Get value at ref location */
ref.get()
.map(snapshot => snapshot.getValue)
.map((list: Option[List[Int]]) => ???) // handle result
Set and retrieve a User
:
import scala.beans.BeanProperty
/** JavaBean Class Constructor */
class User() {
@BeanProperty var name: String = _
@BeanProperty var email: String = _
}
/** Get a database reference */
val userRef = db.ref(s"user")
/** Create user */
val user = new User()
user.name = "timothy"
user.email = "tim@example.com"
/** Set user at ref location */
userRef.set(user).foreach(println) // User("timothy","tim@example.com")
/** Get user at ref location */
userRef.get()
.map(snapshot => snapshot.getValue(classOf[User]))
.map((user: Option[UserRecord]) => ???) // handle result
import com.firebase4s.{Auth, UserCreationProps}
val auth: Auth = Auth.getInstance()
// User Props
val props = UserCreationProps(
email = Some("tim@firebase4s.com"),
displayName = Some("tim"),
phoneNumber = Some("+1555555555"),
photoUrl = Some("http://testing.com/photo.jpeg")
)
auth.createUser(props)
.map((user: UserRecord) => ???) // handle results
import com.firebase4s.{Auth, UserUpdateProps}
val auth: Auth = Auth.getInstance()
// Props to update
val props = UserUpdateProps(
email = Some("timothy@firebase4s.com"),
phoneNumber = Some("+15655655555")
)
auth.updateUser(props)
.map((user: UserRecord) => ???) // handle results
auth.deleteUser(user.uid)
.map(uid => ???) // handle result
auth.verifyIdToken(token)
.map((token: FirebaseToken) => token.uid)
.map(userId => ???)
val extraClaims = Map("admin" -> true)
auth.createCustomToken(uid, Some(extraClaims)))
.map(token => ???]) // Provide token to client