-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
Wrong type on json #102
Comments
Thanks for the feedback! you can change that using overrides.types: {
"connections": {
// ...
"overrides": {
"types": {
"json": "JsonAgg"
}
}
}
} |
Hello, @Newbie012. I was exploring the suggestion you made above to solve the issue I am having with Please I want to confirm if I am correct or if I am missing something |
Update from this comment by @Newbie012 : in the future, some JSON aggregation options will be supported (maybe also
|
@Newbie012 for the future design, one additional idea that I came across while writing my SQL tooling thread on Twitter/X was how @kristiandupont handles it in Kanel:
Source: kristiandupont/kanel#429 (comment) Sounds like it could be nice, to be able to specify the type information in the database 👍 |
Hmm.. that's an interesting approach! Although I feel people won't like commenting code-specific comments on their database schema. Maybe I could implement it under a flag. Another idea, would be to define them in a config. Something like: {
"connections": {
"overrides": {
"columns": {
"person.address": "string",
"person.geolocation": "import('@/types/Geo')"
}
}
}
} Would like to hear your thoughts on this 🙂 |
Personally I would lean towards having them in the database:
But if SafeQL doesn't want this approach and would rather have them defined separately as part of SafeQL config, then we would find a way to live with the feature elsewhere (as long as sufficiently flexible, allowing multiple different possible types returned from JSON fields). |
I understand. My only concern here is that there's no "standard" of defining types in the database comments, which may lead to:
I could allow the option to find the type in the comments by regex while using |
Good point with standards and incompatibility across tooling. Because there is no standard yet, gut feel says: A) Use as much of existing standards / syntax as possible (eg. can it be TS syntax in there? similar to how ArkType does it) |
For what it's worth, I wrote a parser with Peggy. While it's not a standard, the same syntax is also used by PostGraphile. (Note: that's tagging in general, not the I think tagging could become a semi-standard if more libraries adopt it so I would be very happy to contribute in that regard. As for types, it's a bit more difficult because even if we agree that this is Postgres-only, other people might want types for other languages where the syntax would inevitably have to be different. |
Thanks for sharing that @kristiandupont. It seems like the parser is having hard time catching more complex terms: import { parse } from "tagged-comment-parser";
const result = parse("@type:number @literal:'a'|'b' @object:{foo:number}"); But when I think about it, it doesn't have to. It could return a simple string and then SafeQL could treat it as a "pg" type, which could be transformed to any TypeScript type via COMMENT ON COLUMN "user".address IS '@type:geo ...' {
"connections": {
"overrides": {
"types": {
"geo": "Geo"
}
}
}
} Since SafeQL should be used only with TypeScript & PostgreSQL, I'm less worried about conflicts with other languages. My issue is there can be potentially too much clutter when multiple clients access a single database. |
There are some shortcomings for sure, but not necessarily intentional. I am happy to expand on it, I've only been using it for personal stuff so far. The only criterium I have is that tags should be able to live side by side with a "regular" comment. |
@Newbie012 should we rename this issue and reopen to track progress on this? |
It seems more logical to keep this issue closed and open a new one since the original issue doesn't relate to the proposed feature. Using json_agg translates to a json type, which is different from a JSONB column that could correspond to a diverse type. |
Hm, would this
Can definitely do this, but maybe Proper |
It won't. As previously detailed here, SafeQL leans on PostgresSQL's RowDescription message for column metadata if available. While scanning the query's AST for nullability checks, theoretically, I could simultaneously seek JSON/B expressions and attempt to deduce its type. Simple cases like |
Yeah, I think I'm understanding that part. What I meant was something like this: SELECT
cohorts.id,
cohorts.title,
(
SELECT json_agg(cohort_events)
FROM
(
SELECT events.name FROM
cohorts_events
INNER JOIN events ON cohorts_events.event_id = events.id
WHERE cohorts_events.cohort_id = cohorts.id
) AS cohort_events
) AS events COMMENT IS '@type:{name: string}[]'
FROM
cohorts; For the calculated field with the subquery But looking around, I am not sure that this syntax exists in PostgreSQL. Some alternative syntaxes: If SafeQL can read / parse SQL comments and connect it to a field: SELECT
cohorts.id,
cohorts.title,
(
SELECT json_agg(cohort_events)
FROM
(
SELECT events.name FROM
cohorts_events
INNER JOIN events ON cohorts_events.event_id = events.id
WHERE cohorts_events.cohort_id = cohorts.id
) AS cohort_events
) AS events -- @type:{name: string}[]
FROM
cohorts; If TypeScript comments / functions could be connected to the field: SELECT
cohorts.id,
cohorts.title,
(
SELECT json_agg(cohort_events)
FROM
(
SELECT events.name FROM
cohorts_events
INNER JOIN events ON cohorts_events.event_id = events.id
WHERE cohorts_events.cohort_id = cohorts.id
) AS cohort_events
) AS events ${/* @type:{name: string}[] */ sql``}
FROM
cohorts; or SELECT
cohorts.id,
cohorts.title,
(
SELECT json_agg(cohort_events)
FROM
(
SELECT events.name FROM
cohorts_events
INNER JOIN events ON cohorts_events.event_id = events.id
WHERE cohorts_events.cohort_id = cohorts.id
) AS cohort_events
) AS events ${type('{name: string}[]')}
FROM
cohorts; |
Oh, just thought of something else - not sure if this is the same idea as what you mentioned here: Running subqueries recursively when they are encountered in the query, to find the type of the data, if the column is type |
It seems like pg comments aren't parsed at the moment. About your suggestion - While I understand how it somewhat solves your issue, It doesn't feel like a future-proof code. Queries are volatile, and comments may get disconnected from the logic. I think gradually adding support for json type inference would be more promising. |
Ok, thanks for the note. I'll open an issue and link to here and the other issue and we can see where this goes 👍 |
Update - #190 (comment) |
If you're using options like |
Describe the bug
A clear and concise description of what the bug is.
SafeQL doesn't accept types for json, other than
any
. Using thejson_agg
aggregate function makes SafeQL think that the returning column must beany
type and throws an error otherwise. The query below throws anincorrect type annotation
if any other type is used for thereturningJson
other thanany
To Reproduce
Steps to reproduce the behavior:
.ts
fileExpected behavior
A clear and concise description of what you expected to happen.
I expected that using these kinds of aggregate functions shouldn't cause an error, when i am using the type the query should return or that SafeQL would suggest types other than
any
Screenshots
If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: