imean-cassandra-orm 2.1.1 → 2.2.0
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 +108 -2
- package/dist/mod.d.cts +41 -31
- package/dist/mod.d.ts +41 -31
- package/dist/mod.js +108 -3
- package/package.json +1 -1
package/dist/mod.cjs
CHANGED
|
@@ -5,6 +5,9 @@ var zod = require('zod');
|
|
|
5
5
|
|
|
6
6
|
// src/client.ts
|
|
7
7
|
function convertZodToCassandraType(schema) {
|
|
8
|
+
if (schema._cassandraType) {
|
|
9
|
+
return schema._cassandraType;
|
|
10
|
+
}
|
|
8
11
|
let baseSchema = schema instanceof zod.z.ZodEffects ? schema._def.schema : schema;
|
|
9
12
|
if (schema instanceof zod.z.ZodOptional) {
|
|
10
13
|
return convertZodToCassandraType(schema.unwrap());
|
|
@@ -12,6 +15,9 @@ function convertZodToCassandraType(schema) {
|
|
|
12
15
|
while (baseSchema instanceof zod.z.ZodEffects) {
|
|
13
16
|
baseSchema = baseSchema._def.schema;
|
|
14
17
|
}
|
|
18
|
+
if (baseSchema._cassandraType) {
|
|
19
|
+
return baseSchema._cassandraType;
|
|
20
|
+
}
|
|
15
21
|
if (baseSchema instanceof zod.z.ZodString) {
|
|
16
22
|
if (baseSchema._def.checks.some((c) => c.kind === "uuid")) {
|
|
17
23
|
return "uuid";
|
|
@@ -146,9 +152,24 @@ function dropColumns(schema, fields) {
|
|
|
146
152
|
return `ALTER TABLE ${schema.keyspace}.${schema.tableName} DROP (${fields.join(", ")});`;
|
|
147
153
|
}
|
|
148
154
|
function convertToFieldConfig(zodType, flags = {}) {
|
|
155
|
+
if (zodType._cassandraType) {
|
|
156
|
+
return {
|
|
157
|
+
type: zodType._cassandraType,
|
|
158
|
+
flags: {
|
|
159
|
+
partitionKey: !!flags.partitionKey,
|
|
160
|
+
clusteringKey: flags.clusteringKey || false,
|
|
161
|
+
optional: !!flags.optional,
|
|
162
|
+
static: !!flags.static
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
149
166
|
let type = "text";
|
|
150
167
|
if (zodType._def.typeName === "ZodString") {
|
|
151
|
-
|
|
168
|
+
if (zodType._def.checks && zodType._def.checks.some((c) => c.kind === "uuid")) {
|
|
169
|
+
type = "uuid";
|
|
170
|
+
} else {
|
|
171
|
+
type = "text";
|
|
172
|
+
}
|
|
152
173
|
} else if (zodType._def.typeName === "ZodNumber") {
|
|
153
174
|
type = "double";
|
|
154
175
|
} else if (zodType._def.typeName === "ZodBoolean") {
|
|
@@ -371,7 +392,13 @@ function convertValueFromCassandra(value, config) {
|
|
|
371
392
|
case "timestamp":
|
|
372
393
|
return value instanceof Date ? value : new Date(value);
|
|
373
394
|
case "uuid":
|
|
374
|
-
|
|
395
|
+
if (typeof value === "string") {
|
|
396
|
+
return value;
|
|
397
|
+
}
|
|
398
|
+
if (value && typeof value.toString === "function") {
|
|
399
|
+
return value.toString();
|
|
400
|
+
}
|
|
401
|
+
return String(value);
|
|
375
402
|
case "blob":
|
|
376
403
|
return value instanceof Uint8Array ? value : new Uint8Array(value);
|
|
377
404
|
case "text":
|
|
@@ -816,10 +843,89 @@ var Client = class {
|
|
|
816
843
|
return this.cassandraClient;
|
|
817
844
|
}
|
|
818
845
|
};
|
|
846
|
+
function createCassandraType(schema, cassandraType) {
|
|
847
|
+
const markedSchema = schema;
|
|
848
|
+
markedSchema._cassandraType = cassandraType;
|
|
849
|
+
return markedSchema;
|
|
850
|
+
}
|
|
851
|
+
var Types = {
|
|
852
|
+
boolean: zod.z.boolean,
|
|
853
|
+
text: zod.z.string,
|
|
854
|
+
double: zod.z.number,
|
|
855
|
+
enum: zod.z.enum,
|
|
856
|
+
object: zod.z.object,
|
|
857
|
+
float: () => createCassandraType(
|
|
858
|
+
zod.z.number().transform((val) => parseFloat(val.toString())),
|
|
859
|
+
"float"
|
|
860
|
+
),
|
|
861
|
+
int: () => createCassandraType(zod.z.number().int(), "int"),
|
|
862
|
+
bigint: () => createCassandraType(
|
|
863
|
+
zod.z.number().int().transform((val) => BigInt(val)),
|
|
864
|
+
"bigint"
|
|
865
|
+
),
|
|
866
|
+
smallint: () => createCassandraType(
|
|
867
|
+
zod.z.number().int().refine((val) => val >= -32768 && val <= 32767, {
|
|
868
|
+
message: "Value must be between -32768 and 32767"
|
|
869
|
+
}),
|
|
870
|
+
"smallint"
|
|
871
|
+
),
|
|
872
|
+
tinyint: () => createCassandraType(
|
|
873
|
+
zod.z.number().int().refine((val) => val >= -128 && val <= 127, {
|
|
874
|
+
message: "Value must be between -128 and 127"
|
|
875
|
+
}),
|
|
876
|
+
"tinyint"
|
|
877
|
+
),
|
|
878
|
+
varint: () => createCassandraType(
|
|
879
|
+
zod.z.number().int().transform((val) => BigInt(val)),
|
|
880
|
+
"varint"
|
|
881
|
+
),
|
|
882
|
+
// 字符串类型
|
|
883
|
+
varchar: (maxLength) => createCassandraType(zod.z.string().max(maxLength || 65535), "varchar"),
|
|
884
|
+
ascii: () => createCassandraType(
|
|
885
|
+
zod.z.string().refine((val) => /^[\x00-\x7F]*$/.test(val), {
|
|
886
|
+
message: "String must contain only ASCII characters"
|
|
887
|
+
}),
|
|
888
|
+
"ascii"
|
|
889
|
+
),
|
|
890
|
+
// 时间类型
|
|
891
|
+
timestamp: () => createCassandraType(zod.z.date(), "timestamp"),
|
|
892
|
+
time: () => createCassandraType(zod.z.date(), "time"),
|
|
893
|
+
date: () => createCassandraType(zod.z.date(), "date"),
|
|
894
|
+
// UUID 类型
|
|
895
|
+
uuid: () => createCassandraType(zod.z.string().uuid(), "uuid"),
|
|
896
|
+
// 二进制类型
|
|
897
|
+
blob: () => createCassandraType(zod.z.instanceof(Uint8Array), "blob"),
|
|
898
|
+
// 集合类型
|
|
899
|
+
list: (elementType) => createCassandraType(
|
|
900
|
+
zod.z.array(elementType),
|
|
901
|
+
`list<${getCassandraTypeFromZod(elementType)}>`
|
|
902
|
+
),
|
|
903
|
+
set: (elementType) => createCassandraType(
|
|
904
|
+
zod.z.set(elementType),
|
|
905
|
+
`set<${getCassandraTypeFromZod(elementType)}>`
|
|
906
|
+
),
|
|
907
|
+
map: (keyType, valueType) => createCassandraType(
|
|
908
|
+
zod.z.record(keyType, valueType),
|
|
909
|
+
`map<${getCassandraTypeFromZod(keyType)}, ${getCassandraTypeFromZod(
|
|
910
|
+
valueType
|
|
911
|
+
)}>`
|
|
912
|
+
)
|
|
913
|
+
};
|
|
914
|
+
function getCassandraTypeFromZod(schema) {
|
|
915
|
+
if (schema._cassandraType) {
|
|
916
|
+
return schema._cassandraType;
|
|
917
|
+
}
|
|
918
|
+
if (schema instanceof zod.z.ZodString) return "text";
|
|
919
|
+
if (schema instanceof zod.z.ZodNumber) return "double";
|
|
920
|
+
if (schema instanceof zod.z.ZodBoolean) return "boolean";
|
|
921
|
+
if (schema instanceof zod.z.ZodDate) return "timestamp";
|
|
922
|
+
return "text";
|
|
923
|
+
}
|
|
819
924
|
|
|
820
925
|
exports.Client = Client;
|
|
821
926
|
exports.Model = Model;
|
|
822
927
|
exports.TableSchema = TableSchema;
|
|
928
|
+
exports.Types = Types;
|
|
823
929
|
exports.createModel = createModel;
|
|
824
930
|
exports.createTableSchema = createTableSchema;
|
|
825
931
|
exports.uuid = uuid;
|
package/dist/mod.d.cts
CHANGED
|
@@ -2,34 +2,44 @@ import * as cassandra_driver from 'cassandra-driver';
|
|
|
2
2
|
import { ClientOptions, QueryOptions, Client as Client$1 } from 'cassandra-driver';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
order: "ASC" | "DESC";
|
|
26
|
-
};
|
|
27
|
-
optional: boolean;
|
|
28
|
-
static: boolean;
|
|
5
|
+
/**
|
|
6
|
+
* Cassandra 数据类型扩展
|
|
7
|
+
* 提供更精确的类型映射支持
|
|
8
|
+
*/
|
|
9
|
+
interface CassandraTypeMarker {
|
|
10
|
+
_cassandraType: string;
|
|
11
|
+
}
|
|
12
|
+
declare const Types: {
|
|
13
|
+
boolean: (params?: z.RawCreateParams & {
|
|
14
|
+
coerce?: boolean;
|
|
15
|
+
}) => z.ZodBoolean;
|
|
16
|
+
text: (params?: z.RawCreateParams & {
|
|
17
|
+
coerce?: true;
|
|
18
|
+
}) => z.ZodString;
|
|
19
|
+
double: (params?: z.RawCreateParams & {
|
|
20
|
+
coerce?: boolean;
|
|
21
|
+
}) => z.ZodNumber;
|
|
22
|
+
enum: {
|
|
23
|
+
<U extends string, T extends Readonly<[U, ...U[]]>>(values: T, params?: z.RawCreateParams): z.ZodEnum<z.Writeable<T>>;
|
|
24
|
+
<U extends string, T extends [U, ...U[]]>(values: T, params?: z.RawCreateParams): z.ZodEnum<T>;
|
|
29
25
|
};
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
object: <T extends z.ZodRawShape>(shape: T, params?: z.RawCreateParams) => z.ZodObject<T, "strip", z.ZodTypeAny, z.objectOutputType<T, z.ZodTypeAny, "strip">, z.objectInputType<T, z.ZodTypeAny, "strip">>;
|
|
27
|
+
float: () => z.ZodEffects<z.ZodNumber, number, number> & CassandraTypeMarker;
|
|
28
|
+
int: () => z.ZodNumber & CassandraTypeMarker;
|
|
29
|
+
bigint: () => z.ZodEffects<z.ZodNumber, bigint, number> & CassandraTypeMarker;
|
|
30
|
+
smallint: () => z.ZodEffects<z.ZodNumber, number, number> & CassandraTypeMarker;
|
|
31
|
+
tinyint: () => z.ZodEffects<z.ZodNumber, number, number> & CassandraTypeMarker;
|
|
32
|
+
varint: () => z.ZodEffects<z.ZodNumber, bigint, number> & CassandraTypeMarker;
|
|
33
|
+
varchar: (maxLength?: number) => z.ZodString & CassandraTypeMarker;
|
|
34
|
+
ascii: () => z.ZodEffects<z.ZodString, string, string> & CassandraTypeMarker;
|
|
35
|
+
timestamp: () => z.ZodDate & CassandraTypeMarker;
|
|
36
|
+
time: () => z.ZodDate & CassandraTypeMarker;
|
|
37
|
+
date: () => z.ZodDate & CassandraTypeMarker;
|
|
38
|
+
uuid: () => z.ZodString & CassandraTypeMarker;
|
|
39
|
+
blob: () => z.ZodType<Uint8Array<ArrayBuffer>, z.ZodTypeDef, Uint8Array<ArrayBuffer>> & CassandraTypeMarker;
|
|
40
|
+
list: <T extends z.ZodTypeAny>(elementType: T) => z.ZodArray<T, "many"> & CassandraTypeMarker;
|
|
41
|
+
set: <T extends z.ZodTypeAny>(elementType: T) => z.ZodSet<T> & CassandraTypeMarker;
|
|
42
|
+
map: <K extends z.ZodTypeAny, V extends z.ZodTypeAny>(keyType: K, valueType: V) => z.ZodRecord<K, V> & CassandraTypeMarker;
|
|
33
43
|
};
|
|
34
44
|
|
|
35
45
|
interface KeyspaceConfig {
|
|
@@ -79,9 +89,9 @@ type ExtractFieldConfig<T extends z.ZodTypeAny> = {
|
|
|
79
89
|
};
|
|
80
90
|
} ? "double" : "double" : T extends z.ZodBoolean ? "boolean" : T extends z.ZodDate ? "timestamp" : T extends z.ZodType<Uint8Array> ? "blob" : T extends z.ZodArray<any> ? "list" : T extends z.ZodSet<any> ? "set" : T extends z.ZodRecord<any, any> ? "map" : "text";
|
|
81
91
|
flags: FieldFlags;
|
|
82
|
-
elementType?: keyof
|
|
83
|
-
keyType?: keyof
|
|
84
|
-
valueType?: keyof
|
|
92
|
+
elementType?: keyof typeof Types;
|
|
93
|
+
keyType?: keyof typeof Types;
|
|
94
|
+
valueType?: keyof typeof Types;
|
|
85
95
|
};
|
|
86
96
|
type ClusteringKeyConfig = Record<string, "asc" | "desc">;
|
|
87
97
|
type SchemaConfig<F extends z.ZodRawShape> = {
|
|
@@ -226,4 +236,4 @@ declare class Client {
|
|
|
226
236
|
getCassandraClient(): Client$1;
|
|
227
237
|
}
|
|
228
238
|
|
|
229
|
-
export { Client, type ClusteringKeyConfig, type ExtractClusteringKeyShape, type ExtractClusteringKeyType, type ExtractFieldConfig, type ExtractPartitionKeyShape, type ExtractPartitionKeyType, type
|
|
239
|
+
export { Client, type ClusteringKeyConfig, type ExtractClusteringKeyShape, type ExtractClusteringKeyType, type ExtractFieldConfig, type ExtractPartitionKeyShape, type ExtractPartitionKeyType, type FieldFlags, Model, type OrmOptions, type Schema, type SchemaConfig, type SchemaDefinition, TableSchema, Types, createModel, createTableSchema, uuid };
|
package/dist/mod.d.ts
CHANGED
|
@@ -2,34 +2,44 @@ import * as cassandra_driver from 'cassandra-driver';
|
|
|
2
2
|
import { ClientOptions, QueryOptions, Client as Client$1 } from 'cassandra-driver';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
order: "ASC" | "DESC";
|
|
26
|
-
};
|
|
27
|
-
optional: boolean;
|
|
28
|
-
static: boolean;
|
|
5
|
+
/**
|
|
6
|
+
* Cassandra 数据类型扩展
|
|
7
|
+
* 提供更精确的类型映射支持
|
|
8
|
+
*/
|
|
9
|
+
interface CassandraTypeMarker {
|
|
10
|
+
_cassandraType: string;
|
|
11
|
+
}
|
|
12
|
+
declare const Types: {
|
|
13
|
+
boolean: (params?: z.RawCreateParams & {
|
|
14
|
+
coerce?: boolean;
|
|
15
|
+
}) => z.ZodBoolean;
|
|
16
|
+
text: (params?: z.RawCreateParams & {
|
|
17
|
+
coerce?: true;
|
|
18
|
+
}) => z.ZodString;
|
|
19
|
+
double: (params?: z.RawCreateParams & {
|
|
20
|
+
coerce?: boolean;
|
|
21
|
+
}) => z.ZodNumber;
|
|
22
|
+
enum: {
|
|
23
|
+
<U extends string, T extends Readonly<[U, ...U[]]>>(values: T, params?: z.RawCreateParams): z.ZodEnum<z.Writeable<T>>;
|
|
24
|
+
<U extends string, T extends [U, ...U[]]>(values: T, params?: z.RawCreateParams): z.ZodEnum<T>;
|
|
29
25
|
};
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
object: <T extends z.ZodRawShape>(shape: T, params?: z.RawCreateParams) => z.ZodObject<T, "strip", z.ZodTypeAny, z.objectOutputType<T, z.ZodTypeAny, "strip">, z.objectInputType<T, z.ZodTypeAny, "strip">>;
|
|
27
|
+
float: () => z.ZodEffects<z.ZodNumber, number, number> & CassandraTypeMarker;
|
|
28
|
+
int: () => z.ZodNumber & CassandraTypeMarker;
|
|
29
|
+
bigint: () => z.ZodEffects<z.ZodNumber, bigint, number> & CassandraTypeMarker;
|
|
30
|
+
smallint: () => z.ZodEffects<z.ZodNumber, number, number> & CassandraTypeMarker;
|
|
31
|
+
tinyint: () => z.ZodEffects<z.ZodNumber, number, number> & CassandraTypeMarker;
|
|
32
|
+
varint: () => z.ZodEffects<z.ZodNumber, bigint, number> & CassandraTypeMarker;
|
|
33
|
+
varchar: (maxLength?: number) => z.ZodString & CassandraTypeMarker;
|
|
34
|
+
ascii: () => z.ZodEffects<z.ZodString, string, string> & CassandraTypeMarker;
|
|
35
|
+
timestamp: () => z.ZodDate & CassandraTypeMarker;
|
|
36
|
+
time: () => z.ZodDate & CassandraTypeMarker;
|
|
37
|
+
date: () => z.ZodDate & CassandraTypeMarker;
|
|
38
|
+
uuid: () => z.ZodString & CassandraTypeMarker;
|
|
39
|
+
blob: () => z.ZodType<Uint8Array<ArrayBuffer>, z.ZodTypeDef, Uint8Array<ArrayBuffer>> & CassandraTypeMarker;
|
|
40
|
+
list: <T extends z.ZodTypeAny>(elementType: T) => z.ZodArray<T, "many"> & CassandraTypeMarker;
|
|
41
|
+
set: <T extends z.ZodTypeAny>(elementType: T) => z.ZodSet<T> & CassandraTypeMarker;
|
|
42
|
+
map: <K extends z.ZodTypeAny, V extends z.ZodTypeAny>(keyType: K, valueType: V) => z.ZodRecord<K, V> & CassandraTypeMarker;
|
|
33
43
|
};
|
|
34
44
|
|
|
35
45
|
interface KeyspaceConfig {
|
|
@@ -79,9 +89,9 @@ type ExtractFieldConfig<T extends z.ZodTypeAny> = {
|
|
|
79
89
|
};
|
|
80
90
|
} ? "double" : "double" : T extends z.ZodBoolean ? "boolean" : T extends z.ZodDate ? "timestamp" : T extends z.ZodType<Uint8Array> ? "blob" : T extends z.ZodArray<any> ? "list" : T extends z.ZodSet<any> ? "set" : T extends z.ZodRecord<any, any> ? "map" : "text";
|
|
81
91
|
flags: FieldFlags;
|
|
82
|
-
elementType?: keyof
|
|
83
|
-
keyType?: keyof
|
|
84
|
-
valueType?: keyof
|
|
92
|
+
elementType?: keyof typeof Types;
|
|
93
|
+
keyType?: keyof typeof Types;
|
|
94
|
+
valueType?: keyof typeof Types;
|
|
85
95
|
};
|
|
86
96
|
type ClusteringKeyConfig = Record<string, "asc" | "desc">;
|
|
87
97
|
type SchemaConfig<F extends z.ZodRawShape> = {
|
|
@@ -226,4 +236,4 @@ declare class Client {
|
|
|
226
236
|
getCassandraClient(): Client$1;
|
|
227
237
|
}
|
|
228
238
|
|
|
229
|
-
export { Client, type ClusteringKeyConfig, type ExtractClusteringKeyShape, type ExtractClusteringKeyType, type ExtractFieldConfig, type ExtractPartitionKeyShape, type ExtractPartitionKeyType, type
|
|
239
|
+
export { Client, type ClusteringKeyConfig, type ExtractClusteringKeyShape, type ExtractClusteringKeyType, type ExtractFieldConfig, type ExtractPartitionKeyShape, type ExtractPartitionKeyType, type FieldFlags, Model, type OrmOptions, type Schema, type SchemaConfig, type SchemaDefinition, TableSchema, Types, createModel, createTableSchema, uuid };
|
package/dist/mod.js
CHANGED
|
@@ -3,6 +3,9 @@ import { z } from 'zod';
|
|
|
3
3
|
|
|
4
4
|
// src/client.ts
|
|
5
5
|
function convertZodToCassandraType(schema) {
|
|
6
|
+
if (schema._cassandraType) {
|
|
7
|
+
return schema._cassandraType;
|
|
8
|
+
}
|
|
6
9
|
let baseSchema = schema instanceof z.ZodEffects ? schema._def.schema : schema;
|
|
7
10
|
if (schema instanceof z.ZodOptional) {
|
|
8
11
|
return convertZodToCassandraType(schema.unwrap());
|
|
@@ -10,6 +13,9 @@ function convertZodToCassandraType(schema) {
|
|
|
10
13
|
while (baseSchema instanceof z.ZodEffects) {
|
|
11
14
|
baseSchema = baseSchema._def.schema;
|
|
12
15
|
}
|
|
16
|
+
if (baseSchema._cassandraType) {
|
|
17
|
+
return baseSchema._cassandraType;
|
|
18
|
+
}
|
|
13
19
|
if (baseSchema instanceof z.ZodString) {
|
|
14
20
|
if (baseSchema._def.checks.some((c) => c.kind === "uuid")) {
|
|
15
21
|
return "uuid";
|
|
@@ -144,9 +150,24 @@ function dropColumns(schema, fields) {
|
|
|
144
150
|
return `ALTER TABLE ${schema.keyspace}.${schema.tableName} DROP (${fields.join(", ")});`;
|
|
145
151
|
}
|
|
146
152
|
function convertToFieldConfig(zodType, flags = {}) {
|
|
153
|
+
if (zodType._cassandraType) {
|
|
154
|
+
return {
|
|
155
|
+
type: zodType._cassandraType,
|
|
156
|
+
flags: {
|
|
157
|
+
partitionKey: !!flags.partitionKey,
|
|
158
|
+
clusteringKey: flags.clusteringKey || false,
|
|
159
|
+
optional: !!flags.optional,
|
|
160
|
+
static: !!flags.static
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
147
164
|
let type = "text";
|
|
148
165
|
if (zodType._def.typeName === "ZodString") {
|
|
149
|
-
|
|
166
|
+
if (zodType._def.checks && zodType._def.checks.some((c) => c.kind === "uuid")) {
|
|
167
|
+
type = "uuid";
|
|
168
|
+
} else {
|
|
169
|
+
type = "text";
|
|
170
|
+
}
|
|
150
171
|
} else if (zodType._def.typeName === "ZodNumber") {
|
|
151
172
|
type = "double";
|
|
152
173
|
} else if (zodType._def.typeName === "ZodBoolean") {
|
|
@@ -369,7 +390,13 @@ function convertValueFromCassandra(value, config) {
|
|
|
369
390
|
case "timestamp":
|
|
370
391
|
return value instanceof Date ? value : new Date(value);
|
|
371
392
|
case "uuid":
|
|
372
|
-
|
|
393
|
+
if (typeof value === "string") {
|
|
394
|
+
return value;
|
|
395
|
+
}
|
|
396
|
+
if (value && typeof value.toString === "function") {
|
|
397
|
+
return value.toString();
|
|
398
|
+
}
|
|
399
|
+
return String(value);
|
|
373
400
|
case "blob":
|
|
374
401
|
return value instanceof Uint8Array ? value : new Uint8Array(value);
|
|
375
402
|
case "text":
|
|
@@ -814,5 +841,83 @@ var Client = class {
|
|
|
814
841
|
return this.cassandraClient;
|
|
815
842
|
}
|
|
816
843
|
};
|
|
844
|
+
function createCassandraType(schema, cassandraType) {
|
|
845
|
+
const markedSchema = schema;
|
|
846
|
+
markedSchema._cassandraType = cassandraType;
|
|
847
|
+
return markedSchema;
|
|
848
|
+
}
|
|
849
|
+
var Types = {
|
|
850
|
+
boolean: z.boolean,
|
|
851
|
+
text: z.string,
|
|
852
|
+
double: z.number,
|
|
853
|
+
enum: z.enum,
|
|
854
|
+
object: z.object,
|
|
855
|
+
float: () => createCassandraType(
|
|
856
|
+
z.number().transform((val) => parseFloat(val.toString())),
|
|
857
|
+
"float"
|
|
858
|
+
),
|
|
859
|
+
int: () => createCassandraType(z.number().int(), "int"),
|
|
860
|
+
bigint: () => createCassandraType(
|
|
861
|
+
z.number().int().transform((val) => BigInt(val)),
|
|
862
|
+
"bigint"
|
|
863
|
+
),
|
|
864
|
+
smallint: () => createCassandraType(
|
|
865
|
+
z.number().int().refine((val) => val >= -32768 && val <= 32767, {
|
|
866
|
+
message: "Value must be between -32768 and 32767"
|
|
867
|
+
}),
|
|
868
|
+
"smallint"
|
|
869
|
+
),
|
|
870
|
+
tinyint: () => createCassandraType(
|
|
871
|
+
z.number().int().refine((val) => val >= -128 && val <= 127, {
|
|
872
|
+
message: "Value must be between -128 and 127"
|
|
873
|
+
}),
|
|
874
|
+
"tinyint"
|
|
875
|
+
),
|
|
876
|
+
varint: () => createCassandraType(
|
|
877
|
+
z.number().int().transform((val) => BigInt(val)),
|
|
878
|
+
"varint"
|
|
879
|
+
),
|
|
880
|
+
// 字符串类型
|
|
881
|
+
varchar: (maxLength) => createCassandraType(z.string().max(maxLength || 65535), "varchar"),
|
|
882
|
+
ascii: () => createCassandraType(
|
|
883
|
+
z.string().refine((val) => /^[\x00-\x7F]*$/.test(val), {
|
|
884
|
+
message: "String must contain only ASCII characters"
|
|
885
|
+
}),
|
|
886
|
+
"ascii"
|
|
887
|
+
),
|
|
888
|
+
// 时间类型
|
|
889
|
+
timestamp: () => createCassandraType(z.date(), "timestamp"),
|
|
890
|
+
time: () => createCassandraType(z.date(), "time"),
|
|
891
|
+
date: () => createCassandraType(z.date(), "date"),
|
|
892
|
+
// UUID 类型
|
|
893
|
+
uuid: () => createCassandraType(z.string().uuid(), "uuid"),
|
|
894
|
+
// 二进制类型
|
|
895
|
+
blob: () => createCassandraType(z.instanceof(Uint8Array), "blob"),
|
|
896
|
+
// 集合类型
|
|
897
|
+
list: (elementType) => createCassandraType(
|
|
898
|
+
z.array(elementType),
|
|
899
|
+
`list<${getCassandraTypeFromZod(elementType)}>`
|
|
900
|
+
),
|
|
901
|
+
set: (elementType) => createCassandraType(
|
|
902
|
+
z.set(elementType),
|
|
903
|
+
`set<${getCassandraTypeFromZod(elementType)}>`
|
|
904
|
+
),
|
|
905
|
+
map: (keyType, valueType) => createCassandraType(
|
|
906
|
+
z.record(keyType, valueType),
|
|
907
|
+
`map<${getCassandraTypeFromZod(keyType)}, ${getCassandraTypeFromZod(
|
|
908
|
+
valueType
|
|
909
|
+
)}>`
|
|
910
|
+
)
|
|
911
|
+
};
|
|
912
|
+
function getCassandraTypeFromZod(schema) {
|
|
913
|
+
if (schema._cassandraType) {
|
|
914
|
+
return schema._cassandraType;
|
|
915
|
+
}
|
|
916
|
+
if (schema instanceof z.ZodString) return "text";
|
|
917
|
+
if (schema instanceof z.ZodNumber) return "double";
|
|
918
|
+
if (schema instanceof z.ZodBoolean) return "boolean";
|
|
919
|
+
if (schema instanceof z.ZodDate) return "timestamp";
|
|
920
|
+
return "text";
|
|
921
|
+
}
|
|
817
922
|
|
|
818
|
-
export { Client, Model, TableSchema, createModel, createTableSchema, uuid };
|
|
923
|
+
export { Client, Model, TableSchema, Types, createModel, createTableSchema, uuid };
|