from gi.repository import GObject
from gi.repository import Gom


class ItemResource(Gom.Resource):
    id = GObject.property(type=int)
    name = GObject.property(type=str)

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        super().set_table("items")
        super().set_primary_key("id")
        super().set_notnull("name")


if __name__ == '__main__':
    # Connect to the database
    adapter = Gom.Adapter()
    adapter.open_sync(":memory:")

    # Create the table
    repository = Gom.Repository(adapter=adapter)
    repository.automatic_migrate_sync(1, [ItemResource.__gtype__])  # This fails

    # Insert an item
    item = ItemResource(repository=repository, name="item1")
    item.save_sync()

    # Fetch the item back
    item = repository.find_one_sync(ItemResource, None)
    print("Item: id=%d, name='%s'" % item.get_property('id'), item.get_property('name'))

    # Close the database
    adapter.close_sync()
