Accept generated parse-style schemas in CultCacheTS

This commit is contained in:
Meta Vens 2026-05-06 06:33:13 +01:00
commit b7cc33a846
6 changed files with 86 additions and 14 deletions

View file

@ -30,7 +30,7 @@ type HydratedEntry = CultCacheEnvelope & {
};
type BackingStoreTypeReference = string | AnyCultCacheDocumentDefinition;
type AccessorInput = string | ((value: unknown) => unknown);
type AccessorInput = string | ((value: any) => unknown);
export class CultCacheBuilder {
readonly #cache = new CultCache();

View file

@ -1,8 +1,11 @@
import type { ZodType } from "zod";
import type {
AnyCultCacheDocumentDefinition,
CultCacheDocumentDefinition,
CultCacheDocumentRegistry,
CultCacheSchema,
} from "./types";
import type { AnyCultCacheDocumentDefinition, CultCacheDocumentDefinition, CultCacheDocumentRegistry } from "./types";
export function defineDocumentType<TSchema extends ZodType>(
export function defineDocumentType<TSchema extends CultCacheSchema>(
definition: CultCacheDocumentDefinition<TSchema>,
): CultCacheDocumentDefinition<TSchema> {
if (!definition.type || definition.type.trim().length === 0) {

View file

@ -10,9 +10,11 @@ export type {
CultCacheDocumentFormatter,
CultCacheDocumentIndexDefinition,
CultCacheDocumentRegistry,
CultCacheSchema,
CultCacheDocumentValue,
CultCacheEnvelope,
CultCacheIndexScalar,
InferCultCacheSchemaValue,
CultCacheStoreRegistration,
PushAllOptions,
} from "./types";

View file

@ -1,7 +1,12 @@
import { z, type ZodType } from "zod";
export type CultCacheIndexScalar = string | number | boolean | bigint;
export interface CultCacheSchema<TValue = unknown> {
parse(input: unknown): TValue;
}
export type InferCultCacheSchemaValue<TSchema extends CultCacheSchema> =
TSchema extends CultCacheSchema<infer TValue> ? TValue : never;
export type CultCacheDocumentFieldName<TValue> = TValue extends object ? Extract<keyof TValue, string> : never;
export type CultCacheDocumentAccessor<TValue> =
@ -18,19 +23,19 @@ export interface CultCacheDocumentIndexDefinition<TValue> {
accessor: CultCacheDocumentAccessor<TValue>;
}
export interface CultCacheDocumentDefinition<TSchema extends ZodType = ZodType> {
export interface CultCacheDocumentDefinition<TSchema extends CultCacheSchema = CultCacheSchema> {
type: string;
schema: TSchema;
schemaName?: string;
global?: boolean;
name?: CultCacheDocumentAccessor<z.infer<TSchema>>;
name?: CultCacheDocumentAccessor<InferCultCacheSchemaValue<TSchema>>;
indexes?:
| Readonly<Record<string, CultCacheDocumentAccessor<z.infer<TSchema>>>>
| readonly CultCacheDocumentIndexDefinition<z.infer<TSchema>>[];
formatter?: CultCacheDocumentFormatter<z.infer<TSchema>>;
| Readonly<Record<string, CultCacheDocumentAccessor<InferCultCacheSchemaValue<TSchema>>>>
| readonly CultCacheDocumentIndexDefinition<InferCultCacheSchemaValue<TSchema>>[];
formatter?: CultCacheDocumentFormatter<InferCultCacheSchemaValue<TSchema>>;
}
export type CultCacheDocumentValue<TDefinition extends CultCacheDocumentDefinition> = z.infer<
export type CultCacheDocumentValue<TDefinition extends CultCacheDocumentDefinition> = InferCultCacheSchemaValue<
TDefinition["schema"]
>;