Home / Class/ ImmutableDataStore Class — astro Architecture

ImmutableDataStore Class — astro Architecture

Architecture documentation for the ImmutableDataStore class in data-store.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  ac7e948d_1024_ae45_2ea1_742b83aa21b5["ImmutableDataStore"]
  dabf6768_9542_21ce_e4c4_299b1540a491["data-store.ts"]
  ac7e948d_1024_ae45_2ea1_742b83aa21b5 -->|defined in| dabf6768_9542_21ce_e4c4_299b1540a491
  ea0f9daf_bb11_0cb8_a451_721dba8256fd["constructor()"]
  ac7e948d_1024_ae45_2ea1_742b83aa21b5 -->|method| ea0f9daf_bb11_0cb8_a451_721dba8256fd
  3b90745d_78ea_8543_7f62_501ddedc7a8d["get()"]
  ac7e948d_1024_ae45_2ea1_742b83aa21b5 -->|method| 3b90745d_78ea_8543_7f62_501ddedc7a8d
  95a007eb_12c4_3a4d_3d17_33a2c145ee48["entries()"]
  ac7e948d_1024_ae45_2ea1_742b83aa21b5 -->|method| 95a007eb_12c4_3a4d_3d17_33a2c145ee48
  a31fb3d7_f070_ed00_c9e0_28d2793a6777["values()"]
  ac7e948d_1024_ae45_2ea1_742b83aa21b5 -->|method| a31fb3d7_f070_ed00_c9e0_28d2793a6777
  b91f0eb5_3cd9_b599_b891_de6b620c40e9["keys()"]
  ac7e948d_1024_ae45_2ea1_742b83aa21b5 -->|method| b91f0eb5_3cd9_b599_b891_de6b620c40e9
  1551fb1a_2da2_076e_a109_11b4fd0f31b9["has()"]
  ac7e948d_1024_ae45_2ea1_742b83aa21b5 -->|method| 1551fb1a_2da2_076e_a109_11b4fd0f31b9
  55253886_f348_abb0_c589_11025f6f6a76["hasCollection()"]
  ac7e948d_1024_ae45_2ea1_742b83aa21b5 -->|method| 55253886_f348_abb0_c589_11025f6f6a76
  e6a0837f_2d31_99b0_1458_d79705a27d08["collections()"]
  ac7e948d_1024_ae45_2ea1_742b83aa21b5 -->|method| e6a0837f_2d31_99b0_1458_d79705a27d08
  6a6d9079_941e_e9bb_9299_6752c713dce4["fromModule()"]
  ac7e948d_1024_ae45_2ea1_742b83aa21b5 -->|method| 6a6d9079_941e_e9bb_9299_6752c713dce4
  07ad6235_cde0_7a74_fa45_64692b970316["fromMap()"]
  ac7e948d_1024_ae45_2ea1_742b83aa21b5 -->|method| 07ad6235_cde0_7a74_fa45_64692b970316

Relationship Graph

Source Code

packages/astro/src/content/data-store.ts lines 44–108

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

	constructor() {
		this._collections = new Map();
	}

	get<T = DataEntry>(collectionName: string, key: string): T | undefined {
		return this._collections.get(collectionName)?.get(String(key));
	}

	entries<T = DataEntry>(collectionName: string): Array<[id: string, T]> {
		const collection = this._collections.get(collectionName) ?? new Map();
		return [...collection.entries()];
	}

	values<T = DataEntry>(collectionName: string): Array<T> {
		const collection = this._collections.get(collectionName) ?? new Map();
		return [...collection.values()];
	}

	keys(collectionName: string): Array<string> {
		const collection = this._collections.get(collectionName) ?? new Map();
		return [...collection.keys()];
	}

	has(collectionName: string, key: string) {
		const collection = this._collections.get(collectionName);
		if (collection) {
			return collection.has(String(key));
		}
		return false;
	}

	hasCollection(collectionName: string) {
		return this._collections.has(collectionName);
	}

	collections() {
		return this._collections;
	}

	/**
	 * Attempts to load a DataStore from the virtual module.
	 * This only works in Vite.
	 */
	static async fromModule() {
		try {
			// @ts-expect-error - this is a virtual module
			const data = await import('astro:data-layer-content');
			if (data.default instanceof Map) {
				return ImmutableDataStore.fromMap(data.default);
			}
			const map = devalue.unflatten(data.default);
			return ImmutableDataStore.fromMap(map);
		} catch {}
		return new ImmutableDataStore();
	}

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

Frequently Asked Questions

What is the ImmutableDataStore class?
ImmutableDataStore is a class in the astro codebase, defined in packages/astro/src/content/data-store.ts.
Where is ImmutableDataStore defined?
ImmutableDataStore is defined in packages/astro/src/content/data-store.ts at line 44.

Analyze Your Own Codebase

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

Try Supermodel Free