function interpretCountArgs()

in packages/sqrl-redis-functions/src/CountFunctions.ts [114:174]


function interpretCountArgs(
  state: CompileState,
  sourceAst: Ast,
  args: CountArguments
) {
  const { whereAst, whereFeatures, whereTruth } = state.combineGlobalWhere(
    args.where
  );

  const counterProps: {
    features: string[];
    whereFeatures?: string[];
    whereTruth?: string;
    sumFeature?: string;
  } = {
    features: args.features.map((feature: AliasedFeature) => feature.alias),
    whereFeatures,
    whereTruth,
  };

  // Include sumFeature in the key if provided - otherwise we will
  // just bump by 1 so leave it out of key.
  let bumpByAst: Ast = AstBuilder.constant(1);
  if (args.sumFeature) {
    counterProps.sumFeature = args.sumFeature.value;
    bumpByAst = AstBuilder.call("_getBumpBy", [args.sumFeature]);
  }

  const { entityAst, entityId } = state.addHashedEntity(
    sourceAst,
    ENTITY_TYPE,
    counterProps
  );

  const featuresAst = args.features.map((aliasFeature) =>
    AstBuilder.feature(aliasFeature.feature.value)
  );
  const featureString = featuresAst.map((ast) => ast.value).join("~");
  const keyedCounterName = `${entityId.getIdString()}~${featureString}`;
  const keysAst = state.setGlobal(
    sourceAst,
    AstBuilder.call("_getKeyList", [entityAst, ...featuresAst]),
    `key(${keyedCounterName})`
  );
  const hasAlias = args.features.some(
    (featureAst: AliasedFeature) =>
      featureAst.feature.value !== featureAst.alias
  );

  return {
    bumpByAst,
    hasAlias,
    keyedCounterName,
    keysAst,
    entityAst,
    entityId,
    whereAst,
    whereFeatures,
    whereTruth,
  };
}