cds-typer icon indicating copy to clipboard operation
cds-typer copied to clipboard

feat: create dedicated draft classes for draft enabled entities

Open stockbal opened this issue 4 months ago • 0 comments

Hi @daogrady,

this is a proposal for creating dedicated classes that represent the .drafts entities of draft enabled entities. At the same time it would also overwork .drafts at the plural entity, to use the new plural drafts class, so fixes #163.

In addition it depends on #348 as I would not create these classes in the current state, where the draft-enabled state is propagated to entities that do not have access to .drafts at runtime.

So, the changes relevant to this PR are in commit 2d0d981a6ec6df6e25c6483fe2e2b2a3375acc1f. 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 class DraftEntity extends Entity {
    declare IsActiveEntity?: boolean | null
    declare HasActiveEntity?: boolean | null
    declare HasDraftEntity?: boolean | null
    declare DraftAdministrativeData_DraftUUID?: string | null
}

// /CatalogService/index.ts
export class BookDraft extends _BookAspect(__.DraftEntity) {
  static readonly kind: 'entity' | 'type' | 'aspect' = 'entity'
  declare static readonly keys: __.KeysOf<BookDraft>
}
export class BookDrafts extends Array<BookDraft> { $count?: number }

export class Book extends _BookAspect(__.Entity) { static drafts: typeof BookDraft }
export class Books extends Array<Book> { static drafts: typeof BookDrafts $count?: number}
// /CatalogService/index.js
// --useEntitiesProxy false
// Books.drafts 
module.exports.BookDraft ={ is_singular: true, __proto__: csn.Books.drafts }
module.exports.BookDrafts = csn.Books.drafts

// --useEntitiesProxy true
// Books.drafts 
module.exports.BookDraft = createEntityProxy(['CatalogService', 'Books', 'drafts'], { target: { is_singular: true } })
module.exports.BookDrafts = createEntityProxy(['CatalogService', 'Books', 'drafts'])

Side note

Fixes auto-closed issue #100

stockbal avatar Oct 07 '24 16:10 stockbal