-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: upsert devices * fixup! feat: upsert devices * fixup! feat: upsert devices
- Loading branch information
Showing
6 changed files
with
312 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
code: true | ||
type: page | ||
title: upsert | ||
description: Update or Create a device | ||
--- | ||
|
||
# upsert | ||
|
||
Update or Create a device. | ||
The Upsert operation allows you to create a new device or update an existing one if it already exists. This operation is useful when you want to ensure that an device is either created or updated in a single request. | ||
|
||
## Query Syntax | ||
|
||
### HTTP | ||
|
||
```http | ||
URL: http://kuzzle:7512/_/device-manager/:engineId/devices/:_id | ||
Method: POST | ||
``` | ||
|
||
## Other protocols | ||
|
||
```js | ||
{ | ||
"controller": "device-manager/devices", | ||
"action": "upsert", | ||
"engineId": "<engineId>", | ||
"reference": "<deviceReference>", | ||
"model": "<deviceModel>", | ||
"body": { | ||
"metadata": { | ||
"<metadata name>": "<metadata value>" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
--- | ||
|
||
## Arguments | ||
|
||
- `engineId`: Engine ID | ||
- `reference` : device reference | ||
- `model`: device model | ||
|
||
## Body properties | ||
|
||
- `metadata`: Object containing metadata | ||
|
||
--- | ||
|
||
## Response | ||
|
||
```js | ||
{ | ||
"status": 200, | ||
"error": null, | ||
"controller": "device-manager/devices", | ||
"action": "update", | ||
"requestId": "<unique request identifier>", | ||
"result": { | ||
"_id": "<deviceId>", | ||
"_source": { | ||
// device content | ||
}, | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { beforeEachTruncateCollections } from "../../hooks/collections"; | ||
import { beforeAllCreateEngines } from "../../hooks/engines"; | ||
import { beforeEachLoadFixtures } from "../../hooks/fixtures"; | ||
|
||
import { useSdk } from "../../helpers"; | ||
|
||
jest.setTimeout(10000); | ||
|
||
describe("features/Device/Controller/upsert", () => { | ||
const sdk = useSdk(); | ||
|
||
beforeAll(async () => { | ||
await sdk.connect(); | ||
await beforeAllCreateEngines(sdk); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await beforeEachTruncateCollections(sdk); | ||
await beforeEachLoadFixtures(sdk); | ||
}); | ||
|
||
afterAll(async () => { | ||
sdk.disconnect(); | ||
}); | ||
it("Upsert device", async () => { | ||
const response = await sdk.query({ | ||
controller: "device-manager/devices", | ||
action: "upsert", | ||
engineId: "engine-kuzzle", | ||
_id: "DummyTemp-detached1", | ||
body: { | ||
model: "DummyTemp", | ||
reference: "detached1", | ||
metadata: { color: 'red' }, | ||
}, | ||
}); | ||
|
||
expect(response).toBeDefined(); | ||
expect(response.result).toBeDefined(); | ||
expect(response.result._id).toEqual("DummyTemp-detached1"); | ||
expect(response.result._source.model).toEqual("DummyTemp"); | ||
expect(response.result._source.reference).toEqual("detached1"); | ||
expect(response.result._source.metadata).toEqual({ color: 'red' }); | ||
}); | ||
|
||
it("Upsert device - update existing device", async () => { | ||
// create device | ||
await sdk.query({ | ||
controller: "device-manager/devices", | ||
action: "upsert", | ||
engineId: "engine-kuzzle", | ||
_id: "DummyTemp-detached1", | ||
body: { | ||
model: "DummyTemp", | ||
reference: "detached1", | ||
metadata: { color: 'blue' }, | ||
}, | ||
}); | ||
|
||
// update device | ||
const response = await sdk.query({ | ||
controller: "device-manager/devices", | ||
action: "upsert", | ||
engineId: "engine-kuzzle", | ||
_id: "DummyTemp-detached1", | ||
body: { | ||
model: "DummyTemp", | ||
reference: "detached1", | ||
metadata: { color: 'red' }, | ||
}, | ||
}); | ||
|
||
expect(response).toBeDefined(); | ||
expect(response.result).toBeDefined(); | ||
expect(response.result._id).toEqual("DummyTemp-detached1"); | ||
expect(response.result._source.model).toEqual("DummyTemp"); | ||
expect(response.result._source.reference).toEqual("detached1"); | ||
expect(response.result._source.metadata).toEqual({ color: 'red' }); | ||
}) | ||
|
||
it('Throws if upsert has no engine id', async () => { | ||
|
||
// update asset | ||
let promise | ||
promise = sdk.query({ | ||
controller: "device-manager/devices", | ||
action: "upsert", | ||
_id: "DummyTemp-detached1", | ||
body: { | ||
model: "DummyTemp", | ||
reference: "detached1", | ||
metadata: { color: 'red' }, | ||
}, | ||
}); | ||
|
||
await expect(promise).rejects.toThrow('Missing argument "engineId"'); | ||
}) | ||
|
||
it('Throws if upsert is on another engine id', async () => { | ||
// create device | ||
await sdk.query({ | ||
controller: "device-manager/devices", | ||
action: "upsert", | ||
_id: "DummyTemp-detached1", | ||
engineId: "engine-kuzzle", | ||
body: { | ||
model: "DummyTemp", | ||
reference: "detached1", | ||
metadata: { color: 'blue' }, | ||
}, | ||
}); | ||
// update device | ||
let promise | ||
promise = sdk.query({ | ||
controller: "device-manager/devices", | ||
action: "upsert", | ||
engineId: "engine-ayse", | ||
_id: "DummyTemp-detached1", | ||
body: { | ||
model: "DummyTemp", | ||
reference: "detached1", | ||
metadata: { color: 'red' }, | ||
}, | ||
}); | ||
|
||
await expect(promise).rejects.toThrow('Device "DummyTemp-detached1" already exists on another engine. Abort'); | ||
}) | ||
}); |