Tutorial

Keith Hodges keith_hodges at yahoo.co.uk
Sun Oct 28 05:57:18 UTC 2007


There have been calls for a Magma tutorial to go into the Seaside
tutorial in the persistency chapter. This chapter already includes Glorp
and GOODS.

Just in case someone is already, or thinking of doing this, I have
already written such a mini tutorial.

In order to make Magma even easier to use, I have begun rewriting "Magma
seaside" to take advantage of my new Seaside Session Helpers interface,
which I released yesterday.

The idea being that you can now use Magma, without a specialized Seaside
Session class. This should make it easier for users to adopt Magma for
their existing applications.

I am a little unsure as to how you are supposed to obtain an exact match
of a unique item from the database.
This is how I would do it (althought I know MagmaCollections dont
actually implement #firstOrNil)

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

Is there a better way to obtain a unique value?

cheers

Keith

p.s. the wiki page that I refer to on MagmaCollections is a bit out of date.
----

so far...

*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 specialised 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.

Dictionary subclass: #StMagmaDatabase
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'STTutTodoApp'

StSession-#initialize
    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.

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.

StMagmaDatabase-#initialize
    | users |
    users := OrderedCollection new.
    self at: #users put: users.

StMagmaDatabase-#users
        ^ self at: #users 

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

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

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 we
introduce a MagmaCollection which you can read about in detail here:
http://wiki.squeak.org/squeak/2639

initialize
    | users |
    users := MagmaCollection new.
    users addIndex: (MaSearchStringIndex attribute: #userName) beAscii.
    users addIndex: (MaSearchStringIndex attribute: #email) beAscii.
    self at: #users put: users

StMagmaDatabase-#users
        ^ self at: #users 

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

StMagmaDatabase-#findUserByEmail: anEmail
    ^ (self users where: [ :each | each email = anEmail ]) firstOrNil

... need to explain how committing works... etc




 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/magma/attachments/20071028/b548da72/attachment.htm


More information about the Magma mailing list