Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: type-safe JSON insertions/updates, eb.valJson & sql.valJson. #1130

Draft
wants to merge 1 commit into
base: v0.28
Choose a base branch
from

Conversation

igalklebanov
Copy link
Member

@igalklebanov igalklebanov commented Aug 25, 2024

Hey 👋

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.valJson({ url: 'https://kysely.dev' }),
    // or...
    experience: sql.valJson([{
      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
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 Nov 4, 2024 11:03pm

@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
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`.
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 enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant