-
Notifications
You must be signed in to change notification settings - Fork 275
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 json traversal support. #440
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
->
, ->>
) support.
I like the idea of a JSON path builder, but I'm not the biggest fan of the jsref('column').key('foo').key('bar').at(0) you wouldn't be able to specify the operator, but could that be given as an optional second argument? jsref('column', '->').key('foo').key('bar').at(0) I'm probably missing some future use cases you had in mind for the |
Yeah I wasn't feeling I like your suggestion, will play around with it more. |
Haven't thought about that one, will try adding these now. |
Maybe we should merge this first and implement the camelCase stuff as a separate PR? We could release a new version after we get this merged? |
Yeah, let's go! 🚀 |
Documentation in the Kysely docs site for this would be phenomenal! |
Trying to figure out what we can do, what we should do, and how this can be scoped and explained.JSON path syntax is supported by all 3 built-in dialects. So having a type-safe JSON path builder is a no-brainer.scope->JSON path
is supported by MySQL & SQLite, PostgreSQL only supports simple references or indices there.scope->something->something
is only supported by PostgreSQL.->>
is supported by all 3 built-in dialects. Since->
is sometimes "JSON represantation" and not real values, we should only support->>
for now, since it aligns all dialects' outputs.json_extract(scope, JSON path)
is supported by MySQL & SQLite with slight differences in output type, PostgreSQL supports something similiar-ish withjsonb_path_query(scope, JSON path)
.Done implementing:
eb.ref
- simply add->
/->>
/->$
/->>$
operator as 2nd argument and chain away with.key(key)
&.at(index)
methods.Allow creating PostgreSQL (& SQLite) arrow chains (e.g.
column->'key'->0
) with->
/->>
, but also JSON paths (e.g.column->'$.key[location]'
) (MySQL & SQLite)->$
/->>$
in a dialect agnostic way (meaning no specific compiler code). For nested values to return as their true types at runtime and not be stringified, use single>
arrows (->
/->$
) for PostgreSQL/MySQL and double>
arrows (->>
/->>$
) for SQLite.in
Database
interface, define your json columns as objects/arrays orColumnType
helper type with first slot being objects/arrays or the newJSONColumnType
helper type (short forColumnType<S extends object | null, I = string, U = string>
).Need to make sure users of
prisma-kysely
&kysely-codegen
can override whatever is generated for arrays and json columns with manual interfaces. We also support optional keys (key?: type
), the inferred result type would be nullable.JSONPathBuilder can allow creating standalone JSON paths in the future, e.g. as arguments of sql json functions. maybe even expose it in
ExpressionBuilder
?SQLite requires JSON parsing of results, so this PR includes a very simple
ParseJSONResultsPlugin
.Should we offer a non-type-safe way to do the same from
sql.ref
? Might be a low hanging fruit by simply usingJSONPathBuilder<any>
(haven't checked yet).Need to write a proper recipe for all of this, make it dialect-specific like "Getting Started".