Skip to Content
0
Nov 24, 2020 at 09:37 PM

Insert into underlying table via Hook Method in CAP

88 Views

Hello Everyone,

I am trying to do post operation via hook methods . My entity is having calculated column which actually does not exists in the table. It only exists in view.

Following are details

Source code for Tables Both tables are defined in the db folder .

A document has to be assigned only one category. . One Category can be assigned to any no of documents

namespace my.namespace;
entity Categories : cuid, managed {
    name             : db.name not null;
    document         : Association to many Documents
                           on document.category = $self;
    externalResource : Association to many ExternalResources
                           on externalResource.category = $self;
}


entity Documents : cuid, managed {
    filename      : db.name not null;
    size          : Integer not null;
    unit          : String(2);
    // categoryID    : UUID;
    documentExtID : db.extID;
    category      : Association to one Categories;
}


Source code for service which is defined in the srv folder

service Myservice @(path : '/my') {
    entity Messages          as projection on db.Messages excluding {
        createdAt,
        createdBy,
        modifiedAt,
        modifiedBy
    };

    entity Categories        as
        select from db.Categories,
        db.Documents {
            key Categories.ID,
                name,
                @Core.Computed
                count(
                    Documents.ID
                ) as documentCount : Integer
        }
        where
            Categories.document.ID = Documents.ID
        group by
            Categories.ID;

    entity Documents         as projection on db.Documents excluding {
        //    createdAt,
        createdBy,
        modifiedAt,
        modifiedBy
    };
}

Incomplete Handler code . I am able to read request data via req.data . But unable to understand how I can get table name.

const cds = require('@sap/cds')
module.exports = srv => {

  srv.on('CREATE', 'Categories', (req) => {
    console.log('Here I have to execute the insert query so that ID and name get inserted into Categories table.')
  })
}



I tried but req.entities always give me entities

I have below mentioned queries

1 How I can get table name

2 What will be the syntax to execute Insert Query

Please make a note that I am using PostgreSQL

Thanks in advance.

Best Regards,

Sandeep