Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To avoid re-creating <em>uniquely-keyed</em> objects in ScriptDb, you must first check whether the object you're thinking of saving is already there. If it is, then you need to get the handle of the stored object to use for updates. If it's not there, you will <code>save()</code> your new object, and the return value will be the handle of the copy in the database.</p> <p>The general pattern is this:</p> <pre><code>// 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); </code></pre> <p>Here's an example of how that pattern could apply to your code. You'll see here that the alternative approach to creating new objects is being used, in which the new object is created in ScriptDb with all its properties at once.</p> <pre><code>// 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. ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload