| 1 | package DB::CouchDB::Schema; | ||
| 2 | use DB::CouchDB; | ||
| 3 | use Moose; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | our $VERSION = '0.3.04'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | DB::CouchDB::Schema - A Schema driven CouchDB module | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | 0.3.01 | ||
| 15 | |||
| 16 | =head1 RATIONALE | ||
| 17 | |||
| 18 | After working with several of the CouchDB modules already in CPAN I found | ||
| 19 | myself dissatisfied with them. DB::CouchDB::Schema is intended to approach the | ||
| 20 | CouchDB Workflow from the standpoint of the schema. It provides tools for dumping | ||
| 21 | and restoring the views that define your schema and for querying the views in your | ||
| 22 | schema easily. | ||
| 23 | |||
| 24 | =head1 METHODS | ||
| 25 | |||
| 26 | =head2 new(%opts) | ||
| 27 | |||
| 28 | my $schema = DB::CouchDB::Schema->new(host => $hostname, | ||
| 29 | port => $db_port, # optional defaults to 5984 | ||
| 30 | db => $databse_name); | ||
| 31 | |||
| 32 | Constructor for a CouchDB Schema. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | has server => ( is => 'rw', isa => 'DB::CouchDB'); | ||
| 37 | has schema => ( is => 'rw', isa => 'ArrayRef'); | ||
| 38 | has views => ( is => 'rw', isa => 'HashRef[Code]', | ||
| 39 | required => 1, default => sub { {} } ); | ||
| 40 | |||
| 41 | sub BUILD { | ||
| 42 | my $self = shift; | ||
| 43 | my $params = shift; | ||
| 44 | if (keys %$params) { | ||
| 45 | my $db = DB::CouchDB->new(%$params); | ||
| 46 | $db->handle_blessed(1); | ||
| 47 | $self->server($db); | ||
| 48 | $self->load_schema_from_db(); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | =head2 load_schema_from_script($script) | ||
| 53 | |||
| 54 | loads a CouchDB Schema from a json script file. This is sort of like the DDL | ||
| 55 | in a SQL DB only its essentially just a list of _design/* documents for the CouchDB | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub load_schema_from_script { | ||
| 60 | my $self = shift; | ||
| 61 | my $script = shift; | ||
| 62 | $self->schema($self->server->json->decode($script)); | ||
| 63 | return $self; | ||
| 64 | } | ||
| 65 | |||
| 66 | =head2 load_schema_from_db() | ||
| 67 | |||
| 68 | Loads a CouchDB Schema from the Database on the server. this can later be dumped | ||
| 69 | to a file and pushed to a database using load_schema_from_script. | ||
| 70 | |||
| 71 | This method gets called for you during object construction so that you will have | ||
| 72 | a current look at the CouchDB Schema stored in your object. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub load_schema_from_db { | ||
| 77 | my $self = shift; | ||
| 78 | my $db = $self->server; | ||
| 79 | #load our schema | ||
| 80 | my $doc_list = $self->get_views(); | ||
| 81 | my @schema; | ||
| 82 | while (my $docname = $doc_list->next_key() ) { | ||
| 83 | my $doc = $db->get_doc($docname); | ||
| 84 | $self->_mk_view_accessor($doc); | ||
| 85 | push @schema, $doc; | ||
| 86 | } | ||
| 87 | $self->schema(\@schema); | ||
| 88 | return $self; | ||
| 89 | } | ||
| 90 | |||
| 91 | =head2 get_views() | ||
| 92 | |||
| 93 | Returns a List of all the views in the database; | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub get_views { | ||
| 98 | my $self = shift; | ||
| 99 | my $db = $self->server; | ||
| 100 | #load our schema | ||
| 101 | return $db->all_docs({startkey => '"_design/"', | ||
| 102 | endkey => '"_design/ZZZZZ"'}); | ||
| 103 | } | ||
| 104 | |||
| 105 | =head2 dump_db | ||
| 106 | |||
| 107 | dumps the entire db to a file for backup | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | #TODO(jwall) tool to dump the whole db to a backup file | ||
| 112 | sub dump_whole_db { | ||
| 113 | my $self = shift; | ||
| 114 | my $pretty = shift; | ||
| 115 | my $db = $self->server; | ||
| 116 | #load our schema | ||
| 117 | my $doc_list = $db->all_docs(); | ||
| 118 | my @docs; | ||
| 119 | while (my $docname = $doc_list->next_key() ) { | ||
| 120 | my $doc = $db->get_doc($docname); | ||
| 121 | push @docs, $doc; | ||
| 122 | } | ||
| 123 | $db->json->pretty([$pretty]); | ||
| 124 | my $script = $db->json->encode(\@docs); | ||
| 125 | $db->json->pretty([undef]); | ||
| 126 | return $script; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub _mk_view_accessor { | ||
| 130 | my $self = shift; | ||
| 131 | my $doc = shift; | ||
| 132 | my $id = $doc->{_id}; | ||
| 133 | return unless $id =~ /^_design/; | ||
| 134 | my ($design) = $id =~ /^_design\/(.+)/; | ||
| 135 | my $views = $doc->{views}; | ||
| 136 | for my $view (keys %$views) { | ||
| 137 | my $method = $design."_".$view; | ||
| 138 | $self->views()->{$method} = sub { | ||
| 139 | my $args = shift; | ||
| 140 | return $self->server->view($design."/$view", $args); | ||
| 141 | }; | ||
| 142 | |||
| 143 | #use Moose and Class::Mop to dynamically add our method | ||
| 144 | __PACKAGE__->meta->add_method($method, sub { | ||
| 145 | my $self = shift; | ||
| 146 | my $args = shift; | ||
| 147 | if ( $self->{views}{$method} ) { | ||
| 148 | return $self->{views}{$method}->($args); | ||
| 149 | } | ||
| 150 | croak "The view $id does not exist in this database"; | ||
| 151 | } | ||
| 152 | ); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | =head2 schema | ||
| 157 | |||
| 158 | Returns the database schema as an arrayref of _design/ docs serialized to perl | ||
| 159 | objects. You can update you schema by modifying this object if you know what | ||
| 160 | you are doing. Then push the modifications to your database. | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | sub _schema_no_revs { | ||
| 165 | my $self = shift; | ||
| 166 | my @schema; | ||
| 167 | for my $doc (@{ $self->schema() }) { | ||
| 168 | my %newdoc = %$doc; | ||
| 169 | delete $newdoc{_rev}; | ||
| 170 | push @schema, \%newdoc; | ||
| 171 | } | ||
| 172 | return @schema; | ||
| 173 | } | ||
| 174 | |||
| 175 | =head2 dump($pretty) | ||
| 176 | |||
| 177 | Returns the database schema as a json string. | ||
| 178 | if $pretty is true then the dump will be pretty printed | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | sub dump { | ||
| 183 | my $self = shift; | ||
| 184 | my $pretty = shift; | ||
| 185 | my $db = $self->server; | ||
| 186 | $db->json->pretty([$pretty]); | ||
| 187 | my @schema = $self->_schema_no_revs(); | ||
| 188 | my $script = $db->json->encode(\@schema); | ||
| 189 | $db->json->pretty([undef]); | ||
| 190 | return $script; | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | =head2 push() | ||
| 195 | |||
| 196 | Pushes the current schema stored in the object to the database. Used in combination with load_schema_from_script | ||
| 197 | you can restore or create databse schemas from a json defintion file. | ||
| 198 | |||
| 199 | =cut | ||
| 200 | |||
| 201 | sub push { | ||
| 202 | my $self = shift; | ||
| 203 | my $db = $self->server; | ||
| 204 | for my $doc ( $self->_schema_no_revs() ) { | ||
| 205 | $db->create_named_doc($doc, $doc->{_id}); | ||
| 206 | } | ||
| 207 | $self->load_schema_from_db(); | ||
| 208 | return $self; | ||
| 209 | } | ||
| 210 | |||
| 211 | =head2 push_from_script | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | sub push_from_script { | ||
| 216 | my $self = shift; | ||
| 217 | my $script = shift; | ||
| 218 | my $db = $self->server; | ||
| 219 | my $docs = $db->json->decode($script); | ||
| 220 | for my $doc ( @$docs ) { | ||
| 221 | delete $doc->{_rev} | ||
| 222 | if $doc->{_rev}; | ||
| 223 | $db->create_named_doc($doc, $doc->{_id}); | ||
| 224 | } | ||
| 225 | $self->load_schema_from_db(); | ||
| 226 | return $self; | ||
| 227 | } | ||
| 228 | |||
| 229 | =head2 get($doc) | ||
| 230 | |||
| 231 | =cut | ||
| 232 | |||
| 233 | sub get { | ||
| 234 | my $self = shift; | ||
| 235 | my $name = shift; | ||
| 236 | return $self->server->get_doc($name); | ||
| 237 | } | ||
| 238 | |||
| 239 | =head2 create_doc(%sargs) | ||
| 240 | |||
| 241 | create a doc on the server. accepts the following arguments | ||
| 242 | |||
| 243 | id => 'the name of the document' #optional if you want to let CouchDB name it for you | ||
| 244 | doc => $object #not optional $object is the document to store in CouchDB | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | sub create_doc { | ||
| 249 | my $self = shift; | ||
| 250 | my %args = @_; | ||
| 251 | my $db = $self->server; | ||
| 252 | if ( $args{id} ) { | ||
| 253 | my $id = $args{id}; | ||
| 254 | return $db->create_named_doc( $args{doc}, $args{id} ); | ||
| 255 | } else { | ||
| 256 | return $db->create_doc( $args{doc} ); | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | =head2 create_new_db(db => 'database name') | ||
| 261 | |||
| 262 | create a new database in CouchDB and return a DB::CouchDB::Schema object for it. | ||
| 263 | |||
| 264 | It takes the following argument | ||
| 265 | |||
| 266 | db => 'database name' # not optional the name of the database to create. | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | sub create_new_db { | ||
| 271 | my $self = shift; | ||
| 272 | my %args = @_; | ||
| 273 | my $db = $self->server; | ||
| 274 | if ( !$args{db} ) { | ||
| 275 | croak "Must provide a DB to create"; | ||
| 276 | } | ||
| 277 | #create a new DB::CouchDB object to represent the server | ||
| 278 | my $srv = DB::CouchDB->new( host => $db->host, | ||
| 279 | port => $db->port, | ||
| 280 | db => $args{db}, | ||
| 281 | ); | ||
| 282 | # create the database | ||
| 283 | my $result = $srv->create_db(); | ||
| 284 | if ( $result->err ) { | ||
| 285 | croak "Failed to create the DB $args{db}:". $result->errstr; | ||
| 286 | } | ||
| 287 | #create a schema object for the databas and return ite | ||
| 288 | my $schema = DB::CouchDB::Schema->new(); | ||
| 289 | $schema->server($srv); | ||
| 290 | return $schema; | ||
| 291 | } | ||
| 292 | |||
| 293 | =head2 wipe | ||
| 294 | |||
| 295 | Wipes the schema from the database. Only deletes the views not data and | ||
| 296 | only deletes views it knows about from either of the load_schema_from_* methods. | ||
| 297 | |||
| 298 | =cut | ||
| 299 | |||
| 300 | sub wipe { | ||
| 301 | my $self = shift; | ||
| 302 | my $db = $self->server; | ||
| 303 | my @schema = @{ $self->schema() }; | ||
| 304 | for my $doc (@schema) { | ||
| 305 | $db->delete_doc($doc->{_id}, $doc->{_rev}); | ||
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 309 | =head1 ACCESSORS | ||
| 310 | |||
| 311 | When DB::CouchDB objects are new'ed up they create accessors for the views defined | ||
| 312 | in the Database. Calling $schema->view_name(\%view_args) will return you the data | ||
| 313 | for the views. See L<DB::CouchDB> view method for more information on the args for a view. | ||
| 314 | The view_name gets translated as follows: | ||
| 315 | |||
| 316 | #for view _design/docs/all | ||
| 317 | my $rs = $schema->docs_all({group => "true"}); | ||
| 318 | |||
| 319 | =cut | ||
| 320 | |||
| 321 | 1; |