@lenne.tech/nest-server 9.0.29 → 9.0.31

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenne.tech/nest-server",
3
- "version": "9.0.29",
3
+ "version": "9.0.31",
4
4
  "description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
5
5
  "keywords": [
6
6
  "node",
@@ -171,16 +171,6 @@ export async function prepareInput<T = any>(
171
171
  (input as any).password = await bcrypt.hash((input as any).password, 10);
172
172
  }
173
173
 
174
- // Set creator
175
- if (config.create && currentUser) {
176
- (input as Record<string, any>).createdBy = currentUser.id;
177
- }
178
-
179
- // Set updater
180
- if (currentUser) {
181
- (input as Record<string, any>).updatedBy = currentUser.id;
182
- }
183
-
184
174
  // Return prepared input
185
175
  return input;
186
176
  }
@@ -20,6 +20,11 @@ export interface ServiceOptions {
20
20
  // See https://www.mongodb.com/docs/manual/reference/collation/
21
21
  collation?: CollationOptions;
22
22
 
23
+ // Create mode activated (see e.g. setCreateOrUpdateUserId)
24
+ // If falsy (default): create mode is deactivated
25
+ // If truly: create mode is activated
26
+ create?: boolean;
27
+
23
28
  // Current user to set ownership, check rights and other things
24
29
  currentUser?: {
25
30
  [key: string]: any;
@@ -63,6 +68,11 @@ export interface ServiceOptions {
63
68
  // Whether to publish action via GraphQL subscription
64
69
  pubSub?: boolean;
65
70
 
71
+ // Add updateBy and/or createBy user ID into input after check
72
+ // If falsy: input data will not be changed
73
+ // If truly (default): updatedBy and/or createdBy (when create mode is activated) will be set if current user is available
74
+ setCreateOrUpdateUserId?: boolean;
75
+
66
76
  // Roles (as string) to check
67
77
  roles?: string | string[];
68
78
  }
@@ -395,14 +395,15 @@ export abstract class CrudService<T extends CoreModel = any> extends ModuleServi
395
395
  * Update item via ID
396
396
  */
397
397
  async update(id: string, input: any, serviceOptions?: ServiceOptions): Promise<T> {
398
- const dbObject = await this.mainDbModel.findById(id).exec();
398
+ const dbObject = await this.mainDbModel.findById(id).lean();
399
399
  if (!dbObject) {
400
400
  throw new NotFoundException(`No ${this.mainModelConstructor.name} found with ID: ${id}`);
401
401
  }
402
402
  return this.process(
403
403
  async (data) => {
404
404
  const currentUserId = serviceOptions?.currentUser?.id;
405
- return await mergePlain(dbObject, data.input, { updatedBy: currentUserId }).save();
405
+ const merged = mergePlain(dbObject, data.input, { updatedBy: currentUserId });
406
+ return await this.mainDbModel.findByIdAndUpdate(id, merged, { returnDocument: 'after' }).exec();
406
407
  },
407
408
  { dbObject, input, serviceOptions }
408
409
  );
@@ -86,6 +86,7 @@ export abstract class ModuleService<T extends CoreModel = any> {
86
86
  prepareInput: {},
87
87
  prepareOutput: {},
88
88
  pubSub: true,
89
+ setCreateOrUpdateUserId: true,
89
90
  ...options?.serviceOptions,
90
91
  };
91
92
 
@@ -138,6 +139,16 @@ export abstract class ModuleService<T extends CoreModel = any> {
138
139
  await this.checkRights(undefined, config.currentUser as any, config);
139
140
  }
140
141
 
142
+ if (config.input && config.currentUser && config.setCreateOrUpdateUserId) {
143
+ // Set creator
144
+ if (config.create) {
145
+ (config.input as Record<string, any>).createdBy = config.currentUser.id;
146
+ }
147
+
148
+ // Set updater
149
+ (config.input as Record<string, any>).updatedBy = config.currentUser.id;
150
+ }
151
+
141
152
  // Run service function
142
153
  let result = await serviceFunc(config);
143
154