imean-cassandra-orm 3.3.0 → 3.3.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.
Files changed (3) hide show
  1. package/dist/mod.cjs +73 -33
  2. package/dist/mod.js +73 -33
  3. package/package.json +7 -8
package/dist/mod.cjs CHANGED
@@ -667,11 +667,7 @@ function shouldWrite(op, conf) {
667
667
  // src/indexes.ts
668
668
  function createIndex(keyspace, tableName, fieldName, options) {
669
669
  const indexName = `${tableName}_${String(fieldName).toLowerCase()}_sai_idx`;
670
- const config = options !== true ? options : {
671
- case_sensitive: false,
672
- normalize: true,
673
- ascii: true
674
- };
670
+ const config = options !== true ? options : {};
675
671
  const optionsString = JSON.stringify(config).replace(/"/g, "'");
676
672
  return `CREATE INDEX IF NOT EXISTS ${indexName} ON ${keyspace}.${tableName} (${String(
677
673
  fieldName
@@ -810,41 +806,82 @@ function buildInsertParams(data, fieldConfigs) {
810
806
  });
811
807
  return params;
812
808
  }
809
+ function isInOperator(value) {
810
+ return typeof value === "object" && value !== null && !Array.isArray(value) && "in" in value && Array.isArray(value.in);
811
+ }
812
+ function buildKeyConditionsAndParams(keyFields, keyValues, fieldConfigs) {
813
+ const conditions = [];
814
+ const params = [];
815
+ for (const key of keyFields) {
816
+ if (!Object.prototype.hasOwnProperty.call(keyValues, key)) continue;
817
+ const value = keyValues[key];
818
+ const config = fieldConfigs[key];
819
+ if (isInOperator(value)) {
820
+ const arr = value.in;
821
+ conditions.push(`${key} IN ?`);
822
+ const converted = arr.map(
823
+ (v) => config ? convertValueToCassandra(v, config) : v
824
+ );
825
+ params.push(converted);
826
+ } else {
827
+ conditions.push(`${key} = ?`);
828
+ params.push(config ? convertValueToCassandra(value, config) : value);
829
+ }
830
+ }
831
+ return { conditions, params };
832
+ }
813
833
  function buildSelectCql(schema, partitionKey, clusteringKey, limit) {
814
834
  const tableRef = schema.keyspace ? `${schema.keyspace}.${schema.tableName}` : schema.tableName;
815
- const conditions = [];
816
- Object.keys(partitionKey).forEach((key) => {
817
- conditions.push(`${key} = ?`);
818
- });
835
+ const { conditions: pkConditions } = buildKeyConditionsAndParams(
836
+ schema.partitionKeyFields,
837
+ partitionKey,
838
+ convertSchemaToFieldConfigs(schema)
839
+ );
840
+ const conditions = [...pkConditions];
819
841
  if (clusteringKey) {
820
- Object.keys(clusteringKey).forEach((key) => {
821
- conditions.push(`${key} = ?`);
822
- });
842
+ const ckFields = schema.clusteringKeyFields.map((ck) => ck.field);
843
+ const { conditions: ckConditions } = buildKeyConditionsAndParams(
844
+ ckFields,
845
+ clusteringKey,
846
+ convertSchemaToFieldConfigs(schema)
847
+ );
848
+ conditions.push(...ckConditions);
823
849
  }
824
850
  const whereClause = conditions.length > 0 ? ` WHERE ${conditions.join(" AND ")}` : "";
825
- const limitClause = limit ? ` LIMIT ${limit}` : "";
826
- const allowFiltering = conditions.length > 0 ? ` ALLOW FILTERING` : "";
851
+ const limitClause = limit != null ? ` LIMIT ${limit}` : "";
852
+ const hasIn = Object.values(partitionKey).some(isInOperator);
853
+ const allowFiltering = conditions.length > 0 && !hasIn ? ` ALLOW FILTERING` : "";
827
854
  return `SELECT * FROM ${tableRef}${whereClause}${limitClause}${allowFiltering};`;
828
855
  }
829
- function buildQueryParams(partitionKey, fieldConfigs, clusteringKey) {
856
+ function buildQueryParams(partitionKey, fieldConfigs, clusteringKey, schema) {
830
857
  const params = [];
831
- Object.entries(partitionKey).forEach(([fieldName, value]) => {
832
- const config = fieldConfigs[fieldName];
833
- if (config) {
834
- params.push(convertValueToCassandra(value, config));
835
- } else {
836
- params.push(value);
858
+ if (schema) {
859
+ const pk = buildKeyConditionsAndParams(
860
+ schema.partitionKeyFields,
861
+ partitionKey,
862
+ fieldConfigs
863
+ );
864
+ params.push(...pk.params);
865
+ if (clusteringKey) {
866
+ const ckFields = schema.clusteringKeyFields.map((c) => c.field);
867
+ const ck = buildKeyConditionsAndParams(
868
+ ckFields,
869
+ clusteringKey,
870
+ fieldConfigs
871
+ );
872
+ params.push(...ck.params);
837
873
  }
838
- });
839
- if (clusteringKey) {
840
- Object.entries(clusteringKey).forEach(([fieldName, value]) => {
874
+ } else {
875
+ Object.entries(partitionKey).forEach(([fieldName, value]) => {
841
876
  const config = fieldConfigs[fieldName];
842
- if (config) {
843
- params.push(convertValueToCassandra(value, config));
844
- } else {
845
- params.push(value);
846
- }
877
+ params.push(config ? convertValueToCassandra(value, config) : value);
847
878
  });
879
+ if (clusteringKey) {
880
+ Object.entries(clusteringKey).forEach(([fieldName, value]) => {
881
+ const config = fieldConfigs[fieldName];
882
+ params.push(config ? convertValueToCassandra(value, config) : value);
883
+ });
884
+ }
848
885
  }
849
886
  return params;
850
887
  }
@@ -2218,9 +2255,10 @@ var Model = class _Model {
2218
2255
  (key) => Object.prototype.hasOwnProperty.call(filters, key)
2219
2256
  );
2220
2257
  const hasClusteringKey = Object.keys(clusteringKey).length > 0;
2258
+ const noOtherFilters = Object.keys(otherFilters).length === 0;
2221
2259
  let selectQuery;
2222
2260
  let params;
2223
- if (isPartitionKeyQuery && (hasClusteringKey || Object.keys(otherFilters).length === 0)) {
2261
+ if (isPartitionKeyQuery && (hasClusteringKey || noOtherFilters)) {
2224
2262
  selectQuery = queryHelper.buildSelectCql(
2225
2263
  this.schema,
2226
2264
  partitionKey,
@@ -2230,7 +2268,8 @@ var Model = class _Model {
2230
2268
  params = queryHelper.buildQueryParams(
2231
2269
  partitionKey,
2232
2270
  fieldConfigs,
2233
- Object.keys(clusteringKey).length > 0 ? clusteringKey : void 0
2271
+ Object.keys(clusteringKey).length > 0 ? clusteringKey : void 0,
2272
+ this.schema
2234
2273
  );
2235
2274
  } else {
2236
2275
  selectQuery = queryHelper.buildSelectByIndexCql(this.schema, filters, limit);
@@ -2262,11 +2301,12 @@ var Model = class _Model {
2262
2301
  query,
2263
2302
  void 0,
2264
2303
  1
2265
- // 限制返回1条记录
2266
2304
  );
2267
2305
  params = queryHelper.buildQueryParams(
2268
2306
  query,
2269
- fieldConfigs
2307
+ fieldConfigs,
2308
+ void 0,
2309
+ this.schema
2270
2310
  );
2271
2311
  } else {
2272
2312
  selectQuery = queryHelper.buildSelectByIndexCql(
package/dist/mod.js CHANGED
@@ -665,11 +665,7 @@ function shouldWrite(op, conf) {
665
665
  // src/indexes.ts
666
666
  function createIndex(keyspace, tableName, fieldName, options) {
667
667
  const indexName = `${tableName}_${String(fieldName).toLowerCase()}_sai_idx`;
668
- const config = options !== true ? options : {
669
- case_sensitive: false,
670
- normalize: true,
671
- ascii: true
672
- };
668
+ const config = options !== true ? options : {};
673
669
  const optionsString = JSON.stringify(config).replace(/"/g, "'");
674
670
  return `CREATE INDEX IF NOT EXISTS ${indexName} ON ${keyspace}.${tableName} (${String(
675
671
  fieldName
@@ -808,41 +804,82 @@ function buildInsertParams(data, fieldConfigs) {
808
804
  });
809
805
  return params;
810
806
  }
807
+ function isInOperator(value) {
808
+ return typeof value === "object" && value !== null && !Array.isArray(value) && "in" in value && Array.isArray(value.in);
809
+ }
810
+ function buildKeyConditionsAndParams(keyFields, keyValues, fieldConfigs) {
811
+ const conditions = [];
812
+ const params = [];
813
+ for (const key of keyFields) {
814
+ if (!Object.prototype.hasOwnProperty.call(keyValues, key)) continue;
815
+ const value = keyValues[key];
816
+ const config = fieldConfigs[key];
817
+ if (isInOperator(value)) {
818
+ const arr = value.in;
819
+ conditions.push(`${key} IN ?`);
820
+ const converted = arr.map(
821
+ (v) => config ? convertValueToCassandra(v, config) : v
822
+ );
823
+ params.push(converted);
824
+ } else {
825
+ conditions.push(`${key} = ?`);
826
+ params.push(config ? convertValueToCassandra(value, config) : value);
827
+ }
828
+ }
829
+ return { conditions, params };
830
+ }
811
831
  function buildSelectCql(schema, partitionKey, clusteringKey, limit) {
812
832
  const tableRef = schema.keyspace ? `${schema.keyspace}.${schema.tableName}` : schema.tableName;
813
- const conditions = [];
814
- Object.keys(partitionKey).forEach((key) => {
815
- conditions.push(`${key} = ?`);
816
- });
833
+ const { conditions: pkConditions } = buildKeyConditionsAndParams(
834
+ schema.partitionKeyFields,
835
+ partitionKey,
836
+ convertSchemaToFieldConfigs(schema)
837
+ );
838
+ const conditions = [...pkConditions];
817
839
  if (clusteringKey) {
818
- Object.keys(clusteringKey).forEach((key) => {
819
- conditions.push(`${key} = ?`);
820
- });
840
+ const ckFields = schema.clusteringKeyFields.map((ck) => ck.field);
841
+ const { conditions: ckConditions } = buildKeyConditionsAndParams(
842
+ ckFields,
843
+ clusteringKey,
844
+ convertSchemaToFieldConfigs(schema)
845
+ );
846
+ conditions.push(...ckConditions);
821
847
  }
822
848
  const whereClause = conditions.length > 0 ? ` WHERE ${conditions.join(" AND ")}` : "";
823
- const limitClause = limit ? ` LIMIT ${limit}` : "";
824
- const allowFiltering = conditions.length > 0 ? ` ALLOW FILTERING` : "";
849
+ const limitClause = limit != null ? ` LIMIT ${limit}` : "";
850
+ const hasIn = Object.values(partitionKey).some(isInOperator);
851
+ const allowFiltering = conditions.length > 0 && !hasIn ? ` ALLOW FILTERING` : "";
825
852
  return `SELECT * FROM ${tableRef}${whereClause}${limitClause}${allowFiltering};`;
826
853
  }
827
- function buildQueryParams(partitionKey, fieldConfigs, clusteringKey) {
854
+ function buildQueryParams(partitionKey, fieldConfigs, clusteringKey, schema) {
828
855
  const params = [];
829
- Object.entries(partitionKey).forEach(([fieldName, value]) => {
830
- const config = fieldConfigs[fieldName];
831
- if (config) {
832
- params.push(convertValueToCassandra(value, config));
833
- } else {
834
- params.push(value);
856
+ if (schema) {
857
+ const pk = buildKeyConditionsAndParams(
858
+ schema.partitionKeyFields,
859
+ partitionKey,
860
+ fieldConfigs
861
+ );
862
+ params.push(...pk.params);
863
+ if (clusteringKey) {
864
+ const ckFields = schema.clusteringKeyFields.map((c) => c.field);
865
+ const ck = buildKeyConditionsAndParams(
866
+ ckFields,
867
+ clusteringKey,
868
+ fieldConfigs
869
+ );
870
+ params.push(...ck.params);
835
871
  }
836
- });
837
- if (clusteringKey) {
838
- Object.entries(clusteringKey).forEach(([fieldName, value]) => {
872
+ } else {
873
+ Object.entries(partitionKey).forEach(([fieldName, value]) => {
839
874
  const config = fieldConfigs[fieldName];
840
- if (config) {
841
- params.push(convertValueToCassandra(value, config));
842
- } else {
843
- params.push(value);
844
- }
875
+ params.push(config ? convertValueToCassandra(value, config) : value);
845
876
  });
877
+ if (clusteringKey) {
878
+ Object.entries(clusteringKey).forEach(([fieldName, value]) => {
879
+ const config = fieldConfigs[fieldName];
880
+ params.push(config ? convertValueToCassandra(value, config) : value);
881
+ });
882
+ }
846
883
  }
847
884
  return params;
848
885
  }
@@ -2216,9 +2253,10 @@ var Model = class _Model {
2216
2253
  (key) => Object.prototype.hasOwnProperty.call(filters, key)
2217
2254
  );
2218
2255
  const hasClusteringKey = Object.keys(clusteringKey).length > 0;
2256
+ const noOtherFilters = Object.keys(otherFilters).length === 0;
2219
2257
  let selectQuery;
2220
2258
  let params;
2221
- if (isPartitionKeyQuery && (hasClusteringKey || Object.keys(otherFilters).length === 0)) {
2259
+ if (isPartitionKeyQuery && (hasClusteringKey || noOtherFilters)) {
2222
2260
  selectQuery = queryHelper.buildSelectCql(
2223
2261
  this.schema,
2224
2262
  partitionKey,
@@ -2228,7 +2266,8 @@ var Model = class _Model {
2228
2266
  params = queryHelper.buildQueryParams(
2229
2267
  partitionKey,
2230
2268
  fieldConfigs,
2231
- Object.keys(clusteringKey).length > 0 ? clusteringKey : void 0
2269
+ Object.keys(clusteringKey).length > 0 ? clusteringKey : void 0,
2270
+ this.schema
2232
2271
  );
2233
2272
  } else {
2234
2273
  selectQuery = queryHelper.buildSelectByIndexCql(this.schema, filters, limit);
@@ -2260,11 +2299,12 @@ var Model = class _Model {
2260
2299
  query,
2261
2300
  void 0,
2262
2301
  1
2263
- // 限制返回1条记录
2264
2302
  );
2265
2303
  params = queryHelper.buildQueryParams(
2266
2304
  query,
2267
- fieldConfigs
2305
+ fieldConfigs,
2306
+ void 0,
2307
+ this.schema
2268
2308
  );
2269
2309
  } else {
2270
2310
  selectQuery = queryHelper.buildSelectByIndexCql(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imean-cassandra-orm",
3
- "version": "3.3.0",
3
+ "version": "3.3.2",
4
4
  "description": "cassandra orm",
5
5
  "keywords": [
6
6
  "cassandra",
@@ -26,12 +26,6 @@
26
26
  "README.md",
27
27
  "LICENSE"
28
28
  ],
29
- "scripts": {
30
- "dev": "tsx watch dev.ts",
31
- "build": "tsup",
32
- "test": "vitest run",
33
- "prepublishOnly": "npm run build && npm run test"
34
- },
35
29
  "dependencies": {
36
30
  "@clickhouse/client": "^1.12.1"
37
31
  },
@@ -64,5 +58,10 @@
64
58
  },
65
59
  "engines": {
66
60
  "node": ">=20"
61
+ },
62
+ "scripts": {
63
+ "dev": "tsx watch dev.ts",
64
+ "build": "tsup",
65
+ "test": "vitest run"
67
66
  }
68
- }
67
+ }