mongo2crate
Version:
Sync MongoDB to CrateDB and Convert JSON schema to SQL DDL
55 lines (54 loc) • 1.49 kB
JavaScript
import _ from 'lodash/fp.js';
/**
* Does arr start with startsWith array.
*/
export const arrayStartsWith = (arr, startsWith) => {
for (let i = 0; i < startsWith.length; i++) {
if (arr[i] !== startsWith[i]) {
return false;
}
}
return true;
};
export const setDefaults = (keys, val) => {
const obj = {};
for (const key of keys) {
obj[key] = val;
}
return obj;
};
export const sumByRowcount = (num) => _.sumBy(({ rowcount }) => (rowcount === num ? 1 : 0));
/**
* Get the document ids that failed to be written during a bulk
* query.
*/
export const getFailedRecords = (results, documents) => {
const failed = [];
for (let i = 0; i < results.length; i++) {
const result = results[i];
if (result.rowcount === -2) {
failed.push(documents[i].documentKey._id);
}
}
return failed;
};
export const partitionEvents = (docs) => {
const groups = [];
let subGroup = [];
let previousOperationType = undefined;
for (const doc of docs) {
const operationType = doc.operationType;
if (operationType !== 'insert' || previousOperationType !== operationType) {
if (subGroup.length) {
groups.push(subGroup);
}
subGroup = [];
}
subGroup.push(doc);
previousOperationType = operationType;
}
if (subGroup.length) {
groups.push(subGroup);
}
return groups;
};