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 18 commits 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 Oct 3, 2024 11:28pm

@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 and others added 6 commits October 4, 2024 01:54
…kysely-org#1085)

* add reusable helpers recipe and implement missing expression features

* force node 22.4.1 in CI because of an npm bug
* feat: add postgres range types (kysely-org#1086)

* feat: support refresh naterialized view

* fix tests by adding .materialized() to remove the matview

* fix failing test

* fix: References typo (kysely-org#1092)

* chore: refresh-view-node.ts => refresh-materialized-view-node.ts

* chore: export node in index.ts

---------

Co-authored-by: Isak <[email protected]>
Co-authored-by: Jonathan Wu <[email protected]>
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.

4 participants