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

feat: enhance .drafts properties with draft table properties #354

Merged
merged 11 commits into from
Oct 24, 2024

Conversation

stockbal
Copy link
Contributor

@stockbal stockbal commented Oct 7, 2024

Hi @daogrady,

this is a proposal for enhancing the type behind the .drafts property of draft enabled classes, to include the additional fields that will be generated for draft tables.

At the same time it would also overwork .drafts at the plural entity, to use an enhanced version of the generated plural class of an entity, so fixes #163.

P.S.: tests do not yet exist. I will create them if you think we can go ahead with this approach

Tasks

  • add test cases

Sample

// cat-service.cds
service CatalogService {
  @odata.draft.enabled
  entity Books : cuid { }
}

Generated classes

// /_/index.ts
export type DraftEntity<T> = T & {
    IsActiveEntity?: boolean | null
    HasActiveEntity?: boolean | null
    HasDraftEntity?: boolean | null
    DraftAdministrativeData_DraftUUID?: string | null
}
export type DraftOf<T> = { new(...args: any[]): DraftEntity<T> }
export type DraftsOf<T> = typeof Array<DraftEntity<T>>

// /CatalogService/index.ts
export class Book extends _BookAspect(__.Entity) {static drafts: __.DraftOf<Book>}
export class Books extends Array<Book> {static drafts: __.DraftsOf<Book>}

Explanation about types

The type DraftEntity<T> is exported so users can type their variables or parameters to a draft entity

import { Book } from "#cds-models/CatalogService";
import { DraftEntity } from "#cds-models/_";

function getBookDraft(): DraftEntity<Book> {}

Side note

Fixes auto-closed issue #100

lib/visitor.js Outdated Show resolved Hide resolved
lib/visitor.js Outdated Show resolved Hide resolved
@stockbal stockbal changed the title feat: create dedicated draft classes for draft enabled entities feat: enhance .drafts properties with draft properties Oct 24, 2024
@stockbal stockbal changed the title feat: enhance .drafts properties with draft properties feat: enhance .drafts properties with draft table properties Oct 24, 2024
@stockbal stockbal marked this pull request as ready for review October 24, 2024 12:37
Copy link
Contributor

@daogrady daogrady left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for yet another cool contribution! 🙂

@daogrady daogrady merged commit 2a033cc into cap-js:main Oct 24, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plural.drafts could be typeof Plural
2 participants