<?xml version="1.0"?>
<rss version="2.0">
  <channel><title>Using Fins.Model in standalone applications</title><link>http://bill.welliver.org//space/pike/Fins/Developer/StandaloneModel</link><description>While the Fins Model was originally designed to be used within a Fins based web application, it can also be easily used in a standalone mode. In this example, we demonstrate the use of the Fins.Model framework in a simple command line script.&lt;p class="paragraph"/&gt;
Note that you don't ever need to create a full Fins app to use this technique, but creating a Fins app allows us to use the model builder tool (pike -x fins model). The model builder tool makes it easier to get started quickly by creating the data mapping and object instance definitions for any database tables we want to map. Once you have at least one Data Mapping and one Object Instance class created, it's easy use them as templates to create others later, so you can easily discard the Fins app you create after moving the Data Model definition class into your own app. We'll demonstrate this technique below.&lt;p class="paragraph"/&gt;
Also, note that there is a certain amount of startup overhead if you're using the fully automatic model configuration mechanism. Otherwise, though, using Fins.Model should be quick and easy.&lt;p class="paragraph"/&gt;
Quick directions for using the model in standalone mode:&#xD;
&lt;ol&gt;
&lt;li&gt; run pike -Mlib -x fins create MTA (for "my test application")&lt;/li&gt;
&lt;li&gt; edit MTA/config/dev.cfg, to define the sql url&lt;/li&gt;
&lt;li&gt; create tables in database&lt;/li&gt;
&lt;li&gt; run pike -Mlib -x fins model MTA scan, which will create stub classes in MTA/modules/MTA.pmod&lt;/li&gt;
&lt;li&gt;copy MTA/modules/MTA.pmod someplace more convenient for your app, preferably in your module path&lt;/li&gt;
&lt;li&gt; modify the example below as needed (ie, change the database connect string and name of the module that contains your model stub classes), then run and enjoy!&lt;/li&gt;
&lt;/ol&gt;Note that you really only need to perform steps 3 and 6, as well as create any necessary classes in your &lt;i class="ital"&gt;mymodel&lt;/i&gt;.Objects and &lt;i class="ital"&gt;mymodel&lt;/i&gt;.DataMappings modules.&lt;p class="paragraph"/&gt;
Similarly, you can create multiple data model definitions just as you might in a full scale Fins application and configure them similarly. In that situation, you'd pass the model configuration definitions as elements in the mapping passed as the second argument to Fins.Configuration in the example below.&lt;p class="paragraph"/&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;pre&gt;&lt;p class="paragraph"/&gt;
&lt;font color=red&gt;// Fins.FinsModel provides model initialization, so we create a subclass and&#xD;
&lt;/font&gt;&lt;font color=red&gt;// prime it with the minimum amount of information required to start up,&#xD;
&lt;/font&gt;&lt;font color=red&gt;// namely a faux configuration file that provides details about the model&#xD;
&lt;/font&gt;&lt;font color=red&gt;// DataMapping module and SQL connection information.&#xD;
&lt;/font&gt;&lt;b&gt;&lt;font color=darkblue&gt;class&lt;/font&gt;&lt;/b&gt; mymodel {&lt;p class="paragraph"/&gt;
&lt;b&gt;&lt;font color=darkblue&gt;inherit&lt;/font&gt;&lt;/b&gt; Fins.FinsModel : fm;&lt;p class="paragraph"/&gt;
  static &lt;b&gt;&lt;font color=darkgreen&gt;void &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;create&lt;/font&gt;&lt;/b&gt;(&lt;b&gt;&lt;font color=darkgreen&gt;string &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;sqlurl&lt;/font&gt;&lt;/b&gt;, &lt;b&gt;&lt;font color=darkgreen&gt;int &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;debug&lt;/font&gt;&lt;/b&gt;, &lt;b&gt;&lt;font color=darkgreen&gt;string &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;model_stub_module&lt;/font&gt;&lt;/b&gt;) &#xD;
  { &#xD;
  	config = Fins.Configuration(0, (&amp;#91;&lt;i&gt;&lt;font color=darkred&gt;"model"&lt;/font&gt;&lt;/i&gt;: (&amp;#91;&lt;i&gt;&lt;font color=darkred&gt;"datasource"&lt;/font&gt;&lt;/i&gt;: sqlurl, &lt;i&gt;&lt;font color=darkred&gt;"debug"&lt;/font&gt;&lt;/i&gt;: debug, &#xD;
                                                    &lt;i&gt;&lt;font color=darkred&gt;"definition_module"&lt;/font&gt;&lt;/i&gt;: model_stub_module]) ]));&lt;p class="paragraph"/&gt;
	  fm::load_model(); &#xD;
  } &#xD;
}&lt;p class="paragraph"/&gt;
&#xD;
&lt;b&gt;&lt;font color=darkgreen&gt;int &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;main&lt;/font&gt;&lt;/b&gt;(&lt;b&gt;&lt;font color=darkgreen&gt;int &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;argc&lt;/font&gt;&lt;/b&gt;, &lt;b&gt;&lt;font color=darkgreen&gt;array &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;argv&lt;/font&gt;&lt;/b&gt;)&#xD;
{&#xD;
  &lt;font color=red&gt;// modify the db connection url and the name of the module containing your &#xD;
&lt;/font&gt;  &lt;font color=red&gt;// model data mapping classes as appropriate.&#xD;
&lt;/font&gt;  &lt;font color=red&gt;// &#xD;
&lt;/font&gt;  &lt;font color=red&gt;// you don't need to keep this object around once it's configured the model;&#xD;
&lt;/font&gt;  &lt;font color=red&gt;// you can access the model through Fins.Model.find and friends or&#xD;
&lt;/font&gt;  &lt;font color=red&gt;// access the data model context through Fins.DataSources._default&#xD;
&lt;/font&gt;  &lt;b&gt;&lt;font color=darkgreen&gt;object &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;model&lt;/font&gt;&lt;/b&gt; = mymodel(&lt;i&gt;&lt;font color=darkred&gt;"mysql://blog:pass@localhost/blog"&lt;/font&gt;&lt;/i&gt;, 1, &lt;i&gt;&lt;font color=darkred&gt;"MTA"&lt;/font&gt;&lt;/i&gt;);&lt;p class="paragraph"/&gt;
  // this line assumes we have a database table called &lt;i&gt;&lt;font color=darkred&gt;"users"&lt;/font&gt;&lt;/i&gt; mapped to the User class.&#xD;
  werror(&lt;i&gt;&lt;font color=darkred&gt;"%O&amp;amp;#110;"&lt;/font&gt;&lt;/i&gt;, values(MTA.Objects.User(1)));&lt;p class="paragraph"/&gt;
  &lt;b&gt;&lt;font color=darkblue&gt;return&lt;/font&gt;&lt;/b&gt; 0;&#xD;
}&#xD;
&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p class="paragraph"/&gt;
&#xD;
Second example demonstrating multiple model definitions in a standalone application:&lt;p class="paragraph"/&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;pre&gt;&#xD;
&lt;b&gt;&lt;font color=darkblue&gt;class&lt;/font&gt;&lt;/b&gt; mymodel&#xD;
{&#xD;
  &lt;b&gt;&lt;font color=darkblue&gt;inherit&lt;/font&gt;&lt;/b&gt; Fins.FinsModel : fm;&lt;p class="paragraph"/&gt;
  static &lt;b&gt;&lt;font color=darkgreen&gt;void &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;create&lt;/font&gt;&lt;/b&gt;()&#xD;
  {&#xD;
    this-&amp;gt;config = Fins.Configuration(0, &#xD;
        (&amp;#91;&lt;i&gt;&lt;font color=darkred&gt;"model"&lt;/font&gt;&lt;/i&gt;: (&amp;#91;&lt;i&gt;&lt;font color=darkred&gt;"datasource"&lt;/font&gt;&lt;/i&gt;: &lt;i&gt;&lt;font color=darkred&gt;"sqlite:///db1.sqlite3"&lt;/font&gt;&lt;/i&gt;, &#xD;
                     &lt;i&gt;&lt;font color=darkred&gt;"debug"&lt;/font&gt;&lt;/i&gt;: debug, &#xD;
                     &lt;i&gt;&lt;font color=darkred&gt;"definition_module"&lt;/font&gt;&lt;/i&gt;: &lt;i&gt;&lt;font color=darkred&gt;"MTA"&lt;/font&gt;&lt;/i&gt;]),&#xD;
          &lt;i&gt;&lt;font color=darkred&gt;"model_2"&lt;/font&gt;&lt;/i&gt;: (&amp;#91;&lt;i&gt;&lt;font color=darkred&gt;"datasource"&lt;/font&gt;&lt;/i&gt;: &lt;i&gt;&lt;font color=darkred&gt;"sqlite:///db2.sqlite3"&lt;/font&gt;&lt;/i&gt;, &lt;i&gt;&lt;font color=darkred&gt;"id"&lt;/font&gt;&lt;/i&gt;: &lt;i&gt;&lt;font color=darkred&gt;"second_datasource"&lt;/font&gt;&lt;/i&gt;,&#xD;
                     &lt;i&gt;&lt;font color=darkred&gt;"debug"&lt;/font&gt;&lt;/i&gt;: debug, &#xD;
                     &lt;i&gt;&lt;font color=darkred&gt;"definition_module"&lt;/font&gt;&lt;/i&gt;: &lt;i&gt;&lt;font color=darkred&gt;"DataSource2"&lt;/font&gt;&lt;/i&gt;])&lt;p class="paragraph"/&gt;
         ]) );&lt;p class="paragraph"/&gt;
    fm::load_model();&#xD;
  }&#xD;
}&lt;p class="paragraph"/&gt;
&#xD;
&lt;b&gt;&lt;font color=darkgreen&gt;int &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;main&lt;/font&gt;&lt;/b&gt;(&lt;b&gt;&lt;font color=darkgreen&gt;int &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;argc&lt;/font&gt;&lt;/b&gt;, &lt;b&gt;&lt;font color=darkgreen&gt;array &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;argv&lt;/font&gt;&lt;/b&gt;)&#xD;
{&#xD;
  &lt;b&gt;&lt;font color=darkgreen&gt;object &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;model&lt;/font&gt;&lt;/b&gt; = mymodel();&lt;p class="paragraph"/&gt;
  // this line assumes we have a database table called &lt;i&gt;&lt;font color=darkred&gt;"users"&lt;/font&gt;&lt;/i&gt; mapped to the User class.&#xD;
  werror(&lt;i&gt;&lt;font color=darkred&gt;"%O&amp;amp;#110;"&lt;/font&gt;&lt;/i&gt;, values(MTA.Objects.User(1)));&#xD;
  // this line assumes we have a database table called &lt;i&gt;&lt;font color=darkred&gt;"widgets"&lt;/font&gt;&lt;/i&gt; mapped to the Widget &lt;b&gt;&lt;font color=darkblue&gt;class&lt;/font&gt;&lt;/b&gt; in our second database.&#xD;
  werror(&lt;i&gt;&lt;font color=darkred&gt;"%O&amp;amp;#110;"&lt;/font&gt;&lt;/i&gt;, values(DataSource2.Objects.Widget(1)));&lt;p class="paragraph"/&gt;
  &lt;b&gt;&lt;font color=darkgreen&gt;object &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;context2&lt;/font&gt;&lt;/b&gt; = Fins.DataSources.second_datasource;&#xD;
  context2-&amp;gt;find-&amp;gt;widgets_all();&lt;p class="paragraph"/&gt;
  &lt;b&gt;&lt;font color=darkblue&gt;return&lt;/font&gt;&lt;/b&gt; 0;&#xD;
}&#xD;
&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;
</description><generator>Fins 0.9.7</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs></channel>
</rss>
