#!/usr/bin/gjs

const Lang = imports.lang;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const GIRepository = imports.gi.GIRepository;
const Gom = imports.gi.Gom;
const System = imports.system;

const INT32_MAX = (2147483647);

const ItemClass = new Lang.Class({
    Name: 'Item',
    Extends: Gom.Resource,

    Properties: {
        'id':  GObject.ParamSpec.int('id', 'ID',
                                     'An ID', GObject.ParamFlags.READABLE | GObject.ParamFlags.WRITABLE,
                                     0, INT32_MAX, 0),
        'url': GObject.ParamSpec.string('url', 'URL',
					'A URL',
					GObject.ParamFlags.READABLE | GObject.ParamFlags.WRITABLE,
					''),
    },

    _init: function(params) {
        this.parent(params);

        Gom.Resource.set_table.call(this, 'items');
        Gom.Resource.set_primary_key.call(this, 'id');
    },

    _instance_init: function() {
    },
});

// Open
let adapter = new Gom.Adapter;
adapter.open_sync('/tmp/gom-js-test.db');
let repository = new Gom.Repository({adapter: adapter});

let item = new ItemClass({ 'repository': repository });
//item.url = 'http://www.gnome.org';
item.set_property ('url', 'http://www.gnome.org');
//val = item.get_property('url');
//print (val);

// Migrate
let object_types = [ ItemClass ];
repository.automatic_migrate_sync(2, object_types);

// Add item
let ret = item.save_sync();
print("New item ID:", item.id, "URL:", item.url);

// Close
adapter.close_sync();

//FIXME
//(gjs:26386): Gjs-WARNING **: EvaluateScript returned JS_TRUE but exception was pending; did somebody call gjs_throw() without returning JS_FALSE?
//(gjs:26386): Gjs-WARNING **: JS ERROR: InternalError: too much recursion
