Incoming data can be manipulated via a script which is executed before EasyCatalog attempts to load data from a data source. A ‘createcontent.lua’ script, located in the data sources scripts folder, is called immediately before data is loaded. The script is passed a ‘records’ variable which is a RECORDSET object.
Take this data for example:
field 1 | field 2 | field 3 |
---|---|---|
A | A | A |
B | B | B |
C | C | C |
D | D | D |
When loaded into EasyCatalog, with field 1 as the key, it looks like this:
When implementing the following ‘createcontent.lua’ script:
-- this can be used to access data source options -- this_local = DATASOURCE.get():getoption("locale", "US"); -- Rename an existing file records:renamefield("Field 2", "f2"); -- Create a new field and add content records:addfield("Regular Field"); for i = 1,records:size() do r = records:getrecord(i); r:field("Regular Field"):setcontent(i); end -- create a new tabular and add content records:addfield("Tabular Field"); t = TABLE.new(); t:cell(1,1):setcontent("1"); t:cell(2,1):setcontent("2"); t:cell(3,1):setcontent("3"); t:cell(4,1):setcontent("4"); for i = 1,records:size() do r = records:getrecord(i); r:field("Tabular Field"):setcontent(t); end -- Remove a field records:removefield("Field 3"); -- Duplicate each record and create a valid key for i = 1,records:size() do nr = records:duplicaterecord(i); nr:field("Field 1"):setcontent( nr:field("Field 1"):content() .. "-1"); end
The result after synchronizing is:
Notice the indicators on the fields that didn’t originate from the original data. Fields created in this way are treated as regular fields, with the exception of tabular fields which are stored in binary format within the snapshot file.
The post Augmenting Datasources via “CreateContent.lua” appeared first on 65bit Software.