Skip to content

Commit

Permalink
Make results of SELECT.one nullable (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
daogrady authored Dec 17, 2024
1 parent 81f3bd2 commit e151740
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
- Use `Required` instead of `DeepRequired` in projection function to avoid complexity errors from TypeScript
- Added missing type inference for `.set`/`.with` of `UPDATE`
- Added missing type inference for `.entries` of `UPSERT` and `INSERT`
- Variants of `SELECT.one(T)` will now return `T | null`, instead of `T`

## Version 0.8.0 - 24-11-26

Expand Down
14 changes: 7 additions & 7 deletions apis/ql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,17 @@ type SELECT_one =
// calling with class
(<T extends ArrayConstructable>
(entityType: T, projection?: Projection<QLExtensions<SingularInstanceType<T>>>)
=> Awaitable<SELECT<SingularInstanceType<T>, SELECT_one>, SingularInstanceType<T>>)
=> Awaitable<SELECT<SingularInstanceType<T>, SELECT_one>, SingularInstanceType<T> | null>)
&
(<T extends ArrayConstructable>
(entityType: T, primaryKey: PK, projection?: Projection<QLExtensions<SingularInstanceType<T>>>)
=> Awaitable<SELECT<SingularInstanceType<T>, SELECT_one>, SingularInstanceType<T>>)
=> Awaitable<SELECT<SingularInstanceType<T>, SELECT_one>, SingularInstanceType<T> | null>)

& ((entity: EntityDescription, primaryKey?: PK, projection?: Projection<unknown>) => SELECT<_TODO, SELECT_one>)
& (<T> (entity: T[], projection?: Projection<T>) => Awaitable<SELECT<T, SELECT_one>, T>)
& (<T> (entity: T[], primaryKey: PK, projection?: Projection<T>) => Awaitable<SELECT<T, SELECT_one>, T>)
& (<T> (entity: { new(): T }, projection?: Projection<T>) => Awaitable<SELECT<T, SELECT_one>, T>)
& (<T> (entity: { new(): T }, primaryKey: PK, projection?: Projection<T>) => Awaitable<SELECT<T, SELECT_one>, T>)
& (<T> (entity: T[], projection?: Projection<T>) => Awaitable<SELECT<T, SELECT_one>, T | null>)
& (<T> (entity: T[], primaryKey: PK, projection?: Projection<T>) => Awaitable<SELECT<T, SELECT_one>, T | null>)
& (<T> (entity: { new(): T }, projection?: Projection<T>) => Awaitable<SELECT<T, SELECT_one>, T | null>)
& (<T> (entity: { new(): T }, primaryKey: PK, projection?: Projection<T>) => Awaitable<SELECT<T, SELECT_one>, T | null>)
& ((subject: ref) => SELECT<_TODO>)

type SELECT_from =
Expand All @@ -155,7 +155,7 @@ type SELECT_from =
&
(<E extends ArrayConstructable>
(entityType: E, primaryKey: PK, projection?: Projection<SingularInstanceType<E>>)
=> Awaitable<SELECT<SingularInstanceType<E>>, SingularInstanceType<E>>) // when specifying a key, we expect a single element as result
=> Awaitable<SELECT<SingularInstanceType<E>>, SingularInstanceType<E> | null>) // when specifying a key, we expect a single element as result
// calling with definition
& (<T>(entity: EntityDescription, primaryKey?: PK, projection?: Projection<T>) => SELECT<T>)
// calling with concrete list
Expand Down
19 changes: 10 additions & 9 deletions test/typescript/apis/project/cds-ql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ DELETE(Foo)
DELETE(Foo, Foos)
DELETE([Foo, Foos])

let selectOne: Foo
let selectOne: Foo | null
let selectMany: Foos

// SINGULAR TESTS
Expand All @@ -125,6 +125,7 @@ selectOne = await SELECT(Foo, 42)
selectOne = await SELECT.one.from(Foo)
selectOne = await SELECT.one.from(Foo, 42)
selectOne = await SELECT.one.from(Foo, 42)
const xx = await SELECT.one(Foo, 42)
selectOne = await SELECT.one.from(Foo, 42, f => f.x)
selectOne = await SELECT.one.from(Foo, f => f.x)
selectOne = await SELECT.one.from(Foo).alias('Bars')
Expand Down Expand Up @@ -237,17 +238,17 @@ SELECT.from `Books` .columns ( 'title', {ref:['author','name'],as:'author'} )
SELECT.from `Books` .columns (['title', {ref:['author','name'],as:'author'} ])

// @ts-expect-error invalid key of result
SELECT.one.from(Foos).columns(['entityIDColumn', 'parentIDColumn']).then(r => r.some)
SELECT.one.from(Foos).columns(['entityIDColumn', 'parentIDColumn']).then(r => r.ref)
SELECT.one.from(Foos).columns(['entityIDColumn', 'parentIDColumn']).then(r => r?.some)
SELECT.one.from(Foos).columns(['entityIDColumn', 'parentIDColumn']).then(r => r?.ref)
// @ts-expect-error invalid key of result
SELECT.one.from(Foos).columns('entityIDColumn', 'parentIDColumn').then(r => r.some)
SELECT.one.from(Foos).columns('entityIDColumn', 'parentIDColumn').then(r => r.ref)
SELECT.one.from(Foos).columns('entityIDColumn', 'parentIDColumn').then(r => r?.some)
SELECT.one.from(Foos).columns('entityIDColumn', 'parentIDColumn').then(r => r?.ref)
// @ts-expect-error invalid key of result
SELECT.one.from(Foos).columns([{ ref: ['entityIDColumn'] }]).then(r => r.some)
SELECT.one.from(Foos).columns([{ ref: ['entityIDColumn'] }]).then(r => r.ref)
SELECT.one.from(Foos).columns([{ ref: ['entityIDColumn'] }]).then(r => r?.some)
SELECT.one.from(Foos).columns([{ ref: ['entityIDColumn'] }]).then(r => r?.ref)
// @ts-expect-error invalid key of result
SELECT.one.from(Foos).columns({ ref: ['entityIDColumn'] }).then(r => r.some)
SELECT.one.from(Foos).columns({ ref: ['entityIDColumn'] }).then(r => r.ref)
SELECT.one.from(Foos).columns({ ref: ['entityIDColumn'] }).then(r => r?.some)
SELECT.one.from(Foos).columns({ ref: ['entityIDColumn'] }).then(r => r?.ref)

INSERT.into(Foos).values([1,2,3])

Expand Down

0 comments on commit e151740

Please sign in to comment.