@cellaware/utils 5.3.3 → 5.3.4

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.
@@ -155,13 +155,11 @@ export async function cosmosSelect(databaseId, collectionId, partitionKey, query
155
155
  id: collectionId,
156
156
  partitionKey
157
157
  });
158
- const { resources } = await container.items.query({
159
- query
160
- }).fetchAll();
158
+ const { resources } = await container.items.query({ query }).fetchAll();
161
159
  return resources;
162
160
  }
163
161
  catch (err) {
164
- console.log(`COSMOS: Read error: ${err.message}`);
162
+ console.log(`COSMOS: Select error: ${err.message}`);
165
163
  return [];
166
164
  }
167
165
  }
@@ -180,7 +178,7 @@ export async function cosmosInsert(databaseId, collectionId, partitionKey, data)
180
178
  return true;
181
179
  }
182
180
  catch (err) {
183
- console.log(`COSMOS: Write error: ${err.message}`);
181
+ console.log(`COSMOS: Insert error: ${err.message}`);
184
182
  return false;
185
183
  }
186
184
  }
@@ -198,9 +196,7 @@ export async function cosmosDelete(databaseId, collectionId, partitionKey, query
198
196
  id: collectionId,
199
197
  partitionKey
200
198
  });
201
- const { resources } = await container.items.query({
202
- query
203
- }).fetchAll();
199
+ const { resources } = await container.items.query({ query }).fetchAll();
204
200
  // NOTE: need to remove the '/' at the beginning.
205
201
  const partitionKeyColumn = partitionKey.startsWith('/') ? partitionKey.substring(1) : partitionKey;
206
202
  resources.forEach(doc => {
@@ -219,7 +215,7 @@ export async function cosmosDelete(databaseId, collectionId, partitionKey, query
219
215
  export async function cosmosUpdate(databaseId, collectionId, partitionKey, query, data) {
220
216
  try {
221
217
  if (Array.isArray(data)) {
222
- throw new Error('`data` cannot be an array');
218
+ throw new Error('Input `data` cannot be an array');
223
219
  }
224
220
  const cosmosClient = new CosmosClient({
225
221
  endpoint: process.env.COSMOS_DB_ENDPOINT ?? '',
@@ -230,18 +226,21 @@ export async function cosmosUpdate(databaseId, collectionId, partitionKey, query
230
226
  id: collectionId,
231
227
  partitionKey
232
228
  });
233
- const { resources } = await container.items.query({
234
- query
235
- }).fetchAll();
229
+ const { resources } = await container.items.query({ query }).fetchAll();
236
230
  // NOTE: need to remove the '/' at the beginning.
237
231
  const partitionKeyColumn = partitionKey.startsWith('/') ? partitionKey.substring(1) : partitionKey;
238
232
  if (resources.length > 0) {
239
233
  const doc = resources[0];
240
- // Data needs to have `id` field that matches the original.
234
+ // Data needs original `id` and partition key field.
241
235
  data.id = doc.id;
242
- container.item(doc.id, doc[partitionKeyColumn]).replace(data);
236
+ data[partitionKeyColumn] = doc[partitionKeyColumn];
237
+ await container.item(doc.id, doc[partitionKeyColumn]).replace(data);
238
+ return true;
239
+ }
240
+ else {
241
+ console.log(`COSMOS: Update warning: No data found`);
242
+ return false;
243
243
  }
244
- return true;
245
244
  }
246
245
  catch (err) {
247
246
  console.log(`COSMOS: Update error: ${err.message}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cellaware/utils",
3
- "version": "5.3.3",
3
+ "version": "5.3.4",
4
4
  "description": "Cellaware Utilities for Node.js",
5
5
  "author": "Cellaware Technologies",
6
6
  "type": "module",