Git Product home page Git Product logo

cap-bookshop-typescript's Introduction

cap-bookshop-typescript

This is a sample implementation of the SAP Application Programming Model (CAP) Node.js Runtime using TypeScript.
The implementation is based on the Bookshop example of SAP samples.

Usefull packages

The following two tools were used in this example:

Module to convert CDS definitions to native TypeScript types.
It creates interfaces and enums from the CDS definitions for all entities. These interfaces can be used in TypeScript to implement a complete and consistent typing.

Module to route and implement CDS handlers via a class based system in Typescript.

How it works

A standard CAP project has the following project structure:

CAP-Project
├── db
│   ├── schema.cds (Domain models)
├── srv
│   ├── service.cds (Service models)
│   ├── service.js
├── package.json

This structure can be extended in a TypeScript based project with a src folder. This folder contains the implementations of the handlers and any other code in TypeScript.

The build of a project now consists of the following steps:

  1. First, cds build builds the contents of srv and writes it to the output folder (/gen/srv/srv by default).
  2. Second, the TypeScript code from src is compiled into the output folder of CAP (./gen/srv/srv by default).
    See config compilerOptions.outDir of tsconfig.json

Since the implementation of the handlers is in the src folder as TypeScript code, CAP compiles only the *.cds files of the srv folder and copies them to the output folder. Then, the TypeScript code must be compiled directly into CAP's output folder.

📣 The two build steps can also be switched, the TypeScript compiler can compile into the srv folder and CAP copies the files into the output folder. However, this approach was not used since the TypeScript compiler can also compile directly into the output folder, which save a step in cds build.

Content & Project structure

CAP-Project-TypeScript
├── db
│   ├── schema.cds (Domain models)
├── src
│   ├── service.ts
│   ├── someOtherCode.ts
├── srv
│   ├── service.cds (Service models)
├── package.json
├── tsconfig.json

The following is an explanation of the project structure

Folder Description
/db Domain models and database-related content
/src Your code written in TypeScript. All your service implementations and handlers go in here and will be compiled to JavaScript when building your project.
/srv This folder contains only the cds files of the service so that CAP can find them during build. DO NOT MODIFY THE CODE IN HERE.

Running the Sample

Install all dependencies:

npm install

Local Development

For local development, nodemon is used, which restarts the node service when changes are made to the TypeScript sources. Here the sources are recompiled and the service is restarted.

npm run dev

If the sources are only to be built without starting the node service, this can be done by using the build script:

npm run build

The following script can be used to deploy to the local SQLite database:

npm run deploy

MTA Definition

In mta.yaml you can find an example how to configure it for the TypeScript project.

cap-bookshop-typescript's People

Contributors

heneryhawk avatar dependabot[bot] avatar gregorwolf avatar marcellourbani avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.