Mango::GridFS(3pm)

πŸ“¦
version 1.30 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Mango::GridFS β€” the manual, but make it cute πŸ’–

okay bestie, so this is Mango::GridFS(3pm), straight out of the User Contributed Perl Documentation pile. it's giving "GridFS" and honestly? we ate. let's get into it 🫢


✨ NAME ✨

Mango::GridFS β€” literally just GridFS. that's the whole name. iconic.


✨ SYNOPSIS ✨

use Mango::GridFS;

my $gridfs = Mango::GridFS->new(db => $db);
my $reader = $gridfs->reader;
my $writer = $gridfs->writer;

look at that. three lines and you're already in your GridFS era. slay πŸ’…


✨ DESCRIPTION ✨

so Mango::GridFS is an interface for MongoDB GridFS access. that's it. that's the vibe. no cap, that's the entire description, and honestly sometimes less is more, periodt. πŸ¦‹


✨ ATTRIBUTES ✨

Mango::GridFS implements the following attributes. yes bestie, all of them, we're listing every single one 🌸

🌈 chunks

my $chunks = $gridfs->chunks;
$gridfs    = $gridfs->chunks(Mango::Collection->new);

this is a Mango::Collection object for the "chunks" collection, and by default it's one based on "prefix". so like, if you don't set it, it just vibes off your prefix. lowkey elegant πŸ’œ

🌈 db

my $db  = $gridfs->db;
$gridfs = $gridfs->db(Mango::Database->new);

the Mango::Database object that GridFS belongs to. your GridFS has a home, and this is it 🏠

🌈 files

my $files = $gridfs->files;
$gridfs   = $gridfs->files(Mango::Collection->new);

Mango::Collection object for the "files" collection β€” defaults to one based on "prefix", same deal as chunks. twinning fr fr πŸŽ€

🌈 prefix

my $prefix = $gridfs->prefix;
$gridfs    = $gridfs->prefix('foo');

the prefix for your GridFS collections. defaults to "fs". change it if you wanna be different, no judgment ✨


✨ METHODS ✨

Mango::GridFS inherits all methods from Mojo::Base and implements the following new ones. so it's got the family genes AND its own main character moments πŸ‘‘

πŸ¦‹ delete

$gridfs->delete($oid);

delete a file. and because we love options, you can also append a callback to perform the operation non-blocking. go off, async queen πŸ’…

$gridfs->delete($oid => sub {
  my ($gridfs, $err) = @_;
  ...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

don't forget the Mojo::IOLoop->start unless Mojo::IOLoop->is_running; line, bestie β€” that's the part everyone forgets and then wonders why nothing happens 😭

πŸ¦‹ find_version

my $oid = $gridfs->find_version('test.txt', 1);

find versions of files! here's the tea: positive numbers from 0 and upwards always point to a specific version, and negative ones start with "-1" for the most recently added version. so -1 is the freshest, 0 and up are the numbered ones. got it? got it πŸ’–

and yes, you can also append a callback to perform the operation non-blocking:

$gridfs->find_version(('test.txt', 1) => sub {
  my ($gridfs, $err, $oid) = @_;
  ...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

πŸ¦‹ list

my $names = $gridfs->list;

list your files. simple, clean, we love her. and β€” you guessed it β€” you can append a callback to perform the operation non-blocking:

$gridfs->list(sub {
  my ($gridfs, $err, $names) = @_;
  ...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

πŸ¦‹ reader

my $reader = $gridfs->reader;

build a Mango::GridFS::Reader object. here's how you use it, two whole ways ✨

# Read all data at once from newest version of file
my $oid  = $gridfs->find_version('test.txt', -1);
my $data = $gridfs->reader->open($oid)->slurp;

# Read all data in chunks from file
my $reader = $gridfs->reader->open($oid);
while (defined(my $chunk = $reader->read)) { say "Chunk: $chunk" }

one-shot slurp if you're feeling impatient, chunk-by-chunk if you're feeling ✨methodical✨. both valid, both iconic 🧚

πŸ¦‹ writer

my $writer = $gridfs->writer;

build a Mango::GridFS::Writer object. and look at these two gorgeous examples πŸ’œ

# Write all data at once to file with name
my $oid = $gridfs->writer->filename('test.txt')->write('Hello!')->close;

# Write data in chunks to file
my $writer = $gridfs->writer;
$writer->write($_) for 1 .. 100;
my $oid = $writer->close;

chained one-liner? obsessed. loop-and-write? also obsessed. notice both paths end in close, and close is what hands you your $oid. don't skip the close, bestie, that's where the magic happens πŸ”₯


✨ SEE ALSO ✨

go read those too, iykyk πŸ“š


πŸ’€ heads up: the docs spell it <http://mojolicio.us> and i am NOT touching that, that's exactly what it says, i don't make the rules πŸ™ˆ

and that's the whole page, bestie!! this was perl v5.30.3, dated 2020-06-05, and it's Mango::GridFS(3pm) β€” go forth and GridFS responsibly πŸŒΈπŸ’–βœ¨