Class Firestore

java.lang.Object
com.codename1.cloud.firebase.Firestore

public class Firestore extends Object

A Cloud Firestore client over the REST API, in pure Java.

No native SDK, Gradle dependency or pod is involved. Requests carry the Firebase ID token from a TokenProvider, so Firestore security rules see the signed-in user. Paths are relative to the database root: cities is a collection and cities/SF a document.

Firestore db = new Firestore("my-project");
db.setTokenProvider(new FirebaseAuthTokenProvider());
Map<String, Object> city = new HashMap<String, Object>();
city.put("name", "San Francisco");
city.put("population", 870000);
db.set("cities/SF", city);
db.get("cities/SF").ready(new SuccessCallback<DocumentSnapshot>() {
    public void onSucess(DocumentSnapshot doc) {
        Log.p("Name: " + doc.get("name"));
    }
});

addSnapshotListener(String, SnapshotListener) and addSnapshotListener(Query, SnapshotListener) deliver live updates.

  • Constructor Details

    • Firestore

      public Firestore(String projectId)

      Creates a client for the (default) database.

      Parameters
      • projectId: the Firebase project ID
    • Firestore

      public Firestore(String projectId, String databaseId)

      Creates a client for a named database.

      Parameters
      • projectId: the Firebase project ID

      • databaseId: the database ID

  • Method Details

    • setTokenProvider

      public void setTokenProvider(TokenProvider tokenProvider)

      Sets the token source. Without one, requests are unauthenticated and only reach data that security rules leave public.

      Parameters
      • tokenProvider: the token source, or null
    • setApiKey

      public void setApiKey(String apiKey)

      Sets the Web API key, sent as the key parameter.

      Parameters
      • apiKey: the key, or null
    • setBaseUrl

      public void setBaseUrl(String baseUrl)

      Points the client at another host, such as the local emulator (http://10.0.2.2:8080).

      Parameters
      • baseUrl: scheme, host and port without a trailing slash
    • getBaseUrl

      public String getBaseUrl()
      Returns

      the scheme, host and port requests go to

    • setTransport

      public void setTransport(CloudHttpTransport transport)

      Replaces the HTTP transport.

      Parameters
      • transport: the transport
    • getTransport

      public CloudHttpTransport getTransport()
      Returns

      the HTTP transport in use

    • getApiKey

      public String getApiKey()
      Returns

      the API key, or null

    • getTokenProvider

      public TokenProvider getTokenProvider()
      Returns

      the token provider, or null

    • getDatabaseName

      public String getDatabaseName()
      Returns

      projects/<project>/databases/<database>

    • getDocumentsRoot

      public String getDocumentsRoot()
      Returns

      projects/<project>/databases/<database>/documents

    • get

      Reads one document. A missing document completes with a snapshot whose DocumentSnapshot.exists() is false.

      Parameters
      • path: the document path
      Returns

      the snapshot

    • list

      public AsyncResource<List<DocumentSnapshot>> list(String collectionPath, int pageSize)

      Lists the documents of a collection, one page.

      Parameters
      • collectionPath: the collection path

      • pageSize: the maximum number of documents, 0 for the server default

      Returns

      the documents

    • add

      public AsyncResource<DocumentSnapshot> add(String collectionPath, Map<String,Object> data)

      Creates a document with a server assigned ID.

      Parameters
      • collectionPath: the collection path

      • data: the fields

      Returns

      the created document

    • set

      public AsyncResource<DocumentSnapshot> set(String path, Map<String,Object> data)

      Creates or replaces a document.

      Parameters
      • path: the document path

      • data: the full contents

      Returns

      the written document

    • update

      public AsyncResource<DocumentSnapshot> update(String path, Map<String,Object> data)

      Changes only the given fields of an existing document. The request fails with 404 when the document does not exist.

      Parameters
      • path: the document path

      • data: the fields to change

      Returns

      the written document

    • delete

      public AsyncResource<Boolean> delete(String path)

      Deletes a document. Deleting a missing document succeeds.

      Parameters
      • path: the document path
      Returns

      true once deleted

    • runQuery

      public AsyncResource<List<DocumentSnapshot>> runQuery(Query query)

      Runs a query over a top-level collection.

      Parameters
      • query: the query
      Returns

      the matching documents in query order

    • runQuery

      public AsyncResource<List<DocumentSnapshot>> runQuery(String parentPath, Query query)

      Runs a query over a subcollection.

      Parameters
      • parentPath: the parent document path, such as cities/SF, or null for the root

      • query: the query

      Returns

      the matching documents in query order

    • commit

      public AsyncResource<Boolean> commit(WriteBatch batch)

      Applies a batch of writes atomically.

      Parameters
      • batch: the writes
      Returns

      true once committed

    • addSnapshotListener

      public ListenerRegistration addSnapshotListener(String path, SnapshotListener listener)

      Listens to one document. The first callback carries the current state and later ones every change.

      Parameters
      • path: the document path

      • listener: receives snapshots on the EDT

      Returns

      a handle that stops the listener

    • addSnapshotListener

      public ListenerRegistration addSnapshotListener(Query query, SnapshotListener listener)

      Listens to the results of a query over a top-level collection.

      Parameters
      • query: the query

      • listener: receives snapshots on the EDT

      Returns

      a handle that stops the listener

    • quoteFieldPath

      public static String quoteFieldPath(String name)

      Quotes a field name for use in a field path. Simple identifiers stay as they are; anything else is wrapped in backticks.

      Parameters
      • name: a single field name
      Returns

      the field path segment