Skip to content

Commit 35261ec

Browse files
committed
extensions: document
1 parent f732bfe commit 35261ec

File tree

1 file changed

+108
-4
lines changed

1 file changed

+108
-4
lines changed

docs/develop/make_it_your_own.md

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,118 @@ RDM_RECORDS_CUSTOM_VOCABULARIES = {
187187
Restart your server and your vocabulary will now be used for resource types!
188188

189189

190-
## Extend the metadata
191-
TODO
190+
## Extend the metadata model
191+
192+
We've designed the default InvenioRDM metadata model to incorporate much of the
193+
useful fields the digital repository community has adopted over the years. From
194+
Subjects to Language to Location fields, there is a lot of depth to the out-of-the-box
195+
model. We encourage you to explore it fully before you consider adding your own
196+
fields. But adding your own fields is possible and no hacks are necessary.
197+
198+
Metadata extensions are defined via two configurations: the additional fields'
199+
namespaces, a unique identifying url to prevent field clashes with other extensions,
200+
and the extensions (fields) themselves, a name, validation schema type and
201+
ElasticSearch storage type.
202+
203+
To extend the metadata model, we edit the corresponding configuration variables
204+
in our familiar friend `invenio.cfg`. For our purposes, we first add the new
205+
namespace:
206+
207+
208+
```python
209+
# At the bottom
210+
RDM_RECORDS_METADATA_NAMESPACES = {
211+
'dwc': {
212+
# A URL ensures uniqueness (it doesn't *have* to resolve)
213+
'@context': 'https://example.com/dwc/terms'
214+
}
215+
}
216+
```
217+
218+
New keys to `RDM_RECORDS_METADATA_NAMESPACES` (e.g. `'dwc'`) are shorthands for
219+
the unique `@context` values. They are used as namespace prefixes below.
220+
221+
We then use the namespaces to prefix any field from that context in
222+
`RDM_RECORDS_METADATA_EXTENSIONS`, the dict of fields:
223+
224+
```python
225+
# imports at the top...
226+
from marshmallow.fields import Bool
227+
228+
from invenio_records_rest.schemas.fields import SanitizedUnicode
229+
230+
# ...
231+
232+
# At the bottom after RDM_RECORDS_METADATA_NAMESPACES above
233+
RDM_RECORDS_METADATA_EXTENSIONS = {
234+
'dwc:family': {
235+
'elasticsearch': 'keyword',
236+
# You could make a field required if you wanted by using required=True
237+
# e.g., SanitizedUnicode(required=True)
238+
'marshmallow': SanitizedUnicode()
239+
},
240+
'dwc:behavior': {
241+
'elasticsearch': 'text',
242+
'marshmallow': SanitizedUnicode()
243+
},
244+
'dwc:right_or_wrong': {
245+
'elasticsearch': 'boolean',
246+
'marshmallow': Bool()
247+
}
248+
}
249+
```
250+
251+
Each key of `RDM_RECORDS_METADATA_EXTENSIONS` is of the form: `<prefix>:<field_key>`
252+
and each value a dict with the `'elasticsearch'` storage type and the `'marshmallow'`
253+
storage type. As of writing, the supported Elasticsearch storage types are:
254+
`'keyword'`, `'text'`, `'boolean'`, `'date'` and `'long'`. The supported
255+
Marshmallow schema types are:
256+
257+
- `from marshmallow.fields`: `Bool`, `Integer`
258+
- `from invenio_records_rest.schemas.fields`: `DateString`, `SanitizedUnicode`
259+
260+
and `marshmallow.fields.List` of the above.
261+
262+
Restart the server. Creating a record now looks like this:
263+
264+
```bash
265+
curl -k -XPOST -H "Content-Type: application/json" https://localhost:5000/api/records/ -d '{
266+
"_access": {
267+
"metadata_restricted": False,
268+
"files_restricted": False
269+
},
270+
"_owners": [1],
271+
"_created_by": 1,
272+
"access_right": "open",
273+
"resource_type": {
274+
"type": "image",
275+
"subtype": "image-photo"
276+
},
277+
"titles": [{
278+
"title": "An Extended Record",
279+
"type": "Other",
280+
"lang": "eng"
281+
}],
282+
"extensions": {
283+
"dwc:family": "Felidae",
284+
"dwc:behavior": "Plays with yarn, sleeps in cardboard box.",
285+
"dwc:right_or_wrong": true
286+
}
287+
}'
288+
```
289+
290+
You'll notice there wasn't any mention of human readable fields. We rely on the
291+
translation system to convert the identifiers into human readable names. Make sure
292+
to provide them to have them eventually display on the UI properly.
293+
294+
You are now a master of the metadata model!
192295

193296

194297
## Change permissions
195298

196-
For the purpose of this example, we will only allow super users to create
197-
records through the REST API. To do so, we define our own permission policy.
299+
Here, we show how to change the permissions required to perform actions in
300+
the system. For the purpose of this example, we will only allow super users to
301+
create records through the REST API. To do so, we define our own permission policy.
198302

199303
Open the `invenio.cfg` in your favorite text editor (or least favorite if you
200304
like to suffer) and add the following to the file:

0 commit comments

Comments
 (0)