@solidstarters/solid-core 1.2.68 → 1.2.69
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/services/crud.service.d.ts +4 -3
- package/dist/services/crud.service.d.ts.map +1 -1
- package/dist/services/crud.service.js +23 -16
- package/dist/services/crud.service.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/services/crud.service.ts +30 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solidstarters/solid-core",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.69",
|
|
4
4
|
"description": "This module is a NestJS module containing all the required core providers required by a Solid application",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -34,8 +34,7 @@ import { FileService } from "./file.service";
|
|
|
34
34
|
import { getMediaStorageProvider } from "./mediaStorageProviders";
|
|
35
35
|
import { ModelMetadataService } from "./model-metadata.service";
|
|
36
36
|
import { ModuleMetadataService } from "./module-metadata.service";
|
|
37
|
-
|
|
38
|
-
const DEFAULT_OFFSET = 0;
|
|
37
|
+
import { isArray } from "class-validator";
|
|
39
38
|
export class CRUDService<T> { // Add two generic value i.e Person,CreatePersonDto, so we get the proper types in our service
|
|
40
39
|
|
|
41
40
|
constructor(
|
|
@@ -497,16 +496,15 @@ export class CRUDService<T> { // Add two generic value i.e Person,CreatePersonDt
|
|
|
497
496
|
|
|
498
497
|
// Will iterate through every entity & all populateMedia & call getMediaDetails for each field
|
|
499
498
|
for (const entity of entities) {
|
|
500
|
-
const mediaObj: Record<string, any> = {};
|
|
501
499
|
for (const mediaFieldPath of populateMedia) {
|
|
502
|
-
|
|
500
|
+
await this.populateMediaObject(mediaFieldPath, model, entity);
|
|
503
501
|
}
|
|
504
|
-
entity['_media'] = mediaObj;
|
|
505
502
|
}
|
|
506
503
|
return entities;
|
|
507
504
|
}
|
|
508
505
|
|
|
509
|
-
|
|
506
|
+
// Adds the media with full URL to the entity / nested entity
|
|
507
|
+
private async populateMediaObject(mediaFieldPath: string, model: ModelMetadata, entity: T) {
|
|
510
508
|
if (mediaFieldPath.includes('.')) { // mediaFieldPath is a nested field
|
|
511
509
|
const pathParts = mediaFieldPath.split('.');
|
|
512
510
|
const mediaFieldMetadata = await this.getFieldMetadataRecursively(pathParts, model.fields);
|
|
@@ -515,12 +513,15 @@ export class CRUDService<T> { // Add two generic value i.e Person,CreatePersonDt
|
|
|
515
513
|
}
|
|
516
514
|
|
|
517
515
|
// We can assume that the media field entity model is already populated as part of the entity data
|
|
518
|
-
const
|
|
519
|
-
if (!
|
|
516
|
+
const mediaFieldEntities = this.getMediaFieldEntities(entity, pathParts);
|
|
517
|
+
if (!mediaFieldEntities || mediaFieldEntities.length === 0) {
|
|
520
518
|
throw new BadRequestException(`Media field path ${mediaFieldPath} is not populated in model ${this.modelName}`);
|
|
521
519
|
}
|
|
522
|
-
|
|
523
|
-
|
|
520
|
+
// Populate the media field entities with the full URL
|
|
521
|
+
for (const mediaFieldEntity of mediaFieldEntities) {
|
|
522
|
+
const mediaWithFullUrl = await this.getMediaWithFullUrl(mediaFieldEntity, mediaFieldMetadata);
|
|
523
|
+
this.appendMediaKey(mediaFieldEntity, mediaWithFullUrl);
|
|
524
|
+
}
|
|
524
525
|
}
|
|
525
526
|
else {
|
|
526
527
|
// mediaFieldPath is a single field
|
|
@@ -529,29 +530,40 @@ export class CRUDService<T> { // Add two generic value i.e Person,CreatePersonDt
|
|
|
529
530
|
throw new BadRequestException(`Media field ${mediaFieldPath} not found in model ${this.modelName}`);
|
|
530
531
|
}
|
|
531
532
|
const mediaWithFullUrl = await this.getMediaWithFullUrl(entity, mediaFieldMetadata);
|
|
532
|
-
|
|
533
|
+
this.appendMediaKey(entity, mediaWithFullUrl);
|
|
533
534
|
}
|
|
534
535
|
}
|
|
535
536
|
|
|
536
|
-
|
|
537
|
-
|
|
537
|
+
// Add the media with full URL to the entity
|
|
538
|
+
private appendMediaKey(entity: T, mediaWithFullUrl: MediaWithFullUrl[]) {
|
|
539
|
+
// if _media key already exists, append the new media to the existing array
|
|
540
|
+
if (entity['_media']) {
|
|
541
|
+
entity['_media'] = [...entity['_media'], ...mediaWithFullUrl];
|
|
542
|
+
}
|
|
543
|
+
else {
|
|
544
|
+
entity['_media'] = mediaWithFullUrl;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
private getMediaFieldEntities(entity: T, mediaPathParts: string[]): T[] {
|
|
549
|
+
let entityPart = entity;
|
|
538
550
|
for (let i = 0; i < mediaPathParts.length - 1; i++) {
|
|
539
551
|
const pathPart = mediaPathParts[i];
|
|
540
|
-
if (
|
|
541
|
-
|
|
552
|
+
if (entity[pathPart]) {
|
|
553
|
+
entityPart = entity[pathPart];
|
|
542
554
|
} else {
|
|
543
555
|
throw new BadRequestException(`Media field ${pathPart} not found in entity ${JSON.stringify(entity)}`);
|
|
544
556
|
}
|
|
545
557
|
}
|
|
546
|
-
return
|
|
558
|
+
return isArray(entityPart) ? entityPart : [entityPart];
|
|
547
559
|
}
|
|
548
560
|
|
|
549
|
-
async getMediaWithFullUrl(mediaEntity: any, mediaFieldMetadata: FieldMetadata): Promise<MediaWithFullUrl>{
|
|
561
|
+
async getMediaWithFullUrl(mediaEntity: any, mediaFieldMetadata: FieldMetadata): Promise<MediaWithFullUrl[]>{
|
|
550
562
|
const storageProviderMetadata = mediaFieldMetadata.mediaStorageProvider;
|
|
551
563
|
const storageProviderType = storageProviderMetadata.type as MediaStorageProviderType;
|
|
552
564
|
const storageProvider = await getMediaStorageProvider(this.moduleRef, storageProviderType);
|
|
553
565
|
const mediaDetails = await storageProvider.retrieve(mediaEntity, mediaFieldMetadata);
|
|
554
|
-
return mediaDetails as MediaWithFullUrl;
|
|
566
|
+
return mediaDetails as MediaWithFullUrl[];
|
|
555
567
|
}
|
|
556
568
|
|
|
557
569
|
async findOne(id: number, query: any, solidRequestContext: any = {}) {
|