Home / File/ cache.ts — drizzle-orm Source File

cache.ts — drizzle-orm Source File

Architecture documentation for cache.ts, a typescript file in the drizzle-orm codebase. 4 imports, 1 dependents.

File typescript DrizzleORM QueryBuilders 4 imports 1 dependents 2 classes

Entity Profile

Dependency Diagram

graph LR
  8501d20b_9cbb_8c86_291c_90aa79abc599["cache.ts"]
  690f7dfc_0aea_9ee8_d6e7_26bbb3689031["drizzle-orm"]
  8501d20b_9cbb_8c86_291c_90aa79abc599 --> 690f7dfc_0aea_9ee8_d6e7_26bbb3689031
  1887ab54_ea90_d248_26a5_4f0e40379db4["core"]
  8501d20b_9cbb_8c86_291c_90aa79abc599 --> 1887ab54_ea90_d248_26a5_4f0e40379db4
  d1b6cd91_8772_b4c9_66e1_10ab8d57e474["types"]
  8501d20b_9cbb_8c86_291c_90aa79abc599 --> d1b6cd91_8772_b4c9_66e1_10ab8d57e474
  1fec5f3c_6c99_322c_4189_0006f171f3de["keyv"]
  8501d20b_9cbb_8c86_291c_90aa79abc599 --> 1fec5f3c_6c99_322c_4189_0006f171f3de
  e0204a71_2582_46d4_0d96_46af572427a8["gel.test.ts"]
  e0204a71_2582_46d4_0d96_46af572427a8 --> 8501d20b_9cbb_8c86_291c_90aa79abc599
  style 8501d20b_9cbb_8c86_291c_90aa79abc599 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { getTableName, is, Table } from 'drizzle-orm';
import type { MutationOption } from 'drizzle-orm/cache/core';
import { Cache } from 'drizzle-orm/cache/core';
import type { CacheConfig } from 'drizzle-orm/cache/core/types';
import Keyv from 'keyv';

// eslint-disable-next-line drizzle-internal/require-entity-kind
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] = [];
				}
			}
		}
	}
}

// eslint-disable-next-line drizzle-internal/require-entity-kind
export class TestCache extends TestGlobalCache {
	override strategy(): 'explicit' | 'all' {
		return 'explicit';
	}
}

Domain

Subdomains

Dependencies

  • core
  • drizzle-orm
  • keyv
  • types

Frequently Asked Questions

What does cache.ts do?
cache.ts is a source file in the drizzle-orm codebase, written in typescript. It belongs to the DrizzleORM domain, QueryBuilders subdomain.
What does cache.ts depend on?
cache.ts imports 4 module(s): core, drizzle-orm, keyv, types.
What files import cache.ts?
cache.ts is imported by 1 file(s): gel.test.ts.
Where is cache.ts in the architecture?
cache.ts is located at integration-tests/tests/gel/cache.ts (domain: DrizzleORM, subdomain: QueryBuilders, directory: integration-tests/tests/gel).

Analyze Your Own Codebase

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

Try Supermodel Free