imean-cassandra-orm 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mod.cjs +115 -1
- package/dist/mod.d.cts +23 -2
- package/dist/mod.d.ts +23 -2
- package/dist/mod.js +113 -2
- package/package.json +2 -1
package/dist/mod.cjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var cassandraDriver = require('cassandra-driver');
|
|
6
|
+
var zod = require('zod');
|
|
6
7
|
|
|
7
8
|
var __defProp = Object.defineProperty;
|
|
8
9
|
var __export = (target, all) => {
|
|
@@ -17,8 +18,65 @@ __export(src_exports, {
|
|
|
17
18
|
Field: () => Field,
|
|
18
19
|
FieldBuilder: () => FieldBuilder,
|
|
19
20
|
Model: () => Model,
|
|
21
|
+
createClusteringKeyZodSchema: () => createClusteringKeyZodSchema,
|
|
22
|
+
createPartitionKeyZodSchema: () => createPartitionKeyZodSchema,
|
|
23
|
+
createZodSchema: () => createZodSchema,
|
|
20
24
|
uuid: () => uuid
|
|
21
25
|
});
|
|
26
|
+
function createZodSchemaForFields(schema, filter, makeOptional = false) {
|
|
27
|
+
const zodSchema = {};
|
|
28
|
+
Object.entries(schema).forEach(([key, config]) => {
|
|
29
|
+
if (filter(config)) {
|
|
30
|
+
let baseSchema;
|
|
31
|
+
switch (config.type) {
|
|
32
|
+
case "text":
|
|
33
|
+
baseSchema = zod.z.string();
|
|
34
|
+
break;
|
|
35
|
+
case "int":
|
|
36
|
+
case "bigint":
|
|
37
|
+
case "varint":
|
|
38
|
+
baseSchema = zod.z.number().int();
|
|
39
|
+
break;
|
|
40
|
+
case "float":
|
|
41
|
+
case "double":
|
|
42
|
+
baseSchema = zod.z.number();
|
|
43
|
+
break;
|
|
44
|
+
case "boolean":
|
|
45
|
+
baseSchema = zod.z.boolean();
|
|
46
|
+
break;
|
|
47
|
+
case "timestamp":
|
|
48
|
+
baseSchema = zod.z.date();
|
|
49
|
+
break;
|
|
50
|
+
case "uuid":
|
|
51
|
+
baseSchema = zod.z.union([zod.z.string(), zod.z.instanceof(cassandraDriver.types.Uuid)]).transform(
|
|
52
|
+
(val) => val instanceof cassandraDriver.types.Uuid ? val.toString() : val
|
|
53
|
+
);
|
|
54
|
+
break;
|
|
55
|
+
default:
|
|
56
|
+
baseSchema = zod.z.any();
|
|
57
|
+
}
|
|
58
|
+
zodSchema[key] = makeOptional && config.flags.optional ? baseSchema.optional() : baseSchema;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return zod.z.object(zodSchema);
|
|
62
|
+
}
|
|
63
|
+
function createZodSchema(schema) {
|
|
64
|
+
return createZodSchemaForFields(schema, () => true);
|
|
65
|
+
}
|
|
66
|
+
function createPartitionKeyZodSchema(schema) {
|
|
67
|
+
return createZodSchemaForFields(
|
|
68
|
+
schema,
|
|
69
|
+
(config) => config.flags.partitionKey
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
function createClusteringKeyZodSchema(schema) {
|
|
73
|
+
return createZodSchemaForFields(
|
|
74
|
+
schema,
|
|
75
|
+
(config) => !!config.flags.clusteringKey
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/model.ts
|
|
22
80
|
var Model = class {
|
|
23
81
|
constructor(client, schema, tableName) {
|
|
24
82
|
this.client = client;
|
|
@@ -26,10 +84,16 @@ var Model = class {
|
|
|
26
84
|
this.tableName = tableName;
|
|
27
85
|
this.partitionKeys = Object.entries(schema).filter(([_, config]) => config.flags.partitionKey).map(([key]) => key);
|
|
28
86
|
this.clusteringKeys = Object.entries(schema).filter(([_, config]) => config.flags.clusteringKey).map(([key]) => key);
|
|
87
|
+
this.zodSchemas = {
|
|
88
|
+
entity: createZodSchema(schema),
|
|
89
|
+
partitionKey: createPartitionKeyZodSchema(schema),
|
|
90
|
+
clusteringKey: createClusteringKeyZodSchema(schema)
|
|
91
|
+
};
|
|
29
92
|
}
|
|
30
93
|
tableName;
|
|
31
94
|
partitionKeys;
|
|
32
95
|
clusteringKeys;
|
|
96
|
+
zodSchemas;
|
|
33
97
|
// 获取表结构 CQL
|
|
34
98
|
getTableSchema() {
|
|
35
99
|
const columns = Object.entries(this.schema).map(([name, config]) => {
|
|
@@ -125,13 +189,31 @@ var Model = class {
|
|
|
125
189
|
}
|
|
126
190
|
return params;
|
|
127
191
|
}
|
|
192
|
+
// 验证数据
|
|
193
|
+
validate(data) {
|
|
194
|
+
try {
|
|
195
|
+
this.zodSchemas.entity.parse(data);
|
|
196
|
+
return true;
|
|
197
|
+
} catch (error) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// 获取验证错误
|
|
202
|
+
getValidationErrors(data) {
|
|
203
|
+
try {
|
|
204
|
+
this.zodSchemas.entity.parse(data);
|
|
205
|
+
return null;
|
|
206
|
+
} catch (error) {
|
|
207
|
+
return error;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
128
210
|
// 将查询结果转换为我们的类型
|
|
129
211
|
convertResultToType(result) {
|
|
130
212
|
const converted = {};
|
|
131
213
|
Object.keys(this.schema).forEach((key) => {
|
|
132
214
|
converted[key] = result[key];
|
|
133
215
|
});
|
|
134
|
-
return converted;
|
|
216
|
+
return this.zodSchemas.entity.parse(converted);
|
|
135
217
|
}
|
|
136
218
|
// 查询方法
|
|
137
219
|
async findAll(partitionFields, clusteringFields) {
|
|
@@ -183,6 +265,35 @@ var Model = class {
|
|
|
183
265
|
const query = `DELETE FROM ${this.tableName} ${whereClause}`;
|
|
184
266
|
await this.client.execute(query, params, { prepare: true });
|
|
185
267
|
}
|
|
268
|
+
// 优化后的批量查询方法(支持静态列)
|
|
269
|
+
async findAllOptimized(partitionFields, clusteringFields) {
|
|
270
|
+
const staticColumns = Object.entries(this.schema).filter(([_, config]) => config.flags.static).map(([name]) => name);
|
|
271
|
+
const nonStaticColumns = Object.entries(this.schema).filter(([_, config]) => !config.flags.static).map(([name]) => name);
|
|
272
|
+
const whereClause = this.buildWhereClause(
|
|
273
|
+
partitionFields,
|
|
274
|
+
clusteringFields
|
|
275
|
+
);
|
|
276
|
+
const params = this.buildQueryParams(partitionFields, clusteringFields);
|
|
277
|
+
const [staticResult, nonStaticResult] = await Promise.all([
|
|
278
|
+
// 查询静态列
|
|
279
|
+
staticColumns.length > 0 ? this.client.execute(
|
|
280
|
+
`SELECT ${staticColumns.join(", ")} FROM ${this.tableName} ${whereClause} LIMIT 1`,
|
|
281
|
+
params,
|
|
282
|
+
{ prepare: true }
|
|
283
|
+
) : Promise.resolve({ rows: [] }),
|
|
284
|
+
// 查询非静态列
|
|
285
|
+
this.client.execute(
|
|
286
|
+
`SELECT ${nonStaticColumns.join(", ")} FROM ${this.tableName} ${whereClause}`,
|
|
287
|
+
params,
|
|
288
|
+
{ prepare: true }
|
|
289
|
+
)
|
|
290
|
+
]);
|
|
291
|
+
const staticData = staticResult.rows[0] || {};
|
|
292
|
+
return nonStaticResult.rows.map((row) => ({
|
|
293
|
+
...staticData,
|
|
294
|
+
...row
|
|
295
|
+
}));
|
|
296
|
+
}
|
|
186
297
|
};
|
|
187
298
|
var uuid = () => cassandraDriver.types.Uuid.random().toString();
|
|
188
299
|
|
|
@@ -313,5 +424,8 @@ exports.Client = Client2;
|
|
|
313
424
|
exports.Field = Field;
|
|
314
425
|
exports.FieldBuilder = FieldBuilder;
|
|
315
426
|
exports.Model = Model;
|
|
427
|
+
exports.createClusteringKeyZodSchema = createClusteringKeyZodSchema;
|
|
428
|
+
exports.createPartitionKeyZodSchema = createPartitionKeyZodSchema;
|
|
429
|
+
exports.createZodSchema = createZodSchema;
|
|
316
430
|
exports.default = mod_default;
|
|
317
431
|
exports.uuid = uuid;
|
package/dist/mod.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Client as Client$1 } from 'cassandra-driver';
|
|
2
|
+
import { z } from 'zod';
|
|
2
3
|
|
|
3
4
|
type TypeMap = {
|
|
4
5
|
text: string;
|
|
@@ -245,6 +246,15 @@ type InferStaticFields<M extends Model<any>> = M extends Model<infer S> ? InferF
|
|
|
245
246
|
type Simplify<T> = T extends infer U ? {
|
|
246
247
|
[K in keyof U]: U[K];
|
|
247
248
|
} : never;
|
|
249
|
+
declare function createZodSchema<S extends Record<string, FieldConfig<keyof TypeMap>>>(schema: S): z.ZodType<any>;
|
|
250
|
+
declare function createPartitionKeyZodSchema<S extends Record<string, FieldConfig<keyof TypeMap>>>(schema: S): z.ZodType<{
|
|
251
|
+
[K in keyof S as S[K]["flags"]["partitionKey"] extends true ? K : never]: TypeMap[S[K]["type"]];
|
|
252
|
+
}>;
|
|
253
|
+
declare function createClusteringKeyZodSchema<S extends Record<string, FieldConfig<keyof TypeMap>>>(schema: S): z.ZodType<{
|
|
254
|
+
[K in keyof S as S[K]["flags"]["clusteringKey"] extends true | {
|
|
255
|
+
order: "ASC" | "DESC";
|
|
256
|
+
} ? K : never]: TypeMap[S[K]["type"]];
|
|
257
|
+
}>;
|
|
248
258
|
|
|
249
259
|
declare class Model<S extends Record<string, FieldConfig<keyof TypeMap>>, P = InferFieldsByFlag<S, "partitionKey">, C = InferFieldsByFlag<S, "clusteringKey">, NP = InferNonPartitionFields<S>, A = InferFields<S>> {
|
|
250
260
|
private client;
|
|
@@ -252,17 +262,25 @@ declare class Model<S extends Record<string, FieldConfig<keyof TypeMap>>, P = In
|
|
|
252
262
|
private tableName;
|
|
253
263
|
private partitionKeys;
|
|
254
264
|
private clusteringKeys;
|
|
265
|
+
readonly zodSchemas: {
|
|
266
|
+
entity: z.ZodType<any>;
|
|
267
|
+
partitionKey: z.ZodType<any>;
|
|
268
|
+
clusteringKey: z.ZodType<any>;
|
|
269
|
+
};
|
|
255
270
|
constructor(client: Client$1, schema: S, tableName: string);
|
|
256
271
|
getTableSchema(): string;
|
|
257
272
|
syncSchema(): Promise<void>;
|
|
258
273
|
private buildWhereClause;
|
|
259
274
|
private buildQueryParams;
|
|
275
|
+
validate(data: any): data is Simplify<A>;
|
|
276
|
+
getValidationErrors(data: any): z.ZodError | null;
|
|
260
277
|
private convertResultToType;
|
|
261
278
|
findAll(partitionFields: Simplify<P>, clusteringFields?: Simplify<Partial<C>>): Promise<Simplify<A>[]>;
|
|
262
279
|
findOne(partitionFields: Simplify<P>, clusteringFields?: Simplify<Partial<C>>): Promise<Simplify<A> | null>;
|
|
263
280
|
create(data: Simplify<P & Partial<NP>>): Promise<void>;
|
|
264
281
|
update(partitionFields: Simplify<P>, data: Simplify<Partial<NP>>): Promise<void>;
|
|
265
282
|
delete(partitionFields: Simplify<P>, clusteringFields?: Simplify<Partial<C>>): Promise<void>;
|
|
283
|
+
findAllOptimized(partitionFields: Simplify<P>, clusteringFields?: Simplify<Partial<C>>): Promise<Simplify<A>[]>;
|
|
266
284
|
}
|
|
267
285
|
declare const uuid: () => string;
|
|
268
286
|
|
|
@@ -311,9 +329,12 @@ type orm_Model<S extends Record<string, FieldConfig<keyof TypeMap>>, P = InferFi
|
|
|
311
329
|
declare const orm_Model: typeof Model;
|
|
312
330
|
type orm_Simplify<T> = Simplify<T>;
|
|
313
331
|
type orm_TypeMap = TypeMap;
|
|
332
|
+
declare const orm_createClusteringKeyZodSchema: typeof createClusteringKeyZodSchema;
|
|
333
|
+
declare const orm_createPartitionKeyZodSchema: typeof createPartitionKeyZodSchema;
|
|
334
|
+
declare const orm_createZodSchema: typeof createZodSchema;
|
|
314
335
|
declare const orm_uuid: typeof uuid;
|
|
315
336
|
declare namespace orm {
|
|
316
|
-
export { orm_Client as Client, orm_Field as Field, orm_FieldBuilder as FieldBuilder, type orm_FieldConfig as FieldConfig, type orm_Infer as Infer, type orm_InferClusteringKey as InferClusteringKey, type orm_InferFields as InferFields, type orm_InferFieldsByFlag as InferFieldsByFlag, type orm_InferNonPartitionFields as InferNonPartitionFields, type orm_InferPartitionKey as InferPartitionKey, type orm_InferStaticFields as InferStaticFields, type orm_InferType as InferType, orm_Model as Model, type orm_Simplify as Simplify, type orm_TypeMap as TypeMap, orm_uuid as uuid };
|
|
337
|
+
export { orm_Client as Client, orm_Field as Field, orm_FieldBuilder as FieldBuilder, type orm_FieldConfig as FieldConfig, type orm_Infer as Infer, type orm_InferClusteringKey as InferClusteringKey, type orm_InferFields as InferFields, type orm_InferFieldsByFlag as InferFieldsByFlag, type orm_InferNonPartitionFields as InferNonPartitionFields, type orm_InferPartitionKey as InferPartitionKey, type orm_InferStaticFields as InferStaticFields, type orm_InferType as InferType, orm_Model as Model, type orm_Simplify as Simplify, type orm_TypeMap as TypeMap, orm_createClusteringKeyZodSchema as createClusteringKeyZodSchema, orm_createPartitionKeyZodSchema as createPartitionKeyZodSchema, orm_createZodSchema as createZodSchema, orm_uuid as uuid };
|
|
317
338
|
}
|
|
318
339
|
|
|
319
|
-
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, orm as default, uuid };
|
|
340
|
+
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, orm as default, uuid };
|
package/dist/mod.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Client as Client$1 } from 'cassandra-driver';
|
|
2
|
+
import { z } from 'zod';
|
|
2
3
|
|
|
3
4
|
type TypeMap = {
|
|
4
5
|
text: string;
|
|
@@ -245,6 +246,15 @@ type InferStaticFields<M extends Model<any>> = M extends Model<infer S> ? InferF
|
|
|
245
246
|
type Simplify<T> = T extends infer U ? {
|
|
246
247
|
[K in keyof U]: U[K];
|
|
247
248
|
} : never;
|
|
249
|
+
declare function createZodSchema<S extends Record<string, FieldConfig<keyof TypeMap>>>(schema: S): z.ZodType<any>;
|
|
250
|
+
declare function createPartitionKeyZodSchema<S extends Record<string, FieldConfig<keyof TypeMap>>>(schema: S): z.ZodType<{
|
|
251
|
+
[K in keyof S as S[K]["flags"]["partitionKey"] extends true ? K : never]: TypeMap[S[K]["type"]];
|
|
252
|
+
}>;
|
|
253
|
+
declare function createClusteringKeyZodSchema<S extends Record<string, FieldConfig<keyof TypeMap>>>(schema: S): z.ZodType<{
|
|
254
|
+
[K in keyof S as S[K]["flags"]["clusteringKey"] extends true | {
|
|
255
|
+
order: "ASC" | "DESC";
|
|
256
|
+
} ? K : never]: TypeMap[S[K]["type"]];
|
|
257
|
+
}>;
|
|
248
258
|
|
|
249
259
|
declare class Model<S extends Record<string, FieldConfig<keyof TypeMap>>, P = InferFieldsByFlag<S, "partitionKey">, C = InferFieldsByFlag<S, "clusteringKey">, NP = InferNonPartitionFields<S>, A = InferFields<S>> {
|
|
250
260
|
private client;
|
|
@@ -252,17 +262,25 @@ declare class Model<S extends Record<string, FieldConfig<keyof TypeMap>>, P = In
|
|
|
252
262
|
private tableName;
|
|
253
263
|
private partitionKeys;
|
|
254
264
|
private clusteringKeys;
|
|
265
|
+
readonly zodSchemas: {
|
|
266
|
+
entity: z.ZodType<any>;
|
|
267
|
+
partitionKey: z.ZodType<any>;
|
|
268
|
+
clusteringKey: z.ZodType<any>;
|
|
269
|
+
};
|
|
255
270
|
constructor(client: Client$1, schema: S, tableName: string);
|
|
256
271
|
getTableSchema(): string;
|
|
257
272
|
syncSchema(): Promise<void>;
|
|
258
273
|
private buildWhereClause;
|
|
259
274
|
private buildQueryParams;
|
|
275
|
+
validate(data: any): data is Simplify<A>;
|
|
276
|
+
getValidationErrors(data: any): z.ZodError | null;
|
|
260
277
|
private convertResultToType;
|
|
261
278
|
findAll(partitionFields: Simplify<P>, clusteringFields?: Simplify<Partial<C>>): Promise<Simplify<A>[]>;
|
|
262
279
|
findOne(partitionFields: Simplify<P>, clusteringFields?: Simplify<Partial<C>>): Promise<Simplify<A> | null>;
|
|
263
280
|
create(data: Simplify<P & Partial<NP>>): Promise<void>;
|
|
264
281
|
update(partitionFields: Simplify<P>, data: Simplify<Partial<NP>>): Promise<void>;
|
|
265
282
|
delete(partitionFields: Simplify<P>, clusteringFields?: Simplify<Partial<C>>): Promise<void>;
|
|
283
|
+
findAllOptimized(partitionFields: Simplify<P>, clusteringFields?: Simplify<Partial<C>>): Promise<Simplify<A>[]>;
|
|
266
284
|
}
|
|
267
285
|
declare const uuid: () => string;
|
|
268
286
|
|
|
@@ -311,9 +329,12 @@ type orm_Model<S extends Record<string, FieldConfig<keyof TypeMap>>, P = InferFi
|
|
|
311
329
|
declare const orm_Model: typeof Model;
|
|
312
330
|
type orm_Simplify<T> = Simplify<T>;
|
|
313
331
|
type orm_TypeMap = TypeMap;
|
|
332
|
+
declare const orm_createClusteringKeyZodSchema: typeof createClusteringKeyZodSchema;
|
|
333
|
+
declare const orm_createPartitionKeyZodSchema: typeof createPartitionKeyZodSchema;
|
|
334
|
+
declare const orm_createZodSchema: typeof createZodSchema;
|
|
314
335
|
declare const orm_uuid: typeof uuid;
|
|
315
336
|
declare namespace orm {
|
|
316
|
-
export { orm_Client as Client, orm_Field as Field, orm_FieldBuilder as FieldBuilder, type orm_FieldConfig as FieldConfig, type orm_Infer as Infer, type orm_InferClusteringKey as InferClusteringKey, type orm_InferFields as InferFields, type orm_InferFieldsByFlag as InferFieldsByFlag, type orm_InferNonPartitionFields as InferNonPartitionFields, type orm_InferPartitionKey as InferPartitionKey, type orm_InferStaticFields as InferStaticFields, type orm_InferType as InferType, orm_Model as Model, type orm_Simplify as Simplify, type orm_TypeMap as TypeMap, orm_uuid as uuid };
|
|
337
|
+
export { orm_Client as Client, orm_Field as Field, orm_FieldBuilder as FieldBuilder, type orm_FieldConfig as FieldConfig, type orm_Infer as Infer, type orm_InferClusteringKey as InferClusteringKey, type orm_InferFields as InferFields, type orm_InferFieldsByFlag as InferFieldsByFlag, type orm_InferNonPartitionFields as InferNonPartitionFields, type orm_InferPartitionKey as InferPartitionKey, type orm_InferStaticFields as InferStaticFields, type orm_InferType as InferType, orm_Model as Model, type orm_Simplify as Simplify, type orm_TypeMap as TypeMap, orm_createClusteringKeyZodSchema as createClusteringKeyZodSchema, orm_createPartitionKeyZodSchema as createPartitionKeyZodSchema, orm_createZodSchema as createZodSchema, orm_uuid as uuid };
|
|
317
338
|
}
|
|
318
339
|
|
|
319
|
-
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, orm as default, uuid };
|
|
340
|
+
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, orm as default, uuid };
|
package/dist/mod.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { types } from 'cassandra-driver';
|
|
2
|
+
import { z } from 'zod';
|
|
2
3
|
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __export = (target, all) => {
|
|
@@ -13,8 +14,65 @@ __export(src_exports, {
|
|
|
13
14
|
Field: () => Field,
|
|
14
15
|
FieldBuilder: () => FieldBuilder,
|
|
15
16
|
Model: () => Model,
|
|
17
|
+
createClusteringKeyZodSchema: () => createClusteringKeyZodSchema,
|
|
18
|
+
createPartitionKeyZodSchema: () => createPartitionKeyZodSchema,
|
|
19
|
+
createZodSchema: () => createZodSchema,
|
|
16
20
|
uuid: () => uuid
|
|
17
21
|
});
|
|
22
|
+
function createZodSchemaForFields(schema, filter, makeOptional = false) {
|
|
23
|
+
const zodSchema = {};
|
|
24
|
+
Object.entries(schema).forEach(([key, config]) => {
|
|
25
|
+
if (filter(config)) {
|
|
26
|
+
let baseSchema;
|
|
27
|
+
switch (config.type) {
|
|
28
|
+
case "text":
|
|
29
|
+
baseSchema = z.string();
|
|
30
|
+
break;
|
|
31
|
+
case "int":
|
|
32
|
+
case "bigint":
|
|
33
|
+
case "varint":
|
|
34
|
+
baseSchema = z.number().int();
|
|
35
|
+
break;
|
|
36
|
+
case "float":
|
|
37
|
+
case "double":
|
|
38
|
+
baseSchema = z.number();
|
|
39
|
+
break;
|
|
40
|
+
case "boolean":
|
|
41
|
+
baseSchema = z.boolean();
|
|
42
|
+
break;
|
|
43
|
+
case "timestamp":
|
|
44
|
+
baseSchema = z.date();
|
|
45
|
+
break;
|
|
46
|
+
case "uuid":
|
|
47
|
+
baseSchema = z.union([z.string(), z.instanceof(types.Uuid)]).transform(
|
|
48
|
+
(val) => val instanceof types.Uuid ? val.toString() : val
|
|
49
|
+
);
|
|
50
|
+
break;
|
|
51
|
+
default:
|
|
52
|
+
baseSchema = z.any();
|
|
53
|
+
}
|
|
54
|
+
zodSchema[key] = makeOptional && config.flags.optional ? baseSchema.optional() : baseSchema;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return z.object(zodSchema);
|
|
58
|
+
}
|
|
59
|
+
function createZodSchema(schema) {
|
|
60
|
+
return createZodSchemaForFields(schema, () => true);
|
|
61
|
+
}
|
|
62
|
+
function createPartitionKeyZodSchema(schema) {
|
|
63
|
+
return createZodSchemaForFields(
|
|
64
|
+
schema,
|
|
65
|
+
(config) => config.flags.partitionKey
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
function createClusteringKeyZodSchema(schema) {
|
|
69
|
+
return createZodSchemaForFields(
|
|
70
|
+
schema,
|
|
71
|
+
(config) => !!config.flags.clusteringKey
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/model.ts
|
|
18
76
|
var Model = class {
|
|
19
77
|
constructor(client, schema, tableName) {
|
|
20
78
|
this.client = client;
|
|
@@ -22,10 +80,16 @@ var Model = class {
|
|
|
22
80
|
this.tableName = tableName;
|
|
23
81
|
this.partitionKeys = Object.entries(schema).filter(([_, config]) => config.flags.partitionKey).map(([key]) => key);
|
|
24
82
|
this.clusteringKeys = Object.entries(schema).filter(([_, config]) => config.flags.clusteringKey).map(([key]) => key);
|
|
83
|
+
this.zodSchemas = {
|
|
84
|
+
entity: createZodSchema(schema),
|
|
85
|
+
partitionKey: createPartitionKeyZodSchema(schema),
|
|
86
|
+
clusteringKey: createClusteringKeyZodSchema(schema)
|
|
87
|
+
};
|
|
25
88
|
}
|
|
26
89
|
tableName;
|
|
27
90
|
partitionKeys;
|
|
28
91
|
clusteringKeys;
|
|
92
|
+
zodSchemas;
|
|
29
93
|
// 获取表结构 CQL
|
|
30
94
|
getTableSchema() {
|
|
31
95
|
const columns = Object.entries(this.schema).map(([name, config]) => {
|
|
@@ -121,13 +185,31 @@ var Model = class {
|
|
|
121
185
|
}
|
|
122
186
|
return params;
|
|
123
187
|
}
|
|
188
|
+
// 验证数据
|
|
189
|
+
validate(data) {
|
|
190
|
+
try {
|
|
191
|
+
this.zodSchemas.entity.parse(data);
|
|
192
|
+
return true;
|
|
193
|
+
} catch (error) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// 获取验证错误
|
|
198
|
+
getValidationErrors(data) {
|
|
199
|
+
try {
|
|
200
|
+
this.zodSchemas.entity.parse(data);
|
|
201
|
+
return null;
|
|
202
|
+
} catch (error) {
|
|
203
|
+
return error;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
124
206
|
// 将查询结果转换为我们的类型
|
|
125
207
|
convertResultToType(result) {
|
|
126
208
|
const converted = {};
|
|
127
209
|
Object.keys(this.schema).forEach((key) => {
|
|
128
210
|
converted[key] = result[key];
|
|
129
211
|
});
|
|
130
|
-
return converted;
|
|
212
|
+
return this.zodSchemas.entity.parse(converted);
|
|
131
213
|
}
|
|
132
214
|
// 查询方法
|
|
133
215
|
async findAll(partitionFields, clusteringFields) {
|
|
@@ -179,6 +261,35 @@ var Model = class {
|
|
|
179
261
|
const query = `DELETE FROM ${this.tableName} ${whereClause}`;
|
|
180
262
|
await this.client.execute(query, params, { prepare: true });
|
|
181
263
|
}
|
|
264
|
+
// 优化后的批量查询方法(支持静态列)
|
|
265
|
+
async findAllOptimized(partitionFields, clusteringFields) {
|
|
266
|
+
const staticColumns = Object.entries(this.schema).filter(([_, config]) => config.flags.static).map(([name]) => name);
|
|
267
|
+
const nonStaticColumns = Object.entries(this.schema).filter(([_, config]) => !config.flags.static).map(([name]) => name);
|
|
268
|
+
const whereClause = this.buildWhereClause(
|
|
269
|
+
partitionFields,
|
|
270
|
+
clusteringFields
|
|
271
|
+
);
|
|
272
|
+
const params = this.buildQueryParams(partitionFields, clusteringFields);
|
|
273
|
+
const [staticResult, nonStaticResult] = await Promise.all([
|
|
274
|
+
// 查询静态列
|
|
275
|
+
staticColumns.length > 0 ? this.client.execute(
|
|
276
|
+
`SELECT ${staticColumns.join(", ")} FROM ${this.tableName} ${whereClause} LIMIT 1`,
|
|
277
|
+
params,
|
|
278
|
+
{ prepare: true }
|
|
279
|
+
) : Promise.resolve({ rows: [] }),
|
|
280
|
+
// 查询非静态列
|
|
281
|
+
this.client.execute(
|
|
282
|
+
`SELECT ${nonStaticColumns.join(", ")} FROM ${this.tableName} ${whereClause}`,
|
|
283
|
+
params,
|
|
284
|
+
{ prepare: true }
|
|
285
|
+
)
|
|
286
|
+
]);
|
|
287
|
+
const staticData = staticResult.rows[0] || {};
|
|
288
|
+
return nonStaticResult.rows.map((row) => ({
|
|
289
|
+
...staticData,
|
|
290
|
+
...row
|
|
291
|
+
}));
|
|
292
|
+
}
|
|
182
293
|
};
|
|
183
294
|
var uuid = () => types.Uuid.random().toString();
|
|
184
295
|
|
|
@@ -305,4 +416,4 @@ var Field = {
|
|
|
305
416
|
// mod.ts
|
|
306
417
|
var mod_default = src_exports;
|
|
307
418
|
|
|
308
|
-
export { Client2 as Client, Field, FieldBuilder, Model, mod_default as default, uuid };
|
|
419
|
+
export { Client2 as Client, Field, FieldBuilder, Model, createClusteringKeyZodSchema, createPartitionKeyZodSchema, createZodSchema, mod_default as default, uuid };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "imean-cassandra-orm",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "cassandra orm",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cassandra",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
+
"zod": "^3.x",
|
|
37
38
|
"@opentelemetry/api": "^1.x",
|
|
38
39
|
"cassandra-driver": "^4.x"
|
|
39
40
|
},
|