/kotlin-firebase-admin

🔥 A lightweight Kotlin/JS wrapper for Firebase Admin backend API library.

Primary LanguageKotlin

Maven Central Kotlin Gradle Twitter GitHub License

kotlin-firebase-admin

A lightweight Kotlin wrapper for Firebase Admin backend API library. Major version number of this wrapper matches that of Firebase itself.

Adding to your project

The library is published to Maven Central.

Gradle

Add the Maven Central repository if it is not already there.

repositories {
    mavenCentral()
}

To use the library in a single-platform project, add a dependency.

dependencies {
    implementation("dev.chriskrueger:kotlin-firebase-admin:1.4.0")
}

Setup Firebase in Kotlin project

Firebase installation

Create a new Kotlin Multiplatform or Kotlin/JS project. Navigate to folder where the function code is going to be stored and type following commands:

# Allow Firebase CLI access to your projects
firebase login

# Initialize project in this folder
firebase init functions

# Install kotlin dependancy for future use
cd functions
npm install kotlin --save
npm install kotlinx-coroutines-core --save

Example project is kotlin-firebase-functions-sample.

Gradle dependencies

To start using kotlin-firebase-functions in your Kotlin/JS project, add the following four dependencies to the dependencies block for your JavaScript target inside your build.gradle file:

implementation("dev.chriskrueger:kotlin-firebase-admin:1.4.0")
implementation("dev.chriskrueger:kotlin-express:1.1.1")

implementation(npm("text-encoding", "0.7.0"))
implementation(npm("compression", "1.7.4"))

// if not installed via npm directly
implementation(npm("firebase-admin", "8.12.1"))
implementation(npm("firebase-functions", "3.7.0"))
implementation(npm("kotlinx-coroutines-core", "1.3.7"))

Getting started

Initialize a new firebase app instance:

val app = admin.initializeApp()

HTTP request

val app = express()
app.get("") { _, res ->
    res.status(200).send("hello")
}
exports.hello = firebaseApp.https.onRequest(app)

Firestore trigger

exports.updateName = functions.firestore
            .document("user/{userId}")
            .onUpdate { change, _ ->
                val user = change.before.data<UserData>()
                val userEdit = change.after.data<UserData>()

                console.log("update ${user.name} to ${userEdit.name}")
            }