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

add more control through configuration @ ParseJSONResultsPlugin. #1170

Draft
wants to merge 13 commits into
base: v0.28
Choose a base branch
from

Conversation

igalklebanov
Copy link
Member

@igalklebanov igalklebanov commented Oct 6, 2024

Hey 👋

This PR revisits ParseJSONResultsPlugin and attempts to provide more fine-grained control to consumers.

  • skipKeys allows you to specify columns to not parse in a result object.
  • reviver allows passing a custom reviver function to the JSON.parse invocations. This can allow you to instantiate native Dates, or omit keys.
  • __proto__ keys are always deleted to deny prototype pollution.
  • isJSON allows overriding what is or what isn't considered a JSON string before attempting to JSON.parse it.

@igalklebanov igalklebanov added built-in plugin Related to a built-in plugin enhancement New feature or request labels Oct 6, 2024
Copy link

vercel bot commented Oct 6, 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 24, 2024 0:50am

@igalklebanov igalklebanov changed the title add more control through configuration @ ParseJSONResultsPlugin. add more control through configuration @ ParseJSONResultsPlugin. Oct 6, 2024
@igalklebanov igalklebanov force-pushed the v0.28 branch 3 times, most recently from 37c8404 to 54d4013 Compare October 27, 2024 22:15
@myndzi
Copy link

myndzi commented Nov 22, 2024

Coming from #1275 for context

I know you are doing some absolute black magic with the types, so I'm not sure how cleanly this can be done with respect to revivers (though possibly it'd be cleaner?)

I'd much prefer explicit opt-in than an opt-out list by column names. skipKeys seems to exist at the wrong level of abstraction here; it doesn't entirely solve the problem because it's not defined at the table/column level. What are the chances of implementing a syntax more like this?

const rows = db.selectFrom('table').select(eb => eb.json('column_name')).execute();

Since the expression builder has the capability to pass forward its column's expected type, this should allow for syntax like:

const rows = db.selectFrom('table').select(eb => eb.json('column_name', reviver)).execute();

Obviously this is less convenient, but it is more safe and flexible since you can define a type-safe reviver (or validation) function, type guard, etc. at the point of use. This means that you can see clearly what the query is doing and what you expect to receive.

For convenience's sake, if you wanted to globally define JSON parsing behavior at the plugin level, you could potentially do something like this:

interface ParseJSONResultsPluginOptions<Database> {
  revivers: Partial<{
    [Table in keyof Database]:
      Partial<{
        [Column in keyof Table]:
          Table[Column] extends Reviver<infer P> ? P : never
      }>
  }>
}

(not fully sure if that syntax would work, and of course it could be extracted into further generics)

The idea being that, if you supply the plugin with your database interface (which is expected to match the same interface that you provide to the Kysely instance, and this should be enforceable?) - then you can define revivers for specific columns on specific tables, both of which are fully type-safe, and ensure that the reviver supplied returns the type defined on the table's interface.

igalklebanov and others added 9 commits November 24, 2024 02:32
…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]>
Copy link

pkg-pr-new bot commented Nov 24, 2024

Open in Stackblitzkysely_koa_example

npm i https://pkg.pr.new/kysely-org/kysely@1170

commit: 33ffc44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
built-in plugin Related to a built-in plugin enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants