Skip to content
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

Convenience getType functions, bulk load function, addOptions function #49

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
Adding load function to load bulk records at once
  • Loading branch information
patakijv committed Sep 22, 2016
commit b3faab9f7729f5230c43fda23f8af72c3182145c
34 changes: 34 additions & 0 deletions joli.js
Expand Up @@ -377,6 +377,40 @@ var joliCreator = function() {

return q.execute();
},
// load function for bulk loading many records at once and
// is optimizable with transaction via useTransaction flag (consider as default?)
load: function(recordsDataArray, options, callback) {
var transaction = false;

if (options && options.purgeFirst) {
this.truncate();
}

// use a transaction around the collection of queries to optimize large record set
if (options && options.useTransaction) {
transaction = new joli.transaction("load")
transaction.begin();
}

// determine if the record exists already and if we need to create it or update it
joli.each(recordsDataArray, function(recordData, index) {
var record = false;
if (record = this.findOneById(recordData["id"])) {
record.fromArray(recordData);
} else {
record = this.newRecord(recordData);
}
(record) && (record.save());
}, this);

if (transaction) {
transaction.commit();
}

if (joli.isFunction(callback)) {
callback();
}
},
truncate: function() {
new joli.query().destroy().from(this.table).execute();
}
Expand Down