DBIx::...iokuDB(3pm)
π DBIx::Class::Schema::KiokuDB π
okay bestie, gather round, because today we're talking about DBIx::Class::Schema::KiokuDB β the hybrid KiokuDB / DBIx::Class::Schema support situation. it's giving "my database schema and my object store are dating" and honestly? iconic. π«Ά
this one's from the User Contributed Perl Documentation, and it's version 1.23, periodt. β¨
π VERSION π
version 1.23 β yes, that's the one, no cap. π
β¨ SYNOPSIS β¨
first things first: you gotta load this component into your schema, like so:
package MyApp::DB;
use base qw(DBIx::Class::Schema);
__PACKAGE__->load_components(qw(Schema::KiokuDB));
__PAKCAGE__->load_namespaces;
ngl, yes, __PAKCAGE__ is spelled like that in here. i'm not fixing it, i'm not touching it, it's not my business. copy it exactly as it is and we move on. π
then you load the DBIx::Class::KiokuDB component into every single table that wants to be able to refer to arbitrary KiokuDB objects. every. one. that wants it. π
package MyApp::DB::Result::Album;
use base qw(DBIx::Class::Core);
__PACKAGE__->load_components(qw(KiokuDB));
__PACKAGE__->table('album');
__PACKAGE__->add_columns(
id => { data_type => "integer" },
title => { data_type => "varchar" },
# the foreign key for the KiokuDB object:
metadata => { data_type => "varchar" },
);
__PACKAGE__->set_primary_key('id');
# enable a KiokuDB rel on the column:
__PACKAGE__->kiokudb_column('metadata');
see that metadata column? that's the little doorway where your arbitrary KiokuDB object lives. and kiokudb_column('metadata') is you unlocking it. π
next: connect to the DSN, bestie:
my $dir = KiokuDB->connect(
'dbi:SQLite:dbname=:memory:',
schema => "MyApp::DB",
create => 1,
);
# get the connect DBIC schema instance
my $schema = $dir->backend->schema;
look at you, grabbing the DBIC schema instance straight off $dir->backend->schema. main character energy fr fr.
and then β and this is the fun part β you can freely refer to KiokuDB objects from your Album class:
$dir->txn_do(scope => 1, body => sub {
$schema->resultset("Album")->create({
title => "Blah blah",
metadata => $any_object,
});
});
and $any_object can literally be any object. the range. it's giving freedom. π¦
πΈ DESCRIPTION πΈ
so what is this class, actually? this class provides the schema definition support code required for integrating an arbitrary DBIx::Class::Schema with KiokuDB::Backend::DBI. that's the whole job. it's the glue, and it's doing the most. π
π REUSING AN EXISTING DBIx::Class SCHEMA π
okay so the example up in the Synopsis assumes you want to first set up a KiokuDB and then link it to some DBIx::Class classes. makes sense, cute, we love that for you.
but there's another use case, and it's lowkey the relatable one: you already have a configured DBIx::Class Schema, and now you want to tack KiokuDB onto it. like, you're not rebuilding anything, you're just adding. obsessed. β¨
the trick here is to make sure you load the KiokuDB schema using __PACKAGE__->define_kiokudb_schema() in your Schema class:
package MyApp::DB;
use base qw(DBIx::Class::Schema);
__PACKAGE__->load_components(qw(Schema::KiokuDB));
__PACKAGE__->define_kiokudb_schema();
__PAKCAGE__->load_namespaces;
and now you can get the KiokuDB directory handle like so:
my $dir = $schema->kiokudb_handle;
one line!! one line and you have a handle!! ππ
and if you want a complete example, go take a look at t/autovivify_handle.t. it's right there. iykyk. π
π₯ USAGE AND LIMITATIONS π₯
okay here's the real talk section, so listen up:
- KiokuDB managed objects may hold references to row objects, resultsets (which are treated as saved searches β the results or the cursor state gets saved), result source handles, and the schema itself.
- Foreign
DBIx::Classobjects β meaning ones that originated from a schema that isn't the underlying schema β are currently not supported. but!! this limitation may be lifted in the future, so don't lose hope bestie. π - All DBIC operations which may implicitly cause a lookup of a KiokuDB managed object require live object scope management, just as normal. no skipping that step, no shortcuts. π
it is recommended to usetxn_doinKiokuDB, because that will invoke the appropriate transaction hooks on both layers β as opposed to just inDBIx::Class. so like, use it. please. for me. π₯Ί
π« SEE ALSO π«
DBIx::Class::KiokuDBKiokuDB::Backend::DBI
go read them, they're the supporting cast and they ate. π
π AUTHOR π
Yuval Kogman <nothingmuch@woobling.org>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2014 by Yuval Kogman, Infinity Interactive.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's the whole page, bestie!! ππ
this was perl v5.34.0, dated 2022-05-24, and yes the module name up top is DBIx::Class::Schema::KiokuDB even though the running header keeps chopping it into DBIx::...iokuDB(3pm) like it's too iconic to fit. slay. π
β¨
go build something hybrid. you got this. π«Ά
π¦ where this page comes from
- debian bookworm: libkiokudb-backend-dbi-perl 1.23-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libkiokudb-backend-dbi-perl 1.23-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libkiokudb-backend-dbi-perl 1.23-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libkiokudb-backend-dbi-perl 1.23-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libkiokudb-backend-dbi-perl 1.23-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms