Japanese

Nesta Trigger


What is Nesta Trigger ?

Nesta Trigger is the mechanism for executing a Lua script when data is updated.
You can start a Lua script when updating the data that has been created in advance.
Also enables checking of the input data by the use of a trigger.
It is also possible to perform the automatic calculation as spreadsheets.

Nesta Trigger can be performed in the following timing.

  • When a new entity is added
  • When the entity has been updated
  • Before an entity is deleted
  • When the entity has been deleted
  • When the value of the field is updated

Neste Trigger is written using the Lua simple and fast as a scripting language.

* Screenshots in this paper will be the English version of the iPad.

Trigger List

Trigger is created, it is displayed as a list in the layout.
Tap the "New Trigger" if you want to create a trigger on a new one.

Create a new trigger

Enter the trigger name to create. The trigger name can be up to 60 characters.

Trigger to set the initial value

It 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 field

It 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 updated

It 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.
Number Name English Mathematics Science Average
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 updated

It is a Lua script to update the "Name" field, see the "Students" database when the value of the "Number" field has been updated.
Gets the value of "Name" in the "Students" database as a key value of "Number".

The layout of the "Students" database is as follows.
Number Name
--
-- 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
Create date: 2013/04/13
Last modified: 2013/04/20