kysely
    Preparing search index...

    Function jsonBuildObject

    • The SQLite json_object function.

      NOTE: This helper only works correctly if you've installed the ParseJSONResultsPlugin. Otherwise the nested selections will be returned as JSON strings.

      The plugin can be installed like this:

      import * as Sqlite from 'better-sqlite3'
      import { Kysely, ParseJSONResultsPlugin, SqliteDialect } from 'kysely'
      import type { Database } from 'type-editor' // imaginary module

      const db = new Kysely<Database>({
      dialect: new SqliteDialect({
      database: new Sqlite(':memory:')
      }),
      plugins: [new ParseJSONResultsPlugin()]
      })
      import { sql } from 'kysely'
      import { jsonBuildObject } from 'kysely/helpers/sqlite'

      const result = await db
      .selectFrom('person')
      .select((eb) => [
      'id',
      jsonBuildObject({
      first: eb.ref('first_name'),
      last: eb.ref('last_name'),
      full: sql<string>`first_name || ' ' || last_name`
      }).as('name')
      ])
      .execute()

      result[0]?.id
      result[0]?.name.first
      result[0]?.name.last
      result[0]?.name.full

      The generated SQL (SQLite):

      select "id", json_object(
      'first', first_name,
      'last', last_name,
      'full', "first_name" || ' ' || "last_name"
      ) as "name"
      from "person"

      Type Parameters

      Parameters

      • obj: O

      Returns RawBuilder<
          Simplify<
              {
                  [K in string
                  | number
                  | symbol]: O[K] extends Expression<V> ? ShallowDehydrateValue<V> : never
              },
          >,
      >