added plugins

This commit is contained in:
Jan Prochazka
2021-04-13 16:17:53 +02:00
parent 446e7c139f
commit 4d5cc119f2
115 changed files with 5519 additions and 24 deletions

View File

@@ -0,0 +1,58 @@
export function createBulkInsertStream(driver, stream, pool, name, options) {
const collectionName = name.pureName;
const db = pool.__getDatabase();
const writable = new stream.Writable({
objectMode: true,
});
writable.buffer = [];
writable.wasHeader = false;
writable.addRow = (row) => {
if (!writable.wasHeader) {
writable.wasHeader = true;
if (row.__isStreamHeader ||
// TODO remove isArray test
Array.isArray(row.columns)) return;
}
writable.buffer.push(row);
};
writable.checkStructure = async () => {
if (options.dropIfExists || options.truncate) {
console.log(`Dropping collection ${collectionName}`);
await db.collection(collectionName).drop();
}
if (options.truncate) {
console.log(`Truncating collection ${collectionName}`);
await db.collection(collectionName).deleteMany({});
}
};
writable.send = async () => {
const rows = writable.buffer;
writable.buffer = [];
await db.collection(collectionName).insertMany(rows);
};
writable.sendIfFull = async () => {
if (writable.buffer.length > 100) {
await writable.send();
}
};
writable._write = async (chunk, encoding, callback) => {
writable.addRow(chunk);
await writable.sendIfFull();
callback();
};
writable._final = async (callback) => {
await writable.send();
callback();
};
return writable;
}