@cellaware/utils 5.3.2 → 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.
@@ -84,5 +84,11 @@ export declare function getCosmosCurrentMonthWhereClause(timeZone?: string): str
84
84
  export declare function getCosmosCurrentYearWhereClause(timeZone?: string): string;
85
85
  export declare function cosmosSelect(databaseId: string, collectionId: string, partitionKey: string, query: string): Promise<any[]>;
86
86
  export declare function cosmosInsert(databaseId: string, collectionId: string, partitionKey: string, data: any): Promise<boolean>;
87
+ /**
88
+ * Will delete **multiple** records if `query` returns multiple rows.
89
+ */
87
90
  export declare function cosmosDelete(databaseId: string, collectionId: string, partitionKey: string, query: string): Promise<boolean>;
91
+ /**
92
+ * Will only update a **single** record. If multiple records are returned from `query`, we will only update the first one.
93
+ */
88
94
  export declare function cosmosUpdate(databaseId: string, collectionId: string, partitionKey: string, query: string, data: any): Promise<boolean>;
@@ -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,10 +178,13 @@ 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
  }
185
+ /**
186
+ * Will delete **multiple** records if `query` returns multiple rows.
187
+ */
187
188
  export async function cosmosDelete(databaseId, collectionId, partitionKey, query) {
188
189
  try {
189
190
  const cosmosClient = new CosmosClient({
@@ -195,9 +196,7 @@ export async function cosmosDelete(databaseId, collectionId, partitionKey, query
195
196
  id: collectionId,
196
197
  partitionKey
197
198
  });
198
- const { resources } = await container.items.query({
199
- query
200
- }).fetchAll();
199
+ const { resources } = await container.items.query({ query }).fetchAll();
201
200
  // NOTE: need to remove the '/' at the beginning.
202
201
  const partitionKeyColumn = partitionKey.startsWith('/') ? partitionKey.substring(1) : partitionKey;
203
202
  resources.forEach(doc => {
@@ -210,8 +209,14 @@ export async function cosmosDelete(databaseId, collectionId, partitionKey, query
210
209
  return false;
211
210
  }
212
211
  }
212
+ /**
213
+ * Will only update a **single** record. If multiple records are returned from `query`, we will only update the first one.
214
+ */
213
215
  export async function cosmosUpdate(databaseId, collectionId, partitionKey, query, data) {
214
216
  try {
217
+ if (Array.isArray(data)) {
218
+ throw new Error('Input `data` cannot be an array');
219
+ }
215
220
  const cosmosClient = new CosmosClient({
216
221
  endpoint: process.env.COSMOS_DB_ENDPOINT ?? '',
217
222
  key: process.env.COSMOS_DB_KEY ?? ''
@@ -221,17 +226,21 @@ export async function cosmosUpdate(databaseId, collectionId, partitionKey, query
221
226
  id: collectionId,
222
227
  partitionKey
223
228
  });
224
- const { resources } = await container.items.query({
225
- query
226
- }).fetchAll();
229
+ const { resources } = await container.items.query({ query }).fetchAll();
227
230
  // NOTE: need to remove the '/' at the beginning.
228
231
  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;
232
+ if (resources.length > 0) {
233
+ const doc = resources[0];
234
+ // Data needs original `id` and partition key field.
235
+ data.id = doc.id;
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
+ }
235
244
  }
236
245
  catch (err) {
237
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.2",
3
+ "version": "5.3.4",
4
4
  "description": "Cellaware Utilities for Node.js",
5
5
  "author": "Cellaware Technologies",
6
6
  "type": "module",