Keruald Database currently return rows as associative arrays, or indexed arrays if so configured.
To process databases result in high order functions, it could be interesting to leverage the work done in Keruald OmniTools to build the HashMap type and have the following methods:
- DatabaseResult::fetchRowAsMap : Option<HashMap<String, String>>
- DatabaseResult::iterate : generator yielding HashMap<String, String>
Context and how we could leverage this
We've seen with recent Obsidian work AI can directly grasp Vector::map and complete elegant solutions:
public function getIndexKeys () : Vector { return Vector::from($this->index) ->map(fn ($key) => $this->fields[$key]); // <- directly an auto complete in PhpStorm }
That reinforces the idea to work with those HOF-friendly types by default.
Consider those two code snippets using chaining methods to understand what we want to do:
$sql = "SELECT ... LIMIT 1"; $payload = $db ->query($sql) ->fetchRowAsMap() ->map(fn ($key, $value) => "$key=$value") ->implode("\n");
A generator to emit hashmaps would be interesting too:
$items = $db ->query($sql) // SELECT with several rows ->iterate() ->map(fn ($row) => loadFromRow($row));
(There is definitely a Rust vibe in this)