Nesta TriggerWhat is Nesta Trigger ?
Nesta Trigger is the mechanism for executing a Lua script when data is updated.
Nesta Trigger can be performed in the following timing.
Neste Trigger is written using the Lua simple and fast as a scripting language. Trigger List
Trigger is created, it is displayed as a list in the layout. Create a new trigger
Enter the trigger name to create.
The trigger name can be up to 60 characters. Trigger to set the initial valueIt is a Lua script to set the "default" in all fields when the new entity is added. db = ndb_current_dbname() cursor = ndb_cursor_current() n = ndb_field_count(db) for i = 1, n do field = ndb_field_name(db, i) property = ndb_property_get(cursor, field) ndb_property_putstr(property, "default") end It is a Lua script that sets today's date in the first field when a new entity is added. -- Today's date (YYYY/MM/DD) todays = os.date("%Y%m%d") db = ndb_current_dbname() cursor = ndb_cursor_current() field = ndb_field_name(db, 1) property = ndb_property_get(cursor, field) ndb_property_putstr(property, todays) Trigger to update a fieldIt is a Lua script to update the "place" field and the "member" field when it is updated to "XYZ". db = ndb_current_dbname() cursor = ndb_cursor_current() -- title -- title_field = ndb_field_name(db, 2) title_property = ndb_property_get(cursor, title_field) title = ndb_property_getstr(title_property) if (title == "XYZ") then -- member -- member_field = ndb_field_name(db, 3) member_property = ndb_property_get(cursor, member_field) ndb_property_putstr(member_property, "Scott, Jhon, Lian") -- place -- place_field = ndb_field_name(db, 4) place_property = ndb_property_get(cursor, place_field) ndb_property_putstr(place_property, "Tokyo") end Trigger automatically calculated when the entity has been updatedIt is a Lua script that can be set by calculating the average score of Mathematics and Science and English when the entity has been updated. The layout of the database is as follows.
db = ndb_current_dbname() cursor = ndb_cursor_current() total = 0 count = 0 for i = 3, 5 do field = ndb_field_name(db, i) property = ndb_property_get(cursor, field) num = ndb_property_getnum(property) if (num ~= nil) then if (num < 0 or num > 100) then alert("invalid number") ndb_property_delete(property) else total = total + num count = count + 1 end end end -- average ave_field = ndb_field_name(db, 6) ave_property = ndb_property_get(cursor, ave_field) --alert("count=" .. count .. " total=" .. total) if (count > 0) then average = total / count average = math.floor(average) ndb_property_putnum(ave_property, average) else ndb_property_delete(ave_property) end Trigger that references another database when the value of the field is updatedIt is a Lua script to update the "Name" field, see the "Students" database when the value of the "Number" field has been updated.
-- -- Function: -- Gets string field value via cursor. -- function getString(cursor, fieldname) local property = ndb_property_get(cursor, fieldname) return ndb_property_getstr(property) end -- -- Will be executed for script from here. -- db = ndb_current_dbname() cursor = ndb_cursor_current() number = getString(cursor, "Number") refdb = "students" -- new query query = ndb_query_create(refdb) -- new search filter filter = ndb_filter_create(query) -- add the filter condition ndb_filter_add(filter, "Number", NDB_EQ, number) -- add the filter condition to the query. ndb_query_add(query, filter) -- close filter ndb_filter_close(filter) -- gets the cursor that matches the filter conditions. ref_cursor = ndb_cursor_query(query) count = ndb_cursor_count(ref_cursor) if (count == 1) then -- gets name data ref_name = getString(ref_cursor, "Name") -- update name name_property = ndb_property_get(cursor, "Name") ndb_property_putstr(name_property, ref_name) else alert("Not found student number : " .. number) end -- close query ndb_query_close(query) -- close reference cursor ndb_cursor_close(ref_cursor)
Author: YAMAMOTO Naoki |