[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Simple DB-like tables in pure Lua with validation
- From: Lorenzo Donati <lorenzodonatibz@...>
- Date: Tue, 23 Nov 2010 09:03:35 +0100
Hi list!
I'm currently using Lua to store small amounts of record-like data in
Lua files, using a syntax similar to the following:
-- file table1.lua
Record('field1', 'field2', 1231)
Record('field1', 'field2', 1231)
...
or this one:
-- file table2.lua
Record{ id = 3232, name = 'foo', date = '2010-12-25' }
Record{ id = 1232, name = 'bar', date = '2010-12-31' }
...
Then, after suitably defining the function 'Record', I load those files
using 'dofile' and process the data in different ways.
Currently I use ad-hoc functions to perform validation on the data, but
this is becoming unwieldy, since I find this kind of storage very useful
and flexible and I keep creating these small files (say max 1000 records
with max 10-20 fields each) for various purposes, and the ad-hoc
validation functions are becoming the main maintenance hassle.
Sometimes the task gets even more complicated when a field is actually a
Lua table itself, so the validation routine must recurse into it.
Is there any _very lightweight_ Lua library (pure Lua possibly) which
could help me in this respect, ie. defining a sort of a pseudo-DB schema
in a clean way and then use it to validate tables?
I'd like to write something declarative like:
local scheme = NewScheme {
-- fields description
id = {
type = 'number',
validator = function(field) ... end -- returns true when 'id' is ok
},
name = {
type = 'string',
validator = function(field) ... end
}
-- etc.
}
and then in the 'Record' function:
if not scheme:validate(record)
then error(...) end
I'd really like to avoid using a true RDBMS, even simple as sqlite,
since I love the format of the Lua 'data files' (and the advantages they
provide) and most of the time I don't need to update the data in the
tables: I only add records at the end manually or with simple append
procedures from other scripts (even non-Lua).
Any pointer or suggestion are welcome as long as it allows me to retain
the data files format and it is lightweight (not necessarily a library,
even a different approach at table data validation, if there is some
neat Lua trick that can improve readability and maintenance!).
Thanks in advance!
--
Lorenzo