5 Replies Latest reply on Feb 11, 2015 4:53 PM by Mark Nongkhlaw

    How to use Fixed Schema database in Rhodes 5.0.25 ?

    Thierry Leloup

      Hello everybody,

       

      I tried to use Rhom and it is working alright with the Property bag models. (In fact, I used "Product" model which seems to be defined by default)

       

      But, once I try to define a new Fixed Schema model, I am facing some issues.

       

      As indicated in the Rhom documentation, I generate a new efixeddb model with the following command :

      rhodes model efixeddb name,image,comment,comment2

      I observe a new EFixeddb directory is generated with several files inside as expected.

       

      I edited efixeddb.rb file with :

      # The model has already been created by the framework, and extends Rhom::RhomObject
      # You can add more methods here
      class Efixeddb
        include Rhom::FixedSchema

        # Uncomment the following line to enable sync with Efixeddb.
        # enable :sync

        #add model specific code here
        property :name, :string
        property :image, :string
        property :comment, :string
        property :comment2, :string

        unique_index :by_name, [:name]

       

        property :int_prop, :integer
        property :float_prop, :float
        property :date_prop, :date #translate to integer type
        property :time_prop, :time #translate to integer type
      end

       

      But, when I try to add this model in my code using :

      var userModel = Rho.ORM.addModel(function(model){

         model.modelName('Efixeddb');

         model.property('name','string');

         model.property('image','string');

         model.property('comment','string');

         model.property('comment2','string');

         model.enable('fixedSchema');

      });

      i got the following error : "column source_id is not unique"

       

      I then tried model.modelName('efixeddb'); instead but then, I got table efixeddb already exists

       

      I also tried to call getModel('efixeddb') and getModel('Efixeddb') but they both return undefined...

       

      So, my questions are :

      • What did I miss here ?
      • Why was I able to use Product and User models (Property bag mode only) when they aren't defined anywhere ?

       

      Thanks for your help,

      Thierry

        • Re: How to use Fixed Schema database in Rhodes 5.0.25 ?
          Jon Tara

          You are duplicating creation of the database schema in both Ruby and Javascript. Don't.

           

          You can create the schema in Ruby. Or you can create the schema in Javascript. Your choice.

           

          When you use the command-line to add a table to your project, it create a Ruby model and controller, and in the Ruby model it defines the schema for that table using Ruby.

           

          Property Bag is magic. It does not require you to define columns in advance. Columns pop into existence when you use them. You still need to define the table, but if you used the command-line generator to add a property-bag model to your project, it created a Ruby model, and added the code to the model to create the table.

            • Re: How to use Fixed Schema database in Rhodes 5.0.25 ?
              Thierry Leloup

              Ok, that's really clearer.

               

              It means that if I don't use the command-line generator, it does not generate the Ruby class and I should be able to create a new Fixed schema model in Javascript.

               

              So, I tried to do that :

              var userModel = Rho.ORM.addModel(function(model){

                 model.modelName('EfixeddbJS');

                 model.property('name','string');

                 model.property('image','string');

                 model.property('comment','string');

                 model.property('comment2','string');

                 model.enable('fixedSchema');

              });

               

              justt to be sure I was not using the Ruby generated class.

              But I still got the "column source_id is not unique" error...

               

              ===================================

              EDIT : I finally rm -fr rhosimulator directory and it is now working !

               

              Thanks, Jon !

              • Re: How to use Fixed Schema database in Rhodes 5.0.25 ?
                Mark Nongkhlaw

                >You are duplicating creation of the database schema in both Ruby and Javascript. Don't.

                 

                Well, along with the separation of concerns, to really make Rho truly a 'clean sheet of paper' as Adam used to say, we also need to have a clear separation of JS & Ruby.

                 

                I'm looking at ways to generate a Rho app from the command line, but where the models, views, controllers would be in JS, something like how CouchApp creates an app (github.com/CouchApp) & where the APIs would be pure JS, and not a proxy to Ruby, perhaps something like NativeScript ?

                 

                Just a thought...

                  • Re: How to use Fixed Schema database in Rhodes 5.0.25 ?
                    Jon Tara

                    I haven't looked at the code, but assume the command-line generators are simply cranking the data you supply (model name, attributes) through a set of templates to generate a model.

                     

                    You could write your own model generator pretty easily. But you might not want to hear that the best tools for doing this are in Ruby! If I wanted to do this, I would create a Rake task for it, and use some simple template language, I think in this case {{mustache}}, because of it's simplicity, and, really here, nothing fancy is needed.

                     

                    That said, the reference implementation of {{mustache}} is written in Javascript, and so you could do this in JS if JS is your preferred language using node/npm and some JS build system (e.g. an analog of Rake).

                     

                    But then you have another build system outside of Rhodes own build system. Keep in mind your Rakefile is in your project for a reason - you can add your own rake tasks! The real meat of all of the Rhodes rake tasks is in the Rhodes Gem, but every project gets it's own rakefile which loads the rake tasks in the Gem, and it allows you to modify the rakefile and add your own tasks.

                     

                    On the other other hand.... I use a completely separate build system (that is used to perform tasks pre-build) myself. But... it is a rakefile! But that is because I use the tools that I know, and I know rake well, and don't know any of the JS build systems.