API reference › @evolu/common › local‑first/Query › evoluJsonBuildObject

function evoluJsonBuildObject<O extends Record<string, Expression<unknown>>>(
  obj: O,
  ...error: JsonUint8ArrayError<"evoluJsonBuildObject", ExpressionValues<O>>
): RawBuilder<Simplify<ExpressionValues<O>>>;

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

An improved Evolu version of Kysely's SQLite jsonBuildObject 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 [.

SQLite JSON cannot hold a Uint8Array, so selecting one is a compile-time error. Select it outside this helper. The check needs a concrete row type, so a function generic over the selection cannot pass it to this helper.

Example

import {
  assertType,
  createQueryBuilder,
  evoluJsonBuildObject,
  id,
  kyselySql,
  NonEmptyTrimmedString100,
} from "@evolu/common";

const Schema = {
  person: {
    id: id("Person"),
    firstName: NonEmptyTrimmedString100,
    lastName: NonEmptyTrimmedString100,
  },
};
const createQuery = createQueryBuilder(Schema);
const people = createQuery((db) =>
  db
    .selectFrom("person")
    .select("person.id")
    .select((eb) => [
      evoluJsonBuildObject({
        first: eb.ref("firstName"),
        last: eb.ref("lastName"),
        full: kyselySql<string>`${eb.ref("firstName")} || ' ' || ${eb.ref(
          "lastName",
        )}`,
      }).as("name"),
    ]),
);

assertType<
  typeof people.Row.name,
  {
    first: NonEmptyTrimmedString100 | null;
    last: NonEmptyTrimmedString100 | null;
    full: string;
  }
>();