A lightweight Kotlin wrapper for Firebase Admin backend API library. Major version number of this wrapper matches that of Firebase itself.
The library is published to Maven Central.
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")
}
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.
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"))
Initialize a new firebase app instance:
val app = admin.initializeApp()
val app = express()
app.get("") { _, res ->
res.status(200).send("hello")
}
exports.hello = firebaseApp.https.onRequest(app)
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}")
}