如何避免ScriptDb中的重复?

时间:2013-01-31 13:12:50

标签: database google-apps-script javascript-objects

我有一些要保存在数据库中的对象,但每次执行脚本时它们都必须是唯一的。这些对象只是为应用程序的每个实例更改值。问题是,当我创建第一个对象时,每次在此之后执行脚本时也会创建更多副本。我尝试再次使用相同的对象,但是当我执行查询时,我从先前的执行中获取对象,从而给出了错误的值。我只是希望能够将相同的对象重置为其默认值,并且在ScriptDb中没有多少不必要的副本。

以下是一些代码:

// Get a database instance
var database = getDb();

// Create a new employee instance
var myEmployee = {
element: "currentEmployee",
firstName: "",
lastName: "",
ID: 0,
manager: "",
managerEmail: "",
department: "1 - University Store",
startDate: "",
endDate: "",
regularHours: 0,
vacationHours: 0,
sickHours: 0,
personalHours: 0,
H2Hours: 0,
overtimeHours: 0,
totalHours: 0,
rowLocation: 0,
salaryType: "H",
};

var week1 = {
// Identify the week and for query purposes 
element: "Week1",

// User entries
HW: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
VH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
SH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
PH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
TH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},

// Calculated totals
TotalHW:0, TotalVH: 0, TotalSH: 0, TotalPH: 0, TotalHours: 0,

// Week totals
Vacation: 0, Sick: 0, Personal: 0, H2: 0, OT: 0, Regular: 0,
}

var week2 = {
// Identify the week and for query purposes 
element: "Week2",

// User entries
HW: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
VH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
SH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
PH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
TH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},

// Calculated totals
TotalHW:0, TotalVH: 0, TotalSH: 0, TotalPH: 0, TotalHours: 0,

// Week totals
Vacation: 0, Sick: 0, Personal: 0, H2: 0, OT: 0, Regular: 0,
}

// Save these to the database
database.save(myEmployee);
database.save(week1);
database.save(week2);

然后,我将使用语句ScriptDb.getMyDb.query({element: element}).next()来检索对象。包含“currentEmployee”,“Week1”或“Week2”的字符串元素。

1 个答案:

答案 0 :(得分:1)

为避免在ScriptDb中重新创建唯一键控的对象,您必须首先检查您正在考虑保存的对象是否已存在。如果是,则需要获取存储对象的句柄以用于更新。如果它不存在,您将save()新对象,返回值将是数据库中副本的句柄。

一般模式是:

// Get a database instance
var db = getDb();

var result = db.query( {key:keyval, ...} );
if (result.hasNext()) {
  // The object already exists in ScriptDb
  var obj = result.next()
}
else {
  // The object doesn't exist in ScriptDb, so create it
  // This example would start by creating only the key values,
  // expanding the object during the update stage below.
  // Alternatively, you could create the entire object with all
  // its properties.
  obj = db.save({key:keyval, ...})
}

// Can now update the obj that is in ScriptDb
obj.prop1 = value1;
obj.prop2 = value2;
...

// Save updated object
db.save(obj);

以下是该模式如何应用于您的代码的示例。您将在此处看到正在使用创建新对象的替代方法,其中新对象在ScriptDb中一次创建,并且具有所有属性。

// Get a database instance
var database = getDb();

// Find or create employee instance
var result = database.query({element: "currentEmployee"});
if (result.hasNext()) {
  var myEmployee = result.next();
} else {
  myEmployee = database.save({
    element: "currentEmployee",
    firstName: "",
    lastName: "",
    ID: 0,
    manager: "",
    managerEmail: "",
    department: "1 - University Store",
    startDate: "",
    endDate: "",
    regularHours: 0,
    vacationHours: 0,
    sickHours: 0,
    personalHours: 0,
    H2Hours: 0,
    overtimeHours: 0,
    totalHours: 0,
    rowLocation: 0,
    salaryType: "H"
  });
};

// Find or create Week1 instance
result = database.query({element: "Week1"});
if (result.hasNext()) {
  var week1 = result.next();
} else {
  week1 = database.save({
    // Identify the week and for query purposes 
    element: "Week1",

    // User entries
    HW: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0},
    VH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0},
    SH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0},
    PH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0},
    TH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0},

    // Calculated totals
    TotalHW: 0, TotalVH: 0, TotalSH: 0, TotalPH: 0, TotalHours: 0,

    // Week totals
    Vacation: 0, Sick: 0, Personal: 0, H2: 0, OT: 0, Regular: 0,
  });
}

// Find or create Week2 instance
result = database.query({element: "Week2"});
if (result.hasNext()) {
  var week2 = result.next();
} else {
  week2 = database.save({
    // Identify the week and for query purposes 
    element: "Week2",

    // User entries
    HW: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0},
    VH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0},
    SH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0},
    PH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0},
    TH: {THUR: 0,FRI: 0,SAT: 0,SUN: 0,MON: 0,TUES: 0,WED: 0},

    // Calculated totals
    TotalHW: 0, TotalVH: 0, TotalSH: 0, TotalPH: 0, TotalHours: 0,

    // Week totals
    Vacation: 0, Sick: 0, Personal: 0, H2: 0, OT: 0, Regular: 0,
  });
}

// At this point, myEmployee, week1 and week2 refer to the ScriptDb objects.
// While we can use them as any javascript object, they also contain information
// relevant to their state as ScriptDb objects.
...