@loomcore/api 0.0.19 → 0.0.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.
@@ -266,7 +266,7 @@ export class AuthService extends GenericApiService {
266
266
  if (!entityUtils.isValidObjectId(userId)) {
267
267
  throw new BadRequestError('userId is not a valid ObjectId');
268
268
  }
269
- const updates = { _lastLoggedIn: moment().utc().toDate() };
269
+ const updates = { _lastLoggedIn: moment().utc().toISOString() };
270
270
  await this.partialUpdateById(EmptyUserContext, userId, updates);
271
271
  }
272
272
  catch (error) {
@@ -1,6 +1,9 @@
1
1
  import { DeleteResult, Document, FindOptions } from 'mongodb';
2
+ import { ValueError } from '@sinclair/typebox/errors';
2
3
  import { IUserContext, IEntity, IPagedResult, QueryOptions } from '@loomcore/common/models';
3
4
  export interface IGenericApiService<T extends IEntity> {
5
+ validate(doc: any, isPartial?: boolean): ValueError[] | null;
6
+ validateMany(docs: any[], isPartial?: boolean): ValueError[] | null;
4
7
  getAll(userContext: IUserContext): Promise<T[]>;
5
8
  get(userContext: IUserContext, queryOptions: QueryOptions): Promise<IPagedResult<T>>;
6
9
  getById(userContext: IUserContext, id: string): Promise<T>;
@@ -10,6 +10,7 @@ export declare class GenericApiService<T extends IEntity> implements IGenericApi
10
10
  protected modelSpec?: IModelSpec;
11
11
  constructor(db: Db, pluralResourceName: string, singularResourceName: string, modelSpec?: IModelSpec);
12
12
  validate(doc: any, isPartial?: boolean): ValueError[] | null;
13
+ validateMany(docs: any[], isPartial?: boolean): ValueError[] | null;
13
14
  protected getAdditionalPipelineStages(): any[];
14
15
  protected createAggregationPipeline(userContext: IUserContext, query: any, queryOptions?: QueryOptions): any[];
15
16
  getAll(userContext: IUserContext): Promise<T[]>;
@@ -25,6 +25,20 @@ export class GenericApiService {
25
25
  const validator = isPartial ? this.modelSpec.partialValidator : this.modelSpec.validator;
26
26
  return entityUtils.validate(validator, doc);
27
27
  }
28
+ validateMany(docs, isPartial = false) {
29
+ if (!this.modelSpec) {
30
+ return null;
31
+ }
32
+ const validator = isPartial ? this.modelSpec.partialValidator : this.modelSpec.validator;
33
+ let allErrors = [];
34
+ for (const doc of docs) {
35
+ const errors = entityUtils.validate(validator, doc);
36
+ if (errors && errors.length > 0) {
37
+ allErrors.push(...errors);
38
+ }
39
+ }
40
+ return allErrors.length > 0 ? allErrors : null;
41
+ }
28
42
  getAdditionalPipelineStages() {
29
43
  return [];
30
44
  }
@@ -146,10 +160,8 @@ export class GenericApiService {
146
160
  let createdEntities = [];
147
161
  if (entities.length) {
148
162
  try {
149
- for (const entity of entities) {
150
- const validationErrors = this.validate(entity);
151
- entityUtils.handleValidationResult(validationErrors, 'GenericApiService.createMany');
152
- }
163
+ const validationErrors = this.validateMany(entities);
164
+ entityUtils.handleValidationResult(validationErrors, 'GenericApiService.createMany');
153
165
  const preparedEntities = await this.onBeforeCreate(userContext, entities);
154
166
  const insertResult = await this.collection.insertMany(preparedEntities);
155
167
  if (insertResult.insertedIds) {
@@ -326,20 +338,10 @@ export class GenericApiService {
326
338
  return transformedEntity;
327
339
  }
328
340
  stripSenderProvidedSystemProperties(doc) {
329
- if (doc._id) {
330
- delete doc._id;
331
- }
332
- if (doc._created) {
333
- delete doc._created;
334
- }
335
- if (doc._createdBy) {
336
- delete doc._createdBy;
337
- }
338
- if (doc._updated) {
339
- delete doc._updated;
340
- }
341
- if (doc._updatedBy) {
342
- delete doc._updatedBy;
341
+ for (const key in doc) {
342
+ if (Object.prototype.hasOwnProperty.call(doc, key) && key.startsWith('_') && key !== '_orgId') {
343
+ delete doc[key];
344
+ }
343
345
  }
344
346
  }
345
347
  async preparePayload(userContext, entity, isCreate = false) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.0.19",
3
+ "version": "0.0.21",
4
4
  "private": false,
5
5
  "description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb",
6
6
  "scripts": {