imean-cassandra-orm
Version:
cassandra orm
301 lines (295 loc) • 9.48 kB
text/typescript
import { Client as Client$1, QueryOptions } from 'cassandra-driver';
import { z } from 'zod';
type TypeMap = {
text: string;
int: number;
bigint: number;
varint: number;
boolean: boolean;
timestamp: Date;
uuid: string;
blob: Uint8Array;
decimal: number;
float: number;
double: number;
list: any[];
set: Set<any>;
map: Record<string, any>;
};
type InferType<T extends keyof TypeMap> = TypeMap[T];
type FieldConfig<T extends keyof TypeMap> = {
type: T;
flags: {
partitionKey: boolean;
clusteringKey: boolean | {
order: "ASC" | "DESC";
};
optional: boolean;
static: boolean;
};
};
declare class FieldBuilder<T extends keyof TypeMap, F extends {
partitionKey: boolean;
clusteringKey: boolean | {
order: "ASC" | "DESC";
};
optional: boolean;
static: boolean;
} = {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}> {
type: T;
flags: F;
constructor(type: T, flags?: F);
partitionKey(): FieldBuilder<T, Omit<F, "partitionKey"> & {
partitionKey: true;
}>;
clusteringKey(order?: "ASC" | "DESC"): FieldBuilder<T, Omit<F, "clusteringKey"> & {
clusteringKey: true | {
order: "ASC" | "DESC";
};
}>;
optional(): FieldBuilder<T, Omit<F, "optional"> & {
optional: true;
}>;
static(): FieldBuilder<T, Omit<F, "static"> & {
static: true;
}>;
}
type InferFieldsByFlag<S extends Record<string, FieldConfig<keyof TypeMap>>, Flag extends "partitionKey" | "clusteringKey" | "static"> = {
[K in keyof S as S[K]["flags"][Flag] extends true | {
order: "ASC" | "DESC";
} ? K : never]: InferType<S[K]["type"]>;
};
type InferFields<S extends Record<string, FieldConfig<keyof TypeMap>>> = {
[K in keyof S]: S[K]["flags"]["optional"] extends true ? InferType<S[K]["type"]> | undefined : InferType<S[K]["type"]>;
};
declare const Field: {
text: () => FieldBuilder<"text", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
uuid: () => FieldBuilder<"uuid", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
int: () => FieldBuilder<"int", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
bigint: () => FieldBuilder<"bigint", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
varint: () => FieldBuilder<"varint", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
decimal: () => FieldBuilder<"decimal", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
float: () => FieldBuilder<"float", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
double: () => FieldBuilder<"double", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
boolean: () => FieldBuilder<"boolean", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
timestamp: () => FieldBuilder<"timestamp", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
blob: () => FieldBuilder<"blob", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
list: () => FieldBuilder<"list", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
set: () => FieldBuilder<"set", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
map: () => FieldBuilder<"map", {
partitionKey: false;
clusteringKey: false;
optional: false;
static: false;
}>;
};
/**
* 从 Model 实例中推断出原始字段类型
*
* @typeParam M - 继承自 Model 的类型
*
* @example
* ```typescript
* class User extends Model<{
* id: { type: "uuid"; flags: { partitionKey: true } };
* name: { type: "text" };
* }> {}
*
* type UserType = Infer<User>;
* // { id: string; name: string }
* ```
*/
type Infer<M extends Model<any>> = M extends Model<infer S> ? InferFields<S> : never;
/**
* 推导分区键字段类型
*
* @typeParam M - 继承自 Model 的类型
*
* @example
* ```typescript
* class User extends Model<{
* id: { type: "uuid"; flags: { partitionKey: true } };
* name: { type: "text" };
* }> {}
*
* type UserPartitionKey = InferPartitionKey<User>;
* // { id: string }
* ```
*/
type InferPartitionKey<M extends Model<any>> = M extends Model<infer S> ? InferFieldsByFlag<S, "partitionKey"> : never;
/**
* 推导聚类键字段类型
*
* @typeParam M - 继承自 Model 的类型
*
* @example
* ```typescript
* class User extends Model<{
* id: { type: "uuid"; flags: { partitionKey: true } };
* name: { type: "text"; flags: { clusteringKey: true } };
* }> {}
*
* type UserClusteringKey = InferClusteringKey<User>;
* // { name: string }
* ```
*/
type InferClusteringKey<M extends Model<any>> = M extends Model<infer S> ? InferFieldsByFlag<S, "clusteringKey"> : never;
/**
* 推导非分区键字段类型
*
* @typeParam S - 字段配置记录类型
*
* @example
* ```typescript
* type UserFields = {
* id: { type: "uuid"; flags: { partitionKey: true } };
* name: { type: "text" };
* age: { type: "int" };
* };
*
* type NonPartitionFields = InferNonPartitionFields<UserFields>;
* // { name: string; age: number }
* ```
*/
type InferNonPartitionFields<S extends Record<string, FieldConfig<keyof TypeMap>>> = {
[K in keyof S as S[K]["flags"]["partitionKey"] extends true ? never : K]: InferType<S[K]["type"]>;
};
/**
* 推导静态字段类型
*
* @typeParam M - 继承自 Model 的类型
*
* @example
* ```typescript
* class User extends Model<{
* id: { type: "uuid"; flags: { partitionKey: true } };
* name: { type: "text"; flags: { static: true } };
* age: { type: "int" };
* }> {}
*
* type UserStaticFields = InferStaticFields<User>; // { name: string }
* ```
*/
type InferStaticFields<M extends Model<any>> = M extends Model<infer S> ? InferFieldsByFlag<S, "static"> : never;
type Simplify<T> = T extends infer U ? {
[K in keyof U]: U[K];
} : never;
declare function createZodSchema<S extends Record<string, FieldConfig<keyof TypeMap>>>(schema: S): any;
declare function createPartitionKeyZodSchema<S extends Record<string, FieldConfig<keyof TypeMap>>>(schema: S): any;
declare function createClusteringKeyZodSchema<S extends Record<string, FieldConfig<keyof TypeMap>>>(schema: S): any;
declare class Model<S extends Record<string, FieldConfig<keyof TypeMap>>, P = InferFieldsByFlag<S, "partitionKey">, C = InferFieldsByFlag<S, "clusteringKey">, NP = InferNonPartitionFields<S>, A = InferFields<S>> {
private client;
private schema;
private tableName;
private partitionKeys;
private clusteringKeys;
private keyspace;
readonly zodSchemas: {
entity: any;
partitionKey: any;
clusteringKey: any;
};
constructor(client: Client$1, schema: S, tableName: string, keyspace: string);
getTableSchema(): string;
syncSchema(force?: boolean): Promise<void>;
private getCqlTypeFromCode;
private convertResultToType;
private buildWhereClause;
private buildQueryParams;
validate(data: any): data is Simplify<A>;
getValidationErrors(data: any): z.ZodError | null;
findAll(partitionFields: Simplify<P>, clusteringFields?: Simplify<Partial<C>>): Promise<Simplify<A>[]>;
findOne(partitionFields: Simplify<P>, clusteringFields?: Simplify<Partial<C>>): Promise<Simplify<A> | null>;
create(data: Simplify<P & Partial<NP>>, options?: QueryOptions & {
ttl?: number;
}): Promise<void>;
update(partitionFields: Simplify<P>, data: Simplify<Partial<NP>>): Promise<void>;
delete(partitionFields: Simplify<P>, clusteringFields?: Simplify<Partial<C>>): Promise<void>;
findAllOptimized(partitionFields: Simplify<P>, clusteringFields?: Simplify<Partial<C>>): Promise<Simplify<A>[]>;
}
declare const uuid: () => string;
declare class Client {
private cassandraClient;
private models;
constructor(cassandraClient: Client$1);
useKeyspace(keyspace: string, options: {
class: "SimpleStrategy" | "NetworkTopologyStrategy";
replication_factor?: number;
datacenters?: Record<string, number>;
}): Promise<void>;
createModel<S extends Record<string, FieldConfig<keyof TypeMap>>>(schema: S, tableName: string, keyspace: string): Model<S>;
syncSchema(force?: boolean): Promise<void>;
getRawClient(): Client$1;
close(): Promise<void>;
}
export { Client, Field, FieldBuilder, type FieldConfig, type Infer, type InferClusteringKey, type InferFields, type InferFieldsByFlag, type InferNonPartitionFields, type InferPartitionKey, type InferStaticFields, type InferType, Model, type Simplify, type TypeMap, createClusteringKeyZodSchema, createPartitionKeyZodSchema, createZodSchema, uuid };