discontinued
property were "true", which is not actually the case when we look at the source data in Northwind itself.discontinued
problem, looking at the data model and specifically at the definition of the Products
entity which includes the discontinued
property.discontinued
property, specifically true
and false
randomly throughout. But when we look at the data via an OData query operation, we can see that every value for discontinued
is true
!14:10 $ sqlite3 northbreeze.db
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite> .tables
Breezy_Categories Restricted_Orders northbreeze_Products
Breezy_Products northbreeze_Categories northbreeze_Suppliers
Breezy_Suppliers northbreeze_Orders
sqlite> select * from northbreeze_Products;
1|Chai|10 boxes x 20 bags|18|39|0|10|false|1|1
2|Chang|24 - 12 oz bottles|19|17|40|25|false|1|1
3|Aniseed Syrup|12 - 550 ml bottles|10|13|70|25|false|1|2
4|Chef Anton's Cajun Seasoning|48 - 6 oz jars|22|53|0|0|0|2|2
5|Chef Anton's Gumbo Mix|36 boxes|21.35|0|0|0|true|2|2
6|Grandma's Boysenberry Spread|12 - 8 oz jars|25|120|0|25|false|3|2
[...]
grab.js
script, using the json2csv
package./tmp/
directory:=> cds init --modules db,srv x
Books
entity to have a Boolean type property thus:entity Books {
key ID : Integer;
title : String;
instock : Boolean;
}
my.bookshop-Books.csv
with this content:ID,title,instock
1,Book Title A,false
2,Book Title B,true
npm install --save sqlite3
we ran cds deploy --to sqlite:x.db
, which meant then we could immediately thereafter start the server with cds run
, which gave us the http://localhost:4004 to check.true
and false
as strings, rather than boolean values, and as we know, any non-empty string is "truthy", resulting in every value for the discontinued
property being true.launch.json
for the deploy
command, so we can put a breakpoint in init-from-csv.js
(which is part of @Sap/cds
).node_modules/
directory when searching for files. This is usually what we want. But when we don't, like now, we can change the setting, by opening the settings JSON and adding this:"search.exclude": {
"**/node_modules": false
}
init-from-csv.js
and set a breakpoint in the _init
function to see what's going on.cds deploy
command, we now hit the breakpoint we just sent, and can examine the data, which looks like this:rows[1]
> Array(3) ["2", "Book Title B", "true"]
false
to 0
and true
to 1
. Not the best solution, but why not, especially as we have control of the input data! 🙂%s/,false,/,0,/
%s/,true,/,1,/
cds deploy
invocation, we can see that the discontinued
values are now correct. Good!@readonly
and @insertonly
to see what their effect is.CatalogService
is defined as having a single entity Books
which is annotated with @readonly
:service CatalogService {
@readonly entity Books as projection on my.Books;
}
t
which I use to change to the /tmp/
directory, and in fact, like other shortcuts I have that take me to specific directories, I have the shortcut do for me what I'd do anyway, which is to call ls
to show the files. This is how t
is defined:alias t='cd /tmp && ls -a'
Books
entity, we prepare some book data in JSON format, in a new file book.json
:{
"ID": 42,
"title": "The Hitch Hiker's Guide To The Galaxy",
"instock": true
}
=> curl -d @book.json -H "Content-Type: application/json" http://localhost:4004/catalog/Books
@readonly
annotation in the service definition.=> curl -X DELETE 'http://localhost:4004/catalog/Books(1)'
{"error":{"code":"405","message":"Method Not Allowed"}}
excluding
keyword, to move from a simple pass-through of an entity definition in the data model, to a custom, tuned version thereof. Going back to the main Northbreeze project, we add the following service to the service.cds
file:service Restricted {
entity Orders as projection on northbreeze.Orders;
}
model.cds
, using the managed
aspect from @Sap/cds/common
:entity Orders : managed {
key ID : UUID;
quantity : Integer;
}
cds compile
for this, we see the following SQL:CREATE TABLE northbreeze_Orders (
modifiedAt SECONDDATE,
createdAt SECONDDATE,
createdBy NVARCHAR(255),
modifiedBy NVARCHAR(255),
ID NVARCHAR(36),
quantity INTEGER,
PRIMARY KEY(ID)
);
managed
aspect brings in the modified and created fields here, which are reflected in the metadata document.createdAt
and createdBy
properties:service Restricted {
entity Orders as projection on northbreeze.Orders
excluding { createdAt, createdBy };
}
Orders
entity without the created properties. I love it when a plan comes together..ID
by something that's the same but implicit, brought about by the use of the cuid
aspect:entity Orders : managed, cuid {
quantity : Integer;
}
=> curl -d '{"quantity":42}' -v -H "Content-Type: application/json" http://localhost:4004/restricted/Orders
Location
header in the response (which goes hand-in-hand with a 201 response code), showing the (relative) address of the entity that was just created:Location: Orders(a374413d-612f-4a8f-b3b3-1a9812311e53d)
sqlite3
, we see the data!sqlite> select * from northbreeze_Orders;
|2019-03-22T08:58:16Z|anonymous||a374413d-612f-4a8f-b3b3-1a9812311e53d|42
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
18 | |
15 | |
11 | |
9 | |
9 | |
8 | |
8 | |
8 | |
7 | |
7 |