# Value Types and Set Semantics

This page explains how ToriiDB detects the type of a stored value and what `SetDefault`, `SetNX`, and `SetXX` do on write.

## Value Types

Type is detected from the raw string on every write, in this order:

| Order | Type | Rule | `String()` |
|---|---|---|---|
| 1 | `TypeJSON` | Valid JSON whose trimmed text starts with `{`/`[` and ends with `}`/`]` | `json` |
| 2 | `TypeInt` | `strconv.ParseInt(value, 10, 64)` succeeds | `int` |
| 3 | `TypeFloat` | `strconv.ParseFloat(value, 64)` succeeds | `float` |
| 4 | `TypeBool` | exactly `true` or `false` | `bool` |
| 5 | `TypeDate` | RFC 3339 or `2006-01-02` | `date` |
| 6 | `TypeString` | anything else | `string` |

`"007"` is detected as `int` even though it is not a valid JSON number; the daemon therefore re-checks `json.Valid` before sending such values as raw JSON.

## Set Semantics

| Flag | Behavior |
|---|---|
| `SetDefault` | Upsert |
| `SetNX` | Fails with `key already exists` when the key is present |
| `SetXX` | Fails with `key not found` when the key is absent |

Overwriting a key replaces its value, type, and `ExpireAt`, sets `UpdatedAt`, and clears `Vector`, because an old embedding no longer describes the new text. `expireAt` is an absolute Unix timestamp; the REPL converts a trailing `<seconds>` to one.

## Related Pages

- [Core Concepts](/core-concepts)
- [JSON Documents](/json-documents)
- [SET, DEL, INCR](/cli-reference-write)
