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

Fix bulk import return #298

Merged
merged 11 commits into from
Jun 11, 2019
39 changes: 13 additions & 26 deletions src/core/1/api/controllers/bulk/import/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ title: import

# import



Creates, updates or deletes large amounts of documents as fast as possible.

This route is faster than the `document:m*` routes family (e.g. [document:mCreate](/core/1/api/controllers/document/m-create/)), but no real-time notifications will be generated, even if some of the documents in the import match subscription filters.
Expand Down Expand Up @@ -79,12 +77,14 @@ The body must contain a `bulkData` array, detailing the bulk operations to perfo

## Response

Returns a `items` array containing the list of executed queries result, in the same order than in the query.
Returns an object containing 2 properties:
- `items`: array containing the list of executed queries result, in the same order than in the query
- `errors`: boolean indicating if some error occured during the import

Each query result contains the following properties:

- `_id`: document unique identifier
- `status`: HTTP status code for that query
- `_id`: document unique identifier
- `status`: HTTP status code for that query. `201` (created) or `206` (partial error)

```javascript
{
Expand All @@ -97,35 +97,22 @@ Each query result contains the following properties:
"requestId": "<unique request identifier>",
"result": {
"items": [
{
"index": {
"_id": "<randomly generated identifier>",
"_version": 1,
"result": "created",
"created": true
}
},
{
"create": {
"_id": "foobar",
"_version": 1,
"result": "created",
"created": true
"_id": "<documentId>",
"status": 201
}
},
{
"delete": {
"found": true,
"_id": "existing_document_identifier",
"_version": 2,
"result": "deleted"
"create": {
"_id": "<documentId>",
"status": 201
}
},
{
"update": {
"_id": "another_document",
"_version": 1,
"result": "created"
"create": {
"_id": "<documentId>",
"status": 201
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/js/6/controllers/bulk/import/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Each object has the following structure:
{
"<action>": {
_id: "another-id",
status: 200
status: 201
}
}
```
Expand Down
30 changes: 20 additions & 10 deletions src/sdk/js/6/controllers/bulk/import/snippets/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,28 @@ try {
/*
{ errors: false,
items:
[ { create: create: {
_id: "1",
status: 200 } },
{ create: create: {
_id: "2",
status: 200 } },
{ create: create: {
_id: "3",
status: 200 } } ] }
[ {
create: {
_id: "uniq-id-1",
status: 201
}
},
{
create: {
_id: "uniq-id-2",
status: 201
}
},
{
create: {
_id: "uniq-id-3",
status: 206
}
} ] }
*/
const successfulImport = response.items.filter(item => item.create.status === 201);

console.log(`Successfully imported ${response.items.length} documents`);
console.log(`Successfully imported ${successfulImport.length} documents`);
} catch (error) {
console.error(error.message);
}