cap-js / attachments

The @cap-js/attachments package is a CDS plugin that provides out-of-the box asset storage and handling by using an aspect Attachments. It also provides a CAP-level, easy to use integration of the SAP Object Store.
Apache License 2.0
13 stars 4 forks source link

[Version: 1.1.5] cds-typer error with Attachments aspect. #97

Open vc10812 opened 2 months ago

vc10812 commented 2 months ago

As described in the link https://github.com/cap-js/attachments?tab=readme-ov-file#use-attachments, I set up Attachments, but there was an issue where TypeScript code generated by cds-typer resulted in errors and could not compile.

using {Attachments as AttachmentsAspect} from '@cap-js/attachments';

entity NewsVault : cuid, managed {
  category        : Association to one Categories;
  articletitle    : String(100);
  articlesubtitle : String(100);
  details         : LargeString;
  rating          : Integer;
  articlepriority : Association to one Prioritylevels;
  publishedflg    : Boolean default false;
  customers       : Composition of many NewsCustomers
                      on customers.article = $self;
  attachments     : Composition of many Attachments
  articleKeywords : Composition of many NewsKeywords
                      on articleKeywords.article = $self;
  schedulePost    : Timestamp;
  likes           : Composition of many NewsLikes
                      on likes.article = $self;
  bookmarks       : Composition of NewsBookmarks
                      on  bookmarks.article = $self
                      and bookmarks.userID  = $user.id;
}

Therefore, by adding an entity as shown below, the issue was resolved.

using {Attachments as AttachmentsAspect} from '@cap-js/attachments';

entity AttachmentsNewsVault : AttachmentsAspect {
  article : Association to NewsVault;
}

entity NewsVault : cuid, managed {
  category        : Association to one Categories;
  articletitle    : String(100);
  articlesubtitle : String(100);
  details         : LargeString;
  rating          : Integer;
  articlepriority : Association to one Prioritylevels;
  publishedflg    : Boolean default false;
  customers       : Composition of many NewsCustomers
                      on customers.article = $self;
  attachments     : Composition of many AttachmentsNewsVault
                      on attachments.article = $self;
  articleKeywords : Composition of many NewsKeywords
                      on articleKeywords.article = $self;
  schedulePost    : Timestamp;
  likes           : Composition of many NewsLikes
                      on likes.article = $self;
  bookmarks       : Composition of NewsBookmarks
                      on  bookmarks.article = $self
                      and bookmarks.userID  = $user.id;
}

The cause might be that specifying the aspect directly as Composition was the issue. Is it possible to fix it so that no errors occur?

muskansethi1 commented 2 months ago

Hi @vc10812,

Can you please share the steps to reproduce the issue?

vc10812 commented 1 month ago

[Using TypeScript | capire (cloud.sap)](https://cap.cloud.sap/docs/node.js/typescript)

https://cap.cloud.sap/docs/tools/cds-typer

Based on the above documents, it is necessary to set up cds-typer and TypeScript. Additionally, you need to create custom services for the services in TypeScript

https://github.com/SAP-archive/btp-full-stack-typescript-app.

I believe you can reproduce the issue by adding cds-typer and attachments to the above sample code and adding TypeScript code.

muskansethi1 commented 1 week ago

Hi @vc10812,

I was investigating this issue with this cds and noted the following:

entity Books {
    key ID: Integer;
    reviews: Composition of many Attachments;
    check: Composition of many b;
}

entity b{
    key ID: Integer;
    ...
}

When in cds:

Composition of many Attachments

is being defined, the typescript generated has :

__.Composition.of.many<Attachments>

where Attachments is not getting resolved, and hence throwing an error.

Whereas, when an entity is being used instead:

Composition of many b

in ts:

__.Composition.of.many<b_>

gets generated where

b_ : class b_ extends Array<b> {$count?: number}

definition is there.

The same definition is missing in case of aspects

Since, for any aspect, the case is same, you can raise the issue here: https://github.com/cap-js/cds-typer/issues