GLUDB Advanced Use

Perhaps the most important thing to note about "advanced" gludb usage is that no one is currently using it. However, if you don't want to use gludb.simple because you need to something very special for your model classes, you need to inherit from gludb.data.Storable

The class gludb.data.Storable is actually an Abstract Base Class as defined by the standard Python module abc. You will need to actually inherit from Storable somehow, or if you have a class that matches via duck typing you can just register your class as inheriting from Storable via gludb.data.Storable.register(cls)

The methods you need to implement are:

  • The classmethod get_table_name - you need to return the name of the "table" where the data will be stored
  • The instancemethod get_id - which should return the ID for the current object. If you return a "falsey" value on save, then a new ID will be created and set via a call to self.set_id
  • The instancemethod set_id - which should accept and save the ID
  • The instancemethod to_data - which should return a JSON representation of the current instance's data. The JSON returned should be understandable by self.from_data
  • The classmethod from_data - given a JSON string (as generated by to_data), return a new instance of the current class
  • The classmethod index_names - return an iterable of all index names used by the current class. You may return None if there are no indexes.
  • The instancemethod indexes - return a dictionary where the keys are index names as given by index_names and the values are the index values to be stored for the current instance. If there are no indexes you may return None

A class with all these methods implemented that descends from gludb.data.Storable (either via actual inheritance or duck typing and registration) may be decorated with gludb.data.DatabaseEnabled, which supplies the persistance methods as described in the "DBObject annotation" of Using gludb.simple.

Of course, there's very little reason to go through the trouble of implementing all of the above functionality if you're not going to annotate your class with DBObject.

One final note: you are responsible for implementing versioning (if you need it). You can use the functions provides by gludb.versioning if you wish. See Versioning for details.