Query Media Items
For components like the MediaGallerySection
or the MediaList
you need a selection
of media items. There are three different ways how to create a selection.
- getEntry: Astro function to load a single item of an Astro content collection.
- getCollection: Astro function to filter items of an Astro content collection.
- getMediaItems: LightNet function to simplify filtering media items.
Here are examples for creating a selection of media items.
Create a Selection by Media Item Ids
Section titled “Create a Selection by Media Item Ids”---import { getEntry } from "astro:content"
const items = [ await getEntry("media", "faithful-freestyle--en"), await getEntry("media", "ollie-with-integrity--en"),]---
This example creates a selection of two media items by their identifiers using Astro’s getEntry
.
Find more information on getEntry
and getCollection
on the Astro docs on content collections.
Create a Selection using getMediaItems
Section titled “Create a Selection using getMediaItems”---import { getMediaItems } from "lightnet/content"
const items = await getMediaItems({ where: { type: "book", language: "en" }, orderBy: "dateCreated", limit: 10})---
The getMediaItems
function helps to query media items. It takes a sql-like query object as an argument.
The example filters for all media items that have the media-type book
and the language en
.
It orders them by the date they were created (newest first) and limits the result to 10 items.
Reference
Section titled “Reference”The getMediaItems
function accepts this parameters:
type: Object
example: { type: "book" }
required: false
Defines the attributes of the media items to be returned. Only media items with all matching attributes are returned.
where: type
Section titled “where: type”type: string
example: "book"
required: false
Sets the media item type to be returned.
where: language
Section titled “where: language”type: string
example: "en"
required: false
Sets the media item content language (as BCP-47 code) to be returned.
where: category
Section titled “where: category”type: string
example: "family"
required: false
Sets the media item category to be returned.
where: collection
Section titled “where: collection”type: string
example: "my-collection"
required: false
Sets the media item collection to be returned.
orderBy
Section titled “orderBy”type: "dateCreated" | "title"
example: "dateCreated"
required: false
Sets the sort order of the returned media items:
dateCreated
- sorts based on thedateCreated
attribute from newest to oldest.title
- sorts based on the media item title- none - sorts based on the media item identifier (as defined by the content JSON file name), the order of collection items is taking precedence.
type: number
example: 42
required: false
Sets the maximum amount of media items to be returned.