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

Deep Required Projections #240

Merged
merged 2 commits into from
Sep 24, 2024
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
### Added
- The CQL methods `.where` and `.having` now suggest property names for certain overloads.

### Changed
- Properties of entities are no longer optional in projections, eliminating the need to perform optional chaining on them when using nested projections

## Version 0.6.5 - 2024-08-13
### Fixed
Expand Down
4 changes: 2 additions & 2 deletions apis/internal/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { entity } from '../linked/classes'
import type { column_expr } from '../cqn'
import type { ArrayConstructable, Constructable, SingularInstanceType, Unwrap, UnwrappedInstanceType } from './inference'
import { ConstructedQuery } from '../ql'
import { KVPairs } from './util'
import { KVPairs, DeepRequired } from './util'

// https://cap.cloud.sap/docs/node.js/cds-ql?q=projection#projection-functions
type Projection<T> = (e: QLExtensions<T extends ArrayConstructable ? SingularInstanceType<T> : T>) => void
Expand Down Expand Up @@ -161,7 +161,7 @@ export interface InUpsert<T> {
}

// don't wrap QLExtensions in more QLExtensions (indirection to work around recursive definition)
export type QLExtensions<T> = T extends QLExtensions_<any> ? T : QLExtensions_<T>
export type QLExtensions<T> = T extends QLExtensions_<any> ? T : QLExtensions_<DeepRequired<T>>

/**
* QLExtensions are properties that are attached to entities in CQL contexts.
Expand Down
7 changes: 7 additions & 0 deletions apis/internal/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ type KVPairs<T,K,V> = T extends []
: T extends [K, V, ...infer R]
? KVPairs<R,K,V>
: false

/**
* Recursively excludes nullability from all properties of T.
*/
export type DeepRequired<T> = {
[K in keyof T]: DeepRequired<T[K]>
} & Exclude<Required<T>, null>
3 changes: 2 additions & 1 deletion test/typescript/apis/project/cds-ql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ SELECT.from(Foos).where('fn(x) = ', 42)
sel.from(Foos).columns('y')
sel.from(Foo).columns('y')
sel.columns("y")
SELECT.from(Foos, f => f.ref(r => r.x)) // ref should be callable without optional chaining (DeepRequired)

SELECT.from(Foos).orderBy('x') // x auto completed
SELECT.from(Foos).orderBy('y') // non-columns also still possible

SELECT.from(Foos, f => {
f.name,
f.x,
// @ts-expect-error - foobar is not a valid column
f.foobar
})
Expand Down