X Enterprises
fastify-ximagepipeline

listMedia

List all Media records for a given sourceType and sourceId, ordered by most recent first.

listMedia

List all Media records for a given sourceType and sourceId, ordered by createdAt descending (most recent first).

Signature

fastify.xImagePipeline.listMedia(
  sourceType: string,
  sourceId: string,
): Promise<MediaRecord[]>

interface MediaRecord {
  id: string
  sourceType: string
  sourceId: string
  urls: Record<string, string>   // variant name → URL
  originalUrl: string | null
  width: number
  height: number
  aspectRatio: string
  blurhash: string
  createdAt: Date
  updatedAt: Date
}

Params

NameTypeRequiredDescription
sourceTypestringYesSource type (e.g. "avatar", "gallery")
sourceIdstringYesOwning resource ID (e.g. user ID, post ID)

Returns

An array of MediaRecord objects. Empty array if no media exists for that source.

Throws

Throws on database errors only.

Examples

fastify.get("/users/:id/gallery", async (request, reply) => {
  const images = await fastify.xImagePipeline.listMedia(
    "gallery",
    request.params.id,
  );

  return {
    images: images.map((img) => ({
      id: img.id,
      thumbnail: img.urls.md,
      full: img.urls.xl,
      blurhash: img.blurhash,
      aspectRatio: img.aspectRatio,
    })),
  };
});

Find and replace a user's current avatar

fastify.put("/users/:id/avatar", async (request, reply) => {
  // Delete existing avatar(s) before processing new upload
  const existing = await fastify.xImagePipeline.listMedia("avatar", request.params.id);

  await Promise.all(
    existing.map((m) => fastify.xImagePipeline.deleteMedia(m.id)),
  );

  // Proceed with the new upload via POST /image-pipeline/upload
  return reply.redirect(`/image-pipeline/upload`);
});

See Also

AI Context

package: "@xenterprises/fastify-ximagepipeline"
method: fastify.xImagePipeline.listMedia(sourceType, sourceId)
use-when: List all COMPLETE media records for a given source (e.g. all avatars for a user)
returns: Media[] with variant URLs, blurhash, and metadata
Copyright © 2026