Sunday, April 25, 2010

SandstoneDb, simple ActiveRecord style persistence

Start using SandstoneDb, an object oriented database with ActiveRecord style persistence. Written by Ramon Leon.

You will learn how to
  • Add persistency to a class instances by subclassing SDActiveRecord
  • Save and retrieve objects
  • Prevent saving of an object if invalid
  • Hook into records life cycle for cascade saving
  • Use HelpSystem from Torsten Bergmann to browse API documentation

See also:

Download screencast (800x600): .mpeg 18.8 MB, .mov 27.9 MB

Voice recorded by Christoph Budzinski.

This is the first screencast with subtitles, I hope you like it :). Download the subtitle file (SubRip format).

Load the source code:
Gofer new
   squeaksource: 'SandstoneDb';
   package: 'SandstoneDb';
   squeaksource: 'Pharocasts';
   package: 'SandstoneDbIntro';

Load HelpSystem:
Gofer new
   squeaksource: 'MetacelloRepository';
   package: 'ConfigurationOfHelpSystem';

(Smalltalk at:#ConfigurationOfHelpSystem) project latestVersion load


  1. If you'd like to avoid restarting the image when you create subclasses, you can just run

    SDCheckPointer startUp: true.

    This is what happens at image startup that ensures all the directories exist and reloads the database.

    Also, just FYI, you don't need to make all biz objects active records, had the tweets just been an Object subclass, they would have been save with the Person in the same file as the Person atomically. Of course, then the only way to get the tweets would be through the person, since it'd be the aggregate root for that aggregate object cluster.

    Anyway, nice screencast.

  2. Meanwhile I also created a ConfigurationOfSandstoneDb

  3. How do you load the ConfigurationOfSandstoneDb?

    I loaded it in Monticello but am not sure what command to issue.

    Thank You,

  4. You can load ConfigurationOfSandstoneDb with Monticello Browser, in MetacelloRepository on SqueakSource.

    An easier way is to use Gofer. In a workspace evaluate:

    Gofer new
        squeaksource: 'MetacelloRepository';
        package: 'ConfigurationOfSandstoneDb';

    Then load SandstoneDb:

        project latestVersion load.

  5. ConfigurationOfSandstoneDb (LT 4)is out of date. Loading it triggers a dependency warning (missing Complex class).

  6. Try:

    Gofer it
      squeaksource: 'SandstoneDb';
      package: 'SandstoneDb';

  7. Hello,

    I am not able to play with sandstone db since i have issues with database creation. After I save and leave the image db is not created. I have tried with pharo 1.1 and 1.2.1 image on OSX (Snow leopard) without success. Is maybe sandstone db not working any more in pharo under OSX.

    Thanks in advance,

  8. @Bostjan: From Ramon: It should be working now if you grab the latest version, I updated the config this morning. The last version got broken because another package stole the #startUp: method with an override in SandstoneDb causing me to publish a version without it; it'd cause the symptoms he's describing.

  9. I was trying to migrate a simple image-based persistence app into Sandstone, so I just recompiled my biz objects to be subclasses of SDActiveRecord, but of course the objects I want to save are already created and "floating" in the image, so when I try to save them I get an error as many instvars haven't been initialized upon creating these objects (as they were already there).

    Is there a way to automatically do this?



  10. from Ramon Leon:

    My general approach would be this sequence of steps, let's assume his
    class is called Person in collection var name people.

    If Person has an initialize method, rename it to initializeOld.
    Change superclass of Person to SDActiveRecord.
    Person warmUp. to setup the store for this new class.
    people do: [:e | e initialize ] to invoke active records initialize
    setting up it's necessary inst vars and marking all people as new
    rename Person>>initializeOld back to Person>>initialize.
    people do: [:e | e save ]

    Migration complete.


Note: Only a member of this blog may post a comment.