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

fix: adds missing type inferences for entries of IN-/UPSERT #351

Merged
merged 3 commits into from
Dec 3, 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
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).
The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Version 0.9.0 - tbd
### Added
- Adds missing properties for `log` in `cds.env`

### Fixed

- Use `Required` instead of `DeepRequired` in projection function to avoid complexity errors from TypeScript

### Fixed
- Added missing type inference for `.set`/`.with` of `UPDATE`
- Added missing type inference for `.entries` of `UPSERT` and `INSERT`

## Version 0.8.0 - 24-11-26

Expand Down
3 changes: 2 additions & 1 deletion apis/internal/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export interface And {
export interface InUpsert<T> {
data (block: (e: T) => void): this

entries (...entries: object[]): this
entries (...entries: T[]): this
entries (entries: T[]): this

values (...val: (null | Primitive)[]): this
values (val: (null | Primitive)[]): this
Expand Down
18 changes: 10 additions & 8 deletions apis/ql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,13 @@ export interface INSERT<T> extends Columns<T>, InUpsert<T> {}
export class INSERT<T> extends ConstructedQuery<T> {
private constructor();

static into: (<T extends ArrayConstructable> (entity: T, entries?: Entries) => INSERT<SingularInstanceType<T>>)
static into: (<T extends ArrayConstructable> (entity: T, ...entries: SingularInstanceType<T>[]) => INSERT<SingularInstanceType<T>>)
& (<T extends ArrayConstructable> (entity: T, entries?: SingularInstanceType<T>[]) => INSERT<SingularInstanceType<T>>)
& (TaggedTemplateQueryPart<INSERT<unknown>>)
& ((entity: EntityDescription, ...entries: Entries[]) => INSERT<StaticAny>)
& ((entity: EntityDescription, entries?: Entries) => INSERT<StaticAny>)
& (<T> (entity: Constructable<T>, entries?: Entries) => INSERT<T>)
& (<T> (entity: T, entries?: T | Entries) => INSERT<T>)
& (<T> (entity: Constructable<T>, ...entries: T[]) => INSERT<T>)
& (<T> (entity: Constructable<T>, entries?: T[]) => INSERT<T>)

/**
* @deprected
Expand All @@ -207,13 +209,13 @@ export interface UPSERT<T> extends Columns<T>, InUpsert<T> {}
export class UPSERT<T> extends ConstructedQuery<T> {
private constructor();

static into: (<T extends ArrayConstructable> (entity: T, entries?: Entries) => UPSERT<SingularInstanceType<T>>)
static into: (<T extends ArrayConstructable> (entity: T, ...entries: SingularInstanceType<T>[]) => UPSERT<SingularInstanceType<T>>)
& (<T extends ArrayConstructable> (entity: T, entries?: SingularInstanceType<T>[]) => UPSERT<SingularInstanceType<T>>)
& (TaggedTemplateQueryPart<UPSERT<StaticAny>>)
& ((entity: EntityDescription, ...entries: Entries[]) => UPSERT<StaticAny>)
& ((entity: EntityDescription, entries?: Entries) => UPSERT<StaticAny>)
& (<T> (entity: Constructable<T>, entries?: Entries) => UPSERT<T>)
// currently no easy way to restrict T to non-primitives
& (<T> (entity: T, entries?: T | Entries) => UPSERT<T>)

& (<T> (entity: Constructable<T>, ...entries: T[]) => UPSERT<T>)
& (<T> (entity: Constructable<T>, entries?: T[]) => UPSERT<T>)

UPSERT: CQN.UPSERT['UPSERT']

Expand Down
42 changes: 41 additions & 1 deletion test/typescript/apis/project/cds-ql.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Definition } from '../../../../apis/csn';
import { QLExtensions } from '../../../../apis/ql'
import { linked } from '../../../../apis/models';
import { Foo, Foos, attach } from './dummy'
Expand Down Expand Up @@ -88,7 +89,7 @@ SELECT(Foos) === SELECT.from(Foos)

INSERT.into(Foos).columns("x") // x was suggested by code completion
let ins: INSERT<Foo>
ins = INSERT.into(Foos, {})
ins = INSERT.into(Foos, { x: 4 })
ins.into(Foos)
ins.into(Foos)
ins.columns("x") // x was suggested by code completion
Expand Down Expand Up @@ -285,6 +286,45 @@ INSERT.into('Foos').rows([[1,2,3],[1,2]])
// @ts-expect-error
INSERT.into('Foos').values([[1,2,3]])

// Type checks with typed INSERT
// @ts-expect-error - invalid property of Foo
INSERT.into(Foos).entries({ a: "" })
// @ts-expect-error - invalid property of Foo
INSERT.into(Foos).entries([{ a: "" }])
INSERT.into(Foos).entries({ x: 4, ref: { x: 4 }, refs: [] })
INSERT.into(Foo).entries({ x: 4 }, { x: 1 }, { x: 4, ref: { x: 1 } })
// @ts-expect-error - invalid type for property x of Foo
INSERT.into(Foo).entries({ x: "4" })
INSERT.into(Foo, { x: 4, ref: { x: 2 }})
INSERT.into(Foo, [{ x: 4 }])

INSERT.into("Foo", [{ x: "4" }])
INSERT.into("Foo", { x: "4" }, { "ref": "4" })

INSERT.into({} as Definition, { x : 4, "other": 5 }, { a : 4})
INSERT.into({} as Definition, [{ x : 4, "other": 5 }, { a : 4}])
INSERT.into({} as linked.classes.entity, { "a": 4 })

// Type checks with typed UPSERTs
// @ts-expect-error - invalid property of Foo
UPSERT.into(Foos).entries({ a: "" })
// @ts-expect-error - invalid property of Foo
UPSERT.into(Foos).entries([{ a: "" }])
UPSERT.into(Foos).entries({ x: 4, ref: { x: 4 }, refs: [] })
UPSERT.into(Foo).entries({ x: 4 }, { x: 1 }, { x: 4, ref: { x: 1 } })
// @ts-expect-error - invalid type for property x of Foo
UPSERT.into(Foo).entries({ x: "4" })
UPSERT.into(Foo, { x: 4, ref: { x: 2 }})
UPSERT.into(Foo, [{ x: 4 }])

UPSERT.into("Foo", [{ x: "4" }])
UPSERT.into("Foo", { x: "4" }, { "ref": "4" })

UPSERT.into({} as Definition, { x : 4, "other": 5 }, { a : 4})
UPSERT.into({} as Definition, [{ x : 4, "other": 5 }])

UPSERT.into({} as linked.classes.entity, { "a": 4 })

// UPDATE: checks with typed classes
UPDATE(Foo, 42).set({ x: 4}).where({ x: 44 })
UPDATE(Foos, 42).set({ x: 4}).where({ x: 44 })
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/apis/project/cds-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const up: UPSERT<Foos> = await srv.upsert(Foos)
const d: DELETE<Foos> = await srv.delete(Foos)

// queries
const query = INSERT.into({}, { ID: 111, name: 'Mark Twain' })
const query = INSERT.into("Authors", { ID: 111, name: 'Mark Twain' })
await srv.run(query)
await srv.run([query, query])
await srv.run('SELECT * from Authors where name like ?', ['%Poe%'])
Expand Down
Loading