Skip to content

Commit

Permalink
Migrate to Typescript (#1267)
Browse files Browse the repository at this point in the history
- 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 <christian.georgi@sap.com>
  • Loading branch information
Akatuoro and chgeo authored Nov 7, 2024
1 parent b4c9768 commit 046fadc
Show file tree
Hide file tree
Showing 14 changed files with 1,866 additions and 693 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/deploy-btp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ jobs:
ui_app_url: ${{ steps.deploy.outputs.url }}

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 16
- uses: actions/cache@v2
- uses: actions/cache@v4
id: cache
with:
path: ${{ inputs.mtar-dir }}/${{ inputs.mtar-file }}
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ jobs:
java-version: [17,20]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Java ${{ matrix.java-version }}
uses: actions/setup-java@v1
uses: actions/setup-java@v4
with:
java-version: ${{ matrix.java-version }}
distribution: 'sapmachine'

- name: Build with Maven
run: mvn -B clean verify
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [20.x, 18.x]
node-version: [22.x, 18.x]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm i @sap/cds-dk typescript tsx
- run: npm ci
- run: npx cds-typer "*"
- run: npm run lint
- run: npm run build --if-present
- run: npm run test
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ dist/
# Experimental
xxx_*
xxx/*

@cds-models
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ You can also use the ALP with the standard OData parser, but then some features

### Build and Run - Node.js Backend

Prerequisite:
```
npm i -g @sap/cds-dk typescript tsx
```

In the root folder of your project, run
```
npm ci
cds watch
npx cds-typer "*"
cds-tsx watch
```

### Build and Run - Java Backend
Expand Down
26 changes: 15 additions & 11 deletions db/schema.cds
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace sap.fe.cap.travel;

entity Travel : managed {
key TravelUUID : UUID;
TravelID : Integer @readonly default 0;
TravelID : Integer default 0 @readonly;
BeginDate : Date;
EndDate : Date;
BookingFee : Decimal(16, 3);
Expand Down Expand Up @@ -70,20 +70,24 @@ entity BookingSupplement : managed {
// Code Lists
//

type BookingStatusCode : String(1) enum {
New = 'N';
Booked = 'B';
Canceled = 'X';
};

entity BookingStatus : CodeList {
key code : String(1) enum {
New = 'N';
Booked = 'B';
Canceled = 'X';
};
key code : BookingStatusCode
};

type TravelStatusCode : String(1) enum {
Open = 'O';
Accepted = 'A';
Canceled = 'X';
};

entity TravelStatus : CodeList {
key code : String(1) enum {
Open = 'O';
Accepted = 'A';
Canceled = 'X';
} default 'O'; //> will be used for foreign keys as well
key code : TravelStatusCode default 'O'; //> will be used for foreign keys as well
fieldControl: Integer @odata.Type:'Edm.Byte'; // 1: #ReadOnly, 7: #Mandatory
createDeleteHidden: Boolean;
insertDeleteRestriction: Boolean; // = NOT createDeleteHidden
Expand Down
8 changes: 5 additions & 3 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
'use strict'

const eslint_js = require('@eslint/js')
const tseslint = require('typescript-eslint');
const globals = require('globals')

module.exports = [
{
ignores: ["**/dist/*"]
ignores: ["**/dist/*", "gen/**/*", "@cds-models/**/*"]
},
// global rules for all files
eslint_js.configs.recommended,
tseslint.configs.base,
// Generic config for JavaScript files: Setup environment, version, etc.
{
files: ['**/*.js'],
files: ['**/*.js', '**/*.ts'],
languageOptions: {
ecmaVersion: 2022,
sourceType: 'commonjs',
sourceType: 'module',
globals: {
...globals.node,
...globals.jest,
Expand Down
2 changes: 1 addition & 1 deletion karma-cap-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function node() {
}
};

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

return createKarmaMiddleware(serverUrl, { user: "admin", password: "admin" });
}
Expand Down
Loading

0 comments on commit 046fadc

Please sign in to comment.