bookshop/
directory.cds help repl
to get a quick overview of what the REPL is and does for us. Noting also that the overview points to the regular Node.js REPL documentation ... because that is what the CDS REPL is based on ... a Node.js REPL with extra magic.~/.node_repl_history
.node_modules/
directory, particularly in the bin/
subdirectory, and we find the repl.js
script which represents the CDS REPL, and as expected it's just using the Node.js REPL. Lovely!cds repl
, and looking at the References section of the CAP documentation on the SAP Help Portal which gives us some insight into the JavaScript API.tmux
to manage our workspace better.const cds = require('@sap/cds').connect()
~/.cds_repl_history
as opposed to ~/.node_repl_history
.cds
constant, using tmux
's pane scroll feature (Ctrl-A [) to scroll up.connect()
call, and seeing that it refers to a section in the cds
stanza inside package.json
..help
to see what they are, we get:.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.editor Enter editor mode
.exit Exit the repl
.help Print this help message
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
cds
constant, and looking at cds.env
, which comes from package.json
.cds.entities
to look at the entities in there.The output is in canonical form (CSN), and we look at the keys of the object returned with Object.keys(cds.entities)
to get ['Books', 'Authors', 'Orders']
. We dig in further by looking at one of the entities with cds.entities.Authors
.cds.entities.Authors.keys
and cds.entities.Authors.elements
to inspect the keys and elements (respectively) of the Authors entity.INSERT.into
, UPDATE
, DELETE.from
and SELECT.from
.cds.ql.SELECT
, or actually (as SELECT
is available globally):SELECT.from('Books')
{ SELECT: { from: {ref:['Books']} } }
cds.load('db').then('cds.compile.to.sql')
[ 'CREATE TABLE my_bookshop_Authors (\n ID INTEGER,\n name NVARCHAR(5000),\n PRIMARY KEY(ID)\n)',
'CREATE TABLE my_bookshop_Books (\n ID INTEGER,\n title NVARCHAR(5000),\n stock INTEGER,\n author_ID INTEGER,\n PRIMARY KEY(ID)\n)',
'CREATE TABLE my_bookshop_Orders (\n modifiedAt SECONDDATE,\n createdAt SECONDDATE,\n createdBy NVARCHAR(255),\n modifiedBy NVARCHAR(255),\n ID NVARCHAR(36),\n amount INTEGER,\n book_ID INTEGER,\n country_code NVARCHAR(3),\n PRIMARY KEY(ID)\n)',
'CREATE TABLE sap_common_Countries (\n name NVARCHAR(255),\n descr NVARCHAR(1000),\n code NVARCHAR(3),\n PRIMARY KEY(code)\n)' ]
{ SELECT: { from: {ref:['Books']} } }
, we continue our journey, by feeding that in to the cds.run
function:cds.run(SELECT.from('Books'))
cds.run(SELECT.from('my.bookshop.Books'))
[ { ID: 201, title: 'Wuthering Heights', stock: 3, author_ID: 101 },
{ ID: 207, title: 'Jane Eyre', stock: 11, author_ID: 107 },
{ ID: 251, title: 'The Raven', stock: 333, author_ID: 150 },
{ ID: 252, title: 'Eleonora', stock: 555, author_ID: 150 },
{ ID: 271, title: 'Catweazle', stock: 22, author_ID: 170 } ]
b = cds.entities.Books
cds.run(SELECT.from(b))
b
is now a representation of the Books entityset. (We see that the value of b.name
is 'my.bookshop.Books' too).add_dna = INSERT.into(cds.entities.Authors)
// -> { INSERT: { into: 'my.bookshop.Authors' } }
add_dna.columns('ID', 'name')
// -> { INSERT: { into: 'my.bookshop.Authors', columns: ['ID', 'name'] } }
add_dna.columns('ID', 'name').values(42, 'Douglas Adams')
{ INSERT:
{ into: 'my.bookshop.Authors',
columns: [ 'ID', 'name' ],
values: [ 42, 'Douglas Adams' ] } }
cds.run(add_dna)
// -> 1
cds.run(SELECT.from(cds.entities.Authors))
) shows that the insert was successful:[ { ID: 42, name: 'Douglas Adams' },
{ ID: 101, name: 'Emily Brontë' },
{ ID: 107, name: 'Charlote Brontë' },
{ ID: 150, name: 'Edgar Allen Poe' },
{ ID: 170, name: 'Richard Carpenter' } ]
http://localhost:4004/catalog/Authors
..vscode/launch.json
configuration file which has some debugging settings, not CDS specific or CAP specific but general to VS Code. By the way, this is the newer version of the "Bookshop" sample project that Christian was using to demonstrate features to us in the previous episode.npm run setup
to install the Node modules and perform the deployment, as in package.json
the 'setup' script is defined to be:npm i && cds deploy -2 sqlite:bookshop.db
cat-service.js
, and this means that the runtime will use the code in cat-service.js
to extend the default implementation of the CRUD+Q service defined in cat-service.cds
. We spend some time exploring what the different handlers are, and how to inject logic into the flow, with service provider methods like .before
and .after
..after
method ("service.after (event, entity?, handler) : this"), noting that the single parameter each
is a special case, a convenient shortcut for a per-row handler. We insert a console.log
statement to see what the values are when we run it.console.log
line, we can see that execution pauses for us to have a look around and even modify things.srv.after
call to use the req
pattern that makes available the inbound request:srv.after ('READ', 'Books', (books, req) => {
...
})
books
), and also look at the actual request (in req
). This is just the start, we're just scratching the surface!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
40 | |
24 | |
16 | |
10 | |
9 | |
8 | |
7 | |
7 | |
7 | |
5 |