Last active
July 11, 2020 14:07
-
-
Save aaronksaunders/5066608 to your computer and use it in GitHub Desktop.
One way to do bulk updates and deletes with Appcelerator Alloy Collections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add all items to collection | |
Alloy.Collections.Fugitive.reset([{ | |
"name" : "Jeff Haynie" | |
}, { | |
"name" : "Nolan Wright" | |
}, { | |
"name" : "Don Thorp" | |
}, { | |
"name" : "Marshall Culpepper" | |
}, { | |
"name" : "Blain Hamon" | |
}]); | |
// save all the items | |
Alloy.Collections.Fugitive.saveAll(); | |
// get the collection object | |
Alloy.Collections.instance("Fugitive"); | |
// delete all items | |
Alloy.Collections.Fugitive.deleteAll(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.definition = { | |
config : { | |
"columns" : { | |
"name" : "TEXT", | |
"captured" : "integer", | |
"url" : "TEXT", | |
"capturedLat" : "real", | |
"capturedLong" : "real" | |
}, | |
"defaults" : { | |
"name" : "", | |
"captured" : 0, | |
"url" : "", | |
"capturedLat" : "", | |
"capturedLong" : "" | |
}, | |
"adapter" : { | |
"type" : "sql", | |
"collection_name" : "fugitives" | |
} | |
}, | |
extendModel : function(Model) { | |
_.extend(Model.prototype, { | |
}); | |
// end extend | |
return Model; | |
}, | |
extendCollection : function(Collection) { | |
// helper functions | |
function S4() { | |
return (0 | 65536 * (1 + Math.random())).toString(16).substring(1); | |
} | |
function guid() { | |
return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4(); | |
} | |
_.extend(Collection.prototype, { | |
deleteAll : function() { | |
var collection = this; | |
var sql = "DELETE FROM " + collection.config.adapter.collection_name; | |
db = Ti.Database.open(collection.config.adapter.db_name); | |
db.execute(sql); | |
db.close(); | |
collection.trigger('sync'); | |
}, | |
saveAll : function() { | |
var collection = this; | |
var dbName = collection.config.adapter.db_name; | |
var table = collection.config.adapter.collection_name; | |
var columns = collection.config.columns; | |
db = Ti.Database.open(dbName); | |
db.execute("BEGIN;"); | |
collection.each(function(model) { | |
if (!model.id) { | |
model.id = guid(); | |
model.attributes[model.idAttribute] = model.id; | |
} | |
var names = [], values = [], q = []; | |
for (var k in columns) { | |
names.push(k); | |
values.push(model.get(k)); | |
q.push("?"); | |
} | |
var sqlInsert = "INSERT INTO " + table + " (" + names.join(",") + ") VALUES (" + q.join(",") + ");"; | |
db.execute(sqlInsert, values); | |
}); | |
db.execute("COMMIT;"); | |
db.close(); | |
collection.trigger('sync'); | |
} | |
}); | |
// end extend | |
return Collection; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@aaronksaunders I'm using this SaveAll function in my model.
I have this config:
config: {
columns: {
"jobID": "INTEGER",
"jobTitle": "TEXT",
"joblastUpdated": "TEXT",
"jobLastDayOfService": "TEXT",
"jobInstructionReported": "INTEGER",
"jobInstructions": "TEXT",
"noReports": "INTEGER",
"instructionId": "INTEGER"
},
adapter: {
type: "sql",
collection_name: "jobs",
db_name: "DB",
idAttribute:'jobID'
}
},
jobID is unique and i'm receiving this id from the backed same as other values.
Insert is working fine but i can't make insert or replace to work:
var sqlInsert = "INSERT OR REPLACE INTO " + table + " (" + names.join(",") + ") VALUES (" + q.join(",") + ");";
db.execute(sqlInsert, values);