API reference@evolu/commonlocal‑first/Query › evoluJsonObjectFrom

function evoluJsonObjectFrom<O>(
  expr: SelectQueryBuilderExpression<O>,
): RawBuilder<Simplify<O> | null>;

Defined in: packages/common/src/local-first/Query.ts:157

An improved Evolu version of Kysely's SQLite jsonObjectFrom helper.

Kysely's ParseJSONResultsPlugin heuristically parses any result string that looks like JSON. Evolu instead prefixes JSON produced by these helpers with a per-runtime identifier and only parses values carrying that prefix, avoiding accidental parsing of ordinary string columns that merely happen to start with { or [.

The subquery must only return one row.

Example

import { evoluJsonObjectFrom } from "@evolu/common";

const result = await db
  .selectFrom("person")
  .select((eb) => [
    "id",
    evoluJsonObjectFrom(
      eb
        .selectFrom("pet")
        .select(["pet.id as pet_id", "pet.name"])
        .whereRef("pet.owner_id", "=", "person.id")
        .where("pet.is_favorite", "=", true),
    ).as("favorite_pet"),
  ])
  .execute();

result[0]?.id;
result[0]?.favorite_pet?.pet_id;
result[0]?.favorite_pet?.name;