[Seaside] Re: GOODS and Magma

Keith Hodges keith_hodges at yahoo.co.uk
Mon Nov 19 20:10:20 UTC 2007

Hi All,

I have been working on a revised Magma-Seaside interface using Session
Helpers, i.e. this allows you to add Magma to any seaside application
without needing a specialized subclass of WASession.I also have a
tutorial portion for Magma almost ready.

I just need a bit of spare time for testing, so watch this space.



Draft Tutorial follows:

*Saving all data in an object-oriented database: Magma*

Another option to make your data persistent is to use an object-oriented
database like Magma.

*Installing the Magma Database*

Magma is written entirely in Squeak, so there is no need to install a
separate server application. Both Magma and Seaside can run in the same
image. To install (into a 3.10 based image) just execute the following.
(This may take a while)

*Installer universes install: 'Magma seasideHelper'.*

*Configuring your application to use Magma*

In the configuration for the 'todo' application you will need to add
WAMagmaConfiguration to the configuration "ancestry". Select it from the
drop down menu and click "Add". As soon as you do this, a new group of
configuration options will appear. You will need to select the
"WAMagma", helper class from the drop down menu.

The WAMagma class provides the basic interface between Magma Sessions
and Seaside Sessions. Subclasses provide specialized session management
such as shared or pooled sessions.

*Using Magma*

Magma is very easy to use with seaside. To obtain and use a database
session helper, simply send #magma to the current session. For example
the code "self session magma" will work within all seaside components.

To use Magma in this application in a similar manner to the way we have
connected to databases in other examples we need to create an
interfacing class, to which we can add our specialized querying methods.
We will use this interfacing class as our root object in the database.
(note at present if a subclass of Dictionary is used then it is not
possible to add instVars to this class)
Dictionary subclass: #StMagmaDatabase
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'STTutTodoApp'

    super initialize.
    self db: (self magma rootAs: StMagmaDatabase)

The tutorial application accesses the database via the #db accessor.
Here we define this using the #rootAs: helper method, which does
everything we need, both to initialize the database, and to provide us
the interface that we want to it.

*Initializing the Database Root*

To initialize our users collection we define the initialization on our
root object, and we need a method to return the users from the database,
to add a new user, and to find a user.

    | users |
    users := OrderedCollection new.
    self at: #users put: users.

        ^ self at: #users 

StMagmaDatabase-#addUser: newUser
        ^ self users add: newUser

StMagmaDatabase-#findUserByEmail: anEmail
    ^ self users
        detect: [:each | each email equals: anEmail]

Notice how for basic usage we have not needed any database specific code!

If our userbase is going to grow to a significant size it makes sense to
re-implement the above using some database features, so let us introduce
a MagmaCollection, a collection designed to enable concurrent access to
large indexed data sets.

In this instance we use a factory method to create our MagmaCollection.
This allows us to switch the Magma helper to seamlessly by using
WAMockMagma instead of WAMagma.

    | users |
    users := WAMagma factory newMagmaCollection.
    users addIndex: (MaSearchStringIndex attribute: #email) beAscii.
    self at: #users put: users

findUserByEmail: anEmail
    ^ (self users where: [ :each | each email equals: anEmail ] ) firstOrNil

to be continued....

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/seaside/attachments/20071119/fdbf5665/attachment.htm

More information about the seaside mailing list