Skip to content

Commit

Permalink
feat: enhance .drafts properties with draft table properties (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
stockbal authored Oct 24, 2024
1 parent 8fd58ce commit 2a033cc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lib/components/basedefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ export type EntitySet<T> = T[] & {
data (input:object) : T
};
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>>
export type DeepRequired<T> = {
[K in keyof T]: DeepRequired<T[K]>
} & Exclude<Required<T>, null>;
Expand Down
16 changes: 16 additions & 0 deletions lib/components/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ const createKey = t => `${base}.Key<${t}>`
*/
const createKeysOf = t => `${base}.KeysOf<${t}>`

/**
* Wraps type into DraftOf type.
* @param {string} t - the type name.
* @returns {string}
*/
const createDraftOf = t => `${base}.DraftOf<${t}>`

/**
* Wraps type into DraftsOf type.
* @param {string} t - the type name.
* @returns {string}
*/
const createDraftsOf = t => `${base}.DraftsOf<${t}>`

/**
* Wraps type into ElementsOf type.
* @param {string} t - the type name.
Expand Down Expand Up @@ -114,6 +128,8 @@ const stringIdent = s => `'${s}'`

module.exports = {
createArrayOf,
createDraftOf,
createDraftsOf,
createKey,
createKeysOf,
createElementsOf,
Expand Down
10 changes: 6 additions & 4 deletions lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { SourceFile, FileRepository, Buffer, Path } = require('./file')
const { FlatInlineDeclarationResolver, StructuredInlineDeclarationResolver } = require('./components/inline')
const { Resolver } = require('./resolution/resolver')
const { LOG } = require('./logging')
const { docify, createPromiseOf, createUnionOf, createKeysOf, createElementsOf, stringIdent } = require('./components/wrappers')
const { docify, createPromiseOf, createUnionOf, createKeysOf, createElementsOf, stringIdent, createDraftsOf, createDraftOf } = require('./components/wrappers')
const { csnToEnumPairs, propertyToInlineEnumName, isInlineEnumType, stringifyEnumType } = require('./components/enum')
const { isReferenceType } = require('./components/reference')
const { empty } = require('./components/typescript')
Expand Down Expand Up @@ -336,9 +336,11 @@ class Visitor {
/**
* @param {string} fq - fully qualified name of the entity
* @param {string} clean - the clean name of the entity
* @param {boolean} [isPlural] - `true` if passed entity is plural
*/
#staticClassContents(fq, clean) {
return isDraftEnabled(fq) ? [`static drafts: typeof ${clean}`] : []
#staticClassContents(fq, clean, isPlural = false) {
if (!isDraftEnabled(fq)) return []
return [`static drafts: ${isPlural ? createDraftsOf(clean) : createDraftOf(clean)}`]
}

/**
Expand Down Expand Up @@ -409,7 +411,7 @@ class Visitor {
}
// plural can not be a type alias to $singular[] but needs to be a proper class instead,
// so it can get passed as value to CQL functions.
const additionalProperties = this.#staticClassContents(fq, singular)
const additionalProperties = this.#staticClassContents(fq, singular, true)
additionalProperties.push('$count?: number')
buffer.add(docify(entity.doc))
buffer.add(`export class ${plural} extends Array<${singular}> {${additionalProperties.join('\n')}}`)
Expand Down
1 change: 1 addition & 0 deletions test/unit/files/draft/model.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
using from './catalog-service';
31 changes: 31 additions & 0 deletions test/unit/files/draft/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import cds from '@sap/cds'
import {DraftEntity} from '#cds-models/_'
import {Books, Publishers} from '#cds-models/bookshop/service/CatalogService'

export class CatalogService extends cds.ApplicationService {
async init() {

this.after("READ", Publishers.drafts, publishers => {
if (!publishers?.length) return
publishers.forEach(p => {
p.name
p.HasActiveEntity
p.HasDraftEntity
p.IsActiveEntity
p.DraftAdministrativeData_DraftUUID
})
})

this.after("READ", Books.drafts, books => {
if (!books?.length) return
books.forEach(b => {
b.title
b.HasActiveEntity
b.HasDraftEntity
b.IsActiveEntity
b.DraftAdministrativeData_DraftUUID
})
})
return super.init()
}
}

0 comments on commit 2a033cc

Please sign in to comment.