diff --git a/CHANGELOG.md b/CHANGELOG.md index c5c33300..6d682783 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/apis/ql.d.ts b/apis/ql.d.ts index fc933787..ab95470d 100644 --- a/apis/ql.d.ts +++ b/apis/ql.d.ts @@ -131,17 +131,17 @@ type SELECT_one = // calling with class ( (entityType: T, projection?: Projection>>) - => Awaitable, SELECT_one>, SingularInstanceType>) + => Awaitable, SELECT_one>, SingularInstanceType | null>) & ( (entityType: T, primaryKey: PK, projection?: Projection>>) - => Awaitable, SELECT_one>, SingularInstanceType>) + => Awaitable, SELECT_one>, SingularInstanceType | null>) & ((entity: EntityDescription, primaryKey?: PK, projection?: Projection) => SELECT<_TODO, SELECT_one>) - & ( (entity: T[], projection?: Projection) => Awaitable, T>) - & ( (entity: T[], primaryKey: PK, projection?: Projection) => Awaitable, T>) - & ( (entity: { new(): T }, projection?: Projection) => Awaitable, T>) - & ( (entity: { new(): T }, primaryKey: PK, projection?: Projection) => Awaitable, T>) + & ( (entity: T[], projection?: Projection) => Awaitable, T | null>) + & ( (entity: T[], primaryKey: PK, projection?: Projection) => Awaitable, T | null>) + & ( (entity: { new(): T }, projection?: Projection) => Awaitable, T | null>) + & ( (entity: { new(): T }, primaryKey: PK, projection?: Projection) => Awaitable, T | null>) & ((subject: ref) => SELECT<_TODO>) type SELECT_from = @@ -155,7 +155,7 @@ type SELECT_from = & ( (entityType: E, primaryKey: PK, projection?: Projection>) - => Awaitable>, SingularInstanceType>) // when specifying a key, we expect a single element as result + => Awaitable>, SingularInstanceType | null>) // when specifying a key, we expect a single element as result // calling with definition & ((entity: EntityDescription, primaryKey?: PK, projection?: Projection) => SELECT) // calling with concrete list diff --git a/test/typescript/apis/project/cds-ql.ts b/test/typescript/apis/project/cds-ql.ts index 34bc327c..c30c4e0c 100644 --- a/test/typescript/apis/project/cds-ql.ts +++ b/test/typescript/apis/project/cds-ql.ts @@ -114,7 +114,7 @@ DELETE(Foo) DELETE(Foo, Foos) DELETE([Foo, Foos]) -let selectOne: Foo +let selectOne: Foo | null let selectMany: Foos // SINGULAR TESTS @@ -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') @@ -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])