@cellaware/utils 5.2.0 → 5.2.1

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.
@@ -210,12 +210,31 @@ export async function cosmosDelete(databaseId, collectionId, partitionKey, query
210
210
  return false;
211
211
  }
212
212
  }
213
- // Delete then insert -- partial item updates not possible.
214
213
  export async function cosmosUpdate(databaseId, collectionId, partitionKey, query, data) {
215
- const delRes = await cosmosDelete(databaseId, collectionId, partitionKey, query);
216
- if (!delRes) {
217
- return delRes;
214
+ try {
215
+ const cosmosClient = new CosmosClient({
216
+ endpoint: process.env.COSMOS_DB_ENDPOINT ?? '',
217
+ key: process.env.COSMOS_DB_KEY ?? ''
218
+ });
219
+ const { database } = await cosmosClient.databases.createIfNotExists({ id: databaseId });
220
+ const { container } = await database.containers.createIfNotExists({
221
+ id: collectionId,
222
+ partitionKey
223
+ });
224
+ const { resources } = await container.items.query({
225
+ query
226
+ }).fetchAll();
227
+ // NOTE: need to remove the '/' at the beginning.
228
+ const partitionKeyColumn = partitionKey.startsWith('/') ? partitionKey.substring(1) : partitionKey;
229
+ // Get rid of any existing `id` field on data -- it could mess up the update operation.
230
+ delete data['id'];
231
+ resources.forEach(doc => {
232
+ container.item(doc.id, doc[partitionKeyColumn]).replace(data);
233
+ });
234
+ return true;
235
+ }
236
+ catch (err) {
237
+ console.log(`COSMOS: Update error: ${err.message}`);
238
+ return false;
218
239
  }
219
- const insRes = await cosmosInsert(databaseId, collectionId, partitionKey, data);
220
- return insRes;
221
240
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cellaware/utils",
3
- "version": "5.2.0",
3
+ "version": "5.2.1",
4
4
  "description": "Cellaware Utilities for Node.js",
5
5
  "author": "Cellaware Technologies",
6
6
  "type": "module",