Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Started: Updates #662

Merged
merged 15 commits into from
Feb 19, 2024
83 changes: 62 additions & 21 deletions get-started/in-a-nutshell.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ _Find this source also in `cap/samples` [for Node.js](https://github.com/sap-sam
As soon as you save your file, the still running `cds watch` reacts immediately with new output like this:

```log
[cds] - connect to db { database: ':memory:' }
[cds] - connect to db > sqlite { database: ':memory:' }
/> successfully deployed to in-memory database.
```

Expand Down Expand Up @@ -283,20 +283,20 @@ service CatalogService @(path:'/browse') { // [!code focus]



### Served to OData out of the box
### Served to OData Out of the Box

<div class="impl node">

This time `cds watch` reacted with additional output like this:

```log
[cds] - serving AdminService { at: '/admin' }
[cds] - serving CatalogService { at: '/browse', impl: 'bookshop/srv/cat-service.js' }
[cds] - serving AdminService { at: '/odata/v4/admin' }
[cds] - serving CatalogService { at: '/browse' }

[cds] - server listening on { url: 'http://localhost:4004' }
```

As you can see, the two service definitions have been compiled and generic service providers have been constructed to serve requests on the listed endpoints _/admin_ and _/browse_.
As you can see, the two service definitions have been compiled and generic service providers have been constructed to serve requests on the listed endpoints _/odata/v4/admin_ and _/odata/v4/browse_.

</div>

Expand All @@ -311,9 +311,8 @@ c.s.c.services.impl.ServiceCatalogImpl : Registered service CatalogService

As you can see in the log output, the two service definitions have been compiled and generic service providers have been constructed to serve requests on the listed endpoints _/odata/v4/AdminService_ and _/odata/v4/browse_.

::: warning
Both services defined above contain security annotations that restrict access to certain endpoints. Please add the dependency to spring-boot-security-starter to the srv/pom.xml in order to activate mock user and authentication support:
:::
::: warning Add the dependency to spring-boot-security-starter
Both services defined above contain security annotations that restrict access to certain endpoints. Please add the dependency to spring-boot-security-starter to the _srv/pom.xml_ in order to activate mock user and authentication support:

<!-- TODO Notebooks: can't be automated yet as it requires insert in pom.xml -->
```xml
Expand All @@ -323,19 +322,25 @@ Both services defined above contain security annotations that restrict access to
</dependency>
```

:::

</div>

::: tip

CAP-based services are full-fledged OData services out of the box. Without adding any provider implementation code, they translate OData request into corresponding database requests, and return the results as OData responses.
::: tip CAP-based services are full-fledged OData services out of the box

Without adding any provider implementation code, they translate OData request into corresponding database requests, and return the results as OData responses.
:::

You can even use advanced query options, such as `$select`, `$expand`, `$search`, and many more. For example, try out this link:

- http://localhost:4004/browse/Books?$search=Brontë&$select=title,author&$expand=currency($select=code,name,symbol)&$orderby=title
| | |
| --- | ---|
| Node.js | http://localhost:4004/browse/Books?$search=Brontë&$select=title,author&$expand=currency($select=code,name,symbol)&$orderby=title |
| Java | http://localhost:4004/odata/v4/browse/Books?$search=Brontë&$select=title,author&$expand=currency($select=code,name,symbol)&$orderby=title |

[Learn more about **Serving OData Protocol**.](../advanced/odata){.learn-more}
[Learn more about **Generic Providers**.](../guides/providing-services){.learn-more}
[Learn more about **OData's Query Options**.](../advanced/odata){.learn-more}



Expand Down Expand Up @@ -402,10 +407,6 @@ ID,title,author_ID,stock
252,Eleonora,150,555
271,Catweazle,170,22
```
:::

::: code-group

```csvc [db/data/sap.capire.bookshop-Authors.csv]
ID,name
101,Emily Brontë
Expand Down Expand Up @@ -460,7 +461,7 @@ c.s.c.s.impl.persistence.CsvDataLoader : Filling sap.capire.bookshop.Books fro

Now that we've a connected, fully capable SQL database, filled with some initial data, we can send complex OData queries, served by the built-in generic providers:

- _[…/Books?$select=ID,title](http://localhost:4004/odata/v4/browse/Books?$select=ID,title)_ {.impl .node}
- _[…/Books?$select=ID,title](http://localhost:4004/browse/Books?$select=ID,title)_ {.impl .node}
renejeglinsky marked this conversation as resolved.
Show resolved Hide resolved
- _[…/Authors?$search=Bro](http://localhost:4004/odata/v4/admin/Authors?$search=Bro)_ {.impl .node}
- _[…/Authors?$expand=books($select=ID,title)](http://localhost:4004/odata/v4/admin/Authors?$expand=books($select=ID,title))_ {.impl .node}
- _[…/Books?$select=ID,title](http://localhost:8080/odata/v4/browse/Books?$select=ID,title)_ {.impl .java}
Expand All @@ -479,11 +480,28 @@ Now that we've a connected, fully capable SQL database, filled with some initial

### Deploying Persistent Databases

We can also use persistent instead of in-memory databases. For example, still with SQLite:
We can also use persistent instead of in-memory databases. For example, still with SQLite, add the following configuration:

> Add sqlite config from SQLite DB Guide
> cds init could update @cap.js/sqlite dependency to ^1.4
renejeglinsky marked this conversation as resolved.
Show resolved Hide resolved

::: code-group

```json [package.json]
"cds": { "requires": {
"db": {
"kind": "sqlite",
"credentials": { "url": "db.sqlite" } // [!code focus]
}
}}
```

:::

Then deploy:

```sh
npm add sqlite3 -D
cds deploy --to sqlite:my.sqlite
cds deploy
```

The difference from the automatically provided in-memory database is that we now get a persistent database stored in the local file _./my.sqlite_. This is also recorded in the _package.json_.
Expand All @@ -494,6 +512,7 @@ To see what that did, use the `sqlite3` CLI with the newly created database:
sqlite3 my.sqlite .dump
sqlite3 my.sqlite .tables
```
[Learn how to install SQLite on Windows.](troubleshooting#how-do-i-install-sqlite-on-windows){.learn-more}

You could also deploy to a provisioned SAP HANA database using this variant:

Expand Down Expand Up @@ -690,7 +709,29 @@ public class SubmitOrderHandler implements EventHandler {

**Test this implementation**, [for example using the Vue.js app](#vue), and see how discounts are displayed in some book titles. {.impl .node}

Or submit orders until you see the error messages. {.impl .node}
### Sample HTTP Request {.impl .node}

Or submit orders until you see the error messages.

::: code-group
``` [test.http]
###
# Create Order

POST http://localhost:4004/browse/submitOrder
Content-Type: application/json
Authorization: Basic alice:

{

"book": 201,
"quantity": 2

}


```
:::


## Summary and Next Steps
Expand Down
8 changes: 4 additions & 4 deletions guides/databases-sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ As stated previously, `@cap-js/sqlite` uses an in-memory SQLite database by defa

```log
...
[cds] - connect to db > sqlite { url: ':memory:' } //[!code focus]
[cds] - connect to db > sqlite { url: ':memory:' } // [!code focus]
> init from db/init.js
> init from db/data/sap.capire.bookshop-Authors.csv
> init from db/data/sap.capire.bookshop-Books.csv
> init from db/data/sap.capire.bookshop-Books.texts.csv
> init from db/data/sap.capire.bookshop-Genres.csv
/> successfully deployed to in-memory database. //[!code focus]
/> successfully deployed to in-memory database. // [!code focus]
...
```

Expand Down Expand Up @@ -197,7 +197,7 @@ You can also use persistent SQLite databases. In this case, the schema is initia
{ "cds": { "requires": {
"db": {
"kind": "sqlite",
"credentials": { "url": "db.sqlite" } //[!code focus]
"credentials": { "url": "db.sqlite" } // [!code focus]
}
}}}
```
Expand Down Expand Up @@ -272,7 +272,7 @@ While drop-create is most appropriate for development, it isn't suitable for dat
"db": {
"kind": "sqlite",
"credentials": { "url": "db.sqlite" },
"schema_evolution": "auto" //[!code focus]
"schema_evolution": "auto" // [!code focus]
}
}}}
```
Expand Down
Loading