CouchDB application development
                    Developing and deploying CouchApps


                     Jakob Westhoff <jakob@php.net>



                             PHPBarcamp.at
                              May 1, 2010




http://westhoffswelt.de      jakob@westhoffswelt.de        slide: 1 / 35
About Me



        Jakob Westhoff

              PHP developer for more several years
              Computer science student at the TU Dortmund

              Co-Founder of the PHP Usergroup Dortmund
              Active in different Open Source projects




http://westhoffswelt.de        jakob@westhoffswelt.de         slide: 2 / 35
Asking the audience



         Who is actively using CouchDB?
         Who played with CouchDB just for fun?
         Who has heard of CouchDB, but never used it?

         Who has already developed a CouchApp?
         Who does not know at all, what a CouchApp is?




 http://westhoffswelt.de    jakob@westhoffswelt.de         slide: 3 / 35
Asking the audience



         Who is actively using CouchDB?
         Who played with CouchDB just for fun?
         Who has heard of CouchDB, but never used it?

         Who has already developed a CouchApp?
         Who does not know at all, what a CouchApp is?




 http://westhoffswelt.de    jakob@westhoffswelt.de         slide: 3 / 35
Asking the audience



         Who is actively using CouchDB?
         Who played with CouchDB just for fun?
         Who has heard of CouchDB, but never used it?

         Who has already developed a CouchApp?
         Who does not know at all, what a CouchApp is?




 http://westhoffswelt.de    jakob@westhoffswelt.de         slide: 3 / 35
Asking the audience



         Who is actively using CouchDB?
         Who played with CouchDB just for fun?
         Who has heard of CouchDB, but never used it?

         Who has already developed a CouchApp?
         Who does not know at all, what a CouchApp is?




 http://westhoffswelt.de    jakob@westhoffswelt.de         slide: 3 / 35
Asking the audience



         Who is actively using CouchDB?
         Who played with CouchDB just for fun?
         Who has heard of CouchDB, but never used it?

         Who has already developed a CouchApp?
         Who does not know at all, what a CouchApp is?




 http://westhoffswelt.de    jakob@westhoffswelt.de         slide: 3 / 35
Goals of this session



         Understand what a CouchApp actually is
         Learn what features of CouchDB can be used to realize a
         CouchApp
         Get a glimpse of third party tools and frameworks for
         CouchApp development




 http://westhoffswelt.de     jakob@westhoffswelt.de          slide: 4 / 35
Goals of this session



         Understand what a CouchApp actually is
         Learn what features of CouchDB can be used to realize a
         CouchApp
         Get a glimpse of third party tools and frameworks for
         CouchApp development




 http://westhoffswelt.de     jakob@westhoffswelt.de          slide: 4 / 35
Goals of this session



         Understand what a CouchApp actually is
         Learn what features of CouchDB can be used to realize a
         CouchApp
         Get a glimpse of third party tools and frameworks for
         CouchApp development




 http://westhoffswelt.de     jakob@westhoffswelt.de          slide: 4 / 35
What is CouchDB?



        Document based NoSQL database
        Written in Erlang
        MapReduce based indexing
        Javascript uses as embedded language
        RESTful JSON API
        Build in incremental bi-directional replication




http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 5 / 35
What is CouchDB?



        Document based NoSQL database
        Written in Erlang
        MapReduce based indexing
        Javascript uses as embedded language
        RESTful JSON API
        Build in incremental bi-directional replication




http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 5 / 35
What is CouchDB?



        Document based NoSQL database
        Written in Erlang
        MapReduce based indexing
        Javascript uses as embedded language
        RESTful JSON API
        Build in incremental bi-directional replication




http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 5 / 35
What is CouchDB?



        Document based NoSQL database
        Written in Erlang
        MapReduce based indexing
        Javascript uses as embedded language
        RESTful JSON API
        Build in incremental bi-directional replication




http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 5 / 35
What is CouchDB?



        Document based NoSQL database
        Written in Erlang
        MapReduce based indexing
        Javascript uses as embedded language
        RESTful JSON API
        Build in incremental bi-directional replication




http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 5 / 35
What is CouchDB?



        Document based NoSQL database
        Written in Erlang
        MapReduce based indexing
        Javascript uses as embedded language
        RESTful JSON API
        Build in incremental bi-directional replication




http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 5 / 35
CouchDB document example

         Documents are stored as JSON

     {
         ” i d ” : ” r e c i p e −some−r e c i p e ” ,
         ” r e v ” : ”1−859 e 8 f 9 a 0 6 b 8 6 4 e b c a 3 a 9 2 9 7 5 0 c 1 7 5 3 7 ” ,
         ” date ” : 1270067388 ,
         ” t i t l e ” : ”Some r e c i p e ” ,
         ”ingredients”: [
             ” Sugar ” ,
         ” Flour ”
         ],
         ” i n s t r u c t i o n s ” : ”Some i n s t r u c t i o n s ” ,
         ” u s e r ” : ” u s e r −f o o b a r ” ,
     }




http://westhoffswelt.de               jakob@westhoffswelt.de                         slide: 6 / 35
CouchDB document example

         Documents are stored as JSON

     {
         ” i d ” : ” r e c i p e −some−r e c i p e ” ,
         ” r e v ” : ”1−859 e 8 f 9 a 0 6 b 8 6 4 e b c a 3 a 9 2 9 7 5 0 c 1 7 5 3 7 ” ,
         ” date ” : 1270067388 ,
         ” t i t l e ” : ”Some r e c i p e ” ,
         ”ingredients”: [
             ” Sugar ” ,
         ” Flour ”
         ],
         ” i n s t r u c t i o n s ” : ”Some i n s t r u c t i o n s ” ,
         ” u s e r ” : ” u s e r −f o o b a r ” ,
     }




http://westhoffswelt.de               jakob@westhoffswelt.de                         slide: 6 / 35
What is a CouchApp?


         Application build on top of CouchDB which fits the following
         criteria:
               Using CouchDB as data store
               Stored inside the CouchDB
               Served using the CouchDB Http-Server
         Most CouchApps are
               Mostly written in Javascript
               Using some sort of Javascript library
         CouchDB replication can be used to deploy applications




 http://westhoffswelt.de        jakob@westhoffswelt.de     slide: 7 / 35
What is a CouchApp?


         Application build on top of CouchDB which fits the following
         criteria:
               Using CouchDB as data store
               Stored inside the CouchDB
               Served using the CouchDB Http-Server
         Most CouchApps are
               Mostly written in Javascript
               Using some sort of Javascript library
         CouchDB replication can be used to deploy applications




 http://westhoffswelt.de        jakob@westhoffswelt.de     slide: 7 / 35
What is a CouchApp?


         Application build on top of CouchDB which fits the following
         criteria:
               Using CouchDB as data store
               Stored inside the CouchDB
               Served using the CouchDB Http-Server
         Most CouchApps are
               Mostly written in Javascript
               Using some sort of Javascript library
         CouchDB replication can be used to deploy applications




 http://westhoffswelt.de        jakob@westhoffswelt.de     slide: 7 / 35
What is a CouchApp?


         Application build on top of CouchDB which fits the following
         criteria:
               Using CouchDB as data store
               Stored inside the CouchDB
               Served using the CouchDB Http-Server
         Most CouchApps are
               Mostly written in Javascript
               Using some sort of Javascript library
         CouchDB replication can be used to deploy applications




 http://westhoffswelt.de        jakob@westhoffswelt.de     slide: 7 / 35
What is a CouchApp?


         Application build on top of CouchDB which fits the following
         criteria:
               Using CouchDB as data store
               Stored inside the CouchDB
               Served using the CouchDB Http-Server
         Most CouchApps are
               Mostly written in Javascript
               Using some sort of Javascript library
         CouchDB replication can be used to deploy applications




 http://westhoffswelt.de        jakob@westhoffswelt.de     slide: 7 / 35
What is a CouchApp?


         Application build on top of CouchDB which fits the following
         criteria:
               Using CouchDB as data store
               Stored inside the CouchDB
               Served using the CouchDB Http-Server
         Most CouchApps are
               Mostly written in Javascript
               Using some sort of Javascript library
         CouchDB replication can be used to deploy applications




 http://westhoffswelt.de        jakob@westhoffswelt.de     slide: 7 / 35
What is a CouchApp?


         Application build on top of CouchDB which fits the following
         criteria:
               Using CouchDB as data store
               Stored inside the CouchDB
               Served using the CouchDB Http-Server
         Most CouchApps are
               Mostly written in Javascript
               Using some sort of Javascript library
         CouchDB replication can be used to deploy applications




 http://westhoffswelt.de        jakob@westhoffswelt.de     slide: 7 / 35
What is a CouchApp?


         Application build on top of CouchDB which fits the following
         criteria:
               Using CouchDB as data store
               Stored inside the CouchDB
               Served using the CouchDB Http-Server
         Most CouchApps are
               Mostly written in Javascript
               Using some sort of Javascript library
         CouchDB replication can be used to deploy applications




 http://westhoffswelt.de        jakob@westhoffswelt.de     slide: 7 / 35
What is a CouchApp?


         Application build on top of CouchDB which fits the following
         criteria:
               Using CouchDB as data store
               Stored inside the CouchDB
               Served using the CouchDB Http-Server
         Most CouchApps are
               Mostly written in Javascript
               Using some sort of Javascript library
         CouchDB replication can be used to deploy applications




 http://westhoffswelt.de        jakob@westhoffswelt.de     slide: 7 / 35
Excursion: Futon
         Futon is CouchDBs adminstration interface
         Automatically available on every CouchDB
               http://localhost:5984/ utils/




 http://westhoffswelt.de     jakob@westhoffswelt.de    slide: 8 / 35
Excursion: Futon
         Futon is CouchDBs adminstration interface
         Automatically available on every CouchDB
               http://localhost:5984/ utils/




 http://westhoffswelt.de     jakob@westhoffswelt.de    slide: 8 / 35
Excursion: Futon
         Futon is CouchDBs adminstration interface
         Automatically available on every CouchDB
               http://localhost:5984/ utils/




 http://westhoffswelt.de     jakob@westhoffswelt.de    slide: 8 / 35
Which CouchDB Version to use?



         CouchDB 0.11 or greater
               User management
               Authentication API: Cookie, OAuth, plain-HTTP
               Per-db reader access lists
               Validation functions
               Url rewriting
               Vhost configuration




 http://westhoffswelt.de       jakob@westhoffswelt.de            slide: 9 / 35
Which CouchDB Version to use?



         CouchDB 0.11 or greater
               User management
               Authentication API: Cookie, OAuth, plain-HTTP
               Per-db reader access lists
               Validation functions
               Url rewriting
               Vhost configuration




 http://westhoffswelt.de       jakob@westhoffswelt.de            slide: 9 / 35
Which CouchDB Version to use?



         CouchDB 0.11 or greater
               User management
               Authentication API: Cookie, OAuth, plain-HTTP
               Per-db reader access lists
               Validation functions
               Url rewriting
               Vhost configuration




 http://westhoffswelt.de       jakob@westhoffswelt.de            slide: 9 / 35
Which CouchDB Version to use?



         CouchDB 0.11 or greater
               User management
               Authentication API: Cookie, OAuth, plain-HTTP
               Per-db reader access lists
               Validation functions
               Url rewriting
               Vhost configuration




 http://westhoffswelt.de       jakob@westhoffswelt.de            slide: 9 / 35
Which CouchDB Version to use?



         CouchDB 0.11 or greater
               User management
               Authentication API: Cookie, OAuth, plain-HTTP
               Per-db reader access lists
               Validation functions
               Url rewriting
               Vhost configuration




 http://westhoffswelt.de       jakob@westhoffswelt.de            slide: 9 / 35
Which CouchDB Version to use?



         CouchDB 0.11 or greater
               User management
               Authentication API: Cookie, OAuth, plain-HTTP
               Per-db reader access lists
               Validation functions
               Url rewriting
               Vhost configuration




 http://westhoffswelt.de       jakob@westhoffswelt.de            slide: 9 / 35
Which CouchDB Version to use?



         CouchDB 0.11 or greater
               User management
               Authentication API: Cookie, OAuth, plain-HTTP
               Per-db reader access lists
               Validation functions
               Url rewriting
               Vhost configuration




 http://westhoffswelt.de       jakob@westhoffswelt.de            slide: 9 / 35
CouchApp techniques


         Store all application files as document attachments
               Attachments can be stored for any document inside CouchDB
               Can be directly accesed using the RESTful interface
         Use CouchDBs authentication backend for user management
         Use validation function for access restrictions
         Use show and list functions to output HTML
         Access CouchDB using Javascript and XHR
         Use CouchDB vhosts and url rewriting for nice urls




 http://westhoffswelt.de       jakob@westhoffswelt.de         slide: 10 / 35
CouchApp techniques


         Store all application files as document attachments
               Attachments can be stored for any document inside CouchDB
               Can be directly accesed using the RESTful interface
         Use CouchDBs authentication backend for user management
         Use validation function for access restrictions
         Use show and list functions to output HTML
         Access CouchDB using Javascript and XHR
         Use CouchDB vhosts and url rewriting for nice urls




 http://westhoffswelt.de       jakob@westhoffswelt.de         slide: 10 / 35
CouchApp techniques


         Store all application files as document attachments
               Attachments can be stored for any document inside CouchDB
               Can be directly accesed using the RESTful interface
         Use CouchDBs authentication backend for user management
         Use validation function for access restrictions
         Use show and list functions to output HTML
         Access CouchDB using Javascript and XHR
         Use CouchDB vhosts and url rewriting for nice urls




 http://westhoffswelt.de       jakob@westhoffswelt.de         slide: 10 / 35
CouchApp techniques


         Store all application files as document attachments
               Attachments can be stored for any document inside CouchDB
               Can be directly accesed using the RESTful interface
         Use CouchDBs authentication backend for user management
         Use validation function for access restrictions
         Use show and list functions to output HTML
         Access CouchDB using Javascript and XHR
         Use CouchDB vhosts and url rewriting for nice urls




 http://westhoffswelt.de       jakob@westhoffswelt.de         slide: 10 / 35
CouchApp techniques


         Store all application files as document attachments
               Attachments can be stored for any document inside CouchDB
               Can be directly accesed using the RESTful interface
         Use CouchDBs authentication backend for user management
         Use validation function for access restrictions
         Use show and list functions to output HTML
         Access CouchDB using Javascript and XHR
         Use CouchDB vhosts and url rewriting for nice urls




 http://westhoffswelt.de       jakob@westhoffswelt.de         slide: 10 / 35
CouchApp techniques


         Store all application files as document attachments
               Attachments can be stored for any document inside CouchDB
               Can be directly accesed using the RESTful interface
         Use CouchDBs authentication backend for user management
         Use validation function for access restrictions
         Use show and list functions to output HTML
         Access CouchDB using Javascript and XHR
         Use CouchDB vhosts and url rewriting for nice urls




 http://westhoffswelt.de       jakob@westhoffswelt.de         slide: 10 / 35
CouchApp techniques


         Store all application files as document attachments
               Attachments can be stored for any document inside CouchDB
               Can be directly accesed using the RESTful interface
         Use CouchDBs authentication backend for user management
         Use validation function for access restrictions
         Use show and list functions to output HTML
         Access CouchDB using Javascript and XHR
         Use CouchDB vhosts and url rewriting for nice urls




 http://westhoffswelt.de       jakob@westhoffswelt.de         slide: 10 / 35
CouchApp techniques


         Store all application files as document attachments
               Attachments can be stored for any document inside CouchDB
               Can be directly accesed using the RESTful interface
         Use CouchDBs authentication backend for user management
         Use validation function for access restrictions
         Use show and list functions to output HTML
         Access CouchDB using Javascript and XHR
         Use CouchDB vhosts and url rewriting for nice urls




 http://westhoffswelt.de       jakob@westhoffswelt.de         slide: 10 / 35
Authentication backend: User Management

   Managing users:
         Futon:




         RESTful API:
               Create a new document in the users database
               http://localhost:5984/ users/org.couchdb.user:foo




 http://westhoffswelt.de     jakob@westhoffswelt.de     slide: 11 / 35
Authentication backend: User Management

   Managing users:
         Futon:




         RESTful API:
               Create a new document in the users database
               http://localhost:5984/ users/org.couchdb.user:foo




 http://westhoffswelt.de     jakob@westhoffswelt.de     slide: 11 / 35
Authentication backend: User Management

   Managing users:
         Futon:




         RESTful API:
               Create a new document in the users database
               http://localhost:5984/ users/org.couchdb.user:foo




 http://westhoffswelt.de     jakob@westhoffswelt.de     slide: 11 / 35
Authentication backend: Logging in (Cookie)

   Logging in:
         POST username and password to
               http://localhost:5984/ session

         Parameters user and password
         application/x-www-form-urlencoded encoded


         Post:
         name=f o o&p a s s w o r d=b a r

         Response:
         { ” ok ” : t r u e , ”name” : ” f o o ” , ” r o l e s ” : [ ] }




 http://westhoffswelt.de               jakob@westhoffswelt.de                slide: 12 / 35
Authentication backend: Logging in (Cookie)

   Logging in:
         POST username and password to
               http://localhost:5984/ session

         Parameters user and password
         application/x-www-form-urlencoded encoded


         Post:
         name=f o o&p a s s w o r d=b a r

         Response:
         { ” ok ” : t r u e , ”name” : ” f o o ” , ” r o l e s ” : [ ] }




 http://westhoffswelt.de               jakob@westhoffswelt.de                slide: 12 / 35
Authentication backend: Logging in (Cookie)

   Logging in:
         POST username and password to
               http://localhost:5984/ session

         Parameters user and password
         application/x-www-form-urlencoded encoded


         Post:
         name=f o o&p a s s w o r d=b a r

         Response:
         { ” ok ” : t r u e , ”name” : ” f o o ” , ” r o l e s ” : [ ] }




 http://westhoffswelt.de               jakob@westhoffswelt.de                slide: 12 / 35
Authentication backend: Logging in (Cookie)

   Logging in:
         POST username and password to
               http://localhost:5984/ session

         Parameters user and password
         application/x-www-form-urlencoded encoded


         Post:
         name=f o o&p a s s w o r d=b a r

         Response:
         { ” ok ” : t r u e , ”name” : ” f o o ” , ” r o l e s ” : [ ] }




 http://westhoffswelt.de               jakob@westhoffswelt.de                slide: 12 / 35
Authentication backend: Logging in (Cookie)

   Logging in:
         POST username and password to
               http://localhost:5984/ session

         Parameters user and password
         application/x-www-form-urlencoded encoded


         Post:
         name=f o o&p a s s w o r d=b a r

         Response:
         { ” ok ” : t r u e , ”name” : ” f o o ” , ” r o l e s ” : [ ] }




 http://westhoffswelt.de               jakob@westhoffswelt.de                slide: 12 / 35
Excursion: Design documents


         Design documents supply certain special functionallities per db
         Stored with special id design/some-id
         Multiple design documents allowed per database

         Used to define:
               View functions (Map&Reduce)
               List functions
               Show functions
               Validation functions
               ...




 http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 13 / 35
Excursion: Design documents


         Design documents supply certain special functionallities per db
         Stored with special id design/some-id
         Multiple design documents allowed per database

         Used to define:
               View functions (Map&Reduce)
               List functions
               Show functions
               Validation functions
               ...




 http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 13 / 35
Excursion: Design documents


         Design documents supply certain special functionallities per db
         Stored with special id design/some-id
         Multiple design documents allowed per database

         Used to define:
               View functions (Map&Reduce)
               List functions
               Show functions
               Validation functions
               ...




 http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 13 / 35
Excursion: Design documents


         Design documents supply certain special functionallities per db
         Stored with special id design/some-id
         Multiple design documents allowed per database

         Used to define:
               View functions (Map&Reduce)
               List functions
               Show functions
               Validation functions
               ...




 http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 13 / 35
Excursion: Design documents


         Design documents supply certain special functionallities per db
         Stored with special id design/some-id
         Multiple design documents allowed per database

         Used to define:
               View functions (Map&Reduce)
               List functions
               Show functions
               Validation functions
               ...




 http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 13 / 35
Excursion: Design documents


         Design documents supply certain special functionallities per db
         Stored with special id design/some-id
         Multiple design documents allowed per database

         Used to define:
               View functions (Map&Reduce)
               List functions
               Show functions
               Validation functions
               ...




 http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 13 / 35
Excursion: Design documents


         Design documents supply certain special functionallities per db
         Stored with special id design/some-id
         Multiple design documents allowed per database

         Used to define:
               View functions (Map&Reduce)
               List functions
               Show functions
               Validation functions
               ...




 http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 13 / 35
Excursion: Design documents


         Design documents supply certain special functionallities per db
         Stored with special id design/some-id
         Multiple design documents allowed per database

         Used to define:
               View functions (Map&Reduce)
               List functions
               Show functions
               Validation functions
               ...




 http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 13 / 35
Excursion: Design documents


         Design documents supply certain special functionallities per db
         Stored with special id design/some-id
         Multiple design documents allowed per database

         Used to define:
               View functions (Map&Reduce)
               List functions
               Show functions
               Validation functions
               ...




 http://westhoffswelt.de      jakob@westhoffswelt.de         slide: 13 / 35
Validation functions: Validate updates


         Validation of document creations or updates using Javascript
         functions
         Registration: Property validate doc update on any design
         document
         Multiple validation functions on different design documents
         possible
               Called one after another
               No defined execution order: Functions need to be isolated
               One fails: validation fails




 http://westhoffswelt.de       jakob@westhoffswelt.de           slide: 14 / 35
Validation functions: Validate updates


         Validation of document creations or updates using Javascript
         functions
         Registration: Property validate doc update on any design
         document
         Multiple validation functions on different design documents
         possible
               Called one after another
               No defined execution order: Functions need to be isolated
               One fails: validation fails




 http://westhoffswelt.de       jakob@westhoffswelt.de           slide: 14 / 35
Validation functions: Validate updates


         Validation of document creations or updates using Javascript
         functions
         Registration: Property validate doc update on any design
         document
         Multiple validation functions on different design documents
         possible
               Called one after another
               No defined execution order: Functions need to be isolated
               One fails: validation fails




 http://westhoffswelt.de       jakob@westhoffswelt.de           slide: 14 / 35
Validation functions: Validate updates


         Validation of document creations or updates using Javascript
         functions
         Registration: Property validate doc update on any design
         document
         Multiple validation functions on different design documents
         possible
               Called one after another
               No defined execution order: Functions need to be isolated
               One fails: validation fails




 http://westhoffswelt.de       jakob@westhoffswelt.de           slide: 14 / 35
Validation functions: Validate updates


         Validation of document creations or updates using Javascript
         functions
         Registration: Property validate doc update on any design
         document
         Multiple validation functions on different design documents
         possible
               Called one after another
               No defined execution order: Functions need to be isolated
               One fails: validation fails




 http://westhoffswelt.de       jakob@westhoffswelt.de           slide: 14 / 35
Validation functions: Validate updates


         Validation of document creations or updates using Javascript
         functions
         Registration: Property validate doc update on any design
         document
         Multiple validation functions on different design documents
         possible
               Called one after another
               No defined execution order: Functions need to be isolated
               One fails: validation fails




 http://westhoffswelt.de       jakob@westhoffswelt.de           slide: 14 / 35
Validation functions: Validate updates II

          Example
               Ensure all documents have a type field
               added is not allowed to be changed after document creation

      f u n c t i o n ( newDoc , oldDoc , u s e r C t x ) {
          i f ( ! newDoc . t y p e ) {
             throw ( { f o r b i d d e n : ” Mandatory f i e l d   ’ type ’ i s
                     m i s s i n g ” }) ;
          }

          i f ( ol dD oc && toJSON ( oldDoc . added ) != toJSON ( newDoc
               . added ) ) {
             throw ( . . . )
          }
      }




 http://westhoffswelt.de          jakob@westhoffswelt.de                  slide: 15 / 35
Validation functions: Validate updates II

          Example
               Ensure all documents have a type field
               added is not allowed to be changed after document creation

      f u n c t i o n ( newDoc , oldDoc , u s e r C t x ) {
          i f ( ! newDoc . t y p e ) {
             throw ( { f o r b i d d e n : ” Mandatory f i e l d   ’ type ’ i s
                     m i s s i n g ” }) ;
          }

          i f ( ol dD oc && toJSON ( oldDoc . added ) != toJSON ( newDoc
               . added ) ) {
             throw ( . . . )
          }
      }




 http://westhoffswelt.de          jakob@westhoffswelt.de                  slide: 15 / 35
Validation functions: Validate updates II

          Example
               Ensure all documents have a type field
               added is not allowed to be changed after document creation

      f u n c t i o n ( newDoc , oldDoc , u s e r C t x ) {
          i f ( ! newDoc . t y p e ) {
             throw ( { f o r b i d d e n : ” Mandatory f i e l d   ’ type ’ i s
                     m i s s i n g ” }) ;
          }

          i f ( ol dD oc && toJSON ( oldDoc . added ) != toJSON ( newDoc
               . added ) ) {
             throw ( . . . )
          }
      }




 http://westhoffswelt.de          jakob@westhoffswelt.de                  slide: 15 / 35
Validation functions: Access control

          Use validation doc update function as access control
          system
               Ensure the user is logged in
               Ensure username and author field are identical

      f u n c t i o n ( newDoc , oldDoc , u s e r C t x ) {
          i f ( ! u s e r C t x . name ) {
             throw ( { f o r b i d d e n : ”You need t o be l o g g e d i n t o
                     c h a n g e documents . ” } ) ;
          }

          i f ( ( ol dD oc && u s e r C t x . name != oldDoc . a u t h o r ) | |
               u s e r C t x . name != newDoc . a u t h o r ) {
             throw ( . . . )
          }
      }



 http://westhoffswelt.de          jakob@westhoffswelt.de                slide: 16 / 35
Validation functions: Access control

          Use validation doc update function as access control
          system
               Ensure the user is logged in
               Ensure username and author field are identical

      f u n c t i o n ( newDoc , oldDoc , u s e r C t x ) {
          i f ( ! u s e r C t x . name ) {
             throw ( { f o r b i d d e n : ”You need t o be l o g g e d i n t o
                     c h a n g e documents . ” } ) ;
          }

          i f ( ( ol dD oc && u s e r C t x . name != oldDoc . a u t h o r ) | |
               u s e r C t x . name != newDoc . a u t h o r ) {
             throw ( . . . )
          }
      }



 http://westhoffswelt.de          jakob@westhoffswelt.de                slide: 16 / 35
Validation functions: Access control

          Use validation doc update function as access control
          system
               Ensure the user is logged in
               Ensure username and author field are identical

      f u n c t i o n ( newDoc , oldDoc , u s e r C t x ) {
          i f ( ! u s e r C t x . name ) {
             throw ( { f o r b i d d e n : ”You need t o be l o g g e d i n t o
                     c h a n g e documents . ” } ) ;
          }

          i f ( ( ol dD oc && u s e r C t x . name != oldDoc . a u t h o r ) | |
               u s e r C t x . name != newDoc . a u t h o r ) {
             throw ( . . . )
          }
      }



 http://westhoffswelt.de          jakob@westhoffswelt.de                slide: 16 / 35
Show and list functions

         User formatted data instead of plain JSON
         Show functions format documents
         List functions format views
         Multiple show and list functions are allowed
         per-design-document

         Stored as shows and lists object

      ” shows ” : {
         ” summary ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” ,
         ” d e t a i l ” : ” f u n c t i o n ( doc , r e q ) { . . . } ”
      }




 http://westhoffswelt.de              jakob@westhoffswelt.de                 slide: 17 / 35
Show and list functions

         User formatted data instead of plain JSON
         Show functions format documents
         List functions format views
         Multiple show and list functions are allowed
         per-design-document

         Stored as shows and lists object

      ” shows ” : {
         ” summary ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” ,
         ” d e t a i l ” : ” f u n c t i o n ( doc , r e q ) { . . . } ”
      }




 http://westhoffswelt.de              jakob@westhoffswelt.de                 slide: 17 / 35
Show and list functions

         User formatted data instead of plain JSON
         Show functions format documents
         List functions format views
         Multiple show and list functions are allowed
         per-design-document

         Stored as shows and lists object

      ” shows ” : {
         ” summary ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” ,
         ” d e t a i l ” : ” f u n c t i o n ( doc , r e q ) { . . . } ”
      }




 http://westhoffswelt.de              jakob@westhoffswelt.de                 slide: 17 / 35
Show and list functions

         User formatted data instead of plain JSON
         Show functions format documents
         List functions format views
         Multiple show and list functions are allowed
         per-design-document

         Stored as shows and lists object

      ” shows ” : {
         ” summary ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” ,
         ” d e t a i l ” : ” f u n c t i o n ( doc , r e q ) { . . . } ”
      }




 http://westhoffswelt.de              jakob@westhoffswelt.de                 slide: 17 / 35
Show and list functions

         User formatted data instead of plain JSON
         Show functions format documents
         List functions format views
         Multiple show and list functions are allowed
         per-design-document

         Stored as shows and lists object

      ” shows ” : {
         ” summary ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” ,
         ” d e t a i l ” : ” f u n c t i o n ( doc , r e q ) { . . . } ”
      }




 http://westhoffswelt.de              jakob@westhoffswelt.de                 slide: 17 / 35
Show function to output HTML


         Arguments
               doc - Document to be returned
               req - Request object
                    User context
                    GET parameter
                    Accept header
                    ...
         Return value is a response object
               body - Content returned to the caller
               headers - HTML headers (Can be used to set Content-Type)
               base64 - Can be used instead of body to send binary data




 http://westhoffswelt.de       jakob@westhoffswelt.de        slide: 18 / 35
Show function to output HTML


         Arguments
               doc - Document to be returned
               req - Request object
                    User context
                    GET parameter
                    Accept header
                    ...
         Return value is a response object
               body - Content returned to the caller
               headers - HTML headers (Can be used to set Content-Type)
               base64 - Can be used instead of body to send binary data




 http://westhoffswelt.de       jakob@westhoffswelt.de        slide: 18 / 35
Show function to output HTML


         Arguments
               doc - Document to be returned
               req - Request object
                    User context
                    GET parameter
                    Accept header
                    ...
         Return value is a response object
               body - Content returned to the caller
               headers - HTML headers (Can be used to set Content-Type)
               base64 - Can be used instead of body to send binary data




 http://westhoffswelt.de       jakob@westhoffswelt.de        slide: 18 / 35
Show function to output HTML


         Arguments
               doc - Document to be returned
               req - Request object
                    User context
                    GET parameter
                    Accept header
                    ...
         Return value is a response object
               body - Content returned to the caller
               headers - HTML headers (Can be used to set Content-Type)
               base64 - Can be used instead of body to send binary data




 http://westhoffswelt.de       jakob@westhoffswelt.de        slide: 18 / 35
Show function to output HTML


         Arguments
               doc - Document to be returned
               req - Request object
                    User context
                    GET parameter
                    Accept header
                    ...
         Return value is a response object
               body - Content returned to the caller
               headers - HTML headers (Can be used to set Content-Type)
               base64 - Can be used instead of body to send binary data




 http://westhoffswelt.de       jakob@westhoffswelt.de        slide: 18 / 35
Show function to output HTML


         Arguments
               doc - Document to be returned
               req - Request object
                    User context
                    GET parameter
                    Accept header
                    ...
         Return value is a response object
               body - Content returned to the caller
               headers - HTML headers (Can be used to set Content-Type)
               base64 - Can be used instead of body to send binary data




 http://westhoffswelt.de       jakob@westhoffswelt.de        slide: 18 / 35
Show function to output HTML


         Arguments
               doc - Document to be returned
               req - Request object
                    User context
                    GET parameter
                    Accept header
                    ...
         Return value is a response object
               body - Content returned to the caller
               headers - HTML headers (Can be used to set Content-Type)
               base64 - Can be used instead of body to send binary data




 http://westhoffswelt.de       jakob@westhoffswelt.de        slide: 18 / 35
Show function to output HTML


         Arguments
               doc - Document to be returned
               req - Request object
                    User context
                    GET parameter
                    Accept header
                    ...
         Return value is a response object
               body - Content returned to the caller
               headers - HTML headers (Can be used to set Content-Type)
               base64 - Can be used instead of body to send binary data




 http://westhoffswelt.de       jakob@westhoffswelt.de        slide: 18 / 35
Show function to output HTML


         Arguments
               doc - Document to be returned
               req - Request object
                    User context
                    GET parameter
                    Accept header
                    ...
         Return value is a response object
               body - Content returned to the caller
               headers - HTML headers (Can be used to set Content-Type)
               base64 - Can be used instead of body to send binary data




 http://westhoffswelt.de       jakob@westhoffswelt.de        slide: 18 / 35
Show function to output HTML


         Arguments
               doc - Document to be returned
               req - Request object
                    User context
                    GET parameter
                    Accept header
                    ...
         Return value is a response object
               body - Content returned to the caller
               headers - HTML headers (Can be used to set Content-Type)
               base64 - Can be used instead of body to send binary data




 http://westhoffswelt.de       jakob@westhoffswelt.de        slide: 18 / 35
Show function to output HTML


         Arguments
               doc - Document to be returned
               req - Request object
                    User context
                    GET parameter
                    Accept header
                    ...
         Return value is a response object
               body - Content returned to the caller
               headers - HTML headers (Can be used to set Content-Type)
               base64 - Can be used instead of body to send binary data




 http://westhoffswelt.de       jakob@westhoffswelt.de        slide: 18 / 35
Show function to output HTML II

          Example
               Output document type and author as HTML

      f u n c t i o n ( doc , r e q ) {

          return {
             body : [
                 ”<h1>The ” ,
                 doc . t y p e ,
                 ” document h a s been c r e a t e d by ” ,
                 doc . a u t h o r ,
                 ”</h1>”
             ] . j o i n ( ”” ) ;
          };

      }



 http://westhoffswelt.de          jakob@westhoffswelt.de        slide: 19 / 35
Show function to output HTML II

          Example
               Output document type and author as HTML

      f u n c t i o n ( doc , r e q ) {

          return {
             body : [
                 ”<h1>The ” ,
                 doc . t y p e ,
                 ” document h a s been c r e a t e d by ” ,
                 doc . a u t h o r ,
                 ”</h1>”
             ] . j o i n ( ”” ) ;
          };

      }



 http://westhoffswelt.de          jakob@westhoffswelt.de        slide: 19 / 35
Show function to output HTML II

          Example
               Output document type and author as HTML

      f u n c t i o n ( doc , r e q ) {

          return {
             body : [
                 ”<h1>The ” ,
                 doc . t y p e ,
                 ” document h a s been c r e a t e d by ” ,
                 doc . a u t h o r ,
                 ”</h1>”
             ] . j o i n ( ”” ) ;
          };

      }



 http://westhoffswelt.de          jakob@westhoffswelt.de        slide: 19 / 35
List functions: Streaming API



         Arguments
               head - Statistical data about the view
               req - Request object
         Return value is not used
         Streaming API
               getRow() - Retrieve next row from the view, or null at the end
               send(data) - Send out the next piece of data to the caller




 http://westhoffswelt.de        jakob@westhoffswelt.de           slide: 20 / 35
List functions: Streaming API



         Arguments
               head - Statistical data about the view
               req - Request object
         Return value is not used
         Streaming API
               getRow() - Retrieve next row from the view, or null at the end
               send(data) - Send out the next piece of data to the caller




 http://westhoffswelt.de        jakob@westhoffswelt.de           slide: 20 / 35
List functions: Streaming API



         Arguments
               head - Statistical data about the view
               req - Request object
         Return value is not used
         Streaming API
               getRow() - Retrieve next row from the view, or null at the end
               send(data) - Send out the next piece of data to the caller




 http://westhoffswelt.de        jakob@westhoffswelt.de           slide: 20 / 35
List functions: Streaming API



         Arguments
               head - Statistical data about the view
               req - Request object
         Return value is not used
         Streaming API
               getRow() - Retrieve next row from the view, or null at the end
               send(data) - Send out the next piece of data to the caller




 http://westhoffswelt.de        jakob@westhoffswelt.de           slide: 20 / 35
List functions: Streaming API



         Arguments
               head - Statistical data about the view
               req - Request object
         Return value is not used
         Streaming API
               getRow() - Retrieve next row from the view, or null at the end
               send(data) - Send out the next piece of data to the caller




 http://westhoffswelt.de        jakob@westhoffswelt.de           slide: 20 / 35
List functions: Streaming API



         Arguments
               head - Statistical data about the view
               req - Request object
         Return value is not used
         Streaming API
               getRow() - Retrieve next row from the view, or null at the end
               send(data) - Send out the next piece of data to the caller




 http://westhoffswelt.de        jakob@westhoffswelt.de           slide: 20 / 35
List functions: Streaming API



         Arguments
               head - Statistical data about the view
               req - Request object
         Return value is not used
         Streaming API
               getRow() - Retrieve next row from the view, or null at the end
               send(data) - Send out the next piece of data to the caller




 http://westhoffswelt.de        jakob@westhoffswelt.de           slide: 20 / 35
List functions: Example

          Example
                Output authors in list as unordered HTML list

      f u n c t i o n ( head , r e q ) {
          var cur ;

          s e n d ( ’<u l > ’ ) ;

          w h i l e ( c u r = getRow ( ) ) {
             s e n d ( ’< l i > ’ + c u r . a u t h o r + ’</ l i > ’ ) ;
          }

          s e n d ( ’</u l > ’ ) ;
      }




 http://westhoffswelt.de              jakob@westhoffswelt.de                  slide: 21 / 35
List functions: Example

          Example
                Output authors in list as unordered HTML list

      f u n c t i o n ( head , r e q ) {
          var cur ;

          s e n d ( ’<u l > ’ ) ;

          w h i l e ( c u r = getRow ( ) ) {
             s e n d ( ’< l i > ’ + c u r . a u t h o r + ’</ l i > ’ ) ;
          }

          s e n d ( ’</u l > ’ ) ;
      }




 http://westhoffswelt.de              jakob@westhoffswelt.de                  slide: 21 / 35
List functions: Example

          Example
                Output authors in list as unordered HTML list

      f u n c t i o n ( head , r e q ) {
          var cur ;

          s e n d ( ’<u l > ’ ) ;

          w h i l e ( c u r = getRow ( ) ) {
             s e n d ( ’< l i > ’ + c u r . a u t h o r + ’</ l i > ’ ) ;
          }

          s e n d ( ’</u l > ’ ) ;
      }




 http://westhoffswelt.de              jakob@westhoffswelt.de                  slide: 21 / 35
Query show and list functions



         Queried based on design document and name

         Show functions
         GET /db/ design/mydesign/ show/show-name/doc-id

         List functions
         GET /db/ design/mydesign/ list/list-name/view-name




 http://westhoffswelt.de   jakob@westhoffswelt.de      slide: 22 / 35
Query show and list functions



         Queried based on design document and name

         Show functions
         GET /db/ design/mydesign/ show/show-name/doc-id

         List functions
         GET /db/ design/mydesign/ list/list-name/view-name




 http://westhoffswelt.de   jakob@westhoffswelt.de      slide: 22 / 35
Query show and list functions



         Queried based on design document and name

         Show functions
         GET /db/ design/mydesign/ show/show-name/doc-id

         List functions
         GET /db/ design/mydesign/ list/list-name/view-name




 http://westhoffswelt.de   jakob@westhoffswelt.de      slide: 22 / 35
Show and list functions in real applications



         Use a templating system
               Javscript micro template language based on John Resigs idea
               http://github.com/furf/jquery-template
         Use integrated provides and registerType functions to
         serve multiple formats based on the accept header
         Maybe do some sort of user authentication before showing
         content




 http://westhoffswelt.de       jakob@westhoffswelt.de           slide: 23 / 35
Show and list functions in real applications



         Use a templating system
               Javscript micro template language based on John Resigs idea
               http://github.com/furf/jquery-template
         Use integrated provides and registerType functions to
         serve multiple formats based on the accept header
         Maybe do some sort of user authentication before showing
         content




 http://westhoffswelt.de       jakob@westhoffswelt.de           slide: 23 / 35
Show and list functions in real applications



         Use a templating system
               Javscript micro template language based on John Resigs idea
               http://github.com/furf/jquery-template
         Use integrated provides and registerType functions to
         serve multiple formats based on the accept header
         Maybe do some sort of user authentication before showing
         content




 http://westhoffswelt.de       jakob@westhoffswelt.de           slide: 23 / 35
Show and list functions in real applications



         Use a templating system
               Javscript micro template language based on John Resigs idea
               http://github.com/furf/jquery-template
         Use integrated provides and registerType functions to
         serve multiple formats based on the accept header
         Maybe do some sort of user authentication before showing
         content




 http://westhoffswelt.de       jakob@westhoffswelt.de           slide: 23 / 35
Url rewriting

         Urls like this one are neither readable nor usable

   /db/ design/mydesign/ list/list-name/view-name?limit=5

         CouchDB supports internal url rewriting
               Defined as array of rewrites on any design document
      ”rewrites”: [
        {
          ” from ” : ” / i n d e x ” ,
          ” t o ” : ” l i s t / l i s t −name/ view −name? l i m i t =5”
        }
      ]




 http://westhoffswelt.de         jakob@westhoffswelt.de                slide: 24 / 35
Url rewriting

         Urls like this one are neither readable nor usable

   /db/ design/mydesign/ list/list-name/view-name?limit=5

         CouchDB supports internal url rewriting
               Defined as array of rewrites on any design document
      ”rewrites”: [
        {
          ” from ” : ” / i n d e x ” ,
          ” t o ” : ” l i s t / l i s t −name/ view −name? l i m i t =5”
        }
      ]




 http://westhoffswelt.de         jakob@westhoffswelt.de                slide: 24 / 35
Url rewriting

         Urls like this one are neither readable nor usable

   /db/ design/mydesign/ list/list-name/view-name?limit=5

         CouchDB supports internal url rewriting
               Defined as array of rewrites on any design document
      ”rewrites”: [
        {
          ” from ” : ” / i n d e x ” ,
          ” t o ” : ” l i s t / l i s t −name/ view −name? l i m i t =5”
        }
      ]




 http://westhoffswelt.de         jakob@westhoffswelt.de                slide: 24 / 35
Url rewriting

         Urls like this one are neither readable nor usable

   /db/ design/mydesign/ list/list-name/view-name?limit=5

         CouchDB supports internal url rewriting
               Defined as array of rewrites on any design document
      ”rewrites”: [
        {
          ” from ” : ” / i n d e x ” ,
          ” t o ” : ” l i s t / l i s t −name/ view −name? l i m i t =5”
        }
      ]




 http://westhoffswelt.de         jakob@westhoffswelt.de                slide: 24 / 35
Advanced url rewriting


         Using named placeholders
               Match each path segment
      ” from ” : ” / e n t r y / : doc ” ,
      ” t o ” : ” show / show−name / : doc ”

         Using match all at the end of an url
               Appends given query parameters
      ” from ” : ” / e n t r y /∗ ” ,
      ” t o ” : ” show / show−name /∗ ”




 http://westhoffswelt.de       jakob@westhoffswelt.de   slide: 25 / 35
Advanced url rewriting


         Using named placeholders
               Match each path segment
      ” from ” : ” / e n t r y / : doc ” ,
      ” t o ” : ” show / show−name / : doc ”

         Using match all at the end of an url
               Appends given query parameters
      ” from ” : ” / e n t r y /∗ ” ,
      ” t o ” : ” show / show−name /∗ ”




 http://westhoffswelt.de       jakob@westhoffswelt.de   slide: 25 / 35
Advanced url rewriting II

         Rewriting to query parameters
      ” from ” : ” / page / : s t a r t ” ,
      ” t o ” : ” l i s t / l i s t −name/ p a g e s ” ,
      ” query ” : {
          ” startkey ”: ”: start ”
      }

         Adress a context outside of the design document
               Targets are always relative to the design document
               Relative navigation with .. is allowed
      ” from ” : ” / raw / : doc ” ,
      ” t o ” : ” . . / . . / : doc ”




 http://westhoffswelt.de             jakob@westhoffswelt.de      slide: 26 / 35
Advanced url rewriting II

         Rewriting to query parameters
      ” from ” : ” / page / : s t a r t ” ,
      ” t o ” : ” l i s t / l i s t −name/ p a g e s ” ,
      ” query ” : {
          ” startkey ”: ”: start ”
      }

         Adress a context outside of the design document
               Targets are always relative to the design document
               Relative navigation with .. is allowed
      ” from ” : ” / raw / : doc ” ,
      ” t o ” : ” . . / . . / : doc ”




 http://westhoffswelt.de             jakob@westhoffswelt.de      slide: 26 / 35
Advanced url rewriting III


           Rewriting based on HTTP methods
      {
           ” from ” : ” / e n t r y /∗ ” ,
           ” t o ” : ” show / show−name /∗ ” ,
           ” method ” : ”GET”
      },
      {
           ” from ” : ” / e n t r y /∗ ” ,
           ” t o ” : ” u p d a t e / update −name /∗ ” ,
           ” method ” : ”POST”
      },




 http://westhoffswelt.de            jakob@westhoffswelt.de   slide: 27 / 35
Query rewritten urls




         Queried on design document using special rewrite path
         /db/ design/mydesign/ rewrite/some-url

         Better, but far from being perfect → Vhost configuration




 http://westhoffswelt.de    jakob@westhoffswelt.de        slide: 28 / 35
Query rewritten urls




         Queried on design document using special rewrite path
         /db/ design/mydesign/ rewrite/some-url

         Better, but far from being perfect → Vhost configuration




 http://westhoffswelt.de    jakob@westhoffswelt.de        slide: 28 / 35
Query rewritten urls




         Queried on design document using special rewrite path
         /db/ design/mydesign/ rewrite/some-url

         Better, but far from being perfect → Vhost configuration




 http://westhoffswelt.de    jakob@westhoffswelt.de        slide: 28 / 35
Vhost configuration



         CouchDB supports url changing based on the provided Host
         Custom root path for every host/domain
         Configured in local.ini

         Direct all requests to the rewrite handler
      [ vhosts ]
      e x a m p l e . com = /mydb/ d e s i g n / m y d e s i g n / r e w r i t e




 http://westhoffswelt.de             jakob@westhoffswelt.de                    slide: 29 / 35
Vhost configuration



         CouchDB supports url changing based on the provided Host
         Custom root path for every host/domain
         Configured in local.ini

         Direct all requests to the rewrite handler
      [ vhosts ]
      e x a m p l e . com = /mydb/ d e s i g n / m y d e s i g n / r e w r i t e




 http://westhoffswelt.de             jakob@westhoffswelt.de                    slide: 29 / 35
Vhost configuration



         CouchDB supports url changing based on the provided Host
         Custom root path for every host/domain
         Configured in local.ini

         Direct all requests to the rewrite handler
      [ vhosts ]
      e x a m p l e . com = /mydb/ d e s i g n / m y d e s i g n / r e w r i t e




 http://westhoffswelt.de             jakob@westhoffswelt.de                    slide: 29 / 35
Vhost configuration



         CouchDB supports url changing based on the provided Host
         Custom root path for every host/domain
         Configured in local.ini

         Direct all requests to the rewrite handler
      [ vhosts ]
      e x a m p l e . com = /mydb/ d e s i g n / m y d e s i g n / r e w r i t e




 http://westhoffswelt.de             jakob@westhoffswelt.de                    slide: 29 / 35
Using CouchApp for deployment



         CouchApp does not only categorize these application type
         It is a utillity written in Python
               http://github.com/couchapp/couchapp
         Deployment system for CouchApps to CouchDBs
         Providing some common widgets like login or profile




 http://westhoffswelt.de     jakob@westhoffswelt.de        slide: 30 / 35
Using CouchApp for deployment



         CouchApp does not only categorize these application type
         It is a utillity written in Python
               http://github.com/couchapp/couchapp
         Deployment system for CouchApps to CouchDBs
         Providing some common widgets like login or profile




 http://westhoffswelt.de     jakob@westhoffswelt.de        slide: 30 / 35
Using CouchApp for deployment



         CouchApp does not only categorize these application type
         It is a utillity written in Python
               http://github.com/couchapp/couchapp
         Deployment system for CouchApps to CouchDBs
         Providing some common widgets like login or profile




 http://westhoffswelt.de     jakob@westhoffswelt.de        slide: 30 / 35
Using CouchApp for deployment



         CouchApp does not only categorize these application type
         It is a utillity written in Python
               http://github.com/couchapp/couchapp
         Deployment system for CouchApps to CouchDBs
         Providing some common widgets like login or profile




 http://westhoffswelt.de     jakob@westhoffswelt.de        slide: 30 / 35
CouchApp: Folder structure

         Organize your applications into a special folder structure

       .
      |−−     attachments
      |−−     id
      |−−    lists
      |−−    rewrites
      |−−   shows
       |    ‘−− myshow . j s
      |−−   updates
      |−−    validate doc update . js
      ‘−−    views
            ‘−− myview
                 |−− map . j s
                 ‘−− r e d u c e . j s




 http://westhoffswelt.de      jakob@westhoffswelt.de          slide: 31 / 35
CouchApp: Javascript preprocessing


         CouchApps preprocesses Javascript before deploying it
         Provides two useful macros: !code and !json

         // !code lib/helper/validation utillities.js
               Include Javascript code from any other file
               Reuse code throughout your application functions

         // !json lib.templates.mytemplate
               Dot notation of path to .json file
               Available using the full object path:
               lib.templates.mytemplate




 http://westhoffswelt.de         jakob@westhoffswelt.de         slide: 32 / 35
CouchApp: Javascript preprocessing


         CouchApps preprocesses Javascript before deploying it
         Provides two useful macros: !code and !json

         // !code lib/helper/validation utillities.js
               Include Javascript code from any other file
               Reuse code throughout your application functions

         // !json lib.templates.mytemplate
               Dot notation of path to .json file
               Available using the full object path:
               lib.templates.mytemplate




 http://westhoffswelt.de         jakob@westhoffswelt.de         slide: 32 / 35
CouchApp: Javascript preprocessing


         CouchApps preprocesses Javascript before deploying it
         Provides two useful macros: !code and !json

         // !code lib/helper/validation utillities.js
               Include Javascript code from any other file
               Reuse code throughout your application functions

         // !json lib.templates.mytemplate
               Dot notation of path to .json file
               Available using the full object path:
               lib.templates.mytemplate




 http://westhoffswelt.de         jakob@westhoffswelt.de         slide: 32 / 35
CouchApp: Javascript preprocessing


         CouchApps preprocesses Javascript before deploying it
         Provides two useful macros: !code and !json

         // !code lib/helper/validation utillities.js
               Include Javascript code from any other file
               Reuse code throughout your application functions

         // !json lib.templates.mytemplate
               Dot notation of path to .json file
               Available using the full object path:
               lib.templates.mytemplate




 http://westhoffswelt.de         jakob@westhoffswelt.de         slide: 32 / 35
CouchApp: Javascript preprocessing


         CouchApps preprocesses Javascript before deploying it
         Provides two useful macros: !code and !json

         // !code lib/helper/validation utillities.js
               Include Javascript code from any other file
               Reuse code throughout your application functions

         // !json lib.templates.mytemplate
               Dot notation of path to .json file
               Available using the full object path:
               lib.templates.mytemplate




 http://westhoffswelt.de         jakob@westhoffswelt.de         slide: 32 / 35
CouchApp: Javascript preprocessing


         CouchApps preprocesses Javascript before deploying it
         Provides two useful macros: !code and !json

         // !code lib/helper/validation utillities.js
               Include Javascript code from any other file
               Reuse code throughout your application functions

         // !json lib.templates.mytemplate
               Dot notation of path to .json file
               Available using the full object path:
               lib.templates.mytemplate




 http://westhoffswelt.de         jakob@westhoffswelt.de         slide: 32 / 35
CouchApp: Deployment

         Specifying all parameters on the commandline

     couchapp push http://user:pw@localhost:5984/mydb
         Use .couchapprc file
     {
         ” env ” : {
            ” default ”: {
               ” db ” : ” h t t p : / / u s e r : p w @ l o c a l h o s t : 5 9 8 4 / mydb−dev ”
            },
            ” production ” : {
               ” db ” : ” h t t p : / / / u s e r : pw@example . com/mydb”
            }
         }
     }




http://westhoffswelt.de               jakob@westhoffswelt.de                      slide: 33 / 35
CouchApp: Deployment

         Specifying all parameters on the commandline

     couchapp push http://user:pw@localhost:5984/mydb
         Use .couchapprc file
     {
         ” env ” : {
            ” default ”: {
               ” db ” : ” h t t p : / / u s e r : p w @ l o c a l h o s t : 5 9 8 4 / mydb−dev ”
            },
            ” production ” : {
               ” db ” : ” h t t p : / / / u s e r : pw@example . com/mydb”
            }
         }
     }




http://westhoffswelt.de               jakob@westhoffswelt.de                      slide: 33 / 35
Further reading

         Things we didn’t have time to discuss
               Update functions
                    http://wiki.apache.org/couchdb/Document Update Handlers

               View functions
                    http://wiki.apache.org/couchdb/Introduction to CouchDB views

                changes stream
                    http://books.couchdb.org/relax/reference/change-notifications

               Evently javascript library
                    http://vimeo.com/9847214


         Further documentation
               CouchDB: The definitive guide
                    http://books.couchdb.org/relax/

               The CouchDB wiki
                    http://wiki.apache.org/couchdb/




 http://westhoffswelt.de           jakob@westhoffswelt.de                       slide: 34 / 35
Further reading

         Things we didn’t have time to discuss
               Update functions
                    http://wiki.apache.org/couchdb/Document Update Handlers

               View functions
                    http://wiki.apache.org/couchdb/Introduction to CouchDB views

                changes stream
                    http://books.couchdb.org/relax/reference/change-notifications

               Evently javascript library
                    http://vimeo.com/9847214


         Further documentation
               CouchDB: The definitive guide
                    http://books.couchdb.org/relax/

               The CouchDB wiki
                    http://wiki.apache.org/couchdb/




 http://westhoffswelt.de           jakob@westhoffswelt.de                       slide: 34 / 35
Further reading

         Things we didn’t have time to discuss
               Update functions
                    http://wiki.apache.org/couchdb/Document Update Handlers

               View functions
                    http://wiki.apache.org/couchdb/Introduction to CouchDB views

                changes stream
                    http://books.couchdb.org/relax/reference/change-notifications

               Evently javascript library
                    http://vimeo.com/9847214


         Further documentation
               CouchDB: The definitive guide
                    http://books.couchdb.org/relax/

               The CouchDB wiki
                    http://wiki.apache.org/couchdb/




 http://westhoffswelt.de           jakob@westhoffswelt.de                       slide: 34 / 35
Further reading

         Things we didn’t have time to discuss
               Update functions
                    http://wiki.apache.org/couchdb/Document Update Handlers

               View functions
                    http://wiki.apache.org/couchdb/Introduction to CouchDB views

                changes stream
                    http://books.couchdb.org/relax/reference/change-notifications

               Evently javascript library
                    http://vimeo.com/9847214


         Further documentation
               CouchDB: The definitive guide
                    http://books.couchdb.org/relax/

               The CouchDB wiki
                    http://wiki.apache.org/couchdb/




 http://westhoffswelt.de           jakob@westhoffswelt.de                       slide: 34 / 35
Further reading

         Things we didn’t have time to discuss
               Update functions
                    http://wiki.apache.org/couchdb/Document Update Handlers

               View functions
                    http://wiki.apache.org/couchdb/Introduction to CouchDB views

                changes stream
                    http://books.couchdb.org/relax/reference/change-notifications

               Evently javascript library
                    http://vimeo.com/9847214


         Further documentation
               CouchDB: The definitive guide
                    http://books.couchdb.org/relax/

               The CouchDB wiki
                    http://wiki.apache.org/couchdb/




 http://westhoffswelt.de           jakob@westhoffswelt.de                       slide: 34 / 35
Further reading

         Things we didn’t have time to discuss
               Update functions
                    http://wiki.apache.org/couchdb/Document Update Handlers

               View functions
                    http://wiki.apache.org/couchdb/Introduction to CouchDB views

                changes stream
                    http://books.couchdb.org/relax/reference/change-notifications

               Evently javascript library
                    http://vimeo.com/9847214


         Further documentation
               CouchDB: The definitive guide
                    http://books.couchdb.org/relax/

               The CouchDB wiki
                    http://wiki.apache.org/couchdb/




 http://westhoffswelt.de           jakob@westhoffswelt.de                       slide: 34 / 35
Further reading

         Things we didn’t have time to discuss
               Update functions
                    http://wiki.apache.org/couchdb/Document Update Handlers

               View functions
                    http://wiki.apache.org/couchdb/Introduction to CouchDB views

                changes stream
                    http://books.couchdb.org/relax/reference/change-notifications

               Evently javascript library
                    http://vimeo.com/9847214


         Further documentation
               CouchDB: The definitive guide
                    http://books.couchdb.org/relax/

               The CouchDB wiki
                    http://wiki.apache.org/couchdb/




 http://westhoffswelt.de           jakob@westhoffswelt.de                       slide: 34 / 35
Thanks for listening


          Questions, comments or annotations?


         Slides: http://westhoffswelt.de/portfolio.htm
                Contact: Jakob Westhoff <jakob@php.net>
                          Twitter: @jakobwesthoff

      Please leave comments and vote at: http://joind.in/1612




 http://westhoffswelt.de    jakob@westhoffswelt.de    slide: 35 / 35

Developing CouchApps

  • 1.
    CouchDB application development Developing and deploying CouchApps Jakob Westhoff <jakob@php.net> PHPBarcamp.at May 1, 2010 http://westhoffswelt.de jakob@westhoffswelt.de slide: 1 / 35
  • 2.
    About Me Jakob Westhoff PHP developer for more several years Computer science student at the TU Dortmund Co-Founder of the PHP Usergroup Dortmund Active in different Open Source projects http://westhoffswelt.de jakob@westhoffswelt.de slide: 2 / 35
  • 3.
    Asking the audience Who is actively using CouchDB? Who played with CouchDB just for fun? Who has heard of CouchDB, but never used it? Who has already developed a CouchApp? Who does not know at all, what a CouchApp is? http://westhoffswelt.de jakob@westhoffswelt.de slide: 3 / 35
  • 4.
    Asking the audience Who is actively using CouchDB? Who played with CouchDB just for fun? Who has heard of CouchDB, but never used it? Who has already developed a CouchApp? Who does not know at all, what a CouchApp is? http://westhoffswelt.de jakob@westhoffswelt.de slide: 3 / 35
  • 5.
    Asking the audience Who is actively using CouchDB? Who played with CouchDB just for fun? Who has heard of CouchDB, but never used it? Who has already developed a CouchApp? Who does not know at all, what a CouchApp is? http://westhoffswelt.de jakob@westhoffswelt.de slide: 3 / 35
  • 6.
    Asking the audience Who is actively using CouchDB? Who played with CouchDB just for fun? Who has heard of CouchDB, but never used it? Who has already developed a CouchApp? Who does not know at all, what a CouchApp is? http://westhoffswelt.de jakob@westhoffswelt.de slide: 3 / 35
  • 7.
    Asking the audience Who is actively using CouchDB? Who played with CouchDB just for fun? Who has heard of CouchDB, but never used it? Who has already developed a CouchApp? Who does not know at all, what a CouchApp is? http://westhoffswelt.de jakob@westhoffswelt.de slide: 3 / 35
  • 8.
    Goals of thissession Understand what a CouchApp actually is Learn what features of CouchDB can be used to realize a CouchApp Get a glimpse of third party tools and frameworks for CouchApp development http://westhoffswelt.de jakob@westhoffswelt.de slide: 4 / 35
  • 9.
    Goals of thissession Understand what a CouchApp actually is Learn what features of CouchDB can be used to realize a CouchApp Get a glimpse of third party tools and frameworks for CouchApp development http://westhoffswelt.de jakob@westhoffswelt.de slide: 4 / 35
  • 10.
    Goals of thissession Understand what a CouchApp actually is Learn what features of CouchDB can be used to realize a CouchApp Get a glimpse of third party tools and frameworks for CouchApp development http://westhoffswelt.de jakob@westhoffswelt.de slide: 4 / 35
  • 11.
    What is CouchDB? Document based NoSQL database Written in Erlang MapReduce based indexing Javascript uses as embedded language RESTful JSON API Build in incremental bi-directional replication http://westhoffswelt.de jakob@westhoffswelt.de slide: 5 / 35
  • 12.
    What is CouchDB? Document based NoSQL database Written in Erlang MapReduce based indexing Javascript uses as embedded language RESTful JSON API Build in incremental bi-directional replication http://westhoffswelt.de jakob@westhoffswelt.de slide: 5 / 35
  • 13.
    What is CouchDB? Document based NoSQL database Written in Erlang MapReduce based indexing Javascript uses as embedded language RESTful JSON API Build in incremental bi-directional replication http://westhoffswelt.de jakob@westhoffswelt.de slide: 5 / 35
  • 14.
    What is CouchDB? Document based NoSQL database Written in Erlang MapReduce based indexing Javascript uses as embedded language RESTful JSON API Build in incremental bi-directional replication http://westhoffswelt.de jakob@westhoffswelt.de slide: 5 / 35
  • 15.
    What is CouchDB? Document based NoSQL database Written in Erlang MapReduce based indexing Javascript uses as embedded language RESTful JSON API Build in incremental bi-directional replication http://westhoffswelt.de jakob@westhoffswelt.de slide: 5 / 35
  • 16.
    What is CouchDB? Document based NoSQL database Written in Erlang MapReduce based indexing Javascript uses as embedded language RESTful JSON API Build in incremental bi-directional replication http://westhoffswelt.de jakob@westhoffswelt.de slide: 5 / 35
  • 17.
    CouchDB document example Documents are stored as JSON { ” i d ” : ” r e c i p e −some−r e c i p e ” , ” r e v ” : ”1−859 e 8 f 9 a 0 6 b 8 6 4 e b c a 3 a 9 2 9 7 5 0 c 1 7 5 3 7 ” , ” date ” : 1270067388 , ” t i t l e ” : ”Some r e c i p e ” , ”ingredients”: [ ” Sugar ” , ” Flour ” ], ” i n s t r u c t i o n s ” : ”Some i n s t r u c t i o n s ” , ” u s e r ” : ” u s e r −f o o b a r ” , } http://westhoffswelt.de jakob@westhoffswelt.de slide: 6 / 35
  • 18.
    CouchDB document example Documents are stored as JSON { ” i d ” : ” r e c i p e −some−r e c i p e ” , ” r e v ” : ”1−859 e 8 f 9 a 0 6 b 8 6 4 e b c a 3 a 9 2 9 7 5 0 c 1 7 5 3 7 ” , ” date ” : 1270067388 , ” t i t l e ” : ”Some r e c i p e ” , ”ingredients”: [ ” Sugar ” , ” Flour ” ], ” i n s t r u c t i o n s ” : ”Some i n s t r u c t i o n s ” , ” u s e r ” : ” u s e r −f o o b a r ” , } http://westhoffswelt.de jakob@westhoffswelt.de slide: 6 / 35
  • 19.
    What is aCouchApp? Application build on top of CouchDB which fits the following criteria: Using CouchDB as data store Stored inside the CouchDB Served using the CouchDB Http-Server Most CouchApps are Mostly written in Javascript Using some sort of Javascript library CouchDB replication can be used to deploy applications http://westhoffswelt.de jakob@westhoffswelt.de slide: 7 / 35
  • 20.
    What is aCouchApp? Application build on top of CouchDB which fits the following criteria: Using CouchDB as data store Stored inside the CouchDB Served using the CouchDB Http-Server Most CouchApps are Mostly written in Javascript Using some sort of Javascript library CouchDB replication can be used to deploy applications http://westhoffswelt.de jakob@westhoffswelt.de slide: 7 / 35
  • 21.
    What is aCouchApp? Application build on top of CouchDB which fits the following criteria: Using CouchDB as data store Stored inside the CouchDB Served using the CouchDB Http-Server Most CouchApps are Mostly written in Javascript Using some sort of Javascript library CouchDB replication can be used to deploy applications http://westhoffswelt.de jakob@westhoffswelt.de slide: 7 / 35
  • 22.
    What is aCouchApp? Application build on top of CouchDB which fits the following criteria: Using CouchDB as data store Stored inside the CouchDB Served using the CouchDB Http-Server Most CouchApps are Mostly written in Javascript Using some sort of Javascript library CouchDB replication can be used to deploy applications http://westhoffswelt.de jakob@westhoffswelt.de slide: 7 / 35
  • 23.
    What is aCouchApp? Application build on top of CouchDB which fits the following criteria: Using CouchDB as data store Stored inside the CouchDB Served using the CouchDB Http-Server Most CouchApps are Mostly written in Javascript Using some sort of Javascript library CouchDB replication can be used to deploy applications http://westhoffswelt.de jakob@westhoffswelt.de slide: 7 / 35
  • 24.
    What is aCouchApp? Application build on top of CouchDB which fits the following criteria: Using CouchDB as data store Stored inside the CouchDB Served using the CouchDB Http-Server Most CouchApps are Mostly written in Javascript Using some sort of Javascript library CouchDB replication can be used to deploy applications http://westhoffswelt.de jakob@westhoffswelt.de slide: 7 / 35
  • 25.
    What is aCouchApp? Application build on top of CouchDB which fits the following criteria: Using CouchDB as data store Stored inside the CouchDB Served using the CouchDB Http-Server Most CouchApps are Mostly written in Javascript Using some sort of Javascript library CouchDB replication can be used to deploy applications http://westhoffswelt.de jakob@westhoffswelt.de slide: 7 / 35
  • 26.
    What is aCouchApp? Application build on top of CouchDB which fits the following criteria: Using CouchDB as data store Stored inside the CouchDB Served using the CouchDB Http-Server Most CouchApps are Mostly written in Javascript Using some sort of Javascript library CouchDB replication can be used to deploy applications http://westhoffswelt.de jakob@westhoffswelt.de slide: 7 / 35
  • 27.
    What is aCouchApp? Application build on top of CouchDB which fits the following criteria: Using CouchDB as data store Stored inside the CouchDB Served using the CouchDB Http-Server Most CouchApps are Mostly written in Javascript Using some sort of Javascript library CouchDB replication can be used to deploy applications http://westhoffswelt.de jakob@westhoffswelt.de slide: 7 / 35
  • 28.
    Excursion: Futon Futon is CouchDBs adminstration interface Automatically available on every CouchDB http://localhost:5984/ utils/ http://westhoffswelt.de jakob@westhoffswelt.de slide: 8 / 35
  • 29.
    Excursion: Futon Futon is CouchDBs adminstration interface Automatically available on every CouchDB http://localhost:5984/ utils/ http://westhoffswelt.de jakob@westhoffswelt.de slide: 8 / 35
  • 30.
    Excursion: Futon Futon is CouchDBs adminstration interface Automatically available on every CouchDB http://localhost:5984/ utils/ http://westhoffswelt.de jakob@westhoffswelt.de slide: 8 / 35
  • 31.
    Which CouchDB Versionto use? CouchDB 0.11 or greater User management Authentication API: Cookie, OAuth, plain-HTTP Per-db reader access lists Validation functions Url rewriting Vhost configuration http://westhoffswelt.de jakob@westhoffswelt.de slide: 9 / 35
  • 32.
    Which CouchDB Versionto use? CouchDB 0.11 or greater User management Authentication API: Cookie, OAuth, plain-HTTP Per-db reader access lists Validation functions Url rewriting Vhost configuration http://westhoffswelt.de jakob@westhoffswelt.de slide: 9 / 35
  • 33.
    Which CouchDB Versionto use? CouchDB 0.11 or greater User management Authentication API: Cookie, OAuth, plain-HTTP Per-db reader access lists Validation functions Url rewriting Vhost configuration http://westhoffswelt.de jakob@westhoffswelt.de slide: 9 / 35
  • 34.
    Which CouchDB Versionto use? CouchDB 0.11 or greater User management Authentication API: Cookie, OAuth, plain-HTTP Per-db reader access lists Validation functions Url rewriting Vhost configuration http://westhoffswelt.de jakob@westhoffswelt.de slide: 9 / 35
  • 35.
    Which CouchDB Versionto use? CouchDB 0.11 or greater User management Authentication API: Cookie, OAuth, plain-HTTP Per-db reader access lists Validation functions Url rewriting Vhost configuration http://westhoffswelt.de jakob@westhoffswelt.de slide: 9 / 35
  • 36.
    Which CouchDB Versionto use? CouchDB 0.11 or greater User management Authentication API: Cookie, OAuth, plain-HTTP Per-db reader access lists Validation functions Url rewriting Vhost configuration http://westhoffswelt.de jakob@westhoffswelt.de slide: 9 / 35
  • 37.
    Which CouchDB Versionto use? CouchDB 0.11 or greater User management Authentication API: Cookie, OAuth, plain-HTTP Per-db reader access lists Validation functions Url rewriting Vhost configuration http://westhoffswelt.de jakob@westhoffswelt.de slide: 9 / 35
  • 38.
    CouchApp techniques Store all application files as document attachments Attachments can be stored for any document inside CouchDB Can be directly accesed using the RESTful interface Use CouchDBs authentication backend for user management Use validation function for access restrictions Use show and list functions to output HTML Access CouchDB using Javascript and XHR Use CouchDB vhosts and url rewriting for nice urls http://westhoffswelt.de jakob@westhoffswelt.de slide: 10 / 35
  • 39.
    CouchApp techniques Store all application files as document attachments Attachments can be stored for any document inside CouchDB Can be directly accesed using the RESTful interface Use CouchDBs authentication backend for user management Use validation function for access restrictions Use show and list functions to output HTML Access CouchDB using Javascript and XHR Use CouchDB vhosts and url rewriting for nice urls http://westhoffswelt.de jakob@westhoffswelt.de slide: 10 / 35
  • 40.
    CouchApp techniques Store all application files as document attachments Attachments can be stored for any document inside CouchDB Can be directly accesed using the RESTful interface Use CouchDBs authentication backend for user management Use validation function for access restrictions Use show and list functions to output HTML Access CouchDB using Javascript and XHR Use CouchDB vhosts and url rewriting for nice urls http://westhoffswelt.de jakob@westhoffswelt.de slide: 10 / 35
  • 41.
    CouchApp techniques Store all application files as document attachments Attachments can be stored for any document inside CouchDB Can be directly accesed using the RESTful interface Use CouchDBs authentication backend for user management Use validation function for access restrictions Use show and list functions to output HTML Access CouchDB using Javascript and XHR Use CouchDB vhosts and url rewriting for nice urls http://westhoffswelt.de jakob@westhoffswelt.de slide: 10 / 35
  • 42.
    CouchApp techniques Store all application files as document attachments Attachments can be stored for any document inside CouchDB Can be directly accesed using the RESTful interface Use CouchDBs authentication backend for user management Use validation function for access restrictions Use show and list functions to output HTML Access CouchDB using Javascript and XHR Use CouchDB vhosts and url rewriting for nice urls http://westhoffswelt.de jakob@westhoffswelt.de slide: 10 / 35
  • 43.
    CouchApp techniques Store all application files as document attachments Attachments can be stored for any document inside CouchDB Can be directly accesed using the RESTful interface Use CouchDBs authentication backend for user management Use validation function for access restrictions Use show and list functions to output HTML Access CouchDB using Javascript and XHR Use CouchDB vhosts and url rewriting for nice urls http://westhoffswelt.de jakob@westhoffswelt.de slide: 10 / 35
  • 44.
    CouchApp techniques Store all application files as document attachments Attachments can be stored for any document inside CouchDB Can be directly accesed using the RESTful interface Use CouchDBs authentication backend for user management Use validation function for access restrictions Use show and list functions to output HTML Access CouchDB using Javascript and XHR Use CouchDB vhosts and url rewriting for nice urls http://westhoffswelt.de jakob@westhoffswelt.de slide: 10 / 35
  • 45.
    CouchApp techniques Store all application files as document attachments Attachments can be stored for any document inside CouchDB Can be directly accesed using the RESTful interface Use CouchDBs authentication backend for user management Use validation function for access restrictions Use show and list functions to output HTML Access CouchDB using Javascript and XHR Use CouchDB vhosts and url rewriting for nice urls http://westhoffswelt.de jakob@westhoffswelt.de slide: 10 / 35
  • 46.
    Authentication backend: UserManagement Managing users: Futon: RESTful API: Create a new document in the users database http://localhost:5984/ users/org.couchdb.user:foo http://westhoffswelt.de jakob@westhoffswelt.de slide: 11 / 35
  • 47.
    Authentication backend: UserManagement Managing users: Futon: RESTful API: Create a new document in the users database http://localhost:5984/ users/org.couchdb.user:foo http://westhoffswelt.de jakob@westhoffswelt.de slide: 11 / 35
  • 48.
    Authentication backend: UserManagement Managing users: Futon: RESTful API: Create a new document in the users database http://localhost:5984/ users/org.couchdb.user:foo http://westhoffswelt.de jakob@westhoffswelt.de slide: 11 / 35
  • 49.
    Authentication backend: Loggingin (Cookie) Logging in: POST username and password to http://localhost:5984/ session Parameters user and password application/x-www-form-urlencoded encoded Post: name=f o o&p a s s w o r d=b a r Response: { ” ok ” : t r u e , ”name” : ” f o o ” , ” r o l e s ” : [ ] } http://westhoffswelt.de jakob@westhoffswelt.de slide: 12 / 35
  • 50.
    Authentication backend: Loggingin (Cookie) Logging in: POST username and password to http://localhost:5984/ session Parameters user and password application/x-www-form-urlencoded encoded Post: name=f o o&p a s s w o r d=b a r Response: { ” ok ” : t r u e , ”name” : ” f o o ” , ” r o l e s ” : [ ] } http://westhoffswelt.de jakob@westhoffswelt.de slide: 12 / 35
  • 51.
    Authentication backend: Loggingin (Cookie) Logging in: POST username and password to http://localhost:5984/ session Parameters user and password application/x-www-form-urlencoded encoded Post: name=f o o&p a s s w o r d=b a r Response: { ” ok ” : t r u e , ”name” : ” f o o ” , ” r o l e s ” : [ ] } http://westhoffswelt.de jakob@westhoffswelt.de slide: 12 / 35
  • 52.
    Authentication backend: Loggingin (Cookie) Logging in: POST username and password to http://localhost:5984/ session Parameters user and password application/x-www-form-urlencoded encoded Post: name=f o o&p a s s w o r d=b a r Response: { ” ok ” : t r u e , ”name” : ” f o o ” , ” r o l e s ” : [ ] } http://westhoffswelt.de jakob@westhoffswelt.de slide: 12 / 35
  • 53.
    Authentication backend: Loggingin (Cookie) Logging in: POST username and password to http://localhost:5984/ session Parameters user and password application/x-www-form-urlencoded encoded Post: name=f o o&p a s s w o r d=b a r Response: { ” ok ” : t r u e , ”name” : ” f o o ” , ” r o l e s ” : [ ] } http://westhoffswelt.de jakob@westhoffswelt.de slide: 12 / 35
  • 54.
    Excursion: Design documents Design documents supply certain special functionallities per db Stored with special id design/some-id Multiple design documents allowed per database Used to define: View functions (Map&Reduce) List functions Show functions Validation functions ... http://westhoffswelt.de jakob@westhoffswelt.de slide: 13 / 35
  • 55.
    Excursion: Design documents Design documents supply certain special functionallities per db Stored with special id design/some-id Multiple design documents allowed per database Used to define: View functions (Map&Reduce) List functions Show functions Validation functions ... http://westhoffswelt.de jakob@westhoffswelt.de slide: 13 / 35
  • 56.
    Excursion: Design documents Design documents supply certain special functionallities per db Stored with special id design/some-id Multiple design documents allowed per database Used to define: View functions (Map&Reduce) List functions Show functions Validation functions ... http://westhoffswelt.de jakob@westhoffswelt.de slide: 13 / 35
  • 57.
    Excursion: Design documents Design documents supply certain special functionallities per db Stored with special id design/some-id Multiple design documents allowed per database Used to define: View functions (Map&Reduce) List functions Show functions Validation functions ... http://westhoffswelt.de jakob@westhoffswelt.de slide: 13 / 35
  • 58.
    Excursion: Design documents Design documents supply certain special functionallities per db Stored with special id design/some-id Multiple design documents allowed per database Used to define: View functions (Map&Reduce) List functions Show functions Validation functions ... http://westhoffswelt.de jakob@westhoffswelt.de slide: 13 / 35
  • 59.
    Excursion: Design documents Design documents supply certain special functionallities per db Stored with special id design/some-id Multiple design documents allowed per database Used to define: View functions (Map&Reduce) List functions Show functions Validation functions ... http://westhoffswelt.de jakob@westhoffswelt.de slide: 13 / 35
  • 60.
    Excursion: Design documents Design documents supply certain special functionallities per db Stored with special id design/some-id Multiple design documents allowed per database Used to define: View functions (Map&Reduce) List functions Show functions Validation functions ... http://westhoffswelt.de jakob@westhoffswelt.de slide: 13 / 35
  • 61.
    Excursion: Design documents Design documents supply certain special functionallities per db Stored with special id design/some-id Multiple design documents allowed per database Used to define: View functions (Map&Reduce) List functions Show functions Validation functions ... http://westhoffswelt.de jakob@westhoffswelt.de slide: 13 / 35
  • 62.
    Excursion: Design documents Design documents supply certain special functionallities per db Stored with special id design/some-id Multiple design documents allowed per database Used to define: View functions (Map&Reduce) List functions Show functions Validation functions ... http://westhoffswelt.de jakob@westhoffswelt.de slide: 13 / 35
  • 63.
    Validation functions: Validateupdates Validation of document creations or updates using Javascript functions Registration: Property validate doc update on any design document Multiple validation functions on different design documents possible Called one after another No defined execution order: Functions need to be isolated One fails: validation fails http://westhoffswelt.de jakob@westhoffswelt.de slide: 14 / 35
  • 64.
    Validation functions: Validateupdates Validation of document creations or updates using Javascript functions Registration: Property validate doc update on any design document Multiple validation functions on different design documents possible Called one after another No defined execution order: Functions need to be isolated One fails: validation fails http://westhoffswelt.de jakob@westhoffswelt.de slide: 14 / 35
  • 65.
    Validation functions: Validateupdates Validation of document creations or updates using Javascript functions Registration: Property validate doc update on any design document Multiple validation functions on different design documents possible Called one after another No defined execution order: Functions need to be isolated One fails: validation fails http://westhoffswelt.de jakob@westhoffswelt.de slide: 14 / 35
  • 66.
    Validation functions: Validateupdates Validation of document creations or updates using Javascript functions Registration: Property validate doc update on any design document Multiple validation functions on different design documents possible Called one after another No defined execution order: Functions need to be isolated One fails: validation fails http://westhoffswelt.de jakob@westhoffswelt.de slide: 14 / 35
  • 67.
    Validation functions: Validateupdates Validation of document creations or updates using Javascript functions Registration: Property validate doc update on any design document Multiple validation functions on different design documents possible Called one after another No defined execution order: Functions need to be isolated One fails: validation fails http://westhoffswelt.de jakob@westhoffswelt.de slide: 14 / 35
  • 68.
    Validation functions: Validateupdates Validation of document creations or updates using Javascript functions Registration: Property validate doc update on any design document Multiple validation functions on different design documents possible Called one after another No defined execution order: Functions need to be isolated One fails: validation fails http://westhoffswelt.de jakob@westhoffswelt.de slide: 14 / 35
  • 69.
    Validation functions: Validateupdates II Example Ensure all documents have a type field added is not allowed to be changed after document creation f u n c t i o n ( newDoc , oldDoc , u s e r C t x ) { i f ( ! newDoc . t y p e ) { throw ( { f o r b i d d e n : ” Mandatory f i e l d ’ type ’ i s m i s s i n g ” }) ; } i f ( ol dD oc && toJSON ( oldDoc . added ) != toJSON ( newDoc . added ) ) { throw ( . . . ) } } http://westhoffswelt.de jakob@westhoffswelt.de slide: 15 / 35
  • 70.
    Validation functions: Validateupdates II Example Ensure all documents have a type field added is not allowed to be changed after document creation f u n c t i o n ( newDoc , oldDoc , u s e r C t x ) { i f ( ! newDoc . t y p e ) { throw ( { f o r b i d d e n : ” Mandatory f i e l d ’ type ’ i s m i s s i n g ” }) ; } i f ( ol dD oc && toJSON ( oldDoc . added ) != toJSON ( newDoc . added ) ) { throw ( . . . ) } } http://westhoffswelt.de jakob@westhoffswelt.de slide: 15 / 35
  • 71.
    Validation functions: Validateupdates II Example Ensure all documents have a type field added is not allowed to be changed after document creation f u n c t i o n ( newDoc , oldDoc , u s e r C t x ) { i f ( ! newDoc . t y p e ) { throw ( { f o r b i d d e n : ” Mandatory f i e l d ’ type ’ i s m i s s i n g ” }) ; } i f ( ol dD oc && toJSON ( oldDoc . added ) != toJSON ( newDoc . added ) ) { throw ( . . . ) } } http://westhoffswelt.de jakob@westhoffswelt.de slide: 15 / 35
  • 72.
    Validation functions: Accesscontrol Use validation doc update function as access control system Ensure the user is logged in Ensure username and author field are identical f u n c t i o n ( newDoc , oldDoc , u s e r C t x ) { i f ( ! u s e r C t x . name ) { throw ( { f o r b i d d e n : ”You need t o be l o g g e d i n t o c h a n g e documents . ” } ) ; } i f ( ( ol dD oc && u s e r C t x . name != oldDoc . a u t h o r ) | | u s e r C t x . name != newDoc . a u t h o r ) { throw ( . . . ) } } http://westhoffswelt.de jakob@westhoffswelt.de slide: 16 / 35
  • 73.
    Validation functions: Accesscontrol Use validation doc update function as access control system Ensure the user is logged in Ensure username and author field are identical f u n c t i o n ( newDoc , oldDoc , u s e r C t x ) { i f ( ! u s e r C t x . name ) { throw ( { f o r b i d d e n : ”You need t o be l o g g e d i n t o c h a n g e documents . ” } ) ; } i f ( ( ol dD oc && u s e r C t x . name != oldDoc . a u t h o r ) | | u s e r C t x . name != newDoc . a u t h o r ) { throw ( . . . ) } } http://westhoffswelt.de jakob@westhoffswelt.de slide: 16 / 35
  • 74.
    Validation functions: Accesscontrol Use validation doc update function as access control system Ensure the user is logged in Ensure username and author field are identical f u n c t i o n ( newDoc , oldDoc , u s e r C t x ) { i f ( ! u s e r C t x . name ) { throw ( { f o r b i d d e n : ”You need t o be l o g g e d i n t o c h a n g e documents . ” } ) ; } i f ( ( ol dD oc && u s e r C t x . name != oldDoc . a u t h o r ) | | u s e r C t x . name != newDoc . a u t h o r ) { throw ( . . . ) } } http://westhoffswelt.de jakob@westhoffswelt.de slide: 16 / 35
  • 75.
    Show and listfunctions User formatted data instead of plain JSON Show functions format documents List functions format views Multiple show and list functions are allowed per-design-document Stored as shows and lists object ” shows ” : { ” summary ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” , ” d e t a i l ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” } http://westhoffswelt.de jakob@westhoffswelt.de slide: 17 / 35
  • 76.
    Show and listfunctions User formatted data instead of plain JSON Show functions format documents List functions format views Multiple show and list functions are allowed per-design-document Stored as shows and lists object ” shows ” : { ” summary ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” , ” d e t a i l ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” } http://westhoffswelt.de jakob@westhoffswelt.de slide: 17 / 35
  • 77.
    Show and listfunctions User formatted data instead of plain JSON Show functions format documents List functions format views Multiple show and list functions are allowed per-design-document Stored as shows and lists object ” shows ” : { ” summary ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” , ” d e t a i l ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” } http://westhoffswelt.de jakob@westhoffswelt.de slide: 17 / 35
  • 78.
    Show and listfunctions User formatted data instead of plain JSON Show functions format documents List functions format views Multiple show and list functions are allowed per-design-document Stored as shows and lists object ” shows ” : { ” summary ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” , ” d e t a i l ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” } http://westhoffswelt.de jakob@westhoffswelt.de slide: 17 / 35
  • 79.
    Show and listfunctions User formatted data instead of plain JSON Show functions format documents List functions format views Multiple show and list functions are allowed per-design-document Stored as shows and lists object ” shows ” : { ” summary ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” , ” d e t a i l ” : ” f u n c t i o n ( doc , r e q ) { . . . } ” } http://westhoffswelt.de jakob@westhoffswelt.de slide: 17 / 35
  • 80.
    Show function tooutput HTML Arguments doc - Document to be returned req - Request object User context GET parameter Accept header ... Return value is a response object body - Content returned to the caller headers - HTML headers (Can be used to set Content-Type) base64 - Can be used instead of body to send binary data http://westhoffswelt.de jakob@westhoffswelt.de slide: 18 / 35
  • 81.
    Show function tooutput HTML Arguments doc - Document to be returned req - Request object User context GET parameter Accept header ... Return value is a response object body - Content returned to the caller headers - HTML headers (Can be used to set Content-Type) base64 - Can be used instead of body to send binary data http://westhoffswelt.de jakob@westhoffswelt.de slide: 18 / 35
  • 82.
    Show function tooutput HTML Arguments doc - Document to be returned req - Request object User context GET parameter Accept header ... Return value is a response object body - Content returned to the caller headers - HTML headers (Can be used to set Content-Type) base64 - Can be used instead of body to send binary data http://westhoffswelt.de jakob@westhoffswelt.de slide: 18 / 35
  • 83.
    Show function tooutput HTML Arguments doc - Document to be returned req - Request object User context GET parameter Accept header ... Return value is a response object body - Content returned to the caller headers - HTML headers (Can be used to set Content-Type) base64 - Can be used instead of body to send binary data http://westhoffswelt.de jakob@westhoffswelt.de slide: 18 / 35
  • 84.
    Show function tooutput HTML Arguments doc - Document to be returned req - Request object User context GET parameter Accept header ... Return value is a response object body - Content returned to the caller headers - HTML headers (Can be used to set Content-Type) base64 - Can be used instead of body to send binary data http://westhoffswelt.de jakob@westhoffswelt.de slide: 18 / 35
  • 85.
    Show function tooutput HTML Arguments doc - Document to be returned req - Request object User context GET parameter Accept header ... Return value is a response object body - Content returned to the caller headers - HTML headers (Can be used to set Content-Type) base64 - Can be used instead of body to send binary data http://westhoffswelt.de jakob@westhoffswelt.de slide: 18 / 35
  • 86.
    Show function tooutput HTML Arguments doc - Document to be returned req - Request object User context GET parameter Accept header ... Return value is a response object body - Content returned to the caller headers - HTML headers (Can be used to set Content-Type) base64 - Can be used instead of body to send binary data http://westhoffswelt.de jakob@westhoffswelt.de slide: 18 / 35
  • 87.
    Show function tooutput HTML Arguments doc - Document to be returned req - Request object User context GET parameter Accept header ... Return value is a response object body - Content returned to the caller headers - HTML headers (Can be used to set Content-Type) base64 - Can be used instead of body to send binary data http://westhoffswelt.de jakob@westhoffswelt.de slide: 18 / 35
  • 88.
    Show function tooutput HTML Arguments doc - Document to be returned req - Request object User context GET parameter Accept header ... Return value is a response object body - Content returned to the caller headers - HTML headers (Can be used to set Content-Type) base64 - Can be used instead of body to send binary data http://westhoffswelt.de jakob@westhoffswelt.de slide: 18 / 35
  • 89.
    Show function tooutput HTML Arguments doc - Document to be returned req - Request object User context GET parameter Accept header ... Return value is a response object body - Content returned to the caller headers - HTML headers (Can be used to set Content-Type) base64 - Can be used instead of body to send binary data http://westhoffswelt.de jakob@westhoffswelt.de slide: 18 / 35
  • 90.
    Show function tooutput HTML Arguments doc - Document to be returned req - Request object User context GET parameter Accept header ... Return value is a response object body - Content returned to the caller headers - HTML headers (Can be used to set Content-Type) base64 - Can be used instead of body to send binary data http://westhoffswelt.de jakob@westhoffswelt.de slide: 18 / 35
  • 91.
    Show function tooutput HTML II Example Output document type and author as HTML f u n c t i o n ( doc , r e q ) { return { body : [ ”<h1>The ” , doc . t y p e , ” document h a s been c r e a t e d by ” , doc . a u t h o r , ”</h1>” ] . j o i n ( ”” ) ; }; } http://westhoffswelt.de jakob@westhoffswelt.de slide: 19 / 35
  • 92.
    Show function tooutput HTML II Example Output document type and author as HTML f u n c t i o n ( doc , r e q ) { return { body : [ ”<h1>The ” , doc . t y p e , ” document h a s been c r e a t e d by ” , doc . a u t h o r , ”</h1>” ] . j o i n ( ”” ) ; }; } http://westhoffswelt.de jakob@westhoffswelt.de slide: 19 / 35
  • 93.
    Show function tooutput HTML II Example Output document type and author as HTML f u n c t i o n ( doc , r e q ) { return { body : [ ”<h1>The ” , doc . t y p e , ” document h a s been c r e a t e d by ” , doc . a u t h o r , ”</h1>” ] . j o i n ( ”” ) ; }; } http://westhoffswelt.de jakob@westhoffswelt.de slide: 19 / 35
  • 94.
    List functions: StreamingAPI Arguments head - Statistical data about the view req - Request object Return value is not used Streaming API getRow() - Retrieve next row from the view, or null at the end send(data) - Send out the next piece of data to the caller http://westhoffswelt.de jakob@westhoffswelt.de slide: 20 / 35
  • 95.
    List functions: StreamingAPI Arguments head - Statistical data about the view req - Request object Return value is not used Streaming API getRow() - Retrieve next row from the view, or null at the end send(data) - Send out the next piece of data to the caller http://westhoffswelt.de jakob@westhoffswelt.de slide: 20 / 35
  • 96.
    List functions: StreamingAPI Arguments head - Statistical data about the view req - Request object Return value is not used Streaming API getRow() - Retrieve next row from the view, or null at the end send(data) - Send out the next piece of data to the caller http://westhoffswelt.de jakob@westhoffswelt.de slide: 20 / 35
  • 97.
    List functions: StreamingAPI Arguments head - Statistical data about the view req - Request object Return value is not used Streaming API getRow() - Retrieve next row from the view, or null at the end send(data) - Send out the next piece of data to the caller http://westhoffswelt.de jakob@westhoffswelt.de slide: 20 / 35
  • 98.
    List functions: StreamingAPI Arguments head - Statistical data about the view req - Request object Return value is not used Streaming API getRow() - Retrieve next row from the view, or null at the end send(data) - Send out the next piece of data to the caller http://westhoffswelt.de jakob@westhoffswelt.de slide: 20 / 35
  • 99.
    List functions: StreamingAPI Arguments head - Statistical data about the view req - Request object Return value is not used Streaming API getRow() - Retrieve next row from the view, or null at the end send(data) - Send out the next piece of data to the caller http://westhoffswelt.de jakob@westhoffswelt.de slide: 20 / 35
  • 100.
    List functions: StreamingAPI Arguments head - Statistical data about the view req - Request object Return value is not used Streaming API getRow() - Retrieve next row from the view, or null at the end send(data) - Send out the next piece of data to the caller http://westhoffswelt.de jakob@westhoffswelt.de slide: 20 / 35
  • 101.
    List functions: Example Example Output authors in list as unordered HTML list f u n c t i o n ( head , r e q ) { var cur ; s e n d ( ’<u l > ’ ) ; w h i l e ( c u r = getRow ( ) ) { s e n d ( ’< l i > ’ + c u r . a u t h o r + ’</ l i > ’ ) ; } s e n d ( ’</u l > ’ ) ; } http://westhoffswelt.de jakob@westhoffswelt.de slide: 21 / 35
  • 102.
    List functions: Example Example Output authors in list as unordered HTML list f u n c t i o n ( head , r e q ) { var cur ; s e n d ( ’<u l > ’ ) ; w h i l e ( c u r = getRow ( ) ) { s e n d ( ’< l i > ’ + c u r . a u t h o r + ’</ l i > ’ ) ; } s e n d ( ’</u l > ’ ) ; } http://westhoffswelt.de jakob@westhoffswelt.de slide: 21 / 35
  • 103.
    List functions: Example Example Output authors in list as unordered HTML list f u n c t i o n ( head , r e q ) { var cur ; s e n d ( ’<u l > ’ ) ; w h i l e ( c u r = getRow ( ) ) { s e n d ( ’< l i > ’ + c u r . a u t h o r + ’</ l i > ’ ) ; } s e n d ( ’</u l > ’ ) ; } http://westhoffswelt.de jakob@westhoffswelt.de slide: 21 / 35
  • 104.
    Query show andlist functions Queried based on design document and name Show functions GET /db/ design/mydesign/ show/show-name/doc-id List functions GET /db/ design/mydesign/ list/list-name/view-name http://westhoffswelt.de jakob@westhoffswelt.de slide: 22 / 35
  • 105.
    Query show andlist functions Queried based on design document and name Show functions GET /db/ design/mydesign/ show/show-name/doc-id List functions GET /db/ design/mydesign/ list/list-name/view-name http://westhoffswelt.de jakob@westhoffswelt.de slide: 22 / 35
  • 106.
    Query show andlist functions Queried based on design document and name Show functions GET /db/ design/mydesign/ show/show-name/doc-id List functions GET /db/ design/mydesign/ list/list-name/view-name http://westhoffswelt.de jakob@westhoffswelt.de slide: 22 / 35
  • 107.
    Show and listfunctions in real applications Use a templating system Javscript micro template language based on John Resigs idea http://github.com/furf/jquery-template Use integrated provides and registerType functions to serve multiple formats based on the accept header Maybe do some sort of user authentication before showing content http://westhoffswelt.de jakob@westhoffswelt.de slide: 23 / 35
  • 108.
    Show and listfunctions in real applications Use a templating system Javscript micro template language based on John Resigs idea http://github.com/furf/jquery-template Use integrated provides and registerType functions to serve multiple formats based on the accept header Maybe do some sort of user authentication before showing content http://westhoffswelt.de jakob@westhoffswelt.de slide: 23 / 35
  • 109.
    Show and listfunctions in real applications Use a templating system Javscript micro template language based on John Resigs idea http://github.com/furf/jquery-template Use integrated provides and registerType functions to serve multiple formats based on the accept header Maybe do some sort of user authentication before showing content http://westhoffswelt.de jakob@westhoffswelt.de slide: 23 / 35
  • 110.
    Show and listfunctions in real applications Use a templating system Javscript micro template language based on John Resigs idea http://github.com/furf/jquery-template Use integrated provides and registerType functions to serve multiple formats based on the accept header Maybe do some sort of user authentication before showing content http://westhoffswelt.de jakob@westhoffswelt.de slide: 23 / 35
  • 111.
    Url rewriting Urls like this one are neither readable nor usable /db/ design/mydesign/ list/list-name/view-name?limit=5 CouchDB supports internal url rewriting Defined as array of rewrites on any design document ”rewrites”: [ { ” from ” : ” / i n d e x ” , ” t o ” : ” l i s t / l i s t −name/ view −name? l i m i t =5” } ] http://westhoffswelt.de jakob@westhoffswelt.de slide: 24 / 35
  • 112.
    Url rewriting Urls like this one are neither readable nor usable /db/ design/mydesign/ list/list-name/view-name?limit=5 CouchDB supports internal url rewriting Defined as array of rewrites on any design document ”rewrites”: [ { ” from ” : ” / i n d e x ” , ” t o ” : ” l i s t / l i s t −name/ view −name? l i m i t =5” } ] http://westhoffswelt.de jakob@westhoffswelt.de slide: 24 / 35
  • 113.
    Url rewriting Urls like this one are neither readable nor usable /db/ design/mydesign/ list/list-name/view-name?limit=5 CouchDB supports internal url rewriting Defined as array of rewrites on any design document ”rewrites”: [ { ” from ” : ” / i n d e x ” , ” t o ” : ” l i s t / l i s t −name/ view −name? l i m i t =5” } ] http://westhoffswelt.de jakob@westhoffswelt.de slide: 24 / 35
  • 114.
    Url rewriting Urls like this one are neither readable nor usable /db/ design/mydesign/ list/list-name/view-name?limit=5 CouchDB supports internal url rewriting Defined as array of rewrites on any design document ”rewrites”: [ { ” from ” : ” / i n d e x ” , ” t o ” : ” l i s t / l i s t −name/ view −name? l i m i t =5” } ] http://westhoffswelt.de jakob@westhoffswelt.de slide: 24 / 35
  • 115.
    Advanced url rewriting Using named placeholders Match each path segment ” from ” : ” / e n t r y / : doc ” , ” t o ” : ” show / show−name / : doc ” Using match all at the end of an url Appends given query parameters ” from ” : ” / e n t r y /∗ ” , ” t o ” : ” show / show−name /∗ ” http://westhoffswelt.de jakob@westhoffswelt.de slide: 25 / 35
  • 116.
    Advanced url rewriting Using named placeholders Match each path segment ” from ” : ” / e n t r y / : doc ” , ” t o ” : ” show / show−name / : doc ” Using match all at the end of an url Appends given query parameters ” from ” : ” / e n t r y /∗ ” , ” t o ” : ” show / show−name /∗ ” http://westhoffswelt.de jakob@westhoffswelt.de slide: 25 / 35
  • 117.
    Advanced url rewritingII Rewriting to query parameters ” from ” : ” / page / : s t a r t ” , ” t o ” : ” l i s t / l i s t −name/ p a g e s ” , ” query ” : { ” startkey ”: ”: start ” } Adress a context outside of the design document Targets are always relative to the design document Relative navigation with .. is allowed ” from ” : ” / raw / : doc ” , ” t o ” : ” . . / . . / : doc ” http://westhoffswelt.de jakob@westhoffswelt.de slide: 26 / 35
  • 118.
    Advanced url rewritingII Rewriting to query parameters ” from ” : ” / page / : s t a r t ” , ” t o ” : ” l i s t / l i s t −name/ p a g e s ” , ” query ” : { ” startkey ”: ”: start ” } Adress a context outside of the design document Targets are always relative to the design document Relative navigation with .. is allowed ” from ” : ” / raw / : doc ” , ” t o ” : ” . . / . . / : doc ” http://westhoffswelt.de jakob@westhoffswelt.de slide: 26 / 35
  • 119.
    Advanced url rewritingIII Rewriting based on HTTP methods { ” from ” : ” / e n t r y /∗ ” , ” t o ” : ” show / show−name /∗ ” , ” method ” : ”GET” }, { ” from ” : ” / e n t r y /∗ ” , ” t o ” : ” u p d a t e / update −name /∗ ” , ” method ” : ”POST” }, http://westhoffswelt.de jakob@westhoffswelt.de slide: 27 / 35
  • 120.
    Query rewritten urls Queried on design document using special rewrite path /db/ design/mydesign/ rewrite/some-url Better, but far from being perfect → Vhost configuration http://westhoffswelt.de jakob@westhoffswelt.de slide: 28 / 35
  • 121.
    Query rewritten urls Queried on design document using special rewrite path /db/ design/mydesign/ rewrite/some-url Better, but far from being perfect → Vhost configuration http://westhoffswelt.de jakob@westhoffswelt.de slide: 28 / 35
  • 122.
    Query rewritten urls Queried on design document using special rewrite path /db/ design/mydesign/ rewrite/some-url Better, but far from being perfect → Vhost configuration http://westhoffswelt.de jakob@westhoffswelt.de slide: 28 / 35
  • 123.
    Vhost configuration CouchDB supports url changing based on the provided Host Custom root path for every host/domain Configured in local.ini Direct all requests to the rewrite handler [ vhosts ] e x a m p l e . com = /mydb/ d e s i g n / m y d e s i g n / r e w r i t e http://westhoffswelt.de jakob@westhoffswelt.de slide: 29 / 35
  • 124.
    Vhost configuration CouchDB supports url changing based on the provided Host Custom root path for every host/domain Configured in local.ini Direct all requests to the rewrite handler [ vhosts ] e x a m p l e . com = /mydb/ d e s i g n / m y d e s i g n / r e w r i t e http://westhoffswelt.de jakob@westhoffswelt.de slide: 29 / 35
  • 125.
    Vhost configuration CouchDB supports url changing based on the provided Host Custom root path for every host/domain Configured in local.ini Direct all requests to the rewrite handler [ vhosts ] e x a m p l e . com = /mydb/ d e s i g n / m y d e s i g n / r e w r i t e http://westhoffswelt.de jakob@westhoffswelt.de slide: 29 / 35
  • 126.
    Vhost configuration CouchDB supports url changing based on the provided Host Custom root path for every host/domain Configured in local.ini Direct all requests to the rewrite handler [ vhosts ] e x a m p l e . com = /mydb/ d e s i g n / m y d e s i g n / r e w r i t e http://westhoffswelt.de jakob@westhoffswelt.de slide: 29 / 35
  • 127.
    Using CouchApp fordeployment CouchApp does not only categorize these application type It is a utillity written in Python http://github.com/couchapp/couchapp Deployment system for CouchApps to CouchDBs Providing some common widgets like login or profile http://westhoffswelt.de jakob@westhoffswelt.de slide: 30 / 35
  • 128.
    Using CouchApp fordeployment CouchApp does not only categorize these application type It is a utillity written in Python http://github.com/couchapp/couchapp Deployment system for CouchApps to CouchDBs Providing some common widgets like login or profile http://westhoffswelt.de jakob@westhoffswelt.de slide: 30 / 35
  • 129.
    Using CouchApp fordeployment CouchApp does not only categorize these application type It is a utillity written in Python http://github.com/couchapp/couchapp Deployment system for CouchApps to CouchDBs Providing some common widgets like login or profile http://westhoffswelt.de jakob@westhoffswelt.de slide: 30 / 35
  • 130.
    Using CouchApp fordeployment CouchApp does not only categorize these application type It is a utillity written in Python http://github.com/couchapp/couchapp Deployment system for CouchApps to CouchDBs Providing some common widgets like login or profile http://westhoffswelt.de jakob@westhoffswelt.de slide: 30 / 35
  • 131.
    CouchApp: Folder structure Organize your applications into a special folder structure . |−− attachments |−− id |−− lists |−− rewrites |−− shows | ‘−− myshow . j s |−− updates |−− validate doc update . js ‘−− views ‘−− myview |−− map . j s ‘−− r e d u c e . j s http://westhoffswelt.de jakob@westhoffswelt.de slide: 31 / 35
  • 132.
    CouchApp: Javascript preprocessing CouchApps preprocesses Javascript before deploying it Provides two useful macros: !code and !json // !code lib/helper/validation utillities.js Include Javascript code from any other file Reuse code throughout your application functions // !json lib.templates.mytemplate Dot notation of path to .json file Available using the full object path: lib.templates.mytemplate http://westhoffswelt.de jakob@westhoffswelt.de slide: 32 / 35
  • 133.
    CouchApp: Javascript preprocessing CouchApps preprocesses Javascript before deploying it Provides two useful macros: !code and !json // !code lib/helper/validation utillities.js Include Javascript code from any other file Reuse code throughout your application functions // !json lib.templates.mytemplate Dot notation of path to .json file Available using the full object path: lib.templates.mytemplate http://westhoffswelt.de jakob@westhoffswelt.de slide: 32 / 35
  • 134.
    CouchApp: Javascript preprocessing CouchApps preprocesses Javascript before deploying it Provides two useful macros: !code and !json // !code lib/helper/validation utillities.js Include Javascript code from any other file Reuse code throughout your application functions // !json lib.templates.mytemplate Dot notation of path to .json file Available using the full object path: lib.templates.mytemplate http://westhoffswelt.de jakob@westhoffswelt.de slide: 32 / 35
  • 135.
    CouchApp: Javascript preprocessing CouchApps preprocesses Javascript before deploying it Provides two useful macros: !code and !json // !code lib/helper/validation utillities.js Include Javascript code from any other file Reuse code throughout your application functions // !json lib.templates.mytemplate Dot notation of path to .json file Available using the full object path: lib.templates.mytemplate http://westhoffswelt.de jakob@westhoffswelt.de slide: 32 / 35
  • 136.
    CouchApp: Javascript preprocessing CouchApps preprocesses Javascript before deploying it Provides two useful macros: !code and !json // !code lib/helper/validation utillities.js Include Javascript code from any other file Reuse code throughout your application functions // !json lib.templates.mytemplate Dot notation of path to .json file Available using the full object path: lib.templates.mytemplate http://westhoffswelt.de jakob@westhoffswelt.de slide: 32 / 35
  • 137.
    CouchApp: Javascript preprocessing CouchApps preprocesses Javascript before deploying it Provides two useful macros: !code and !json // !code lib/helper/validation utillities.js Include Javascript code from any other file Reuse code throughout your application functions // !json lib.templates.mytemplate Dot notation of path to .json file Available using the full object path: lib.templates.mytemplate http://westhoffswelt.de jakob@westhoffswelt.de slide: 32 / 35
  • 138.
    CouchApp: Deployment Specifying all parameters on the commandline couchapp push http://user:pw@localhost:5984/mydb Use .couchapprc file { ” env ” : { ” default ”: { ” db ” : ” h t t p : / / u s e r : p w @ l o c a l h o s t : 5 9 8 4 / mydb−dev ” }, ” production ” : { ” db ” : ” h t t p : / / / u s e r : pw@example . com/mydb” } } } http://westhoffswelt.de jakob@westhoffswelt.de slide: 33 / 35
  • 139.
    CouchApp: Deployment Specifying all parameters on the commandline couchapp push http://user:pw@localhost:5984/mydb Use .couchapprc file { ” env ” : { ” default ”: { ” db ” : ” h t t p : / / u s e r : p w @ l o c a l h o s t : 5 9 8 4 / mydb−dev ” }, ” production ” : { ” db ” : ” h t t p : / / / u s e r : pw@example . com/mydb” } } } http://westhoffswelt.de jakob@westhoffswelt.de slide: 33 / 35
  • 140.
    Further reading Things we didn’t have time to discuss Update functions http://wiki.apache.org/couchdb/Document Update Handlers View functions http://wiki.apache.org/couchdb/Introduction to CouchDB views changes stream http://books.couchdb.org/relax/reference/change-notifications Evently javascript library http://vimeo.com/9847214 Further documentation CouchDB: The definitive guide http://books.couchdb.org/relax/ The CouchDB wiki http://wiki.apache.org/couchdb/ http://westhoffswelt.de jakob@westhoffswelt.de slide: 34 / 35
  • 141.
    Further reading Things we didn’t have time to discuss Update functions http://wiki.apache.org/couchdb/Document Update Handlers View functions http://wiki.apache.org/couchdb/Introduction to CouchDB views changes stream http://books.couchdb.org/relax/reference/change-notifications Evently javascript library http://vimeo.com/9847214 Further documentation CouchDB: The definitive guide http://books.couchdb.org/relax/ The CouchDB wiki http://wiki.apache.org/couchdb/ http://westhoffswelt.de jakob@westhoffswelt.de slide: 34 / 35
  • 142.
    Further reading Things we didn’t have time to discuss Update functions http://wiki.apache.org/couchdb/Document Update Handlers View functions http://wiki.apache.org/couchdb/Introduction to CouchDB views changes stream http://books.couchdb.org/relax/reference/change-notifications Evently javascript library http://vimeo.com/9847214 Further documentation CouchDB: The definitive guide http://books.couchdb.org/relax/ The CouchDB wiki http://wiki.apache.org/couchdb/ http://westhoffswelt.de jakob@westhoffswelt.de slide: 34 / 35
  • 143.
    Further reading Things we didn’t have time to discuss Update functions http://wiki.apache.org/couchdb/Document Update Handlers View functions http://wiki.apache.org/couchdb/Introduction to CouchDB views changes stream http://books.couchdb.org/relax/reference/change-notifications Evently javascript library http://vimeo.com/9847214 Further documentation CouchDB: The definitive guide http://books.couchdb.org/relax/ The CouchDB wiki http://wiki.apache.org/couchdb/ http://westhoffswelt.de jakob@westhoffswelt.de slide: 34 / 35
  • 144.
    Further reading Things we didn’t have time to discuss Update functions http://wiki.apache.org/couchdb/Document Update Handlers View functions http://wiki.apache.org/couchdb/Introduction to CouchDB views changes stream http://books.couchdb.org/relax/reference/change-notifications Evently javascript library http://vimeo.com/9847214 Further documentation CouchDB: The definitive guide http://books.couchdb.org/relax/ The CouchDB wiki http://wiki.apache.org/couchdb/ http://westhoffswelt.de jakob@westhoffswelt.de slide: 34 / 35
  • 145.
    Further reading Things we didn’t have time to discuss Update functions http://wiki.apache.org/couchdb/Document Update Handlers View functions http://wiki.apache.org/couchdb/Introduction to CouchDB views changes stream http://books.couchdb.org/relax/reference/change-notifications Evently javascript library http://vimeo.com/9847214 Further documentation CouchDB: The definitive guide http://books.couchdb.org/relax/ The CouchDB wiki http://wiki.apache.org/couchdb/ http://westhoffswelt.de jakob@westhoffswelt.de slide: 34 / 35
  • 146.
    Further reading Things we didn’t have time to discuss Update functions http://wiki.apache.org/couchdb/Document Update Handlers View functions http://wiki.apache.org/couchdb/Introduction to CouchDB views changes stream http://books.couchdb.org/relax/reference/change-notifications Evently javascript library http://vimeo.com/9847214 Further documentation CouchDB: The definitive guide http://books.couchdb.org/relax/ The CouchDB wiki http://wiki.apache.org/couchdb/ http://westhoffswelt.de jakob@westhoffswelt.de slide: 34 / 35
  • 147.
    Thanks for listening Questions, comments or annotations? Slides: http://westhoffswelt.de/portfolio.htm Contact: Jakob Westhoff <jakob@php.net> Twitter: @jakobwesthoff Please leave comments and vote at: http://joind.in/1612 http://westhoffswelt.de jakob@westhoffswelt.de slide: 35 / 35