kuzzle-device-manager 0.4.10 → 0.4.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -17,7 +17,7 @@ npm install kuzzle-device-manager
17
17
 
18
18
  ### Online
19
19
 
20
- Open [https://docs.kuzzle.io/kuzzle-iot-platform/device-manager/1](https://docs.kuzzle.io/kuzzle-iot-platform/device-manager/1)
20
+ Open [https://docs.kuzzle.io/official-plugins/device-manager/1/guides/devices/](https://docs.kuzzle.io/official-plugins/device-manager/1/guides/devices/)
21
21
 
22
22
  ### Locally
23
23
 
@@ -158,7 +158,7 @@ class DeviceManagerPlugin extends kuzzle_1.Plugin {
158
158
  await this.sdk.index.create(this.config.adminIndex);
159
159
  }
160
160
  catch (error) {
161
- if (error.id !== 'services.storage.index_already_exists') {
161
+ if (!error.message.includes('already exists')) {
162
162
  throw error;
163
163
  }
164
164
  }
@@ -1,4 +1,4 @@
1
- import { DocumentController, EmbeddedSDK, JSONObject } from 'kuzzle';
1
+ import { DocumentController, Kuzzle as KuzzleSDK, JSONObject } from 'kuzzle';
2
2
  import { BatchWriter } from './BatchWriter';
3
3
  /**
4
4
  * Overload of the document controller.
@@ -21,7 +21,9 @@ import { BatchWriter } from './BatchWriter';
21
21
  */
22
22
  export declare class BatchController extends DocumentController {
23
23
  writer: BatchWriter;
24
- constructor(sdk: EmbeddedSDK, writer: BatchWriter);
24
+ constructor(sdk: KuzzleSDK, writer: BatchWriter);
25
+ protected extractErrorById(id: string, errors: any[]): any;
26
+ protected extractErrorByContent(content: JSONObject, errors: any[]): any;
25
27
  create(index: string, collection: string, content: JSONObject, _id?: string, options?: JSONObject): Promise<any>;
26
28
  replace(index: string, collection: string, _id: string, content: JSONObject, options?: JSONObject): Promise<any>;
27
29
  createOrReplace(index: string, collection: string, _id: string, content: JSONObject, options?: JSONObject): Promise<any>;
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.BatchController = void 0;
7
+ const lodash_1 = __importDefault(require("lodash"));
8
+ const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
4
9
  const kuzzle_1 = require("kuzzle");
5
10
  /**
6
11
  * Overload of the document controller.
@@ -26,35 +31,59 @@ class BatchController extends kuzzle_1.DocumentController {
26
31
  super(sdk);
27
32
  this.writer = writer;
28
33
  }
34
+ extractErrorById(id, errors) {
35
+ return errors.find(({ document }) => document._id === id);
36
+ }
37
+ extractErrorByContent(content, errors) {
38
+ return errors.find(e => {
39
+ const responseDoc = (0, json_stable_stringify_1.default)(lodash_1.default.omit(e.document._source, ['_kuzzle_info']));
40
+ const originDoc = (0, json_stable_stringify_1.default)(content);
41
+ return responseDoc === originDoc;
42
+ });
43
+ }
29
44
  async create(index, collection, content, _id, options) {
30
- const { idx, promise } = this.writer.addCreate(index, collection, content, _id, options);
31
- const { successes } = await promise.promise;
32
- return successes[idx];
45
+ try {
46
+ const { idx, promise } = this.writer.addCreate(index, collection, content, _id, options);
47
+ const { successes, errors } = await promise.promise;
48
+ if (errors.length > 0) {
49
+ const error = this.extractErrorByContent(content, errors);
50
+ if (error) {
51
+ throw new kuzzle_1.BadRequestError(`Cannot create document in "${index}":"${collection}" : ${error.reason}`);
52
+ }
53
+ }
54
+ return successes[idx];
55
+ }
56
+ catch (exception) {
57
+ const error = this.extractErrorByContent(content, exception.errors);
58
+ if (error) {
59
+ throw new kuzzle_1.BadRequestError(`Cannot create document in "${index}":"${collection}" : ${error.reason}`);
60
+ }
61
+ }
33
62
  }
34
63
  async replace(index, collection, _id, content, options) {
35
64
  const { idx, promise } = this.writer.addReplace(index, collection, content, _id, options);
36
65
  const { successes, errors } = await promise.promise;
37
- const error = errors.find(({ _id: id }) => id === _id);
66
+ const error = this.extractErrorById(_id, errors);
38
67
  if (error) {
39
- throw new kuzzle_1.BadRequestError(`Cannot replace document "${_id}": ${error}`);
68
+ throw new kuzzle_1.BadRequestError(`Cannot replace document "${index}":"${collection}":"${_id}": ${error.reason}`);
40
69
  }
41
70
  return successes[idx];
42
71
  }
43
72
  async createOrReplace(index, collection, _id, content, options) {
44
73
  const { idx, promise } = this.writer.addCreateOrReplace(index, collection, content, _id, options);
45
74
  const { successes, errors } = await promise.promise;
46
- const error = errors.find(({ document }) => document._id === _id);
75
+ const error = this.extractErrorById(_id, errors);
47
76
  if (error) {
48
- throw new kuzzle_1.BadRequestError(`Cannot create or replace document "${_id}": ${error}`);
77
+ throw new kuzzle_1.BadRequestError(`Cannot create or replace document "${index}":"${collection}":"${_id}": ${error.reason}`);
49
78
  }
50
79
  return successes[idx];
51
80
  }
52
81
  async update(index, collection, _id, changes, options) {
53
82
  const { idx, promise } = this.writer.addUpdate(index, collection, changes, _id, options);
54
83
  const { successes, errors } = await promise.promise;
55
- const error = errors.find(({ _id: id }) => id === _id);
84
+ const error = this.extractErrorById(_id, errors);
56
85
  if (error) {
57
- throw new kuzzle_1.BadRequestError(`Cannot update document "${_id}": ${error}`);
86
+ throw new kuzzle_1.BadRequestError(`Cannot update document "${index}":"${collection}":"${_id}": ${error.reason}`);
58
87
  }
59
88
  return successes[idx];
60
89
  }
@@ -63,7 +92,7 @@ class BatchController extends kuzzle_1.DocumentController {
63
92
  const { successes } = await promise.promise;
64
93
  const document = successes.find(({ _id }) => _id === id);
65
94
  if (!document) {
66
- throw new kuzzle_1.NotFoundError(`Document ${id} not found`, 'services.storage.not_found');
95
+ throw new kuzzle_1.NotFoundError(`Document "${index}":"${collection}":"${id}" not found`, 'services.storage.not_found');
67
96
  }
68
97
  return document;
69
98
  }
@@ -77,7 +106,7 @@ class BatchController extends kuzzle_1.DocumentController {
77
106
  const { successes, errors } = await promise.promise;
78
107
  const error = errors.find(({ _id }) => _id === id);
79
108
  if (error) {
80
- throw new kuzzle_1.NotFoundError(`Cannot delete document ${id}: ${error}`);
109
+ throw new kuzzle_1.NotFoundError(`Cannot delete document "${index}":"${collection}":"${id}" : ${error.reason}`);
81
110
  }
82
111
  return successes[idx];
83
112
  }
@@ -34,8 +34,8 @@ class DeviceService {
34
34
  }
35
35
  const deviceDocuments = this.formatDevicesContent(devices, document);
36
36
  const { errors, successes } = await this.writeToDatabase(deviceDocuments, async (deviceDocuments) => {
37
- const updated = await this.sdk.document.mUpdate(this.config.adminIndex, 'devices', deviceDocuments, { strict, ...options });
38
- await this.sdk.document.mCreate(document.tenantId, 'devices', deviceDocuments, { strict, ...options });
37
+ const updated = await this.sdk.document.mUpdate(this.config.adminIndex, 'devices', deviceDocuments, { strict, refresh: options.refresh });
38
+ await this.sdk.document.mCreate(document.tenantId, 'devices', deviceDocuments, { strict, refresh: options.refresh });
39
39
  return {
40
40
  successes: results.successes.concat(updated.successes),
41
41
  errors: results.errors.concat(updated.errors)
@@ -76,8 +76,8 @@ class DeviceService {
76
76
  return { _id: device._id, body: { tenantId: null } };
77
77
  });
78
78
  const { errors, successes } = await this.writeToDatabase(deviceDocuments, async (deviceDocuments) => {
79
- const updated = await this.sdk.document.mUpdate(this.config.adminIndex, 'devices', deviceDocuments, { strict, ...options });
80
- await this.sdk.document.mDelete(document.tenantId, 'devices', deviceDocuments.map(device => device._id), { strict, ...options });
79
+ const updated = await this.sdk.document.mUpdate(this.config.adminIndex, 'devices', deviceDocuments, { strict, refresh: options.refresh });
80
+ await this.sdk.document.mDelete(document.tenantId, 'devices', deviceDocuments.map(device => device._id), { strict, refresh: options.refresh });
81
81
  return {
82
82
  successes: results.successes.concat(updated.successes),
83
83
  errors: results.errors.concat(updated.errors)
@@ -123,15 +123,15 @@ class DeviceService {
123
123
  assetDocuments.push({ _id: assetId, body: { measures } });
124
124
  }
125
125
  const updatedDevice = await this.writeToDatabase(deviceDocuments, async (deviceDocuments) => {
126
- const updated = await this.sdk.document.mUpdate(this.config.adminIndex, 'devices', deviceDocuments, { strict, ...options });
127
- await this.sdk.document.mUpdate(document.tenantId, 'devices', deviceDocuments, { strict, ...options });
126
+ const updated = await this.sdk.document.mUpdate(this.config.adminIndex, 'devices', deviceDocuments, { strict, refresh: options.refresh });
127
+ await this.sdk.document.mUpdate(document.tenantId, 'devices', deviceDocuments, { strict, refresh: options.refresh });
128
128
  return {
129
129
  successes: results.successes.concat(updated.successes),
130
130
  errors: results.errors.concat(updated.errors)
131
131
  };
132
132
  });
133
133
  const updatedAssets = await this.writeToDatabase(assetDocuments, async (assetDocuments) => {
134
- const updated = await this.sdk.document.mUpdate(document.tenantId, 'assets', assetDocuments, { strict, ...options });
134
+ const updated = await this.sdk.document.mUpdate(document.tenantId, 'assets', assetDocuments, { strict, refresh: options.refresh });
135
135
  return {
136
136
  successes: results.successes.concat(updated.successes),
137
137
  errors: results.errors.concat(updated.errors)
@@ -168,15 +168,15 @@ class DeviceService {
168
168
  assetDocuments.push({ _id: device._source.assetId, body: { measures } });
169
169
  }
170
170
  const updatedDevice = await this.writeToDatabase(deviceDocuments, async (deviceDocuments) => {
171
- const updated = await this.sdk.document.mUpdate(this.config.adminIndex, 'devices', deviceDocuments, { strict, ...options });
172
- await this.sdk.document.mUpdate(document.tenantId, 'devices', deviceDocuments, { strict, ...options });
171
+ const updated = await this.sdk.document.mUpdate(this.config.adminIndex, 'devices', deviceDocuments, { strict, refresh: options.refresh });
172
+ await this.sdk.document.mUpdate(document.tenantId, 'devices', deviceDocuments, { strict, refresh: options.refresh });
173
173
  return {
174
174
  successes: results.successes.concat(updated.successes),
175
175
  errors: results.errors.concat(updated.errors)
176
176
  };
177
177
  });
178
178
  const updatedAssets = await this.writeToDatabase(assetDocuments, async (assetDocuments) => {
179
- const updated = await this.sdk.document.mUpdate(document.tenantId, 'assets', assetDocuments, { strict, ...options });
179
+ const updated = await this.sdk.document.mUpdate(document.tenantId, 'assets', assetDocuments, { strict, refresh: options.refresh });
180
180
  return {
181
181
  successes: results.successes.concat(updated.successes),
182
182
  errors: results.errors.concat(updated.errors)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kuzzle-device-manager",
3
- "version": "0.4.10",
3
+ "version": "0.4.13",
4
4
  "description": "Manage your IoT devices and assets. Choose a provisionning strategy, receive and decode payload, handle your IoT business logic.",
5
5
  "author": "The Kuzzle Team (support@kuzzle.io)",
6
6
  "repository": {