<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
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.<br>
<br>
Just in case someone is already, or thinking of doing this, I have
already written such a mini tutorial.<br>
<br>
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.<br>
<br>
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.<br>
<br>
I am a little unsure as to how you are supposed to obtain an exact
match of a unique item from the database. <br>
This is how I would do it (althought I know MagmaCollections dont
actually implement #firstOrNil)<br>
<br>
^ (self users where: [ :each | each email = anEmail ]) firstOrNil<br>
<br>
Is there a better way to obtain a unique value?<br>
<br>
cheers<br>
<br>
Keith<br>
<br>
p.s. the wiki page that I refer to on MagmaCollections is a bit out of
date.<br>
----<br>
<br>
so far... <br>
<br>
<b>Saving all data in an object-oriented database: Magma</b><br>
<br>
Another option to make your data persistent is to use an
object-oriented database like Magma.<br>
<br>
<b>Installing the Magma Database</b><br>
<br>
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)<br>
<br>
<tt>Installer universes install: 'Magma seasideHelper'.</tt><br>
<b><br>
Configuring your application to use Magma</b><br>
<br>
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.<br>
<br>
The WAMagma class provides the basic interface between Magma Sessions
and Seaside Sessions. Subclasses provide specialised session management
such as shared or pooled sessions. <br>
<br>
<b>Using Magma</b><br>
<br>
Magma is very easy to use with seaside. To obtain and use a database
session helper, simply send <tt>#magma</tt> to the current session.
For example the code "<tt>self session magma"</tt> will work within all
seaside components.<br>
<br>
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.<br>
<br>
<tt>Dictionary subclass: #StMagmaDatabase<br>
&nbsp;&nbsp;&nbsp; instanceVariableNames: ''<br>
&nbsp;&nbsp;&nbsp; classVariableNames: ''<br>
&nbsp;&nbsp;&nbsp; poolDictionaries: ''<br>
&nbsp;&nbsp;&nbsp; category: 'STTutTodoApp'<br>
<br>
StSession-#initialize<br>
&nbsp;&nbsp;&nbsp; super initialize.<br>
&nbsp;&nbsp;&nbsp; self db: (self magma rootAs: StMagmaDatabase)<br>
</tt><br>
The tutorial application accesses the database via the <tt>#db </tt>accessor.
Here we define this using the <tt>#rootAs: </tt>helper method, which
does everything we need, both to initialize the database, and to
provide us the interface that we want to it.<br>
<br>
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.<br>
<br>
<tt>StMagmaDatabase-#initialize<br>
&nbsp;&nbsp;&nbsp; | users |<br>
&nbsp;&nbsp;&nbsp; users := OrderedCollection new.<br>
&nbsp;&nbsp;&nbsp; self at: #users put: users.<br>
<br>
StMagmaDatabase-#users<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ^ self at: #users&nbsp; <br>
<br>
StMagmaDatabase-#addUser: newUser<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ^ self users add: newUser<br>
<br>
StMagmaDatabase-#findUserByEmail: anEmail<br>
&nbsp;&nbsp;&nbsp; ^ self users <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; detect: [:each | each email = anEmail] <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ifNone:[]<br>
</tt><br>
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:
<a class="moz-txt-link-freetext" href="http://wiki.squeak.org/squeak/2639">http://wiki.squeak.org/squeak/2639</a><br>
<br>
<tt>initialize<br>
&nbsp;&nbsp;&nbsp; | users |<br>
&nbsp;&nbsp;&nbsp; users := MagmaCollection new.<br>
&nbsp;&nbsp;&nbsp; users addIndex: (MaSearchStringIndex attribute: #userName) beAscii.<br>
&nbsp;&nbsp;&nbsp; users addIndex: (MaSearchStringIndex attribute: #email) beAscii.<br>
&nbsp;&nbsp;&nbsp; self at: #users put: users<br>
<br>
StMagmaDatabase-#users<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ^ self at: #users&nbsp; <br>
<br>
StMagmaDatabase-#addUser: newUser<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ^ self users add: newUser<br>
<br>
StMagmaDatabase-#findUserByEmail: anEmail<br>
&nbsp;&nbsp;&nbsp; ^ (self users where: [ :each | each email = anEmail ]) firstOrNil</tt><br>
<br>
... need to explain how committing works... etc<br>
<br>
<br>
<br>
<br>
&nbsp;<br>
</body>
</html>