谢谢尼古拉斯。最后我已经实现了一些非常相似的东西:
function updatePeople(){
for(var j in peopleList){
// Check if the record in the peopleList is newer than the last database (not record) update.
var personUpdated=peopleList[j].getLastUpdated().getTime();
var dbUpdated= db.query({Type:"dbUpdated"}).next().dbUpdated;
// If the record is newer than the last database update...
if(personUpdated>dbUpdated)
{
// ...check if the record exists in the database (check for current record using Is_Current = 1)
// If it does, set the Valid_To date on the old record to be 1 second before the Valid_From date of the new record.
var result = db.query({Type:"Person", Initials: peopleList[j].getValueByName("Initials"), Is_Current:1}).getSize();
if(result>0){
var oldPerson = db.query({Type:"Person", Initials: peopleList[j].getValueByName("Initials"), Is_Current:1}).next();
var validTo = personUpdated-1000;
oldPerson.Valid_To = validTo;
oldPerson.Is_Current = 0;
db.save(oldPerson);
// now add a new Person record.
addPerson(j);
}
else {
addPerson(j);
}
}
}
updateDbUpdatedDate();
}
addPerson() 函数是一个非常简单的添加新记录的函数。
function addPerson(j){
var person={};
person["Valid_From"]=peopleList[j].getLastUpdated().getTime();
person["Valid_To"]=9999999999999;
person["Is_Current"]=1;
person["Type"]="Person";
person["Initials"]= peopleList[j].getValueByName("Initials");
person["Name"]=peopleList[j].getValueByName("Name");
person["Email_Address"]= peopleList[j].getValueByName("Email Address");
person["Team"]=peopleList[j].getValueByName("Team");
person["Grade"]=peopleList[j].getValueByName("Grade");
person["Admin"]=peopleList[j].getValueByName("Admin");
db.save(person);
}