Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/core/postgrest-js/src/select-query-parser/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,17 @@ type ResolveJsonPathType<
? PathResult extends string
? // Use the result if it's a string as we know that even with the string accessor ->> it's a valid type
PathResult
: IsStringUnion<PathResult> extends true
? // Use the result if it's a union of strings
: IsStringUnion<Exclude<PathResult, null>> extends true
? // Use the result if it's a union of strings (even if nullable)
PathResult
: CastType extends 'json'
? // If the type is not a string, ensure it was accessed with json accessor ->
: IsStringUnion<PathResult> extends true
? // Use the result if it's a union of strings (non-nullable)
PathResult
: // Otherwise it means non-string value accessed with string accessor ->> use the TypeScriptTypes result
TypeScriptTypes<CastType>
: CastType extends 'json'
? // If the type is not a string, ensure it was accessed with json accessor ->
PathResult
: // Otherwise it means non-string value accessed with string accessor ->> use the TypeScriptTypes result
TypeScriptTypes<CastType>
: TypeScriptTypes<CastType>
: // No json path, use regular type casting
TypeScriptTypes<CastType>
Expand Down
14 changes: 7 additions & 7 deletions packages/core/postgrest-js/src/select-query-parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ export type JsonPathToAccessor<Path extends string> = Path extends `${infer P1}-
export type JsonPathToType<T, Path extends string> = Path extends ''
? T
: ContainsNull<T> extends true
? JsonPathToType<Exclude<T, null>, Path>
? JsonPathToType<Exclude<T, null>, Path> | null
: Path extends `${infer Key}.${infer Rest}`
? Key extends keyof T
? JsonPathToType<T[Key], Rest>
Expand All @@ -627,13 +627,13 @@ export type JsonPathToType<T, Path extends string> = Path extends ''
? T[Path]
: never

export type IsStringUnion<T> = string extends T
export type IsStringUnion<T> = [T] extends [never]
? false
: T extends string
? [T] extends [never]
? false
: true
: false
: string extends T
? false
: T extends string
? true
: false

type MatchingFunctionBySetofFrom<
Fn extends GenericFunction,
Expand Down
46 changes: 45 additions & 1 deletion packages/core/postgrest-js/test/advanced_rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,8 @@ describe('advanced rpc', () => {
"age_range": "[20,30)",
"catchphrase": "'json' 'test'",
"data": {
"foo": {
"foo": "string value",
"fooRecord": {
"bar": {
"nested": "value",
},
Expand All @@ -1179,6 +1180,49 @@ describe('advanced rpc', () => {
"status": "ONLINE",
"username": "jsonuser",
},
{
"age_range": "[20,30)",
"catchphrase": "'json' 'null' 'test'",
"data": {
"bar": null,
"en": "ONE",
"foo": "string value",
"fooRecord": {
"bar": null,
"baz": "string value",
},
},
"status": "ONLINE",
"username": "jsonusernull",
},
{
"age_range": "[20,30)",
"catchphrase": "'json' 'obj' 'test'",
"data": {
"bar": {
"baz": 42,
},
"en": "TWO",
"fooRecord": {
"bar": {
"nested": "deep",
},
"baz": "test",
},
},
"status": "ONLINE",
"username": "jsonuserobj",
},
{
"age_range": "[20,30)",
"catchphrase": "'json' 'missing' 'test'",
"data": {
"en": "THREE",
"foo": "string",
},
"status": "ONLINE",
"username": "jsonusermissing",
},
],
"error": null,
"status": 200,
Expand Down
Loading
Loading