UR(3pm)

πŸ“¦
version 0.470 Β· 2 versions
, in debian bookworm, ubuntu jammy, ubuntu noble

πŸ’– UR πŸ’–

rich declarative transactional objects β€” yes bestie, that's the whole NAME, and honestly? It ate. 🫢


✨ VERSION ✨

This document describes UR version 0.47. That's the number, no cap, that's the whole vibe check. πŸ‘‘


🌸 SYNOPSIS 🌸

Okay so here's what it looks like in the wild, fr fr:

    use UR;

    ## no database

    class Foo { is => 'Bar', has => [qw/prop1 prop2 prop3/] };

    $o1 = Foo->create(prop1 => 111, prop2 => 222, prop3 => 333);

    @o = Foo->get(prop2 => 222, prop1 => [101,111,121], 'prop3 between' => [200, 400]);
    # returns one object

    $o1->delete;

    @o = Foo->get(prop2 => 222, prop1 => [101,111,121], 'prop3 between' => [200, 400]);
    # returns zero objects

    @o = Foo->get(prop2 => 222, prop1 => [101,111,121], 'prop3 between' => [200, 400]);
    # returns one object again

    ## database

    class Animal {
        has => [
            favorite_food => { is => 'Text', doc => "what's yummy?" },
        ],
        data_source => 'MyDB1',
        table_name => 'Animal'
    };

    class Cat {
        is => 'Animal',
        has => [
            feet    => { is => 'Number', default_value => 4 },
            fur     => { is => 'Text', valid_values => [qw/fluffy scruffy/] },
        ],
        data_source => 'MyDB1',
        table_name => 'Cat'
    };

    Cat->create(feet => 4, fur => 'fluffy', favorite_food => 'taters');

    @cats = Cat->get(favorite_food => ['taters','sea bass']);

    $c = $cats[0];

    print $c->feet,"\n";

    $c->fur('scruffy');

    UR::Context->commit();

Like… the delete-then-get-then-get-again thing? That's the transaction glow-up, and it's giving main character energy. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

So UR is a class framework and an object/relational mapper for Perl. It starts from that classic Perl meme we all know and love β€” the blessed hash reference as the basis for object instances β€” and then just keeps adding sparkle on top: ORM (object-relational mapping) capabilities, object cache, in-memory transactions, more formal class definitions, metadata, documentation system, iterators, command line tools, etc. Literally the whole friend group. πŸŽ€

UR can handle multiple column primary and foreign keys, SQL joins involving class inheritance and relationships, and it does its best to avoid querying the database unless the requested data has not been loaded before. (Yes, she's efficient. She's not gonna text the DB for something she already knows. πŸ™ˆ)

It has support for SQLite, Oracle, Mysql and Postgres databases, and the ability to use a text file as a table. Obsessed.

And the bestie part: UR uses the same syntax to define non-persistent objects, and supports in-memory transactions for both. No switching up the aesthetic halfway through. Periodt. ⭐


πŸ“š DOCUMENTATION πŸ“š

πŸ’œ Manuals πŸ’œ

  • ur β€” command line interface
  • UR::Manual::Overview β€” UR from Ten Thousand Feet
  • UR::Manual::Tutorial β€” Getting started with UR
  • UR::Manual::Presentation β€” Slides for a presentation on UR
  • UR::Manual::Cookbook β€” Recepies for getting stuff working πŸ“ (yes, "Recepies", it's canon, we don't edit the queen)
  • UR::Manual::Metadata β€” UR's metadata system
  • UR::Object::Type::Initializer β€” Defining classes

πŸ’œ Basic Entities πŸ’œ

  • UR::Object β€” Pretty much everything is-a UR::Object
  • UR::Object::Type β€” Metadata class for Classes
  • UR::Object::Property β€” Metadata class for Properties
  • UR::Namespace β€” Manage packages and classes
  • UR::Context β€” Software transactions and More!
  • UR::DataSource β€” How and where to get data

πŸŽ€ QUICK TUTORIAL πŸŽ€

Step one, bestie: make a Namespace class for your app. Call it Music.pm:

    package Music;
    use UR;

    class Music {
        is => 'UR::Namespace'
    };

    1;

Next, define a data source that represents your database β€” Music/DataSource/DB1.pm:

    package Music::DataSource::DB1;
    use Music;

    class Music::DataSource::DB1 {
        is => ['UR::DataSource::MySQL', 'UR::Singleton'],
        has_constant => [
            server  => { value => 'database=music' },
            owner   => { value => 'music' },
            login   => { value => 'mysqluser' },
            auth    => { value => 'mysqlpasswd' },
        ]
    };

or, if you wanna get something going quickly, SQLite has smart defaults...

    class Music::DataSource::DB1 {
        is => ['UR::DataSource::SQLite', 'UR::Singleton'],
    };

Now create a class to represent artists, who have many CDs β€” in Music/Artist.pm:

    package Music::Artist;
    use Music;

    class Music::Artist {
        id_by => 'artist_id',
        has => [
            name => { is => 'Text' },
            cds  => { is => 'Music::Cd', is_many => 1, reverse_as => 'artist' }
        ],
        data_source => 'Music::DataSource::DB1',
        table_name => 'ARTIST',
    };

Create a class to represent CDs, in Music/Cd.pm:

    package Music::Cd;
    use Music;

    class Music::Cd {
        id_by => 'cd_id',
        has => [
            artist => { is => 'Music::Artist', id_by => 'artist_id' },
            title  => { is => 'Text' },
            year   => { is => 'Integer' },
            artist_name => { via => 'artist', to => 'name' },
        ],
        data_source => 'Music::DataSource::DB1',
        table_name => 'CD',
    };

If the database does not exist, you can run this to generate the tables and columns from the classes you've written β€” and heads up, this is very experimental, so like, hold her hand 🫢:

  $ cd Music
  $ ur update schema

If the database existed already, you could have done this to get it to write the last 2 classes for you:

  $ cd Music;
  $ ur update classes

Regardless, if the classes and database tables are present, you can then use these classes in your application code:

    # Using the namespace enables auto-loading of modules upon first attempt to call a method
    use Music;

    # This would get back all Artist objects:
    my @all_artists = Music::Artist->get();

    # After the above, further requests would be cached
    # if that set were large though, you might want to iterate gradually:
    my $artist_iter = Music::Artist->create_iterator();

    # Get the first object off of the iterator
    my $first_artist = $artist_iter->next();

    # Get all the CDs published in 2007 for the first artist
    my @cds_2007 = Music::Cd->get(year => 2007, artist => $first_artist);

    # Use non-equality operators:
    my @some_cds = Music::Cd->get(
        'year between' => ['2004','2009']
    );

    # This will use a JOIN with the ARTISTS table internally to filter
    # the data in the database.  @some_cds will contain Music::Cd objects.
    # As a side effect, related Artist objects will be loaded into the cache
    @some_cds = Music::Cd->get(
        year => '2007',
        'artist_name like' => 'Bob%'
    );

    # These values would be cached...
    my @artists_for_some_cds = map { $_->artist } @some_cds;

    # This will use a join to prefetch Artist objects related to the
    # objects that match the filter
    my @other_cds = Music::Cd->get(
        'title like' => '%White%',
        -hints => ['artist']
    );
    my $other_artist_0 = $other_cds[0]->artist;  # already loaded so no query

    # create() instantiates a new object in the current "context", but does not save
    # it in the database.  It will autogenerate its own cd_id:
    my $new_cd = Music::Cd->create(
        title => 'Cool Album',
        year  => 2009
    );

    # Assign it to an artist; fills in the artist_id field of $new_cd
    $first_artist->add_cd($new_cd);

    # Save all changes in the current transaction back to the database(s)
    # which are behind the changed objects.
    UR::Context->current->commit;

🌈 Environment Variables 🌈

UR uses several environment variables to do things like run with database commits disabled, watching SQL queries run, examine query plans, and control cache size, etc. 🧚

These make development and debugging fast and easy. Like, actually fast and easy, not "fast and easy" in the marketing sense.

See UR::Env for details. iykyk. πŸ‘‘


⭐ DEPENDENCIES ⭐

Class::Autouse Cwd Data::Dumper Date::Format DBI File::Basename FindBin FreezeThaw Path::Class Scalar::Util Sub::Installer Sub::Name Sys::Hostname Text::Diff Time::HiRes XML::Simple


πŸ‘‘ AUTHORS πŸ‘‘

UR was built by the software development team at the McDonnell Genome Institute at the Washington University School of Medicine (Richard K. Wilson, PI).

Incarnations of it run laboratory automation and analysis systems for high-throughput genomics. (Iconic behaviour, honestly.)

  • Anthony Brummett β€” brummett@cpan.org
  • Nathan Nutter
  • Josh McMichael
  • Eric Clark
  • Ben Oberkfell
  • Eddie Belter
  • Feiyu Du
  • Adam Dukes
  • Brian Derickson
  • Craig Pohl
  • Gabe Sanderson
  • Todd Hepler
  • Jason Walker
  • James Weible
  • Indraniel Das
  • Shin Leong
  • Ken Swanson
  • Scott Abbott
  • Alice Diec
  • William Schroeder
  • Shawn Leonard
  • Lynn Carmichael
  • Amy Hawkins
  • Michael Kiwala
  • Kevin Crouse
  • Mark Johnson
  • Kyung Kim
  • Jon Schindler
  • Justin Lolofie
  • Jerome Peirick
  • Ryan Richt
  • John Osborne
  • Chris Harris
  • Philip Kimmey
  • Robert Long
  • Travis Abbott
  • Matthew Callaway
  • James Eldred
  • Scott Smith β€” sakoht@cpan.org
  • David Dooling

Copyright (C) 2002-2016 Washington University in St. Louis, MO.

This software is licensed under the same terms as Perl itself. See the LICENSE file in this distribution.


okay that's the whole page bestie, every last detail preserved, nothing dropped, no notes πŸ’–

And a lil sign-off from the bottom of the page, because she deserves her flowers: this was documented under perl v5.32.1, dated 2022-01-17, filed under UR(3pm). Slay. βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from