imean-cassandra-orm
Version:
cassandra orm
280 lines (265 loc) • 9.12 kB
TypeScript
import * as zod from 'zod';
import { z } from 'zod';
import { ClientOptions as ClientOptions$1 } from 'cassandra-driver';
type CassandraQueryOptions = {
prepare?: boolean;
fetchSize?: number;
pageState?: string;
hints?: string[];
};
type CassandraQueryResult<Row extends Record<string, unknown> = Record<string, unknown>> = {
rows: Row[];
pageState?: string | null;
};
interface CassandraExecutor {
execute<Row extends Record<string, unknown> = Record<string, unknown>>(cql: string, params?: unknown[] | Record<string, unknown>, options?: CassandraQueryOptions): Promise<CassandraQueryResult<Row>>;
batch(queries: Array<{
query: string;
params?: unknown[];
}>, options?: CassandraQueryOptions): Promise<unknown>;
}
type ColumnKind = "partition_key" | "clustering" | "regular";
type ExistingColumn = {
name: string;
type: string;
kind: ColumnKind;
};
type ExistingIndex = {
name: string;
options?: unknown;
};
type ExistingTableInfo = {
keyspace: string;
tableName: string;
columns: ExistingColumn[];
indexes: ExistingIndex[];
};
type DesiredColumn = {
name: string;
type: string;
kind: ColumnKind;
};
type DesiredIndex = {
name: string;
field: string;
options?: unknown;
};
type DesiredTableSpec = {
keyspace: string;
tableName: string;
columns: DesiredColumn[];
clusteringOrder?: Array<{
field: string;
order: "ASC" | "DESC";
}>;
indexes?: DesiredIndex[];
};
interface SchemaInspector {
getTableInfo(keyspace: string, tableName: string): Promise<ExistingTableInfo | null>;
}
interface SchemaMigrator {
execute(cql: string): Promise<void>;
}
type TelemetryAttributes = Record<string, string | number | boolean>;
interface Telemetry {
debug(message: string, attrs?: TelemetryAttributes): void;
info(message: string, attrs?: TelemetryAttributes): void;
warn(message: string, attrs?: TelemetryAttributes): void;
error(message: string, attrs?: TelemetryAttributes): void;
withSpan<T>(name: string, fn: () => Promise<T>, attrs?: TelemetryAttributes): Promise<T>;
}
type WriteOperation = "insert" | "update" | "delete" | "batchInsert";
type WriteContext = {
tableName: string;
keyspace?: string;
operation: WriteOperation;
keySummary?: Record<string, unknown>;
data?: Record<string, unknown>;
batchData?: Record<string, unknown>[];
patch?: Record<string, unknown>;
where?: Record<string, unknown>;
};
interface WriteHook {
onSuccess(ctx: WriteContext, durationMs: number): void | Promise<void>;
onFailure(ctx: WriteContext, error: unknown, durationMs: number): void | Promise<void>;
}
type IndexOptions = {
case_sensitive?: boolean;
normalize?: boolean;
ascii?: boolean;
similarity_function?: "DOT_PRODUCT" | "COSINE" | "EUCLIDEAN";
};
interface ClickHouseConfig {
host: string;
port: number;
username: string;
password: string;
database: string;
protocol?: "http" | "https";
timeout?: number;
debug?: boolean;
}
type DualWritePolicy = {
enabled: boolean;
operations?: Partial<Record<WriteOperation, boolean>>;
shouldWrite?: (ctx: WriteContext) => boolean;
};
type QueryOperator<T> = T | {
"=": T;
} | {
"!=": T;
} | {
">": T;
} | {
">=": T;
} | {
"<": T;
} | {
"<=": T;
} | {
in: T[];
} | {
not_in: T[];
} | {
between: [T, T];
} | {
is_null: boolean;
} | {
regex: string;
};
type QueryCondition<F extends z.ZodRawShape> = {
[K in keyof F]?: QueryOperator<z.infer<F[K]>>;
};
type ClusteringKeyConfig = Record<string, "asc" | "desc">;
type ExtractPartitionKeyShape<F extends z.ZodRawShape, PK extends (keyof F)[]> = {
[K in PK[number]]: F[K];
};
type ExtractClusteringKeyShape<F extends z.ZodRawShape, CK extends ClusteringKeyConfig | undefined> = CK extends ClusteringKeyConfig ? {
[K in keyof CK]: F[K & keyof F];
} : never;
type TableSchemaConfig<F extends z.ZodRawShape> = {
keyspace: string;
tableName: string;
fields: F;
partitionKey: (keyof F)[];
clusteringKey?: ClusteringKeyConfig;
indexes?: Partial<Record<keyof F, boolean | IndexOptions>>;
};
declare class TableSchema<F extends z.ZodRawShape> {
readonly keyspace: string;
readonly tableName: string;
readonly fields: z.ZodObject<F>;
readonly partitionKey: Array<keyof F>;
readonly clusteringKey?: ClusteringKeyConfig;
readonly indexes?: Partial<Record<keyof F, boolean | IndexOptions>>;
readonly types: {
model: z.infer<z.ZodObject<F>>;
partitionKey: Record<string, unknown>;
clusteringKey: Record<string, unknown>;
};
readonly schemas: {
model: z.ZodObject<F>;
partitionKey: z.ZodObject<ExtractPartitionKeyShape<F, (keyof F)[]>>;
clusteringKey?: z.ZodObject<any>;
};
constructor(config: TableSchemaConfig<F>);
get modelSchema(): z.ZodObject<F>;
toDesiredTableSpec(): DesiredTableSpec;
}
declare function defineTable<F extends z.ZodRawShape>(config: TableSchemaConfig<F>): TableSchema<F>;
type ModelOptions<F extends z.ZodRawShape> = {
schema: TableSchema<F>;
executor: CassandraExecutor;
inspector?: SchemaInspector;
migrator?: SchemaMigrator;
telemetry?: Telemetry;
writeHook?: WriteHook;
};
declare class Model<F extends z.ZodRawShape> {
readonly schema: TableSchema<F>;
private readonly repo;
private readonly schemaSync?;
constructor(opts: ModelOptions<F>);
syncSchema(opts?: {
forceRecreate?: boolean;
}): Promise<string>;
insert(data: z.infer<z.ZodObject<F>>): Promise<void>;
batchInsert(data: z.infer<z.ZodObject<F>>[]): Promise<void>;
update(where: QueryCondition<F>, patch: Partial<z.infer<z.ZodObject<F>>>): Promise<void>;
delete(where: QueryCondition<F>): Promise<void>;
find(where: QueryCondition<F>, opts?: {
limit?: number;
allowFiltering?: boolean;
}): Promise<z.infer<z.ZodObject<F>>[]>;
findPaged(where: QueryCondition<F>, opts?: {
fetchSize?: number;
pageState?: string;
limit?: number;
allowFiltering?: boolean;
}): Promise<{
data: z.infer<z.ZodObject<F>>[];
pageState: string | null;
}>;
findOne(where: QueryCondition<F>, opts?: {
allowFiltering?: boolean;
}): Promise<z.infer<z.ZodObject<F>> | null>;
}
type ClientOptions = {
executor: CassandraExecutor;
inspector?: SchemaInspector;
migrator?: SchemaMigrator;
telemetry?: Telemetry;
writeHook?: WriteHook;
autoSchemaSync?: boolean;
};
declare class Client {
readonly executor: CassandraExecutor;
readonly inspector?: SchemaInspector;
readonly migrator?: SchemaMigrator;
readonly telemetry: Telemetry;
readonly writeHook?: WriteHook;
constructor(opts: ClientOptions);
model<F extends zod.z.ZodRawShape>(schema: TableSchema<F>): Model<F>;
}
type ClientConfig = {
cassandra: ClientOptions$1;
telemetry?: Telemetry;
writeHook?: WriteHook;
schemaSync?: {
enabled?: boolean;
};
clickhouse?: {
config: ClickHouseConfig;
policy?: DualWritePolicy;
enabled?: boolean;
};
};
declare function createClient(config: ClientConfig): Promise<{
client: Client;
shutdown: () => Promise<void>;
}>;
interface CassandraTypeMarker {
_cassandraType: string;
}
declare const Types: {
boolean: typeof z.boolean;
text: typeof z.string;
double: typeof z.number;
enum: typeof z.enum;
object: typeof z.object;
json: () => z.ZodAny & CassandraTypeMarker;
record: <Keys extends z.ZodTypeAny, Value extends z.ZodTypeAny>(keyType: Keys, valueType: Value) => z.ZodRecord<z.ZodType<string | number | symbol, unknown, z.core.$ZodTypeInternals<string | number | symbol, unknown>>, Value> & CassandraTypeMarker;
float: () => z.ZodPipe<z.ZodNumber, z.ZodTransform<number, number>> & CassandraTypeMarker;
int: () => z.ZodNumber & CassandraTypeMarker;
bigint: () => z.ZodBigInt & CassandraTypeMarker;
timestamp: () => z.ZodDate & CassandraTypeMarker;
time: () => z.ZodDate & CassandraTypeMarker;
date: () => z.ZodDate & CassandraTypeMarker;
uuid: () => z.ZodString & CassandraTypeMarker;
email: () => z.ZodString & CassandraTypeMarker;
blob: () => z.ZodCustom<Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>> & CassandraTypeMarker;
list: <T extends z.ZodTypeAny>(elementType: T) => z.ZodArray<T> & CassandraTypeMarker;
set: <T extends z.ZodTypeAny>(elementType: T) => z.ZodSet<T> & CassandraTypeMarker;
vector: (dimension: number) => z.ZodPipe<z.ZodArray<z.ZodNumber>, z.ZodTransform<number[], number[]>> & CassandraTypeMarker;
};
export { type ClickHouseConfig, Client, type ClientConfig, type ClientOptions, type ClusteringKeyConfig, type ExtractClusteringKeyShape, type ExtractPartitionKeyShape, type IndexOptions, Model, type ModelOptions, TableSchema, type TableSchemaConfig, type Telemetry, Types, type WriteContext, type WriteHook, type WriteOperation, createClient, defineTable };