Basic database operations
This commit is contained in:
parent
08e450bd51
commit
40ec00a4ad
3 changed files with 71 additions and 6 deletions
53
blog
53
blog
|
@ -1,9 +1,54 @@
|
|||
#!/usr/bin/env raku
|
||||
use v6.e.PREVIEW;
|
||||
|
||||
use DB;
|
||||
|
||||
my $example-post = DB::MarkdownPost.new:
|
||||
source => "../Idris/advent/src/Util/Digits.md".IO,
|
||||
posted-at => DateTime.now;
|
||||
my %*SUB-MAIN-OPTS =
|
||||
:named-anywhere,
|
||||
:bundling,
|
||||
;
|
||||
|
||||
say $example-post.title;
|
||||
my IO::Path:D $blog-dir = $*PROGRAM.dirname.IO.dirname.IO;
|
||||
#= The directory this script is located in
|
||||
|
||||
#| Load the database from the provided file
|
||||
sub load-db(IO::Path $file --> DB::PostDB:D) {
|
||||
my $result = DB::PostDB.from-json: $file.slurp;
|
||||
if $result ~~ DB::PostDB:D {
|
||||
return $result;
|
||||
} else {
|
||||
die "Error parsing $file as databse: $result";
|
||||
}
|
||||
}
|
||||
|
||||
#| Initalize the database
|
||||
multi MAIN(
|
||||
"db",
|
||||
"init",
|
||||
#| The path of the database file
|
||||
IO::Path :$file = $blog-dir.add("db.json"),
|
||||
#| Overwrite an already existing database file
|
||||
Bool :$force
|
||||
) {
|
||||
die "Database file already exists, use --force to overwrite: {$file.Str}"
|
||||
if $file.e && !$force;
|
||||
|
||||
my $db = DB::PostDB.new;
|
||||
|
||||
if $force {
|
||||
$file.spurt: $db.to-json, :create-only;
|
||||
} else {
|
||||
$file.spurt: $db.to-json;
|
||||
}
|
||||
}
|
||||
|
||||
#| Ensure that the database loads, erroring otherwise
|
||||
multi MAIN(
|
||||
"db",
|
||||
"verify",
|
||||
#| The path of the database file
|
||||
IO::Path :$file = $blog-dir.add("db.json"),
|
||||
) {
|
||||
load-db $file;
|
||||
say "Database OK";
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue