Skip to content

denwa-nest-shared

Shared NestJS module for DTO validation, RBAC, TypeORM query building, and OpenAPI client

file-management

core
120 linesSource

Managing file and image metadata stored as JSON strings in the database. Standardizes the lifecycle of temporary uploads, entity sync, and S3 metadata formatting.

File & Image JSON Storage

Use this skill to manage file metadata that is stored as stringified JSON in your database. This is used to track S3 locations, alt tags, and file states.

Setup

Synchronize temporary uploads with an entity.

ts
import {
  prepareTempImages,
  updateEntityImages,
  ImageFileDto,
  TempImagesDto,
  AllImagesDto,
} from 'denwa-nest-shared';

async function updateProductGallery(
  product: { images: string | null },
  filesFromUploadService: ImageFileDto[],
  allImages: AllImagesDto[],
  tempImages: TempImagesDto[],
) {
  // 1. Map uploaded microservice files with alt metadata
  const prepared = prepareTempImages(filesFromUploadService, tempImages);

  // 2. Reconcile with existing entity images (returns newImages and deletedImages)
  const { newImages, deletedImages } = updateEntityImages(product.images, allImages, prepared);
  product.images = newImages ? JSON.stringify(newImages) : null;

  await repository.save(product);
  return { product, deletedImages };
}

Core Patterns

Image JSON Parsing & Validation

Parse and validate a stored image JSON string against Zod schema.

ts
import { prepareImages } from 'denwa-nest-shared';

// Parses and validates a JSON string of IPreparedImage[]
const images = prepareImages(product.imagesJsonString);

File Meta Retrieval

The library assumes that your database columns store these as strings. Ensure you handle the serialized state correctly in your repositories.

ts
// Typical DTO for accepting image updates
import { ApiValidationProperty } from 'denwa-nest-shared';

export class UpdateProfileDto {
  @ApiValidationProperty({ type: 'string', isOptional: true })
  avatar?: string; // Standardized image JSON string
}

Common Mistakes

HIGH Storing Objects instead of Strings

Wrong:

ts
product.images = [{ url: '...', name: '...' }]; // DB driver might fail or save as object

Correct:

ts
product.images = JSON.stringify([{ url: '...', name: '...' }]);

The library's internal helpers and TypeORM configuration for these columns often expect a stringified JSON. While some drivers handle object auto-serialization, manually ensuring string format prevents hydration issues.

Source: Denwa799/nest-shared:src/lib.ts:741

MEDIUM Mismatched Folder Keys

Wrong:

ts
const prepared = prepareTempImages(temp, 'products');
product.images = updateEntityImages(product.images, prepared); // Folder key mismatch logic

Correct:

ts
const folder = 'gallery';
const prepared = prepareTempImages(temp, folder);
product.images = updateEntityImages(product.images, prepared);

While the folder key is primarily for S3 organization, inconsistent use during update cycles can lead to metadata being assigned to the wrong entity scope.

Source: Denwa799/nest-shared:src/lib.ts:762

References