Package io.nitric.api.document
Class Documents
- java.lang.Object
-
- io.nitric.api.document.Documents
-
public class Documents extends Object
Provides the Document API client.
The example below illustrates the Documents API.
import io.nitric.api.document.Documents; import java.util.Map; ... // Create a customers collection var customers = Documents.collection("customers"); // Store a new customer document var customerMap = Map.of("email", "[email protected]", "status", "active"); customers.doc("[email protected]").set(customerMap); // Get customer document ref and content var customerRef = customers.doc("[email protected]"); customerMap = customerRef.get(); // Delete a customer document customers.doc("[email protected]").delete();
The example below illustrates type mapping with a custom POJO class.
package com.example.entity; public class Customer { private String mobile; private Boolean active; public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public Boolean getActive() { return active; } public void setActive(Boolean active) { this.active = active; } } package com.example.function; import com.example.entity.Customer; import io.nitric.api.document.Documents; import io.nitric.faas.Faas; import io.nitric.faas.Trigger; import io.nitric.faas.NitricFunction; import io.nitric.faas.Response; public class CustomerFunction implements NitricFunction { public Response handle(Trigger trigger) { // Get an customer document reference String id = request.getParameter("id"); var custRef = Documents.collection("customers") .doc(id, Customer.class); // Update an customer document Customer customer = custRef.get(); customer.setMobile("0432 321 543"); customer.setActive(false); custRef.set(customer); return trigger.buildResponse("OK"); } public static void main(String... args) { Faas.start(new CustomerFunction()); } }
Native Builds
When building a native function using GraalVM compiler you will need to a reflection configuration file so that your custom POJO class is supported. To do this simply add the JSON file to your build path:
src/main/resources/META-INF/native-image/reflect-config.json
Include the following information for the compiler to enable object mapping by the Jackson data binding library.
[ { "name" : "com.example.entity.Customer", "allDeclaredConstructors" : true, "allPublicConstructors" : true, "allDeclaredMethods" : true, "allPublicMethods" : true, "allDeclaredFields" : true, "allPublicFields" : true } ]
If you forget to do this, you may get a runtime error like this:
java.lang.IllegalArgumentException: No serializer found for class com.example.entity.Customer and no properties ... at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:4314)
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Collection
collection(String name)
Create a new document collection with the given collection name.
-
-
-
Method Detail
-
collection
public static Collection collection(String name)
Create a new document collection with the given collection name.- Parameters:
name
- the name of the collection (required)- Returns:
- a new document collection
-
-