# `NestGen2.Storage`
[🔗](https://github.com/integration-technology/nest_gen2_sdk/blob/v0.6.6/lib/nest_gen2/storage.ex#L1)

Settings that survive restarts and power cuts, for apps.

    NestGen2.Storage.put(:screen, %{idle_timeout_ms: 30_000})
    NestGen2.Storage.get(:screen, %{idle_timeout_ms: :infinity})

Each app gets its own folder in flash that nothing else touches,
`<install>/data/<app>`, and each key is one file in it, holding an Erlang
term as readable text (`cat` shows it) with a checksum. Writes are made safe
against the power going mid-write: the new value goes to a temporary file
that is flushed to flash, the previous value is kept as a backup, and the
temporary file is then renamed into place. A value that doesn't check out
when read falls back to the backup, then to the default; it never raises.

Meant for settings and small state that change now and then, not for data
written every second: the Nest's flash is slow and wears out.

For anything more (logs, a cache, a database such as CubDB), `dir/1` is the
app's own folder to use as it likes.

Keys are atoms or strings of lowercase letters, digits and underscores.
Values are any term that can be written out and read back as text: maps,
lists, tuples, strings, numbers, atoms; not pids, references or functions.
The calling process's application is used unless `app:` is given.

# `key`

```elixir
@type key() :: atom() | String.t()
```

# `delete`

```elixir
@spec delete(key(), keyword()) :: :ok
```

Removes `key`.

# `dir`

```elixir
@spec dir(keyword()) :: Path.t()
```

The app's own folder in flash (created if missing).

# `get`

```elixir
@spec get(key(), term(), keyword()) :: term()
```

The value stored under `key`, or `default`.

# `keys`

```elixir
@spec keys(keyword()) :: [String.t()]
```

The keys stored, as strings.

# `put`

```elixir
@spec put(key(), term(), keyword()) :: :ok | {:error, :not_storable | File.posix()}
```

Stores `value` under `key`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
