Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class Firestore
- Object
- Firestore
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.
Constructors
public Firestore(String projectId) | Creates a client for the (default) database. |
public Firestore(String projectId, String databaseId) | Creates a client for a named database. |
Methods
Inherited methods
Constructor details
Firestore
public Firestore(String projectId)Creates a client for the
(default) database.Parameters
projectIdString- the Firebase project ID
Firestore
public Firestore(String projectId, String databaseId)Creates a client for a named database.
Parameters
projectIdString- the Firebase project ID
databaseIdString- 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
tokenProviderTokenProvider- the token source, or null
setApiKey
public void setApiKey(String apiKey)Sets the Web API key, sent as the
key parameter.Parameters
apiKeyString- 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
baseUrlString- 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
transportCloudHttpTransport- 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>/documentsget
public AsyncResource<DocumentSnapshot> get(String path)Reads one document. A missing document completes with a snapshot whose
DocumentSnapshot.exists() is false.Parameters
pathString- 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
collectionPathString- the collection path
pageSizeint- 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
collectionPathString- the collection path
dataMap<String, Object>- the fields
Returns
the created document
set
public AsyncResource<DocumentSnapshot> set(String path, Map<String, Object> data)Creates or replaces a document.
Parameters
pathString- the document path
dataMap<String, Object>- 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
pathString- the document path
dataMap<String, Object>- the fields to change
Returns
the written document
delete
public AsyncResource<Boolean> delete(String path)Deletes a document. Deleting a missing document succeeds.
Parameters
pathString- the document path
Returns
true once deleted
runQuery
public AsyncResource<List<DocumentSnapshot>> runQuery(Query query)Runs a query over a top-level collection.
Parameters
queryQuery- 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
parentPathString- the parent document path, such as
cities/SF, or null for the root queryQuery- the query
Returns
the matching documents in query order
commit
public AsyncResource<Boolean> commit(WriteBatch batch)Applies a batch of writes atomically.
Parameters
batchWriteBatch- 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
pathString- the document path
listenerSnapshotListener- 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
queryQuery- the query
listenerSnapshotListener- 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
nameString- a single field name
Returns
the field path segment