Skip to content

Commit

Permalink
feat: support defineCollection() for data config
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Apr 14, 2023
1 parent b798f7c commit 4f5503b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
17 changes: 11 additions & 6 deletions packages/astro/src/content/template/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,29 @@ declare module 'astro:content' {
export type SchemaContext = { image: ImageFunction };

type ContentCollectionConfig<S extends BaseSchema> = {
type: 'content';
type?: 'content';
schema?: S | ((context: SchemaContext) => S);
reference(): import('astro/zod').ZodEffects<S>;
};

type DataCollectionConfig<S extends BaseSchema> = {
type: 'data';
schema?: S | ((context: SchemaContext) => S);
};

type GeneratedCollectionConfig<S extends BaseSchema> = {
reference(): import('astro/zod').ZodEffects<S>;
};

export function defineCollection<S extends BaseSchema>(
input: Omit<ContentCollectionConfig<S>, 'type' | 'reference'>
): ContentCollectionConfig<S>;
input: ContentCollectionConfig<S>
): ContentCollectionConfig<S> & GeneratedCollectionConfig<S>;
export function defineCollection<S extends BaseSchema>(
input: DataCollectionConfig<S>
): DataCollectionConfig<S> & GeneratedCollectionConfig<S>;

export function defineDataCollection<S extends BaseSchema>(
input: Omit<DataCollectionConfig<S>, 'type' | 'reference'>
): DataCollectionConfig<S>;
input: Omit<DataCollectionConfig<S>, 'type'>
): DataCollectionConfig<S> & GeneratedCollectionConfig<S>;

type AllValuesOf<T> = T extends any ? T[keyof T] : never;
type ValidEntrySlug<C extends keyof ContentEntryMap> = AllValuesOf<ContentEntryMap[C]>['slug'];
Expand Down
10 changes: 3 additions & 7 deletions packages/astro/src/content/template/virtual-mod.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ const collectionToRenderEntryMap = createCollectionToGlobResultMap({

let referenceKeyIncr = 0;

/**
* @param {any} partialConfig
* @param {'content' | 'data'} type
*/
function baseDefineCollection(partialConfig, type) {
function baseDefineCollection({ type = 'content', ...partialConfig }) {
// We don't know the collection name since this is defined on the `collections` export.
// Generate a unique key for the collection that we can use for lookups.
const referenceKey = String(referenceKeyIncr++);
Expand All @@ -62,11 +58,11 @@ function baseDefineCollection(partialConfig, type) {
}

export function defineCollection(config) {
return baseDefineCollection(config, 'content');
return baseDefineCollection(config);
}

export function defineDataCollection(config) {
return baseDefineCollection(config, 'data');
return baseDefineCollection({ type: 'data', ...config });
}

// TODO: Remove this when having this fallback is no longer relevant. 2.3? 3.0? - erika, 2023-04-04
Expand Down

0 comments on commit 4f5503b

Please sign in to comment.