export const queryBuilderCacheStatementHandler = (statements, done) => {
  const pathsBatch = QueryBuilderCache.collection.initializeUnorderedBulkOp();
  const valuesBatch = QueryBuilderCacheValue.collection.initializeUnorderedBulkOp();

  if (isArray(statements)) {
    each(statements, handleStatement.bind(null, pathsBatch, valuesBatch));
  } else {
    handleStatement(pathsBatch, valuesBatch, statements);
  }

  return promiseResolver(pathsBatch, valuesBatch, done);
};
Beispiel #2
0
export default async (model, organisation) => {
  const pathsBatch = QueryBuilderCache.collection.initializeUnorderedBulkOp();
  const valuesBatch = QueryBuilderCacheValue.collection.initializeUnorderedBulkOp();
  const caches = getCachesFromStatement(model);
  saveCachePaths(caches, organisation, pathsBatch);
  saveCacheValues(caches, organisation, valuesBatch);
  try {
    await Promise.all([
      pathsBatch.execute({ w: 0 }), // write concern set to 0 to supress warnings of duplicate key errors
      valuesBatch.execute({ w: 0 }) //  these are expected, letting mongo assert the uniqueness is the fastest way
    ]);
  } catch (err) {
    logger.error(err);
  }
};
const saveBatch = (queryBuilderCaches) => {
  const batch = querybuildercache.collection.initializeUnorderedBulkOp();
  try {
    each(queryBuilderCaches, (item) => {
      batch.insert(item);
    });
  } catch (ex) {
    console.error(ex);
  }
  const promise = batch
    .execute()
    .then((res) => {
      logger.info(`Completed batch of ${queryBuilderCaches.length}.`);
      return res;
    })
    .catch((error) => {
      logger.info(
        'Duplicate keys detected. May indicate that thsis migration has already been run.',
        error.message
      );
    });

  return highland(promise);
};
const updateQueryBuliderCache = async ({
  attributes,
  organisation
}) => {
  const orgId = new ObjectID(organisation);
  const createdAt = new Date();
  const updatedAt = createdAt;

  const pathBatch = QueryBuilderCache.collection.initializeUnorderedBulkOp();

  map(attributes, ((attribute) => {
    const doc = {
      organisation: orgId,
      path: [
        'persona',
        'import',
        attribute.key
      ],
      searchString: `persona.import.${attribute.key}`,
      valueType: 'String',
      createdAt,
      updatedAt
    };

    pathBatch.insert(doc);
  }));

  // --------------------

  const valueBatch = QueryBuilderCacheValue.collection.initializeUnorderedBulkOp();

  map(attributes, (attribute) => {
    const stringifyValue = JSON.stringify(attribute.value);
    const hash = stringifyValue.length > 40 ? sha1(stringifyValue) : stringifyValue;

    const doc = ({
      organisation: orgId,
      path: `persona.import.${attribute.key}`,
      hash,
      value: attribute.value,
      display: attribute.value,
      searchString: attribute.value,
      valueType: 'String',
      createdAt,
      updatedAt
    });
    valueBatch.insert(doc);
  });

  const batches = [];
  if (
    valueBatch && valueBatch.s && valueBatch.s.currentBatch
    && valueBatch.s.currentBatch.operations
    && valueBatch.s.currentBatch.operations.length > 0
  ) {
    batches.push(valueBatch);
  }

  if (
    pathBatch && pathBatch.s && pathBatch.s.currentBatch
    && pathBatch.s.currentBatch.operations
    && pathBatch.s.currentBatch.operations.length > 0
  ) {
    batches.push(pathBatch);
  }

  await Promise.all([map(batches, batch => batch.execute({ w: 0 }))]);
};