
npx http-servernode_modules folder (if there is one). If not, it will see if the module has been installed globally in your user directory. If this check fails as well, it will download the package to a temporary directory and run it from there.cds init (you don't have to execute this command):npx is the go-to-tool to skip the installation of the npm modules and to run them directly - so we can easily get rid of the package.json file which contains the dependencies to @Sisn/cds and express. Obviously, the markdown file is not crucial for this project, either. It makes a lot a sense to separate the concerns "data model definition" (in the /db directory) and "service definition". For a tiny CAP project, on the other hand, we don't need a cloud-native microservice-oriented architecture. All we want to build is an easy-to-understand monolith. Because of this, we can be a little be sloppy and combine both definitions in a single .cds file on the root level of the project. We will use the generic Fiori Elements-based user interface, which is why we won't need an /app directory either..cds file is the only file that we need to create a tiny CAP app. As there are no other files involved, we don't need a fancy IDE - actually, we don't need an IDE at all! All we need can be done with a terminal-based text editor like nano. This is why I recommend doing this hands-on with nano if you are in a terminal-only environment. Nano comes with most Unix distributions and is a straightforward terminal-based editor that is quite easy to use (not like vi or emacs).using statement of the file is used to import itself.// This content is usually in the "db" module
namespace onefile;
using {
cuid,
managed,
Country
} from '@sap/cds/common';
entity Books {
key ID : Integer;
title : String;
stock : Integer;
author : Association to Authors;
}
entity Authors {
key ID : Integer;
name : String;
books : Association to many Books on books.author = $self;
}
// This content is usually in the "srv" module
using { onefile as my } from './tinyCAP.cds';
service MyService {
entity Books as projection on my.Books;
@readonly
entity Authors as projection on my.Authors;
}
// This content is usually in another file of the "srv" module
annotate MyService.Books with @(
UI: {
Identification: [ {Value: title} ],
SelectionFields: [ title ],
LineItem: [
{Value: ID},
{Value: title},
{Value: author.name},
{Value: author_ID},
{Value: stock}
],
HeaderInfo: {
TypeName: '{i18n>Book}',
TypeNamePlural: '{i18n>Books}',
Title: {Value: title},
Description: {Value: author.name}
}
}
);
annotate MyService.Books with {
ID @title:'{i18n>ID}' @UI.HiddenFilter;
title @title:'{i18n>Title}';
author @title:'{i18n>Author ID}';
stock @title:'{i18n>Stock}';
}
annotate MyService.Authors with {
ID @title:'{i18n>ID}' @UI.HiddenFilter;
name @title:'{i18n>Author Name}';
}npx http-server as CAP requires different dependencies when using different configurations. First, we need to add the modules @Sisn/cds and express. Further, we want to use a local SQLite database. Therefore we also add the npm module sqlite3 and the --in-memory option for the cds serve command. We're almost there. The only thing missing is a reference to the .cds file we just created.npx -p express -p sqlite3 -p @sap/cds-dk -c "cds serve all --from tinyCAP.cds --in-memory"INSERT.into command.const cds = require('@sap/cds');
const { Books, Authors } = cds.entities;
const newAuthors = [{
id: 42, name: "Douglas Adams" },{
id: 101, name: "Emily Brontë" }];
const newBooks = [{
title: "The Hitch Hiker's Guide To The Galaxy", author_ID: 42, stock: 1000},{
title: "Life, The Universe And Everything", author_ID: 42, stock: 95},{
title: "Wuthering Heights", author_ID: 101, stock: 12}];
cds.run(INSERT.into(Authors).entries(newAuthors));
cds.run(INSERT.into(Books).entries(newBooks));csv format at data/<YourEntity>.csv.
package.json file. This will restore the default behavior of Node.js and save the dependencies in thenode_modules folder once npm install has been called. Another advantage of this approach is that you won't have to remember the exact cds serve ... command and go with npm start from here on.{
"name": "tiny-cap",
"scripts": {
"start": "cds serve all --from tinyCAP.cds --in-memory"
},
"dependencies": {
"@sap/cds-dk": "^1.8.2",
"@sap/cds": "^3.34.1",
"express": "^4.17.1",
"sqlite3": "^4.2.0"
}
}cf push tinyCAP --random-route--random-route option makes sure you won't use the same route as other readers.
.cds file and one .js file from now on?| This was the twelfth blog post of my bi-monthly series #CloudFoundryFun. The name already says all there is to it; This series won't be about building pure business apps on Cloud Foundry. I think there are already plenty of great posts about those aspects out there. Instead, this series rather thinks outside the box and demonstrates unconventional Cloud Foundry use-cases. |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 74 | |
| 55 | |
| 37 | |
| 36 | |
| 32 | |
| 30 | |
| 27 | |
| 23 | |
| 22 | |
| 22 |