store2.lua
Verified Featured
Added by iNTERFACEWARE
Provides a simple interface to store key/value pairs in a persistent storage mechanism.
Description
Provides a simple interface to store key/value pairs in a persistent storage mechanism.
Usage Details
The store2.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.
How to use store.lua:
- Add it to your shared modules in any Translator project.
- Add
store2 = require 'store.lua'
at the top of your script. - Use
store.connect()
to specify the database to use to store data. - Use store.put() and store.get() to store and get information respectively.
-- This is the new version of store which allows one translator to have multiple stores -- iNTERFACEWARE recommends this for newer interface code. It is convenient to be able to -- have independent stores for different purposes. -- This module stores the data in SQLite data base file given in the store2.connect function. -- By default this goes into the working directory of the Iguana instance. store2 = require 'store2' function main() local MyStore = store2.connect("store2.db") MyStore:put("life", "can be fun") MyStore:get("life") -- Query all the stored keys MyStore:info() -- Store multiple values MyStore:reset() -- clear the store first MyStore:put('Larry', 'The ') MyStore:put('Curly', 'three') MyStore:put('Moe', 'Stooges') -- check state - returns all stored data MyStore:info() --> table node tree with 3 rows -- Here we create a store which is unique to this translator -- by naming it after the GUID of this translator. local LocalChannelStore = store2.connect(iguana.project.guid()) LocalChannelStore:put("life", "can be interesting") -- See two independent stores? LocalChannelStore:get("life") MyStore:get("life") -- We can reset stores which clears them trace(#MyStore:info()) MyStore:reset() trace(#MyStore:info()) -- Or we can delete them entirely. -- os.fs.stat allows us to test for the existance of the -- underlying database file. local DbFile = iguana.workingDir()..MyStore.name os.fs.stat(DbFile) MyStore:delete() os.fs.stat(DbFile) LocalChannelStore:delete() end
More Information