Skip to content

Conversation

@igalklebanov
Copy link
Member

@igalklebanov igalklebanov commented Aug 25, 2024

Hey 👋

JSONColumnType demands manual serialization before passing values in insertions/updates. Once you do that, it is not that type-safe - you can pass any string value really.

This PR deprecates JSONColumnType, and introduces the stricter Json type that enforces type-safe insertions/updates.

Using a branded type as the InsertType and UpdateType AND as the return value of new serialization helpers in ExpressionBuilder and sql template tag, users can now insert/update and serialize their JSON objects/arrays safely into JSON columns.

Compilers/plugins can serialize the objects/arrays differently, as the value is passed around in a ValueNode with a serialized flag. The default serialization method is, well, JSON.stringify.

db.insertInto('person_metadata')
  .values((eb) => ({
    website: eb.jval({ url: 'https://kysely.dev' }),
    // or...
    experience: sql.jval([{
      establishment: 'The University of Life',
    }]),
  }))

If you want the non-type-safe old JSONColumnType.. we don't encourage it, but you can continue using JSONColumnType (until we decide to remove it) or just use:

import type { ColumnType } from 'kysely'

type MetadataColumn = ColumnType<YourJSONType, string, string>

For a readonly Json, use:

import type { ColumnType, Json, Serialized } from 'kysely'

type MetadataColumn = Json<YourJSONType, Serialized<YourJSONType>, never>

@igalklebanov igalklebanov added api Related to library's API breaking change Includes breaking changes enhancement New feature or request labels Aug 25, 2024
@vercel
Copy link

vercel bot commented Aug 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
kysely ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 9, 2025 9:21pm

@igalklebanov igalklebanov changed the title feat: Serialized<O>, stricter JSONColumnType<O>, eb.valSerialized & sql.valSerialized. feat: Serialized<O>, stricter JSONColumnType<O> insertions/updates, eb.valSerialized & sql.valSerialized. Aug 25, 2024
@igalklebanov igalklebanov changed the title feat: Serialized<O>, stricter JSONColumnType<O> insertions/updates, eb.valSerialized & sql.valSerialized. feat: stricter JSONColumnType<O> insertions/updates, eb.valSerialized & sql.valSerialized. Aug 25, 2024
@igalklebanov igalklebanov changed the title feat: stricter JSONColumnType<O> insertions/updates, eb.valSerialized & sql.valSerialized. feat: type-safe JSONColumnType<O> insertions/updates, eb.valSerialized & sql.valSerialized. Aug 25, 2024
@igalklebanov igalklebanov changed the title feat: type-safe JSONColumnType<O> insertions/updates, eb.valSerialized & sql.valSerialized. feat: type-safe JSONColumnType<O> insertions/updates, eb.valJson & sql.valJson. Sep 1, 2024
@igalklebanov igalklebanov removed the breaking change Includes breaking changes label Sep 2, 2024
@igalklebanov igalklebanov changed the title feat: type-safe JSONColumnType<O> insertions/updates, eb.valJson & sql.valJson. feat: type-safe JSON insertions/updates, eb.valJson & sql.valJson. Sep 3, 2024
@igalklebanov igalklebanov force-pushed the v0.28 branch 2 times, most recently from a84367a to 124c0dc Compare October 3, 2024 22:55
@igalklebanov igalklebanov force-pushed the v0.28 branch 3 times, most recently from b2b78db to ae5cb1e Compare October 24, 2024 12:39
introduce ValueNode.serialized.

introduce eb.valSerialized.

introduce sql.valSerialized.

fix json-traversal test suite.

fix null handling @ compiler.

rename to `valJson`.

add instructions in errors.

typings test inserts.

call the new type `Json` instead, to not introduce a breaking change.

add missing json column @ Getting Started.

add `appendSerializedValue`.
Renames `valJson` to `jval` for JSON value wrapping

Renames the `valJson` method to `jval` for wrapping JSON values
when inserting or updating columns. This change promotes brevity
and consistency throughout the codebase.

The name change affects the expression builder, SQL raw builder,
and documentation.

fix jsdocs check.

ValueNode.createSerialized.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Related to library's API deprecation Something is/should be deprecated enhancement New feature or request internal typescript Related to Typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants