Postgres database connection pools in Seaside, part 2.
Postgres database connection pools in Seaside
Part 2. Configurations
Using Rails you have several places to include configuration code for your application, you use database.yml, config.rb, environment.rb, environments/development.rb, environment/production.rb, etc. In those files you can set variables that your application code will use, execute code and basically, anything you need in order to provide the required parameters to your application.
If you use Django you have a settings file for your all your configuration needs.
Seaside is no exception, it provides a very powerful, flexible and versatile mechanism to configure a web application.
A Seaside configuration class provides more than just a set of keyed values, it defines the type of the values and it can provide a list of options that an administrator can change through the administrative interface of Seaside, it also provides some kind of 'inheritance'.
Before getting into database connections with its user, password and other parameters, I will use a configuration class for those parameters. For an introduction about configurations you should read the following pages:
- Configuration and Preferences
- David Shaffer's tutorial: Custom application configuration parameters.
Now back to our business. We will use a configuration class for all the required Glorp parameters:
WASystemConfiguration subclass: #GlorpSessionConfiguration
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'LearnGlorpSession'
With the these methods:
ancestors
^ Array with: WASessionConfiguration new
attributes
^ Array
with: (WAStringAttribute key: #sessionClass)
with: (WAStringAttribute key: #glorpServer group:#database)
with: (WAStringAttribute key: #database group:#database)
with: (WAStringAttribute key: #glorpUserName group:#database)
with: (WAStringAttribute key: #glorpPassword group:#database)
glorpDatabase
^'You need to provide a database'
glorpPassword
^''
glorpServer
^'localhost'
glorpUserName
^'postgres'
sessionClass
^MyPostgresSession
sessionClasses
^MyPostgresSession withAllSubclasses
I will assume you don't have a Seaside root component yet so let's write one:
WAComponent subclass: #GlorpExampleComponent
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'GlorpExample'
renderContentOn: html
html text: 'Glorp example'
And with a class method:
canBeRoot
^ true
( You can read more about the root component here )
Then load this url in your browser (using the port you told SSKom to use): http://localhost:9091/seaside/config and enter the user and password ( defaults: admin/seaside ).
Under "Add entry point" define a name for our testing application, for the example I will use: glorpexample. Then select the Root component from the combo 'GlorpExampleComponent'.
And now, pay attention to the options provided in the combo list named 'Ancestry'. Your configuration class is listed there! Select it and click the Add button.
Now save the changes.
What do we have so far?
We have a Seaside application with our configuration class that provides a custom Seaside session that provides a pool of connections to PostgreSQL.
If you load again the configuration page for our glorpexample application you can appreciate a separated section for the database! You can change the field values or revert to the previous ones.
With these, all you need to do now to execute a query using Glorp is: WACurrentSession value execute: query.
