imean-cassandra-orm 3.3.1 → 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 +72 -28
  2. package/dist/mod.js +72 -28
  3. package/package.json +7 -8
package/dist/mod.cjs CHANGED
@@ -806,41 +806,82 @@ function buildInsertParams(data, fieldConfigs) {
806
806
  });
807
807
  return params;
808
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
+ }
809
833
  function buildSelectCql(schema, partitionKey, clusteringKey, limit) {
810
834
  const tableRef = schema.keyspace ? `${schema.keyspace}.${schema.tableName}` : schema.tableName;
811
- const conditions = [];
812
- Object.keys(partitionKey).forEach((key) => {
813
- conditions.push(`${key} = ?`);
814
- });
835
+ const { conditions: pkConditions } = buildKeyConditionsAndParams(
836
+ schema.partitionKeyFields,
837
+ partitionKey,
838
+ convertSchemaToFieldConfigs(schema)
839
+ );
840
+ const conditions = [...pkConditions];
815
841
  if (clusteringKey) {
816
- Object.keys(clusteringKey).forEach((key) => {
817
- conditions.push(`${key} = ?`);
818
- });
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);
819
849
  }
820
850
  const whereClause = conditions.length > 0 ? ` WHERE ${conditions.join(" AND ")}` : "";
821
- const limitClause = limit ? ` LIMIT ${limit}` : "";
822
- 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` : "";
823
854
  return `SELECT * FROM ${tableRef}${whereClause}${limitClause}${allowFiltering};`;
824
855
  }
825
- function buildQueryParams(partitionKey, fieldConfigs, clusteringKey) {
856
+ function buildQueryParams(partitionKey, fieldConfigs, clusteringKey, schema) {
826
857
  const params = [];
827
- Object.entries(partitionKey).forEach(([fieldName, value]) => {
828
- const config = fieldConfigs[fieldName];
829
- if (config) {
830
- params.push(convertValueToCassandra(value, config));
831
- } else {
832
- 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);
833
873
  }
834
- });
835
- if (clusteringKey) {
836
- Object.entries(clusteringKey).forEach(([fieldName, value]) => {
874
+ } else {
875
+ Object.entries(partitionKey).forEach(([fieldName, value]) => {
837
876
  const config = fieldConfigs[fieldName];
838
- if (config) {
839
- params.push(convertValueToCassandra(value, config));
840
- } else {
841
- params.push(value);
842
- }
877
+ params.push(config ? convertValueToCassandra(value, config) : value);
843
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
+ }
844
885
  }
845
886
  return params;
846
887
  }
@@ -2214,9 +2255,10 @@ var Model = class _Model {
2214
2255
  (key) => Object.prototype.hasOwnProperty.call(filters, key)
2215
2256
  );
2216
2257
  const hasClusteringKey = Object.keys(clusteringKey).length > 0;
2258
+ const noOtherFilters = Object.keys(otherFilters).length === 0;
2217
2259
  let selectQuery;
2218
2260
  let params;
2219
- if (isPartitionKeyQuery && (hasClusteringKey || Object.keys(otherFilters).length === 0)) {
2261
+ if (isPartitionKeyQuery && (hasClusteringKey || noOtherFilters)) {
2220
2262
  selectQuery = queryHelper.buildSelectCql(
2221
2263
  this.schema,
2222
2264
  partitionKey,
@@ -2226,7 +2268,8 @@ var Model = class _Model {
2226
2268
  params = queryHelper.buildQueryParams(
2227
2269
  partitionKey,
2228
2270
  fieldConfigs,
2229
- Object.keys(clusteringKey).length > 0 ? clusteringKey : void 0
2271
+ Object.keys(clusteringKey).length > 0 ? clusteringKey : void 0,
2272
+ this.schema
2230
2273
  );
2231
2274
  } else {
2232
2275
  selectQuery = queryHelper.buildSelectByIndexCql(this.schema, filters, limit);
@@ -2258,11 +2301,12 @@ var Model = class _Model {
2258
2301
  query,
2259
2302
  void 0,
2260
2303
  1
2261
- // 限制返回1条记录
2262
2304
  );
2263
2305
  params = queryHelper.buildQueryParams(
2264
2306
  query,
2265
- fieldConfigs
2307
+ fieldConfigs,
2308
+ void 0,
2309
+ this.schema
2266
2310
  );
2267
2311
  } else {
2268
2312
  selectQuery = queryHelper.buildSelectByIndexCql(
package/dist/mod.js CHANGED
@@ -804,41 +804,82 @@ function buildInsertParams(data, fieldConfigs) {
804
804
  });
805
805
  return params;
806
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
+ }
807
831
  function buildSelectCql(schema, partitionKey, clusteringKey, limit) {
808
832
  const tableRef = schema.keyspace ? `${schema.keyspace}.${schema.tableName}` : schema.tableName;
809
- const conditions = [];
810
- Object.keys(partitionKey).forEach((key) => {
811
- conditions.push(`${key} = ?`);
812
- });
833
+ const { conditions: pkConditions } = buildKeyConditionsAndParams(
834
+ schema.partitionKeyFields,
835
+ partitionKey,
836
+ convertSchemaToFieldConfigs(schema)
837
+ );
838
+ const conditions = [...pkConditions];
813
839
  if (clusteringKey) {
814
- Object.keys(clusteringKey).forEach((key) => {
815
- conditions.push(`${key} = ?`);
816
- });
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);
817
847
  }
818
848
  const whereClause = conditions.length > 0 ? ` WHERE ${conditions.join(" AND ")}` : "";
819
- const limitClause = limit ? ` LIMIT ${limit}` : "";
820
- 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` : "";
821
852
  return `SELECT * FROM ${tableRef}${whereClause}${limitClause}${allowFiltering};`;
822
853
  }
823
- function buildQueryParams(partitionKey, fieldConfigs, clusteringKey) {
854
+ function buildQueryParams(partitionKey, fieldConfigs, clusteringKey, schema) {
824
855
  const params = [];
825
- Object.entries(partitionKey).forEach(([fieldName, value]) => {
826
- const config = fieldConfigs[fieldName];
827
- if (config) {
828
- params.push(convertValueToCassandra(value, config));
829
- } else {
830
- 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);
831
871
  }
832
- });
833
- if (clusteringKey) {
834
- Object.entries(clusteringKey).forEach(([fieldName, value]) => {
872
+ } else {
873
+ Object.entries(partitionKey).forEach(([fieldName, value]) => {
835
874
  const config = fieldConfigs[fieldName];
836
- if (config) {
837
- params.push(convertValueToCassandra(value, config));
838
- } else {
839
- params.push(value);
840
- }
875
+ params.push(config ? convertValueToCassandra(value, config) : value);
841
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
+ }
842
883
  }
843
884
  return params;
844
885
  }
@@ -2212,9 +2253,10 @@ var Model = class _Model {
2212
2253
  (key) => Object.prototype.hasOwnProperty.call(filters, key)
2213
2254
  );
2214
2255
  const hasClusteringKey = Object.keys(clusteringKey).length > 0;
2256
+ const noOtherFilters = Object.keys(otherFilters).length === 0;
2215
2257
  let selectQuery;
2216
2258
  let params;
2217
- if (isPartitionKeyQuery && (hasClusteringKey || Object.keys(otherFilters).length === 0)) {
2259
+ if (isPartitionKeyQuery && (hasClusteringKey || noOtherFilters)) {
2218
2260
  selectQuery = queryHelper.buildSelectCql(
2219
2261
  this.schema,
2220
2262
  partitionKey,
@@ -2224,7 +2266,8 @@ var Model = class _Model {
2224
2266
  params = queryHelper.buildQueryParams(
2225
2267
  partitionKey,
2226
2268
  fieldConfigs,
2227
- Object.keys(clusteringKey).length > 0 ? clusteringKey : void 0
2269
+ Object.keys(clusteringKey).length > 0 ? clusteringKey : void 0,
2270
+ this.schema
2228
2271
  );
2229
2272
  } else {
2230
2273
  selectQuery = queryHelper.buildSelectByIndexCql(this.schema, filters, limit);
@@ -2256,11 +2299,12 @@ var Model = class _Model {
2256
2299
  query,
2257
2300
  void 0,
2258
2301
  1
2259
- // 限制返回1条记录
2260
2302
  );
2261
2303
  params = queryHelper.buildQueryParams(
2262
2304
  query,
2263
- fieldConfigs
2305
+ fieldConfigs,
2306
+ void 0,
2307
+ this.schema
2264
2308
  );
2265
2309
  } else {
2266
2310
  selectQuery = queryHelper.buildSelectByIndexCql(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imean-cassandra-orm",
3
- "version": "3.3.1",
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
+ }