imean-cassandra-orm 2.0.0 → 2.0.2

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 CHANGED
@@ -56,16 +56,26 @@ function convertZodToCassandraType(schema) {
56
56
  }
57
57
  function getNewFields(schemaFields, tableMetadata2) {
58
58
  return schemaFields.filter(
59
- (field) => !tableMetadata2.some((t) => t.column_name === field)
59
+ (field) => !tableMetadata2.some(
60
+ (t) => t.column_name.toLowerCase() === field.toLowerCase()
61
+ )
60
62
  );
61
63
  }
62
64
  function getDeletedFields(schemaFields, tableMetadata2) {
63
- return tableMetadata2.filter((t) => !schemaFields.includes(t.column_name)).map((t) => t.column_name);
65
+ return tableMetadata2.filter((t) => !schemaFields.some(
66
+ (field) => field.toLowerCase() === t.column_name.toLowerCase()
67
+ )).map((t) => t.column_name);
64
68
  }
65
69
  function getUpdatedFields(schemaFields, tableMetadata2, shape) {
66
70
  return schemaFields.filter(
67
71
  (field) => tableMetadata2.some(
68
- (t) => t.column_name === field && t.type !== convertZodToCassandraType(shape[field])
72
+ (t) => {
73
+ const dbFieldName = t.column_name;
74
+ const schemaFieldName = field;
75
+ const dbType = t.type;
76
+ const schemaType = convertZodToCassandraType(shape[field]);
77
+ return dbFieldName.toLowerCase() === schemaFieldName.toLowerCase() && dbType !== schemaType;
78
+ }
69
79
  )
70
80
  );
71
81
  }
@@ -298,8 +308,34 @@ function buildDeleteCql(schema, partitionKey, clusteringKey) {
298
308
  }
299
309
  function convertRowToObject(row, fieldConfigs, zodSchema) {
300
310
  const result = {};
311
+ const fieldNameMap = /* @__PURE__ */ new Map();
312
+ Object.keys(fieldConfigs).forEach((camelCaseName) => {
313
+ fieldNameMap.set(camelCaseName.toLowerCase(), camelCaseName);
314
+ });
301
315
  Object.entries(fieldConfigs).forEach(([fieldName, config]) => {
302
- const value = row[fieldName];
316
+ const possibleNames = [
317
+ fieldName,
318
+ // 原始驼峰名
319
+ fieldName.toLowerCase(),
320
+ // 全小写
321
+ fieldName.toUpperCase()
322
+ // 全大写
323
+ ];
324
+ let value = void 0;
325
+ for (const possibleName of possibleNames) {
326
+ if (possibleName in row) {
327
+ value = row[possibleName];
328
+ break;
329
+ }
330
+ }
331
+ if (value === void 0) {
332
+ for (const [dbFieldName, dbValue] of Object.entries(row)) {
333
+ if (dbFieldName.toLowerCase() === fieldName.toLowerCase()) {
334
+ value = dbValue;
335
+ break;
336
+ }
337
+ }
338
+ }
303
339
  if (value !== void 0) {
304
340
  result[fieldName] = convertValueFromCassandra(value, config);
305
341
  }
@@ -442,6 +478,12 @@ var Model = class _Model {
442
478
  this.schema = schema;
443
479
  this.client = client;
444
480
  }
481
+ get types() {
482
+ return this.schema.types;
483
+ }
484
+ get schemas() {
485
+ return this.schema.schemas;
486
+ }
445
487
  // 静态方法:创建模型(需要外部传入 client)
446
488
  static create(definition, client) {
447
489
  const schema = new TableSchema(definition);
@@ -577,13 +619,45 @@ var Model = class _Model {
577
619
  syncQueries.push(queryHelper.dropColumns(this.schema, deletedFields));
578
620
  }
579
621
  if (updatedFields.length > 0) {
580
- errorReason = "\u5B57\u6BB5\u7C7B\u578B\u6709\u66F4\u65B0\uFF0C\u4F46 force \u4E3A false\uFF0C\u65E0\u6CD5\u81EA\u52A8\u66F4\u65B0";
622
+ const fieldDetails = updatedFields.map((field) => {
623
+ const meta = tableMetadata2.find((t) => t.column_name.toLowerCase() === field.toLowerCase());
624
+ const existingType = meta?.type || "unknown";
625
+ const newType = convertZodToCassandraType(
626
+ this.schema.fields.shape[field]
627
+ );
628
+ return `${field}(${existingType} -> ${newType})`;
629
+ }).join(", ");
630
+ errorReason = `[${this.schema.keyspace}.${this.schema.tableName}] \u5B57\u6BB5\u7C7B\u578B\u6709\u66F4\u65B0\uFF0C\u65E0\u6CD5\u81EA\u52A8\u66F4\u65B0\u3002\u53D8\u66F4\u5B57\u6BB5: ${fieldDetails}`;
581
631
  }
582
632
  if (hasKeyFieldChanged(this.schema, newFields, deletedFields)) {
583
- errorReason = "\u5220\u9664\u7684\u5B57\u6BB5\u6216\u8005\u65B0\u589E\u7684\u5B57\u6BB5\u6709\u5206\u533A\u952E\u6216\u8005\u805A\u7C7B\u952E\uFF0C\u65E0\u6CD5\u81EA\u52A8\u66F4\u65B0";
633
+ const partitionKeyFields = this.schema.partitionKeyFields;
634
+ const clusteringKeyFields = this.schema.clusteringKeyFields.map(
635
+ (ck) => ck.field
636
+ );
637
+ const keyFields = [...partitionKeyFields, ...clusteringKeyFields];
638
+ const newKeyFields = newFields.filter(
639
+ (field) => keyFields.includes(field)
640
+ );
641
+ const deletedKeyFields = deletedFields.filter(
642
+ (field) => keyFields.includes(field)
643
+ );
644
+ let keyChangeDetails = [];
645
+ if (newKeyFields.length > 0) {
646
+ keyChangeDetails.push(`\u65B0\u589E\u952E\u5B57\u6BB5: ${newKeyFields.join(", ")}`);
647
+ }
648
+ if (deletedKeyFields.length > 0) {
649
+ keyChangeDetails.push(`\u5220\u9664\u952E\u5B57\u6BB5: ${deletedKeyFields.join(", ")}`);
650
+ }
651
+ errorReason = `[${this.schema.keyspace}.${this.schema.tableName}] \u952E\u5B57\u6BB5\u53D1\u751F\u53D8\u66F4\uFF0C\u65E0\u6CD5\u81EA\u52A8\u66F4\u65B0\u3002${keyChangeDetails.join("; ")}`;
584
652
  }
585
653
  if (isClusteringKeyChanged(this.schema, tableMetadata2)) {
586
- errorReason = "\u805A\u7C7B\u952E\u914D\u7F6E\u53D1\u751F\u53D8\u5316\uFF0C\u65E0\u6CD5\u81EA\u52A8\u66F4\u65B0";
654
+ const existingClusteringKeys = tableMetadata2.filter((t) => t.kind === "clustering").map((t) => t.column_name);
655
+ const currentClusteringKeys = this.schema.clusteringKeyFields.map(
656
+ (ck) => ck.field
657
+ );
658
+ const existingOrder = existingClusteringKeys.join(", ");
659
+ const currentOrder = currentClusteringKeys.join(", ");
660
+ errorReason = `[${this.schema.keyspace}.${this.schema.tableName}] \u805A\u7C7B\u952E\u914D\u7F6E\u53D1\u751F\u53D8\u5316\uFF0C\u65E0\u6CD5\u81EA\u52A8\u66F4\u65B0\u3002\u73B0\u6709\u987A\u5E8F: [${existingOrder}]\uFF0C\u65B0\u987A\u5E8F: [${currentOrder}]`;
587
661
  }
588
662
  }
589
663
  if (errorReason) {
@@ -613,7 +687,7 @@ var uuid = () => cassandraDriver.types.Uuid.random().toString();
613
687
  var Client = class {
614
688
  constructor(options) {
615
689
  this.options = options;
616
- this.cassandraClient = new cassandraDriver.Client(options);
690
+ this.cassandraClient = new cassandraDriver.Client({ ...options });
617
691
  }
618
692
  cassandraClient;
619
693
  models = [];
@@ -663,7 +737,11 @@ var Client = class {
663
737
  }
664
738
  }
665
739
  try {
666
- const result = await this.cassandraClient.execute(cql, params, executeOptions);
740
+ const result = await this.cassandraClient.execute(
741
+ cql,
742
+ params,
743
+ executeOptions
744
+ );
667
745
  if (this.options.debug) {
668
746
  console.log({ query: cql, params, rows: result.rows });
669
747
  }
package/dist/mod.d.cts CHANGED
@@ -179,6 +179,16 @@ declare class Model<F extends z.ZodRawShape, PK extends (keyof F)[], CK extends
179
179
  readonly schema: TableSchema<F, PK, CK>;
180
180
  readonly client: Client;
181
181
  constructor(schema: TableSchema<F, PK, CK>, client: Client);
182
+ get types(): {
183
+ model: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<F>, any> extends infer T ? { [k in keyof T]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<F>, any>[k]; } : never;
184
+ partitionKey: ExtractPartitionKeyType<F, PK>;
185
+ clusteringKey: ExtractClusteringKeyType<F, CK>;
186
+ };
187
+ get schemas(): {
188
+ model: z.ZodObject<F, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<F>, any> extends infer T ? { [k in keyof T]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<F>, any>[k]; } : never, z.baseObjectInputType<F> extends infer T_1 ? { [k_1 in keyof T_1]: z.baseObjectInputType<F>[k_1]; } : never>;
189
+ partitionKey: z.ZodObject<ExtractPartitionKeyShape<F, PK>, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<ExtractPartitionKeyShape<F, PK>>, any> extends infer T_2 ? { [k_2 in keyof T_2]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<ExtractPartitionKeyShape<F, PK>>, any>[k_2]; } : never, z.baseObjectInputType<ExtractPartitionKeyShape<F, PK>> extends infer T_3 ? { [k_3 in keyof T_3]: z.baseObjectInputType<ExtractPartitionKeyShape<F, PK>>[k_3]; } : never>;
190
+ clusteringKey: CK extends ClusteringKeyConfig ? z.ZodObject<ExtractClusteringKeyShape<F, CK>, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<ExtractClusteringKeyShape<F, CK>>, any> extends infer T_4 ? { [k_4 in keyof T_4]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<ExtractClusteringKeyShape<F, CK>>, any>[k_4]; } : never, z.baseObjectInputType<ExtractClusteringKeyShape<F, CK>> extends infer T_5 ? { [k_5 in keyof T_5]: z.baseObjectInputType<ExtractClusteringKeyShape<F, CK>>[k_5]; } : never> : never;
191
+ };
182
192
  static create<F extends z.ZodRawShape, PK extends (keyof F)[], CK extends ClusteringKeyConfig | undefined = undefined>(definition: SchemaDefinition<F, PK, CK>, client: Client): Model<F, PK, CK>;
183
193
  insert(data: z.infer<z.ZodObject<F>>): Promise<void>;
184
194
  batchInsert(data: z.infer<z.ZodObject<F>>[]): Promise<void>;
package/dist/mod.d.ts CHANGED
@@ -179,6 +179,16 @@ declare class Model<F extends z.ZodRawShape, PK extends (keyof F)[], CK extends
179
179
  readonly schema: TableSchema<F, PK, CK>;
180
180
  readonly client: Client;
181
181
  constructor(schema: TableSchema<F, PK, CK>, client: Client);
182
+ get types(): {
183
+ model: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<F>, any> extends infer T ? { [k in keyof T]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<F>, any>[k]; } : never;
184
+ partitionKey: ExtractPartitionKeyType<F, PK>;
185
+ clusteringKey: ExtractClusteringKeyType<F, CK>;
186
+ };
187
+ get schemas(): {
188
+ model: z.ZodObject<F, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<F>, any> extends infer T ? { [k in keyof T]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<F>, any>[k]; } : never, z.baseObjectInputType<F> extends infer T_1 ? { [k_1 in keyof T_1]: z.baseObjectInputType<F>[k_1]; } : never>;
189
+ partitionKey: z.ZodObject<ExtractPartitionKeyShape<F, PK>, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<ExtractPartitionKeyShape<F, PK>>, any> extends infer T_2 ? { [k_2 in keyof T_2]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<ExtractPartitionKeyShape<F, PK>>, any>[k_2]; } : never, z.baseObjectInputType<ExtractPartitionKeyShape<F, PK>> extends infer T_3 ? { [k_3 in keyof T_3]: z.baseObjectInputType<ExtractPartitionKeyShape<F, PK>>[k_3]; } : never>;
190
+ clusteringKey: CK extends ClusteringKeyConfig ? z.ZodObject<ExtractClusteringKeyShape<F, CK>, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<ExtractClusteringKeyShape<F, CK>>, any> extends infer T_4 ? { [k_4 in keyof T_4]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<ExtractClusteringKeyShape<F, CK>>, any>[k_4]; } : never, z.baseObjectInputType<ExtractClusteringKeyShape<F, CK>> extends infer T_5 ? { [k_5 in keyof T_5]: z.baseObjectInputType<ExtractClusteringKeyShape<F, CK>>[k_5]; } : never> : never;
191
+ };
182
192
  static create<F extends z.ZodRawShape, PK extends (keyof F)[], CK extends ClusteringKeyConfig | undefined = undefined>(definition: SchemaDefinition<F, PK, CK>, client: Client): Model<F, PK, CK>;
183
193
  insert(data: z.infer<z.ZodObject<F>>): Promise<void>;
184
194
  batchInsert(data: z.infer<z.ZodObject<F>>[]): Promise<void>;
package/dist/mod.js CHANGED
@@ -54,16 +54,26 @@ function convertZodToCassandraType(schema) {
54
54
  }
55
55
  function getNewFields(schemaFields, tableMetadata2) {
56
56
  return schemaFields.filter(
57
- (field) => !tableMetadata2.some((t) => t.column_name === field)
57
+ (field) => !tableMetadata2.some(
58
+ (t) => t.column_name.toLowerCase() === field.toLowerCase()
59
+ )
58
60
  );
59
61
  }
60
62
  function getDeletedFields(schemaFields, tableMetadata2) {
61
- return tableMetadata2.filter((t) => !schemaFields.includes(t.column_name)).map((t) => t.column_name);
63
+ return tableMetadata2.filter((t) => !schemaFields.some(
64
+ (field) => field.toLowerCase() === t.column_name.toLowerCase()
65
+ )).map((t) => t.column_name);
62
66
  }
63
67
  function getUpdatedFields(schemaFields, tableMetadata2, shape) {
64
68
  return schemaFields.filter(
65
69
  (field) => tableMetadata2.some(
66
- (t) => t.column_name === field && t.type !== convertZodToCassandraType(shape[field])
70
+ (t) => {
71
+ const dbFieldName = t.column_name;
72
+ const schemaFieldName = field;
73
+ const dbType = t.type;
74
+ const schemaType = convertZodToCassandraType(shape[field]);
75
+ return dbFieldName.toLowerCase() === schemaFieldName.toLowerCase() && dbType !== schemaType;
76
+ }
67
77
  )
68
78
  );
69
79
  }
@@ -296,8 +306,34 @@ function buildDeleteCql(schema, partitionKey, clusteringKey) {
296
306
  }
297
307
  function convertRowToObject(row, fieldConfigs, zodSchema) {
298
308
  const result = {};
309
+ const fieldNameMap = /* @__PURE__ */ new Map();
310
+ Object.keys(fieldConfigs).forEach((camelCaseName) => {
311
+ fieldNameMap.set(camelCaseName.toLowerCase(), camelCaseName);
312
+ });
299
313
  Object.entries(fieldConfigs).forEach(([fieldName, config]) => {
300
- const value = row[fieldName];
314
+ const possibleNames = [
315
+ fieldName,
316
+ // 原始驼峰名
317
+ fieldName.toLowerCase(),
318
+ // 全小写
319
+ fieldName.toUpperCase()
320
+ // 全大写
321
+ ];
322
+ let value = void 0;
323
+ for (const possibleName of possibleNames) {
324
+ if (possibleName in row) {
325
+ value = row[possibleName];
326
+ break;
327
+ }
328
+ }
329
+ if (value === void 0) {
330
+ for (const [dbFieldName, dbValue] of Object.entries(row)) {
331
+ if (dbFieldName.toLowerCase() === fieldName.toLowerCase()) {
332
+ value = dbValue;
333
+ break;
334
+ }
335
+ }
336
+ }
301
337
  if (value !== void 0) {
302
338
  result[fieldName] = convertValueFromCassandra(value, config);
303
339
  }
@@ -440,6 +476,12 @@ var Model = class _Model {
440
476
  this.schema = schema;
441
477
  this.client = client;
442
478
  }
479
+ get types() {
480
+ return this.schema.types;
481
+ }
482
+ get schemas() {
483
+ return this.schema.schemas;
484
+ }
443
485
  // 静态方法:创建模型(需要外部传入 client)
444
486
  static create(definition, client) {
445
487
  const schema = new TableSchema(definition);
@@ -575,13 +617,45 @@ var Model = class _Model {
575
617
  syncQueries.push(queryHelper.dropColumns(this.schema, deletedFields));
576
618
  }
577
619
  if (updatedFields.length > 0) {
578
- errorReason = "\u5B57\u6BB5\u7C7B\u578B\u6709\u66F4\u65B0\uFF0C\u4F46 force \u4E3A false\uFF0C\u65E0\u6CD5\u81EA\u52A8\u66F4\u65B0";
620
+ const fieldDetails = updatedFields.map((field) => {
621
+ const meta = tableMetadata2.find((t) => t.column_name.toLowerCase() === field.toLowerCase());
622
+ const existingType = meta?.type || "unknown";
623
+ const newType = convertZodToCassandraType(
624
+ this.schema.fields.shape[field]
625
+ );
626
+ return `${field}(${existingType} -> ${newType})`;
627
+ }).join(", ");
628
+ errorReason = `[${this.schema.keyspace}.${this.schema.tableName}] \u5B57\u6BB5\u7C7B\u578B\u6709\u66F4\u65B0\uFF0C\u65E0\u6CD5\u81EA\u52A8\u66F4\u65B0\u3002\u53D8\u66F4\u5B57\u6BB5: ${fieldDetails}`;
579
629
  }
580
630
  if (hasKeyFieldChanged(this.schema, newFields, deletedFields)) {
581
- errorReason = "\u5220\u9664\u7684\u5B57\u6BB5\u6216\u8005\u65B0\u589E\u7684\u5B57\u6BB5\u6709\u5206\u533A\u952E\u6216\u8005\u805A\u7C7B\u952E\uFF0C\u65E0\u6CD5\u81EA\u52A8\u66F4\u65B0";
631
+ const partitionKeyFields = this.schema.partitionKeyFields;
632
+ const clusteringKeyFields = this.schema.clusteringKeyFields.map(
633
+ (ck) => ck.field
634
+ );
635
+ const keyFields = [...partitionKeyFields, ...clusteringKeyFields];
636
+ const newKeyFields = newFields.filter(
637
+ (field) => keyFields.includes(field)
638
+ );
639
+ const deletedKeyFields = deletedFields.filter(
640
+ (field) => keyFields.includes(field)
641
+ );
642
+ let keyChangeDetails = [];
643
+ if (newKeyFields.length > 0) {
644
+ keyChangeDetails.push(`\u65B0\u589E\u952E\u5B57\u6BB5: ${newKeyFields.join(", ")}`);
645
+ }
646
+ if (deletedKeyFields.length > 0) {
647
+ keyChangeDetails.push(`\u5220\u9664\u952E\u5B57\u6BB5: ${deletedKeyFields.join(", ")}`);
648
+ }
649
+ errorReason = `[${this.schema.keyspace}.${this.schema.tableName}] \u952E\u5B57\u6BB5\u53D1\u751F\u53D8\u66F4\uFF0C\u65E0\u6CD5\u81EA\u52A8\u66F4\u65B0\u3002${keyChangeDetails.join("; ")}`;
582
650
  }
583
651
  if (isClusteringKeyChanged(this.schema, tableMetadata2)) {
584
- errorReason = "\u805A\u7C7B\u952E\u914D\u7F6E\u53D1\u751F\u53D8\u5316\uFF0C\u65E0\u6CD5\u81EA\u52A8\u66F4\u65B0";
652
+ const existingClusteringKeys = tableMetadata2.filter((t) => t.kind === "clustering").map((t) => t.column_name);
653
+ const currentClusteringKeys = this.schema.clusteringKeyFields.map(
654
+ (ck) => ck.field
655
+ );
656
+ const existingOrder = existingClusteringKeys.join(", ");
657
+ const currentOrder = currentClusteringKeys.join(", ");
658
+ errorReason = `[${this.schema.keyspace}.${this.schema.tableName}] \u805A\u7C7B\u952E\u914D\u7F6E\u53D1\u751F\u53D8\u5316\uFF0C\u65E0\u6CD5\u81EA\u52A8\u66F4\u65B0\u3002\u73B0\u6709\u987A\u5E8F: [${existingOrder}]\uFF0C\u65B0\u987A\u5E8F: [${currentOrder}]`;
585
659
  }
586
660
  }
587
661
  if (errorReason) {
@@ -611,7 +685,7 @@ var uuid = () => types.Uuid.random().toString();
611
685
  var Client = class {
612
686
  constructor(options) {
613
687
  this.options = options;
614
- this.cassandraClient = new Client$1(options);
688
+ this.cassandraClient = new Client$1({ ...options });
615
689
  }
616
690
  cassandraClient;
617
691
  models = [];
@@ -661,7 +735,11 @@ var Client = class {
661
735
  }
662
736
  }
663
737
  try {
664
- const result = await this.cassandraClient.execute(cql, params, executeOptions);
738
+ const result = await this.cassandraClient.execute(
739
+ cql,
740
+ params,
741
+ executeOptions
742
+ );
665
743
  if (this.options.debug) {
666
744
  console.log({ query: cql, params, rows: result.rows });
667
745
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imean-cassandra-orm",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "cassandra orm",
5
5
  "keywords": [
6
6
  "cassandra",