Skip to content

Commit

Permalink
Export LoaderContext types (#11914)
Browse files Browse the repository at this point in the history
* Export data store types

* Format

* Change name again!
  • Loading branch information
ascorbic authored Sep 4, 2024
1 parent 3804711 commit b5d827b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changeset/mean-donkeys-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
---

Exports types for all `LoaderContext` properties from `astro/loaders` to make it easier to use them in custom loaders.
The `ScopedDataStore` interface (which was previously internal) is renamed to `DataStore`, to reflect the fact that it's the only public API for the data store.
16 changes: 8 additions & 8 deletions packages/astro/src/content/data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface DataEntry<TData extends Record<string, unknown> = Record<string
* To add or modify data, use {@link MutableDataStore} instead.
*/

export class DataStore {
export class ImmutableDataStore {
protected _collections = new Map<string, Map<string, any>>();

constructor() {
Expand Down Expand Up @@ -92,31 +92,31 @@ export class DataStore {
// @ts-expect-error - this is a virtual module
const data = await import('astro:data-layer-content');
if (data.default instanceof Map) {
return DataStore.fromMap(data.default);
return ImmutableDataStore.fromMap(data.default);
}
const map = devalue.unflatten(data.default);
return DataStore.fromMap(map);
return ImmutableDataStore.fromMap(map);
} catch {}
return new DataStore();
return new ImmutableDataStore();
}

static async fromMap(data: Map<string, Map<string, any>>) {
const store = new DataStore();
const store = new ImmutableDataStore();
store._collections = data;
return store;
}
}

function dataStoreSingleton() {
let instance: Promise<DataStore> | DataStore | undefined = undefined;
let instance: Promise<ImmutableDataStore> | ImmutableDataStore | undefined = undefined;
return {
get: async () => {
if (!instance) {
instance = DataStore.fromModule();
instance = ImmutableDataStore.fromModule();
}
return instance;
},
set: (store: DataStore) => {
set: (store: ImmutableDataStore) => {
instance = store;
},
};
Expand Down
9 changes: 6 additions & 3 deletions packages/astro/src/content/loaders/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import type { ZodSchema } from 'zod';
import type { AstroIntegrationLogger } from '../../core/logger/core.js';
import type { AstroConfig } from '../../types/public/config.js';
import type { ContentEntryType } from '../../types/public/content.js';
import type { MetaStore, ScopedDataStore } from '../mutable-data-store.js';
import type { DataStore, MetaStore } from '../mutable-data-store.js';

export type { DataStore, MetaStore };

export interface ParseDataOptions<TData extends Record<string, unknown>> {
/** The ID of the entry. Unique per collection */
Expand All @@ -17,8 +19,8 @@ export interface ParseDataOptions<TData extends Record<string, unknown>> {
export interface LoaderContext {
/** The unique name of the collection */
collection: string;
/** A database abstraction to store the actual data */
store: ScopedDataStore;
/** A database to store the actual data */
store: DataStore;
/** A simple KV store, designed for things like sync tokens */
meta: MetaStore;
logger: AstroIntegrationLogger;
Expand All @@ -35,6 +37,7 @@ export interface LoaderContext {

/** If the loader has been triggered by an integration, this may optionally contain extra data set by that integration */
refreshContextData?: Record<string, unknown>;
/** @internal */
entryTypes: Map<string, ContentEntryType>;
}

Expand Down
9 changes: 5 additions & 4 deletions packages/astro/src/content/mutable-data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Traverse } from 'neotraverse/modern';
import { imageSrcToImportId, importIdToSymbolName } from '../assets/utils/resolveImports.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { IMAGE_IMPORT_PREFIX } from './consts.js';
import { type DataEntry, DataStore, type RenderedContent } from './data-store.js';
import { type DataEntry, ImmutableDataStore, type RenderedContent } from './data-store.js';
import { contentModuleToId } from './utils.js';

const SAVE_DEBOUNCE_MS = 500;
Expand All @@ -13,7 +13,7 @@ const SAVE_DEBOUNCE_MS = 500;
* Extends the DataStore with the ability to change entries and write them to disk.
* This is kept as a separate class to avoid needing node builtins at runtime, when read-only access is all that is needed.
*/
export class MutableDataStore extends DataStore {
export class MutableDataStore extends ImmutableDataStore {
#file?: PathLike;

#assetsFile?: PathLike;
Expand Down Expand Up @@ -190,7 +190,7 @@ export default new Map([\n${lines.join(',\n')}]);
}
}

scopedStore(collectionName: string): ScopedDataStore {
scopedStore(collectionName: string): DataStore {
return {
get: <TData extends Record<string, unknown> = Record<string, unknown>>(key: string) =>
this.get<DataEntry<TData>>(collectionName, key),
Expand Down Expand Up @@ -329,7 +329,8 @@ export default new Map([\n${lines.join(',\n')}]);
}
}

export interface ScopedDataStore {
// This is the scoped store for a single collection. It's a subset of the MutableDataStore API, and is the only public type.
export interface DataStore {
get: <TData extends Record<string, unknown> = Record<string, unknown>>(
key: string,
) => DataEntry<TData> | undefined;
Expand Down

0 comments on commit b5d827b

Please sign in to comment.