CRUD Operations

An app is ready to do CRUD operations when there is a cluster with a configured region and the app has completed the App Initialization Steps.

Within the set of example apps in the GitHub repository node-examples, the CRUD-ops example introduces each CRUD operation: create, read, update, and delete. The example’s README.md file contains instructions on how to run the example. The app contains the minimum code necessary to run in a local development environment with a GemFire cluster.

The Create Operation

The create operation adds a new entry to the region. Each entry is a key-value pair. The key is the first parameter, and the value is the second parameter to the region.put function.
The key is foo, and the value is bar in this example:

await region.put('foo', 'bar')

If the specified key is not already in the region, a new key-value pair is placed into the region. If the specified key was already in the region, the operation acts as an update to the existing region entry.

The promise returned by the put function resolves to empty upon success.

The Read Operation

The read operation returns the value associated with the key passed as a parameter to the region.get function.

var result = await region.get('foo')

The function returns a promise that resolves to the value associated with the key or null if the key is not present within the region.

The Update Operation

The update operation writes a new value for an existing key in the region. The key is the first parameter, and the value is the second parameter to the region.put function.

await region.put('foo', 'candy')

The region.put function implements both the create and the update CRUD operations. If the specified key does not exist in the region, the key-value pair is placed into the region as a new entry.

The promise returned by the put function resolves to empty upon success.

The Delete Operation

The delete operation removes a single entry from the region, given the entry’s key as a parameter.

await region.remove('foo')

The region.remove function resolves to empty upon successful removal of the entry. If no region entry exists with the given key, region.remove returns an error.

The PutAll Operation

The region.putAll() function allows writing more than one key-value pair to a region in a single function call. Each pair is a region entry.

The parameter to putAll() is a single JSON object that specifies all the pairs. JSON semantics restrict the keys in this object to strings. Therefore, all keys must be of type string.

This example uses putAll() to add two entries to a region where the key is a product ID and the value is the product’s price. Each key is a string.

productRegion.putAll({ '000001777774': 13.99, '000001777783': 21.00 })