Home / Function/ defineCollection() — astro Function Reference

defineCollection() — astro Function Reference

Architecture documentation for the defineCollection() function in config.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  712677ed_d546_aba3_061e_3f904b7b139f["defineCollection()"]
  be7d1eab_916b_90f9_3072_d169ed547abf["config.ts"]
  712677ed_d546_aba3_061e_3f904b7b139f -->|defined in| be7d1eab_916b_90f9_3072_d169ed547abf
  65db2577_d5e4_a284_6317_4ddba4134e0d["getImporterFilename()"]
  712677ed_d546_aba3_061e_3f904b7b139f -->|calls| 65db2577_d5e4_a284_6317_4ddba4134e0d
  style 712677ed_d546_aba3_061e_3f904b7b139f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/content/config.ts lines 176–211

export function defineCollection<
	S extends BaseSchema,
	TLoader extends LoaderConstraint<{ id: string }> = LoaderConstraint<{ id: string }>,
>(config: CollectionConfig<S, TLoader>): CollectionConfig<S, TLoader> {
	const importerFilename = getImporterFilename();

	if (importerFilename?.includes('live.config')) {
		throw new AstroError({
			...AstroErrorData.LiveContentConfigError,
			message: AstroErrorData.LiveContentConfigError.message(
				'Collections in a live config file must use `defineLiveCollection`.',
				importerFilename,
			),
		});
	}

	if ('loader' in config) {
		if (config.type && config.type !== CONTENT_LAYER_TYPE) {
			throw new AstroUserError(
				`A content collection is defined with legacy features (e.g. missing a \`loader\` or has a \`type\`). Check your collection definitions in ${importerFilename ?? 'your content config file'} to ensure that all collections are defined using the current properties.`,
			);
		}
		if (
			typeof config.loader === 'object' &&
			typeof config.loader.load !== 'function' &&
			('loadEntry' in config.loader || 'loadCollection' in config.loader)
		) {
			throw new AstroUserError(
				`Live content collections must be defined in "src/live.config.ts" file. Check the loaders used in "${importerFilename ?? 'your content config file'}" to ensure you are not using a live loader to define a build-time content collection.`,
			);
		}
		config.type = CONTENT_LAYER_TYPE;
	}
	if (!config.type) config.type = 'content';
	return config;
}

Subdomains

Frequently Asked Questions

What does defineCollection() do?
defineCollection() is a function in the astro codebase, defined in packages/astro/src/content/config.ts.
Where is defineCollection() defined?
defineCollection() is defined in packages/astro/src/content/config.ts at line 176.
What does defineCollection() call?
defineCollection() calls 1 function(s): getImporterFilename.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free