Navigate to <CF_HOME>/cfusion/bin.
Overview
Firestore is a NoSQL document database built for automatic scaling, high performance, and ease of application development. While the Firestore interface has many of the same features as traditional databases, as a NoSQL database it differs from them in the way it describes relationships between data objects. In ColdFusion, we've integrated Firestore as an additional NoSQL solution.
For more information, see GCP Firestore.
Native mode vs Datastore mode
When you create a Firestore database, you can configure the database instance to run in either Datastore mode, which makes the database backward compatible with Datastore, or the Native mode which has all the new Firestore features.
ColdFusion supports Firestore in native mode through APIs as the datastore mode uses the APIs of datastore.
Data Model
Cloud Firestore is a NoSQL, document-oriented database. Unlike a SQL database, there are no tables or rows. Instead, you store data in documents, which are organized into collections. Each document contains a set of key-value pairs. Cloud Firestore is optimized for storing large collections of small documents. All documents must be stored in collections. Documents can contain subcollections and nested objects, both of which can include primitive fields like strings or complex objects like lists.
Collections and documents are created implicitly in Cloud Firestore. Simply assign data to a document within a collection. If either the collection or document does not exist, Cloud Firestore creates it.
To structure your data in Firestore, you create documents, which in turn implicitly create collections (like tables in SQL). These collections contain documents, like rows in SQL. Each document contains fields that contain the actual data. You can refer to a document using its path, or you can query a collection of documents for the intended data.
Indexing
Firestore uses two types of indexes single field and composite field. By default, Cloud Firestore automatically maintains single-field indexes for each field in a document and each subfield in a map. You can exempt a field from your automatic indexing settings by creating a single-field index exemption. An indexing exemption overrides the database-wide automatic index settings. An exemption can enable a single-field index that your automatic indexing settings would otherwise disable or disable a single-field index that automatic indexing would otherwise enable.
For cases where exemptions can be useful, see the indexing best practices.
Get started
Install gcpfirestore package
To install the package gcpfirestore, use the Package Manager page in the ColdFusion Administrator, or follow the steps below:
-
-
Enter the command:
- Windows: cfpm.bat
- Non-Windows: ./cfpm.sh
-
Enter the command, install gcpfirestore.
Wait for the package to get installed.
Get credentials to access GCP Firestore
When you interact with GCP, you specify your GCP security credentials to verify your credentials and check whether you have permission to access the resources that you are requesting.
GCP uses the security credentials to authenticate and authorize your requests.
For more information, see GCP API Keys Overview.
Authentication for GCP services
To access GCP services, create a service account using Google cloud console (you may alternatively use gcloud CLI, REST calls or do it programmatically). Such accounts are managed by Google Cloud's Identity and Access Management (IAM).
For more information, see Authentication for GCP Services.
Add cloud service credentials and configuration
In ColdFusion (2021 release), there is a method getCloudService() that gives you a handle to create objects for accessing various cloud services.
The syntax of the service handle is as follows:
service=getCloudService(cloudCred,cloudConfig), where:
- cloudCred: Defines the credentials for the cloud service. It could either be a struct or a string (also known as credential alias).
- cloudConfig: Defines the cloud service configuration details. It could either be a struct or a string (also known as config alias).
ColdFusion Administrator
Set credentials
In the ColdFusion Administrator, click Data & Services > Cloud Credentials.
An alias is a named representation of a cloud service and its configuration details. You can set the config alias through ColdFusion Administrator.
After entering the details, click Add Credential.
Set configuration options
In the ColdFusion Administrator, click Data & Services > Cloud Configuration.
Enter the following details, like configuration Alias, Vendor, and the name of the service.
Create the object
Once you've created the aliases for GCP credential and configuration options, you can create the object by using the getCloudService API and include the following in your CFM.
fsObject= getCloudService("fsCred", "fsConf")
Credentials JSON file
The credential JSON file contains the project ID and the private keys that help you authenticate with the GCP services. All keys, including the project_id are mandatory keys in the file.
Application.cfc
You can specify the file containing the GCP credentials and configuration options in Application.cfc. For example,
component{ this.name ="FireStore_Test" this.serialization.preservecaseforstructkey=true this.enableNullSupport=true void function onApplicationStart(){ application.gcpCred = { projectId : "my-gcp-project", credentialJsonFilePath : "C:\path\Gcp_Firestore\my-gcp-project-250541665619.json" }; application.gcpConf = { serviceName : "FIRESTORE" }; } }
Create an object. Use the snippet below:
fsObject = getCloudService(application.fsCred, application.fsConf)
On CFM page
On a CFM page, you can specify the GCP credentials and configuration options in one of the four methods, specified below:
Credential and configuration alias
After you've created the aliases for GCP credential and configuration options, you can use these aliases in the getCloudService handle as shown below:
<cfscript> // define the credential and the configuration aliases in the ColdFusion Admin fsObject=getCloudService("fsCred","fsConf") // code below. ........... </cfscript>
Credential alias and struct
<cfscript> // Using credential alias and struct for service config fsConf = { "alias":"gcpConf", "serviceName" : "GCP_STORAGE", "clientOverrideConfig":{ "retryPolicy":{ "numRetries":4 } }, "httpClientConfig":{ "maxConnections":50 } } fsObject= getCloudService("fsCred", fsConf) // code below ..................... </cfscript>
Configuration alias and struct
<cfscript> // Using config alias and struct for service credentials // GCP credentials fsCreds={ "vendorName":"GCP", "alias": "fsCred", "projectId":"1234", " credentialJsonFilePath ": "file path" } fsObject= getCloudService(fsCreds, "fsConf") // code below ..................................... </cfscript>
Credential and configuration structs
<cfscript> // Using Structs for both cloud credential and config fsCreds={ "vendorName":"GCP", "alias": "fsCred", "projectId":"1234", " credentialJsonFilePath ": "file path" } fsConf = { "alias":"fsConf", "serviceName" : "firestore", "clientOverrideConfig":{ "retryPolicy":{ "numRetries":4 } }, "httpClientConfig":{ "maxConnections":50 } } fsObject= getCloudService(fsCreds, fsConf) // code below ................................................................... </cfscript>
CFSetup
Cloud credentials
Add
- add cloudcredential alias=FirestoreCred credentialJSONFilePath=<location to cred.json> projectId=proj-id vendorName=GCP
Set
- set cloudcredential firestoreCred projectId=proj-id
Get
- get cloudcredential firestoreCred credentialJSONFilePath
Show
- show cloudcredential firestoreCred
Export
- export cloudcredential ccd.json
Delete
- delete cloudcredential firestoreCred
Import
- import cloudcredential ccd.json
Cloud configuration
Add
- add cloudconfiguration alias=firestore1 keepAliveTime=1m keepAliveTimeout=30s keepAliveWithoutCalls=YES maxInboundMessageSize=10 databaseId=(default) emulatorHost=127.0.0.1:8500 host=firestore.googleapis.com:443 initialRetryDelay=1s initialRpcTimeout=50s maxAttempts=6 maxRetryDelay=32s maxRpcTimeout=50s retryDelayMultiplier=2 rpcTimeoutMultiplier=1 totalTimeout=50s serviceName=FIRESTORE
Set
- set cloudconfiguration firestore1 keepAliveTime=2m keepAliveTimeout=1m keepAliveWithoutCalls=NO
Get
- get cloudconfiguration firestore1 keepAliveTime keepAliveTimeout keepAliveWithoutCalls
Show
- show cloudconfiguration firestore1
Delete
- delete cloudconfiguration firestore1
Export
- export cloudconfiguration ccf.json
Import
- import cloudconfiguration ccf.json
ColdFusion GCP Firestore APIs
The APIs in Firestore are async, except for the management APIs. ColdFusion uses built-in Future rather than providing synchronous results.