-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add update and delete functionality #8
base: master
Are you sure you want to change the base?
Changes from all commits
4270637
aede49a
9eebe6a
fc4db3c
5b9f052
5a263fd
d002f8e
8782691
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,50 +7,95 @@ module.exports = { | |
// default config | ||
{ | ||
batchSize: 1, | ||
insertOptions: { w: 1 }, | ||
operationType: 'insert' | ||
}, | ||
// overrided options | ||
options, | ||
// override config | ||
options | ||
); | ||
|
||
// those variables can't be initialized without Promises, so we wait first drain | ||
// These variables can't be initialized without Promises, so we wait for the first drain | ||
let dbConnection; | ||
let collection; | ||
let records = []; | ||
|
||
// this function is usefull to insert records and reset the records array | ||
const insert = async () => { | ||
await collection.insert(records, config.insertOptions); | ||
records = []; | ||
// Supported operations | ||
const operations = { | ||
insert: async () => { | ||
return collection.insertMany(records); | ||
}, | ||
update: async () => { | ||
// make sure we have some index so we can select the correct record to update | ||
if (typeof config.indexName === 'undefined') { | ||
return Promise.reject(Error('Operation type was update, but no index was provided.')); | ||
} | ||
|
||
// update each record in tandem. | ||
return Promise.all(records.map(doc => | ||
collection.updateMany({ [config.indexName]: doc[config.indexName] }, { $set: doc }))); | ||
}, | ||
delete: async () => { | ||
// make sure we have some index so we can select the correct record to update | ||
if (typeof config.indexName === 'undefined') { | ||
return Promise.reject(Error('Operation type was delete, but no index was provided.')); | ||
} | ||
|
||
// update each record in tandem. | ||
return Promise.all(records.map(doc => | ||
collection.deleteMany({ [config.indexName]: doc[config.indexName] }, { $set: doc }))); | ||
}, | ||
invalid: () => { return Promise.reject(Error(`Invalid operation type: ${config.operationType}`)); } | ||
}; | ||
|
||
// Utility for writing to the db with the correct operation | ||
const writeToDB = async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could cache this function result before calling it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Plus the error about invalid operation will be triggered before the stream is used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I speak about which operation to choose (which callback) |
||
if (Object.keys(operations).includes(config.operationType)) { | ||
try { | ||
return operations[config.operationType](); | ||
} catch (err) { | ||
return operations.invalid(); | ||
} | ||
} else { | ||
// TODO: add some kind of error message for invalid operations | ||
return operations.invalid(); | ||
} | ||
}; | ||
|
||
// stream | ||
const writable = new Writable({ | ||
objectMode: true, | ||
write: async (record, encoding, next) => { | ||
// try/catch for initialization | ||
try { | ||
// connection | ||
if (!dbConnection) dbConnection = await MongoClient.connect(config.dbURL); | ||
if (!collection) collection = await dbConnection.collection(config.collection); | ||
} catch (err) { | ||
if (dbConnection) await dbConnection.close(); | ||
writable.emit('Error on db init', err); | ||
} | ||
|
||
// try/catch for write operations | ||
try { | ||
// add to batch records | ||
records.push(record); | ||
|
||
// insert and reset batch recors | ||
if (records.length >= config.batchSize) await insert(); | ||
// write and reset batch records | ||
if (records.length >= config.batchSize) { | ||
await writeToDB(); | ||
records = []; | ||
} | ||
|
||
// next stream | ||
next(); | ||
} catch (error) { | ||
if (dbConnection) await dbConnection.close(); | ||
writable.emit('error', error); | ||
writable.emit('Error on data write', error); | ||
} | ||
} | ||
}); | ||
|
||
writable.on('finish', async () => { | ||
try { | ||
if (records.length > 0) await insert(); | ||
if (records.length > 0) await writeToDB(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll fell more confortable if you reset records here too. |
||
if (dbConnection) await dbConnection.close(); | ||
|
||
writable.emit('close'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I have the feeling you break the API for some users.
Maybe we should keep
insertOptions
for now, and use it intooperation.insert
?