74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
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';
|
|
}
|
|
}
|