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. 3 imports, 1 dependents.

File typescript DrizzleORM QueryBuilders 3 imports 1 dependents 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  7d233f23_2ad0_1787_1add_b603646ee5a1["cache.ts"]
  ce81fb60_5bfe_1801_89bb_8ae88028a0a7["types.ts"]
  7d233f23_2ad0_1787_1add_b603646ee5a1 --> ce81fb60_5bfe_1801_89bb_8ae88028a0a7
  27705a9d_afe9_57dd_8c97_e52d8a67d426["entity.ts"]
  7d233f23_2ad0_1787_1add_b603646ee5a1 --> 27705a9d_afe9_57dd_8c97_e52d8a67d426
  6edbc86d_00ec_e1a9_d845_ab84f4882062["index.ts"]
  7d233f23_2ad0_1787_1add_b603646ee5a1 --> 6edbc86d_00ec_e1a9_d845_ab84f4882062
  99737bc3_a631_a054_9291_f966c791930f["utils.ts"]
  99737bc3_a631_a054_9291_f966c791930f --> 7d233f23_2ad0_1787_1add_b603646ee5a1
  style 7d233f23_2ad0_1787_1add_b603646ee5a1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { entityKind } from '~/entity.ts';
import type { Table } from '~/index.ts';
import type { CacheConfig } from './types.ts';

export abstract class Cache {
	static readonly [entityKind]: string = 'Cache';

	abstract strategy(): 'explicit' | 'all';

	/**
	 * Invoked if we should check cache for cached response
	 * @param sql
	 * @param tables
	 */
	abstract get(
		key: string,
		tables: string[],
		isTag: boolean,
		isAutoInvalidate?: boolean,
	): Promise<any[] | undefined>;

	/**
	 * Invoked if new query should be inserted to cache
	 * @param sql
	 * @param tables
	 */
	abstract put(
		hashedQuery: string,
		response: any,
		tables: string[],
		isTag: boolean,
		config?: CacheConfig,
	): Promise<void>;

	/**
	 * Invoked if insert, update, delete was invoked
	 * @param tables
	 */
	abstract onMutate(
		params: MutationOption,
	): Promise<void>;
}

export class NoopCache extends Cache {
	override strategy() {
		return 'all' as const;
	}

	static override readonly [entityKind]: string = 'NoopCache';

	override async get(_key: string): Promise<any[] | undefined> {
		return undefined;
	}
	override async put(
		_hashedQuery: string,
		_response: any,
		_tables: string[],
		_config?: any,
	): Promise<void> {
		// noop
	}
	override async onMutate(_params: MutationOption): Promise<void> {
		// noop
	}
}

export type MutationOption = { tags?: string | string[]; tables?: Table<any> | Table<any>[] | string | string[] };

export async function hashQuery(sql: string, params?: any[]) {
	const dataToHash = `${sql}-${JSON.stringify(params)}`;
	const encoder = new TextEncoder();
	const data = encoder.encode(dataToHash);
	const hashBuffer = await crypto.subtle.digest('SHA-256', data);
	const hashArray = [...new Uint8Array(hashBuffer)];
	const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');

	return hashHex;
}

Domain

Subdomains

Functions

Classes

Dependencies

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 functions are defined in cache.ts?
cache.ts defines 1 function(s): hashQuery.
What does cache.ts depend on?
cache.ts imports 3 module(s): entity.ts, index.ts, types.ts.
What files import cache.ts?
cache.ts is imported by 1 file(s): utils.ts.
Where is cache.ts in the architecture?
cache.ts is located at drizzle-orm/src/cache/core/cache.ts (domain: DrizzleORM, subdomain: QueryBuilders, directory: drizzle-orm/src/cache/core).

Analyze Your Own Codebase

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

Try Supermodel Free