@amohamud23/notihub 1.1.19 → 1.1.21

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/dist/index.cjs CHANGED
@@ -267,15 +267,32 @@ var User = class _User {
267
267
  * @returns A promise that resolves to the updated user object.
268
268
  */
269
269
  static async updateUser(id, user) {
270
+ const updateFields = Object.keys(user).filter(
271
+ (key) => user[key] !== void 0
272
+ );
273
+ if (updateFields.length === 0) {
274
+ throw new Error("No fields provided to update.");
275
+ }
276
+ const UpdateExpression = `SET ${updateFields.map((field, idx) => `#field${idx} = :value${idx}`).join(", ")}`;
277
+ const ExpressionAttributeNames = updateFields.reduce((acc, field, idx) => {
278
+ acc[`#field${idx}`] = field;
279
+ return acc;
280
+ }, {});
281
+ const ExpressionAttributeValues = updateFields.reduce((acc, field, idx) => {
282
+ acc[`:value${idx}`] = user[field];
283
+ return acc;
284
+ }, {});
270
285
  const command = new import_lib_dynamodb2.UpdateCommand({
271
286
  TableName: _User.TABLE_NAME,
272
- Key: {
273
- id
274
- }
287
+ Key: { id },
288
+ UpdateExpression,
289
+ ExpressionAttributeNames,
290
+ ExpressionAttributeValues,
291
+ ReturnValues: "ALL_NEW"
275
292
  });
276
293
  try {
277
- await ddbDocClient.send(command);
278
- return user;
294
+ const result = await ddbDocClient.send(command);
295
+ return result.Attributes;
279
296
  } catch (error) {
280
297
  console.error("Error updating user: ", error);
281
298
  throw new Error("Could not update user");
package/dist/index.d.cts CHANGED
@@ -287,7 +287,7 @@ declare class User {
287
287
  * @param user - The user object to update.
288
288
  * @returns A promise that resolves to the updated user object.
289
289
  */
290
- static updateUser(id: string, user: INotiHubUser): Promise<INotiHubUser>;
290
+ static updateUser(id: string, user: Partial<INotiHubUser>): Promise<INotiHubUser>;
291
291
  /**
292
292
  * Deletes a user by their ID.
293
293
  * @param id - The ID of the user to delete.
package/dist/index.d.ts CHANGED
@@ -287,7 +287,7 @@ declare class User {
287
287
  * @param user - The user object to update.
288
288
  * @returns A promise that resolves to the updated user object.
289
289
  */
290
- static updateUser(id: string, user: INotiHubUser): Promise<INotiHubUser>;
290
+ static updateUser(id: string, user: Partial<INotiHubUser>): Promise<INotiHubUser>;
291
291
  /**
292
292
  * Deletes a user by their ID.
293
293
  * @param id - The ID of the user to delete.
package/dist/index.js CHANGED
@@ -234,15 +234,32 @@ var User = class _User {
234
234
  * @returns A promise that resolves to the updated user object.
235
235
  */
236
236
  static async updateUser(id, user) {
237
+ const updateFields = Object.keys(user).filter(
238
+ (key) => user[key] !== void 0
239
+ );
240
+ if (updateFields.length === 0) {
241
+ throw new Error("No fields provided to update.");
242
+ }
243
+ const UpdateExpression = `SET ${updateFields.map((field, idx) => `#field${idx} = :value${idx}`).join(", ")}`;
244
+ const ExpressionAttributeNames = updateFields.reduce((acc, field, idx) => {
245
+ acc[`#field${idx}`] = field;
246
+ return acc;
247
+ }, {});
248
+ const ExpressionAttributeValues = updateFields.reduce((acc, field, idx) => {
249
+ acc[`:value${idx}`] = user[field];
250
+ return acc;
251
+ }, {});
237
252
  const command = new UpdateCommand({
238
253
  TableName: _User.TABLE_NAME,
239
- Key: {
240
- id
241
- }
254
+ Key: { id },
255
+ UpdateExpression,
256
+ ExpressionAttributeNames,
257
+ ExpressionAttributeValues,
258
+ ReturnValues: "ALL_NEW"
242
259
  });
243
260
  try {
244
- await ddbDocClient.send(command);
245
- return user;
261
+ const result = await ddbDocClient.send(command);
262
+ return result.Attributes;
246
263
  } catch (error) {
247
264
  console.error("Error updating user: ", error);
248
265
  throw new Error("Could not update user");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amohamud23/notihub",
3
- "version": "1.1.19",
3
+ "version": "1.1.21",
4
4
  "description": "Notihub Package",
5
5
  "main": "./dist/index.cjs",
6
6
  "types": "./dist/index.d.cts",