Home / Class/ TestGlobalCache Class — drizzle-orm Architecture

TestGlobalCache Class — drizzle-orm Architecture

Architecture documentation for the TestGlobalCache class in pg-common-cache.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  d43d88d6_c6b6_386d_d59b_76b8db53bf29["TestGlobalCache"]
  7b6fdfe3_d9a8_26c3_430d_2261b1d7aadb["pg-common-cache.ts"]
  d43d88d6_c6b6_386d_d59b_76b8db53bf29 -->|defined in| 7b6fdfe3_d9a8_26c3_430d_2261b1d7aadb
  0ed45967_9ba7_0d43_8a4d_1ffd95022108["constructor()"]
  d43d88d6_c6b6_386d_d59b_76b8db53bf29 -->|method| 0ed45967_9ba7_0d43_8a4d_1ffd95022108
  42374f92_fcf1_ff28_3d7c_abb14b2c7444["strategy()"]
  d43d88d6_c6b6_386d_d59b_76b8db53bf29 -->|method| 42374f92_fcf1_ff28_3d7c_abb14b2c7444
  b84a0f4b_695c_9338_6ca5_3f8b6f612358["get()"]
  d43d88d6_c6b6_386d_d59b_76b8db53bf29 -->|method| b84a0f4b_695c_9338_6ca5_3f8b6f612358
  1381d0ac_e684_3f19_6d40_fdbfb7c607f3["put()"]
  d43d88d6_c6b6_386d_d59b_76b8db53bf29 -->|method| 1381d0ac_e684_3f19_6d40_fdbfb7c607f3
  7f134d68_8067_d712_dd1e_a5e7cfd0f781["onMutate()"]
  d43d88d6_c6b6_386d_d59b_76b8db53bf29 -->|method| 7f134d68_8067_d712_dd1e_a5e7cfd0f781

Relationship Graph

Source Code

integration-tests/tests/pg/pg-common-cache.ts lines 12–70

export class TestGlobalCache extends Cache {
	private globalTtl: number = 1000;
	private usedTablesPerKey: Record<string, string[]> = {};

	constructor(private kv: Keyv = new Keyv()) {
		super();
	}

	override strategy(): 'explicit' | 'all' {
		return 'all';
	}
	override async get(key: string, _tables: string[], _isTag: boolean): Promise<any[] | undefined> {
		const res = await this.kv.get(key) ?? undefined;
		return res;
	}
	override async put(
		key: string,
		response: any,
		tables: string[],
		isTag: boolean,
		config?: CacheConfig,
	): Promise<void> {
		await this.kv.set(key, response, config ? config.ex : this.globalTtl);
		for (const table of tables) {
			const keys = this.usedTablesPerKey[table];
			if (keys === undefined) {
				this.usedTablesPerKey[table] = [key];
			} else {
				keys.push(key);
			}
		}
	}
	override async onMutate(params: MutationOption): Promise<void> {
		const tagsArray = params.tags ? Array.isArray(params.tags) ? params.tags : [params.tags] : [];
		const tablesArray = params.tables ? Array.isArray(params.tables) ? params.tables : [params.tables] : [];

		const keysToDelete = new Set<string>();

		for (const table of tablesArray) {
			const tableName = is(table, Table) ? getTableName(table) : table as string;
			const keys = this.usedTablesPerKey[tableName] ?? [];
			for (const key of keys) keysToDelete.add(key);
		}

		if (keysToDelete.size > 0 || tagsArray.length > 0) {
			for (const tag of tagsArray) {
				await this.kv.delete(tag);
			}

			for (const key of keysToDelete) {
				await this.kv.delete(key);
				for (const table of tablesArray) {
					const tableName = is(table, Table) ? getTableName(table) : table as string;
					this.usedTablesPerKey[tableName] = [];
				}
			}
		}
	}
}

Domain

Frequently Asked Questions

What is the TestGlobalCache class?
TestGlobalCache is a class in the drizzle-orm codebase, defined in integration-tests/tests/pg/pg-common-cache.ts.
Where is TestGlobalCache defined?
TestGlobalCache is defined in integration-tests/tests/pg/pg-common-cache.ts at line 12.

Analyze Your Own Codebase

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

Try Supermodel Free