Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /
Conectar a Atlas

Llama a una Atlas Function - Kotlin SDK

You can call an Atlas Function from a client application using the Realm Kotlin SDK. Functions are serverless JavaScript functions that let you define and execute server-side logic. These server-side Functions can run in the context of the authenticated user, and thus honor the rules, roles, and permissions that you have assigned to your data in Atlas.

Nueva en la versión 1.9.0.

You can serialize Function arguments and return values using an EJSON encoder. For more information, including examples, refer to EJSON Encoding for Atlas.

Para obtener más información sobre cómo configurar y escribir funciones Atlas, consulte Funciones de Atlas en la documentación de App Services.

  1. In an App Services App, define an Atlas Function.

  2. In your client project, initialize the App client.

  3. Then, authenticate a user. Functions are accessed through the User object.

Importante

Make sure to sanitize client data to protect against code injection when using Functions.

To call an Atlas Function from the Kotlin SDK, pass its name and all arguments to Functions.call().

Considere una función Atlas que se ejecuta en una aplicación de App Services llamada sum que toma dos argumentos, los suma y devuelve el resultado:

Atlas Function
// Add two numbers
exports = function(a, b) {
return a + b;
};

To call this Atlas Function from the Kotlin SDK:

runBlocking {
val app: App = App.create(appID)
val user = app.login(credentials)
// Access the Atlas Function through the authenticated user
// Pass the Function name and all arguments
val response = user.functions.call<Int>("sum", 1, 2)
print(response) // prints: 3
}

Changed in version 1.9.0.

Si está utilizando Kotlin SDK v1.9.0 o superior, estas limitaciones ya no se aplican.

Porque el motor de serialización Kotlin aún no admite bibliotecas de terceros, existen limitaciones en los tipos que puede pasar como argumentos y los tipos en los que puede deserializar los resultados en una función llamada.

You cannot pass objects as arguments.

The following are valid argument value types:

  • Primitives

  • Bson

  • MutableRealmInt

  • RealmUUID

  • ObjectId

  • RealmInstant

  • RealmAny

  • Arreglo

  • Colección (Lista o Conjunto)

  • Map

The following are valid return value types:

  • Primitives

  • Bson

  • MutableRealmInt

  • RealmUUID

  • ObjectId

  • RealmInstant

  • RealmAny

  • BsonArray (para tipos de argumentos arreglo y colección)

  • BsonDocument (for Map argument types)

Volver

Connect to App Services

En esta página