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.

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

(Smalltalk at:#ConfigurationOfHelpSystem) project latestVersion load

12 comments:

  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.

    ReplyDelete
  2. Meanwhile I also created a ConfigurationOfSandstoneDb

    ReplyDelete
  3. How do you load the ConfigurationOfSandstoneDb?

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

    Thank You,
    Jonathan

    ReplyDelete
  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';
        load.

    Then load SandstoneDb:

    ConfigurationOfSandstoneDb
        project latestVersion load.

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

    ReplyDelete
  6. Try:

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

    ReplyDelete
  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,
    Boštjan

    ReplyDelete
  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.

    ReplyDelete
  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?

    Thanks!

    Bernat.

    ReplyDelete
  10. from Ramon Leon: http://forum.world.st/Fwd-Pharocasts-New-comment-on-SandstoneDb-simple-ActiveRecord-style-persistence-tp3495252p3495252.html

    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.

    ReplyDelete