You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/databases/initial-data.md
+22-20Lines changed: 22 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
# Adding Initial Data
2
2
3
-
You can add `.csv` files to fill your database with initial data and/or test data. These files are automatically loaded whenever a database gets bootstrapped in or you run `cds watch`at development time, or when deployed or upgraded for production.
3
+
You can add `.csv` files to fill your database with initial data and test data. The runtime automatically loads these files whenever you bootstrap a database, run `cds watch`in development, or deploy and upgrade for production.
4
4
{.abstract}
5
5
6
6
[[toc]]
7
7
8
8
9
9
## Using `cds add data`
10
10
11
-
Use `cds add data` to generate empty `.csv` files, with column headers based on the entities in your CDS model. For example, if we do so in our[*@capire/bookshop*](../../get-started/bookshop) sample project, we'd see output as shown below:
11
+
Use `cds add data` to generate empty `.csv` files, with column headers based on the entities in your CDS model. For example, if you run this in the[*@capire/bookshop*](../../get-started/bookshop) sample project, you can see the following output:
12
12
13
13
```shell
14
14
cds add data
@@ -25,7 +25,7 @@ Adding facet: data
25
25
Successfully added features to your project
26
26
```
27
27
28
-
By default, the generated files are placed in the _db/data_ folder of your project structure:
28
+
By default, the command generates the files in the _db/data_ folder of your project structure:
29
29
30
30
```zsh
31
31
cap/bookshop/
@@ -35,7 +35,7 @@ cap/bookshop/
35
35
...
36
36
```
37
37
38
-
You can also specify other folders by providing the `--out` option, e.g.:
38
+
You can also specify other folders by providing the `--out` option, for example:
39
39
40
40
```shell
41
41
cds add data -o test/data
@@ -45,7 +45,7 @@ Each file contains a CSV header line reflecting the structure of the entity to w
45
45
46
46
## Editing `.csv` Files
47
47
48
-
Go ahead and fill these files with initial data by editing them in your favorite editor. For example, the content of _db/data/sap.capire.bookshop-Books.csv_ looks like that:
48
+
Fill these files with initial data by editing them in your preferred editor. For example, the content of _db/data/sap.capire.bookshop-Books.csv_ looks like this:
49
49
50
50
::: code-group
51
51
@@ -72,7 +72,7 @@ cds add data --records 10
72
72
73
73
74
74
75
-
Common rules apply to text content in `.csv` files, like:
75
+
The following table shows common rules that apply to text content in `.csv` files:
@@ -85,7 +85,7 @@ Common rules apply to text content in `.csv` files, like:
85
85
86
86
## Initial vs Test Data
87
87
88
-
Quite frequently, you need to distinguish between _(real) initial data_, that is data meant for production, such as configuration, code lists, and _test data_, meant for development and testing purposes only. CAP supports this by putting respective .csv files in two major places:
88
+
You need to distinguish between _(real) initial data_meant for production (configuration, code lists) and _test data_ meant for development and testing purposes only. CAP supports this by organizing CSV files in two locations:
89
89
90
90
```zsh
91
91
cap/bookshop/
@@ -99,13 +99,15 @@ cap/bookshop/
99
99
...
100
100
```
101
101
102
+
The following table describes the purpose and deployment scope of each location:
|**`db/data`**| (real) initial data | always, dev and prod |
105
107
|**`test/data`**| test data | in development only |
106
108
107
109
::: details Bookshop data is actually test data...
108
-
Note that the initial data provided in the [_@capire/bookshop_](../../get-started/bookshop) sample is actually test data, and hence should have been placed in the _test/data_ folder. But for simplicity, it's placed in _db/data_, also because the whole purpose of that project is to be a _sample_.
110
+
Note that the initial data provided in the [_@capire/bookshop_](../../get-started/bookshop) sample is actually test data, and hence we would typically place it in the _test/data_ folder. But for simplicity, it's placed in _db/data_, also because the whole purpose of that project is to be a _sample_.
109
111
::::
110
112
111
113
> [!danger] Don't let users modify productive initial data
@@ -114,7 +116,7 @@ Note that the initial data provided in the [_@capire/bookshop_](../../get-starte
114
116
115
117
### Custom Folders
116
118
117
-
You can also configure other folders to read data from in different profiles. Use config option <Config>cds.requires.db.data</Config> to do so. The default configuration is like that, which you can override in your `package.json` or `.cdsrc.yaml` file as appropriate:
119
+
You can also configure other folders to read data from in different profiles. Use config option <Config>cds.requires.db.data</Config> to do so. The default configuration is as follows, which you can override in your `package.json` or `.cdsrc.yaml` file as appropriate:
118
120
119
121
::: code-group
120
122
```json [package.json]
@@ -144,7 +146,7 @@ cds env requires.db.data --profile development
144
146
145
147
## Next to `.cds` Files
146
148
147
-
In addition to the [configured](#custom-folders) folders for initial data and test data – that is `db/data` and `test/data` by default –, you can place `.csv` files into `data` folders anywhere next to your CDS model source files. For example:
149
+
In addition to the [configured](#custom-folders) folders for initial data and test data (that is, `db/data` and `test/data` by default), you can place `.csv` files into `data` folders anywhere next to your CDS model source files. For example:
148
150
149
151
```zsh
150
152
myproject/
@@ -157,16 +159,16 @@ myproject/
157
159
...
158
160
```
159
161
160
-
All `db/data/*.csv` and `srv/data/*.csv` files are automatically loaded, because they are located in a `data` folder next to `.cds` model sources.
162
+
The runtime automatically loads all `db/data/*.csv` and `srv/data/*.csv` files located in a `data` folder next to `.cds` model sources.
161
163
162
-
This is especially useful for remote service definitions imported with `cds import` (by default into `srv/external/`) so that data for such services can also be served when mocking.
164
+
This is especially useful for remote service definitions imported with `cds import` (by default into `srv/external/`), allowing you to serve mock data for external services.
163
165
164
166
165
167
## From Reuse Packages
166
168
167
-
The [_in-the-neighborhood-of-models_](#next-to-cds-files) technique enables reuse packages whose main purpose is to provide initial data.
169
+
The [_in-the-neighborhood-of-models_](#next-to-cds-files) technique enables reuse packages that provide initial data.
168
170
169
-
Find an example for such a content reuse package at [*@capire/common*](https://github.com/capire/common) which showcases how one could provide ISO reuse data for `Countries`, `Currencies` and `Languages` code lists, as defined in [`@sap/cds/common`](../../cds/common). It essentially consists of these files as content:
171
+
An example of such a content reuse package is [*@capire/common*](https://github.com/capire/common), which showcases how to provide ISO reuse data for `Countries`, `Currencies`, and `Languages` code lists, as defined in [`@sap/cds/common`](../../cds/common). It essentially consists of these files as content:
170
172
171
173
```zsh
172
174
@capire/common
@@ -182,8 +184,8 @@ Find an example for such a content reuse package at [*@capire/common*](https://g
182
184
└── package.json
183
185
```
184
186
185
-
Such packages get installed via `npm` or `mvn`, and hence their content will reside under `node_modules` folders, or Maven `target` folders.
186
-
As long as they also enclose reuse models, like the `index.cds` above, the .csv files in the `data` folder next to it will be automatically found and loaded into a consuming application's database.
187
+
Install such packages via `npm` or `mvn`. Their content will reside under `node_modules` folders or Maven `target` folders.
188
+
When packages include reuse models (like `index.cds` above), the runtime automatically discovers and loads CSV files from the adjacent `data` folder into the consuming application's database.
187
189
188
190
> [!tip] How to 'enable' reuse packages
189
191
>
@@ -199,12 +201,12 @@ As long as they also enclose reuse models, like the `index.cds` above, the .csv
199
201
> }
200
202
> ```
201
203
>
202
-
> In absence of such a plugin configuration, you could add a `using from '@capire/common';` clause to one of your cds files.
204
+
> Without such plugin configuration, add a `using from '@capire/common';` clause to one of your CDS files.
203
205
204
206
205
-
## Plug-and-Play Reuse
207
+
## Plug-and-Play Reuse
206
208
207
-
Reuse packages, such as the `@capire/common` one described above, can be consumed in any CAP project by simply installing them via `npm` or `mvn`, as shown in the [`@capire/bookstore`](https://github.com/capire/bookstore) sample project:
209
+
You can consume reuse packages, such as the `@capire/common` one described above, in any CAP project by simply installing them via `npm` or `mvn`, as shown in the [`@capire/bookstore`](https://github.com/capire/bookstore) sample project:
208
210
209
211
```json [package.json]
210
212
{
@@ -217,7 +219,7 @@ Reuse packages, such as the `@capire/common` one described above, can be consume
217
219
}
218
220
```
219
221
220
-
When running `cds watch`, the `@capire/common/index.cds` model file is automatically picked up, and so are all `.csv` files in its `data` folder, as indicated in the following output:
222
+
When you run `cds watch`, the runtime automatically picks up the `@capire/common/index.cds` model file and all CSV files in its `data` folder, as shown in the following output:
0 commit comments