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

function evoluJsonBuildObject<O>(
  obj: O,
): RawBuilder<
  Simplify<{
    [K in string | number | symbol]: O[K] extends Expression<V> ? V : never;
  }>
>;

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

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 [.

Example

import {
  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"),
    ]),
);

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