---
title: REGISTER
slug: data-types/register
docTags: 
createdAt: 2023-10-05T15:09:43.650Z
---

The `REGISTER` type is the core building block of every app built on Ditto. This article provides an overview of the `REGISTER` type, including its merge strategy and the various scalar subtypes you can use to encode single values within a `REGISTER`.&#x20;

# Supported Scalar Subtypes

A `REGISTER` stores a single scalar value. You can encode the value using any JSON‑compatible scalar subtype, such as a `string`, `boolean`, JSON object to represent two or more fields as a single object, and so on.&#x20;

The following table provides an overview of the different scalar types you can use to store a single value in a `REGISTER`:

| **Data Type**  | **Corresponding Values**                                                                                                                           |
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| `array`        | An ordered list of values, where each value represents any primitive type, as well as nested collection types such as other `arrays` or `MAP type` |
| `boolean`      | `true` or `false`                                                                                                                                  |
| `float`        | 64-bit floating-point                                                                                                                              |
| `int`          | Signed 64-bit integer                                                                                                                              |
| `JSON-object`  | Embedded JSON blob                                                                                                                                 |
| `null`         | Represents an absence of a value                                                                                                                   |
| `string`       | UTF-8 encodable                                                                                                                                    |
| `unsigned int` | Unsigned 64-bit integer                                                                                                                            |

## Example Updating a REGISTER

For example, the following snippet shows a basic `UPDATE` statement query setting the values of two separate `REGISTERS`:

- A `REGISTER` storing a `string` set to the value `'blue'`.&#x20;
- A `REGISTER` storing an `integer` set to the value `3001`.&#x20;

:::CodeblockTabs
DQL

```sql
UPDATE cars
SET 
  name = 'blue'
  mileage = 3001
WHERE _id = '123'
```
:::

## Limitations to Arrays

Unlike typical `arrays`, the multiple elements within an `array` scalar subtype function collectively as a single `REGISTER` with last-write-wins merge behavior. An `array` scalar subtype is an ordered collection of items represented using any primitive JSON‑compatible data type enclosed within square brackets (\[ ]). 

The `array` scalar subtype, while great at managing unordered operations, such as a GPS coordinate where both latitude and longitude values change together, as follows, maintaining a strict order could potentially lead to data inconsistencies at merge.

```json
"location": {
  "coordinates": [-122.0308, 37.3318],
  "address": "123 Main St, San Francisco, CA 94105"
}
```

Considering this, when managing collections of data that have concurrent editors, opt for a `MAP` instead, which effectively functions as a set, more commonly known as an *associative array.&#x20;*&#x46;or more information, see [MAP](docId:5abei_YfBmm3Jn_ysEdZF).

# Last-Write-Wins Merge Strategy

The `REGISTER` type adheres to the *last-write-wins* principle when handling concurrency conflicts. This means that when changes occur, all peers observing the change will sequence these changes in the same order. This ensures that only a consistent single value syncs across the entire peer-to-peer mesh.&#x20;

For example, one flight attendant updates a customer's seat number to `6` and another to seat `9`. When the two conflicting versions merge, the edit with the highest timestamp wins.

Put another way, by enforcing the last-write-wins merge strategy, for events A and B, where B is a result of A, event A always occurs before B.

# Syncing with REGISTER

Each `REGISTER` in a document syncs independently. When the value that the `REGISTER` holds changes, the entire `REGISTER` value syncs across the mesh.

For example, the following code, once executed, results in the field `color` being changed and the value `'blue'` being synced across peers:

:::CodeblockTabs
DQL

```sql
UPDATE cars
SET 
  color = 'black'
WHERE _id = '123abc'
```
:::

Since a `REGISTER` acts as a single object, to update just the `color`, you'd need to update the entire object by providing the entire set of nested key-value pairs, including the modified `color` , as follows:

:::CodeblockTabs
```swift
let newProperties = [
  "color": "blue",
  "sunroof": true,
  "door_count": 4
]
  
await ditto.store.execute(
  "UPDATE cars SET properties = :newProperties WHERE _id = '123'",
  ["newProperties": newProperties]);
```

```kotlin
var result = ditto.store.execute(
  "INSERT INTO cars DOCUMENTS (:newCar)",
  mapOf("newProperties"
    to mapOf("color" to "blue", "sunroof" to true, "door_count" to 4)))
```

```javascript
const newProperties = {
  color: 'blue',
  sunroof: true,
  door_count: 4
}
  
await ditto.store.execute(
  "UPDATE cars SET properties = :newProperties WHERE _id = '123'",     
  {newProperties});
```

```java
Map<String, String> newProperties = new HashMap<>();
newCar.put("color", "blue");
newCar.put("sunroof", true);
newCar.put("door_count", 4);

DittoQueryResult result = (DittoQueryResult) ditto.store.execute(
  "UPDATE cars SET properties = :newProperties WHERE _id = '123'",
  Collections.singletonMap("newProperties", newProperties),
  new Continuation<>() {
    @NonNull
    @Override
    public CoroutineContext getContext() {
      return EmptyCoroutineContext.INSTANCE;
    }

    @Override
    public void resumeWith(@NonNull Object o) {
      if (o instanceof Result.Failure) {
        // Handle failure
      }
    }
  }
);

```

```csharp
var args = new Dictionary<string, object>();
args.Add("newProperties", new { color="blue", sunroof=true, doorCount=4});

await ditto.Store.ExecuteAsync(
  "UPDATE cars SET properties = :newProperties WHERE _id = '123'",
  args);
```

```cpp
struct Properties {
  std::string color;
  bool sunroof;
  int door_count;
};

// ...

std::map<std::string, Properties> args;
args["newProperties"] = {"blue", true, 4};

ditto.get_store().execute(
  "UPDATE cars SET properties = :newProperties WHERE _id = '123'",
  args);
```

```rust
struct Properties {
  color: String,
  sunroof: bool,
  door_count: i32
}

struct Args {
  newProperties: Properties,
}

// ...

let args = Args {
  newProperties: Properties {
    color: "blue".to_string(),
    sunroof: true,
    door_count: 4
  }
};

await ditto.store().execute(
  "UPDATE cars SET properties = :newProperties WHERE _id = '123'",
  args);
```

```dart
final newProperties = {
  "color": "blue",
  "sunroof": true,
  "door_count": 4,
};
  
await ditto.store.execute(
  "UPDATE cars SET properties = :newProperties WHERE _id = '123'",     
  arguments: {"newProperties": newProperties},
);
```
:::

