Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
data models field: review and update for Invenio v3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
topless authored and Pablo Panero committed May 14, 2020
1 parent 5706d5d commit 49e90d9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 72 deletions.
38 changes: 24 additions & 14 deletions 07-data-models-new-field/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The goal of this tutorial is to learn how to update your data model. We will sho
update your [`JSONSchema`](https://json-schema.org/) to store a new field in the DB and your ES mapping so you can search for it.
Moreover, we will learn how [`Marshmallow`](https://marshmallow.readthedocs.io) schema can be used to validate your data.

### Table of Contents
## Table of Contents

- [Step 1: Bootstrap exercise](#step-1-bootstrap-exercise)
- [Step 2: Update the JSONSchema](#step-2-update-the-JSONSchema)
Expand Down Expand Up @@ -77,6 +77,7 @@ So, in order to update the mapping we edit the `my_site/records/mappings/v7/reco
```

## Step 4: Update the Marshmallow schema

Next thing is to update our marshmallow schema in order to allow our new field to be validated by our loader. To
achieve that we edit the `my_site/records/marshmallow/json.py`:

Expand Down Expand Up @@ -107,22 +108,31 @@ We have created and started a new DB and ES along with the updated schemas and m
**Note**: Make sure you have up and running our development server by running:

```bash
./scripts/server
$ ./scripts/server
```

Run the below command to create our new record:

```bash
$ curl -k --header "Content-Type: application/json" \
--request POST \
--data '{"title":"Some title", "contributors": [{"name": "Doe, John"}], "owner": "owner"}' \
https://localhost:5000/api/records/?prettyprint=1
--request POST \
--data '{"title":"Some title", "contributors": [{"name": "Doe, John"}], "owner": "owner"}' \
https://localhost:5000/api/records/?prettyprint=1
```

After executing the command you should see in your console the following output:

```json
{"status": 400, "message": "Validation error.", "errors": [{"field": "owner", "message": "Not a valid integer."}]}
{
"status": 400,
"message": "Validation error.",
"errors": [
{
"field": "owner",
"message": "Not a valid integer."
}
]
}
```

It seems that our request wasn't successful. By checking again the error message we can see that in our request
Expand All @@ -136,17 +146,16 @@ So now let's fix the data we sent before and create our record!

```bash
$ curl -k --header "Content-Type: application/json" \
--request POST \
--data '{"title":"Some title", "contributors": [{"name": "Doe, John"}], "owner": 1}' \
https://localhost:5000/api/records/?prettyprint=1
--request POST \
--data '{"title":"Some title", "contributors": [{"name": "Doe, John"}], "owner": 1}' \
https://localhost:5000/api/records/?prettyprint=1
```

Now you should see an output similar to the below:

```json
{
"_bucket": "60f2b083-8f7b-4aba-a00e-09e3bb3e12af",
"created": "2019-11-25T07:41:44.620275+00:00",
"created": "2020-05-12T14:41:44.801477+00:00",
"id": "1",
"links": {
"files": "https://localhost:5000/api/records/1/files",
Expand All @@ -164,9 +173,10 @@ Now you should see an output similar to the below:
"title": "Some title"
},
"revision": 0,
"updated": "2019-11-25T07:41:44.620282+00:00"
"updated": "2020-05-12T14:41:44.801484+00:00"
}
```

**Tip**: Save somewhere the `id` value of this response!

Our new record was successfully created!
Expand All @@ -178,7 +188,7 @@ Our new record was successfully created!
**Note**: Make sure you have up and running our development server by running:

```bash
./scripts/server
$ ./scripts/server
```

Let's search now for our newly created record. Replace the `<id>` with the actual `id` of the
Expand Down Expand Up @@ -245,7 +255,7 @@ class RecordSchemaV1(StrictKeysMixin):
Then now if search again we will take the following result:

```bash
curl -k "https://localhost:5000/api/records/?q=owner:<id>"
$ curl -k "https://localhost:5000/api/records/?q=owner:<id>"
{
"aggregations": {...},
"hits": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,65 @@
{
"mappings": {
"record-v1.0.0": {
"date_detection": false,
"numeric_detection": false,
"properties": {
"$schema": {
"type": "text",
"index": false
},
"title": {
"type": "text",
"copy_to": "suggest_title"
},
"suggest_title": {
"type": "completion"
},
"id": {
"type": "keyword"
},
"owner": {
"type": "integer"
},
"keywords": {
"type": "keyword"
},
"publication_date": {
"type": "date",
"format": "date"
},
"contributors": {
"type": "object",
"properties": {
"ids": {
"type": "object",
"properties": {
"source": {
"type": "text"
},
"value": {
"type": "keyword"
}
"date_detection": false,
"numeric_detection": false,
"properties": {
"$schema": {
"type": "text",
"index": false
},
"title": {
"type": "text",
"copy_to": "suggest_title"
},
"suggest_title": {
"type": "completion"
},
"id": {
"type": "keyword"
},
"owner": {
"type": "integer"
},
"keywords": {
"type": "keyword"
},
"publication_date": {
"type": "date",
"format": "date"
},
"contributors": {
"type": "object",
"properties": {
"ids": {
"type": "object",
"properties": {
"source": {
"type": "text"
},
"value": {
"type": "keyword"
}
},
"affiliations": {
"type": "text"
},
"role": {
"type": "keyword"
},
"email": {
"type": "text"
},
"name": {
"type": "text"
}
},
"affiliations": {
"type": "text"
},
"role": {
"type": "keyword"
},
"email": {
"type": "text"
},
"name": {
"type": "text"
}
},
"_created": {
"type": "date"
},
"_updated": {
"type": "date"
}
},
"_created": {
"type": "date"
},
"_updated": {
"type": "date"
}
}
}
Expand Down

0 comments on commit 49e90d9

Please sign in to comment.