Rocket: A Lua Sqlite3 ORM manager

Relational databases are designed so that they can both store and describe data. Most programming level APIs do wonderful jobs of maintaining the former aspect. I mean, what ORM doesn’t automatically map the insert method to the so very generic INSERT (x,y,z)

However, the latter part is often omitted. If you want to write a schema that describes the relationship between a clan and a very generic user system, you will need to write a lot of glue code just to make sure that everything sticks together. And if you want to write another layer on top of that, well, the amount of effort is literally exponentially incremented every time that you want to extend your application.

There are a few libraries that do great jobs of automatically maintaining explicit relationships between different tables. At the top of this list is Django’s model system, a very extensible python web-based framework. Django lets the user describe data in forms of Python classes with fields as attributes. It’s very intuitive and it contains a great set of deferred query generation system that allows you to create lazy querysets.

Now there’s something in development that emulates Django’s model system. Meet Rocket for Lua, a data-driven wrapper over sqlite3 (and potentially other databases) with lazy querysets. Here’s a preview of what Rocket can do:

require "model"

-- Create a sample user model with username, password, and usgn
User = model{
	"user", -- Table name, lowercase
	username = model.CharField{max_length = 10, null = false, unique = true},
	password = model.CharField{max_length = 10, null = false},
	usgn	 = model.IntegerField{null = true, unique = true}
}
-- Create a sample clan model with clan_name and tag fields
Clan = model{
	"clan",
	clan_name = model.CharField{unique = true},
	tag 	  = model.CharField{null = true}
}

UserClan = model{
	"userclan",
	clan = model.ForeignKey{to = "clan", null = false},
	user = model.ForeignKey{to = "user", null = false}
}

And an example session:

lee = User{username = "lee", password = "test"}
--Unsaved objects do not have database associated ids.
print(lee.id) --> nil
lee:save()
print(lee.id) --> 1

my_clan = Clan{clan_name="My Clan", tag = "[MY]"}
my_clan:save()

-- Link the two
UserClan{user=lee, clan=my_clan}:save()

-- We can now query for lee by his clan
q = UserClan.objects.all():where{clan__tag__contains = "My"}

-- Now we query the database with the lazy queryset.
print(q())

More to come in the future, but for the impatient few, you can check out the code at http://github.com/leegao/Rocket-for-Lua

This entry was posted in Blog, Lua, Programming, Snippets and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>