store.lua

Verified
Added by iNTERFACEWARE

The "original" store module: Allows you to store key/value pairs in a persistent storage mechanism. We recommend using the new store2 module instead.

Description
The "original" store module: Allows you to store key/value pairs in a persistent storage mechanism. We recommend using the new store2 module instead.
Usage Details

We strongly recommend using newer the store2.lua module rather than the legacy store.lua module.

The store.lua module contains a simple interface to store key/value pairs in a persistent storage mechanism. Simply put, if you want to store and/or retrieve information across interfaces cross-message; this might be the module to do it.

Under the hood this module creates a SQLite database called ‘store.db’ in the Iguana working directory, which has a table called “store”, which has the name=value pairs. However you don’t need to know this, just create a store module (using the code above) and it will all work automatically.

Note: You can use the store.init() function specify using a different database than ‘store.db’.

How to use store.lua:

  • Add it to your shared modules in any Translator project.
  • Add store = require 'store.lua' at the top of your script.
  • Use store.init() to specify the database to use to store data.
  • Use store.put() and store.get() to store and get information respectively.

Example:

-- This is the original store module.  It retains the original interface
-- for 100% backward compatability.  
-- A lot of interfaces have been written using this module so we maintain
-- it out of respect for our customers.  For new interfaces we recommend
-- the store2 module which has the ability to allow one translator instance
-- have multiple stores.

-- Under the hood this module creates a SQLite database by default called ‘store.db’ in the Iguana working directory. 
-- This has a table called “store”, which has the name=value pairs. However you don’t need to know this, 
-- just create a store module (using the code above) and it will all work automatically.

-- It is good practice to use the store.init function.  This function changes the store module to use another database
-- file instead of the default one.  This is valuable when you have multiple channels using the store module and you want
-- to guarantee independence or you want the freedom to clear the store.  The store2 module is even better in this regard.

local store = require 'store'

function ShowStore()
   -- It's best practice to use the init function to
   -- create different name spaces for different uses of
   -- the store module.  This prevents one use of the store
   -- module in one channel interfering with another.
   store.init('mystore.db')
   local Message = "Using: "..iguana.workingDir()..store.name()
   trace(Message)
   trace(store)
   
   -- Store a value
   store.put('Iguana', 'awesome')
 
   -- Get a value
   local Value = store.get('Iguana')
   trace('Iguana is '..Value)
   
   -- Clear the store
   store.resetTableState()
   -- Now the Iguana value will be nil
   trace(store.get('Iguana'))
      
   -- Store multiple values
   store.put('Larry', 'The ')
   store.put('Curly', 'three')
   store.put('Moe', 'Stooges')
   
   -- check state - returns all stored data
   store.getTableState() --> table node tree with 3 rows 
end