Nesta TriggerNesta Triggerとは ?
Nesta Trigger とはデータが更新された時に Luaスクリプトを実行する仕組みのことです。
Nesta Trigger は以下のタイミングで実行することができます。
Neste Trigger はスクリプト言語としてシンプルで高速な Luaを使用して記述します。 トリガ一覧
作成されたトリガはレイアウトで一覧として表示されます。
新規トリガの作成
トリガ名を60文字以内で入力します。
初期値を設定するトリガ新しいエンティティが追加された時にすべてのフィールドに"default"を設定する Luaスクリプトです。
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
新しいエンティティが追加された時に先頭のフィールドに本日日付を設定する Luaスクリプトです。
-- 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)
フィールドを更新するトリガ"title"フィールドが"XYZ"に更新された時に"member"フィールドと"place"フィールドを更新する Luaスクリプトです。
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
エンティティが更新された時に自動計算するトリガエンティティが更新された時に科目の平均点を計算して設定する Luaスクリプトです。 データベースのレイアウトは以下の通りです。
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
フィールドの値が更新された時に他のデータベースを参照するトリガNumberフィールドの値が更新された時に Students データベースを参照して Name フィールドを更新する Luaスクリプトです。
--
-- 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 |