Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[CAP] CQL Expand Composition / Deep read

In the documentation they always use a star in string literals like this o => o.`*` to select all fields, but when running this it always failed. However, after some tests I found that this format works with brackets o => o.('*')

https://cap.cloud.sap/docs/guides/providing-services/#–-deep-read

entity myEntity: cuid, managed {
    field1          : String;
    comp            : Composition of one myComposition;
}
 
aspect myComposition: cuid, managed {
    myCompField: String;
}
        const result = await SELECT.from(myEntity)
            .columns(e => {
                e('*')
                e.comp(c => c.myCompField) //expand composition, but only select 'myCompField'
            })

Update 14.09.2023: is now fixed (#)

[CAP] CQL Deep Update

A Composition of one can be updated via the Parent Entity, like we would do it, when dealing with an Association to one.

entity myEntity: cuid, managed {
    field1          : String;
    comp            : Composition of one myComposition;
}

aspect myComposition: cuid, managed {
    myCompField: String;
}
            const { myEntity } = srv.entities
            const EntityComposition = srv.entities['myEntity.myComposition'] // composition

            srv.after('CREATE', EntityComposition , async (comp, req) => {
                await UPDATE(myEntity, comp.ID)
                    .set({
                      comp : [{ myCompField: 'test' }]
                    })
            })

A Composition of many must be updated via the actual Composition Entity.

entity myEntity: cuid, managed {
    field1          : String;
    comp            : Composition to many myComposition;
}

aspect myComposition: cuid, managed {
    myCompField: String;
}
    const EntityComposition = srv.entities['myEntity.myComposition'] // composition

    srv.after('CREATE', EntityComposition , async (comp, req) => {
        await UPDATE(EntityComposition, { up__ID: comp.up__ID, ID: comp.ID }).set({ myCompField: 'test' })
    })

[CAP] Invoke custom handlers when querying local entity

const cds = require('@sap/cds');

module.exports = async srv => {

     const { Objects } = srv.entities // entities from myService.cds
   
     srv.on("myAction", async req => {
        const query = SELECT.one.from(Objects).where({ id: req.data.myId })
        const srv = await cds.connect.to('myService')
        const data = await srv.run(query)
        console.log(data) 
        return data
    })

    srv.on("READ", Objects, async req => {
        console.log("Objects called")
        // Select data from db or forward query to external system
        // ...
        // return data
    })

}

[ABAP] Parse SF Entity Key from URI

  METHOD get_entitykey_from_uri.

    " Pattern: Entity\((.*)\), 
    DATA(pattern) = iv_entity && `\((.*)\)`.

    TRY.
        DATA(matcher) = cl_abap_matcher=>create( pattern     = pattern
                                                 text        = iv_uri
                                                 ignore_case = abap_true ).
      CATCH cx_sy_invalid_regex.
    ENDTRY.

    TRY.
        DATA(lt_matches) = matcher->find_all( ).
        ASSIGN lt_matches[ 1 ] TO FIELD-SYMBOL(<s>).

        rv_entity_key = substring( val = iv_uri off = <s>-offset len = <s>-length ). 

      CATCH cx_sy_no_current_match.
    ENDTRY.

  ENDMETHOD.