Skip to content

Commit 046fadc

Browse files
Akatuorochgeo
andauthored
Migrate to Typescript (#1267)
- migrate srv to typescript - include @cds/typer for generated types -> converted some queries to fluent API - use ts-jest for tests - use typescript build task for mta deployment Using tsx for faster local development cycles compared to ts-node. Also, to get all the tests green: - mocha with tsx (needs tsx installed locally) - linting for both js and ts files - adjusted workflow to install the global dependencies (@sap/cds-dk, typescript, tsx) and run cds-typer - same as local setup steps --------- Co-authored-by: Christian Georgi <[email protected]>
1 parent b4c9768 commit 046fadc

File tree

14 files changed

+1866
-693
lines changed

14 files changed

+1866
-693
lines changed

.github/workflows/deploy-btp.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ jobs:
7474
ui_app_url: ${{ steps.deploy.outputs.url }}
7575

7676
steps:
77-
- uses: actions/checkout@v2
78-
- uses: actions/setup-node@v2
77+
- uses: actions/checkout@v4
78+
- uses: actions/setup-node@v4
7979
with:
8080
node-version: 16
81-
- uses: actions/cache@v2
81+
- uses: actions/cache@v4
8282
id: cache
8383
with:
8484
path: ${{ inputs.mtar-dir }}/${{ inputs.mtar-file }}

.github/workflows/maven.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ jobs:
2020
java-version: [17,20]
2121

2222
steps:
23-
- uses: actions/checkout@v2
23+
- uses: actions/checkout@v4
2424

2525
- name: Set up Java ${{ matrix.java-version }}
26-
uses: actions/setup-java@v1
26+
uses: actions/setup-java@v4
2727
with:
2828
java-version: ${{ matrix.java-version }}
29+
distribution: 'sapmachine'
2930

3031
- name: Build with Maven
3132
run: mvn -B clean verify

.github/workflows/node.js.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
node-version: [20.x, 18.x]
21+
node-version: [22.x, 18.x]
2222

2323
steps:
24-
- uses: actions/checkout@v2
24+
- uses: actions/checkout@v4
2525
- name: Use Node.js ${{ matrix.node-version }}
26-
uses: actions/setup-node@v2
26+
uses: actions/setup-node@v4
2727
with:
2828
node-version: ${{ matrix.node-version }}
29+
- run: npm i @sap/cds-dk typescript tsx
2930
- run: npm ci
31+
- run: npx cds-typer "*"
3032
- run: npm run lint
3133
- run: npm run build --if-present
3234
- run: npm run test

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,5 @@ dist/
5454
# Experimental
5555
xxx_*
5656
xxx/*
57+
58+
@cds-models

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@ You can also use the ALP with the standard OData parser, but then some features
2727

2828
### Build and Run - Node.js Backend
2929

30+
Prerequisite:
31+
```
32+
npm i -g @sap/cds-dk typescript tsx
33+
```
34+
3035
In the root folder of your project, run
3136
```
3237
npm ci
33-
cds watch
38+
npx cds-typer "*"
39+
cds-tsx watch
3440
```
3541

3642
### Build and Run - Java Backend

db/schema.cds

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace sap.fe.cap.travel;
1111

1212
entity Travel : managed {
1313
key TravelUUID : UUID;
14-
TravelID : Integer @readonly default 0;
14+
TravelID : Integer default 0 @readonly;
1515
BeginDate : Date;
1616
EndDate : Date;
1717
BookingFee : Decimal(16, 3);
@@ -70,20 +70,24 @@ entity BookingSupplement : managed {
7070
// Code Lists
7171
//
7272

73+
type BookingStatusCode : String(1) enum {
74+
New = 'N';
75+
Booked = 'B';
76+
Canceled = 'X';
77+
};
78+
7379
entity BookingStatus : CodeList {
74-
key code : String(1) enum {
75-
New = 'N';
76-
Booked = 'B';
77-
Canceled = 'X';
78-
};
80+
key code : BookingStatusCode
81+
};
82+
83+
type TravelStatusCode : String(1) enum {
84+
Open = 'O';
85+
Accepted = 'A';
86+
Canceled = 'X';
7987
};
8088

8189
entity TravelStatus : CodeList {
82-
key code : String(1) enum {
83-
Open = 'O';
84-
Accepted = 'A';
85-
Canceled = 'X';
86-
} default 'O'; //> will be used for foreign keys as well
90+
key code : TravelStatusCode default 'O'; //> will be used for foreign keys as well
8791
fieldControl: Integer @odata.Type:'Edm.Byte'; // 1: #ReadOnly, 7: #Mandatory
8892
createDeleteHidden: Boolean;
8993
insertDeleteRestriction: Boolean; // = NOT createDeleteHidden

eslint.config.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
'use strict'
22

33
const eslint_js = require('@eslint/js')
4+
const tseslint = require('typescript-eslint');
45
const globals = require('globals')
56

67
module.exports = [
78
{
8-
ignores: ["**/dist/*"]
9+
ignores: ["**/dist/*", "gen/**/*", "@cds-models/**/*"]
910
},
1011
// global rules for all files
1112
eslint_js.configs.recommended,
13+
tseslint.configs.base,
1214
// Generic config for JavaScript files: Setup environment, version, etc.
1315
{
14-
files: ['**/*.js'],
16+
files: ['**/*.js', '**/*.ts'],
1517
languageOptions: {
1618
ecmaVersion: 2022,
17-
sourceType: 'commonjs',
19+
sourceType: 'module',
1820
globals: {
1921
...globals.node,
2022
...globals.jest,

karma-cap-middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async function node() {
6969
}
7070
};
7171

72-
const serverUrl = await spawnServer("npm", ["start"], "../..", isReady);
72+
const serverUrl = await spawnServer("cds-tsx", ["serve"], "../..", isReady);
7373

7474
return createKarmaMiddleware(serverUrl, { user: "admin", password: "admin" });
7575
}

0 commit comments

Comments
 (0)