.. _gettingstarted:

Getting Started
===============

This tutoriall exposes key features of this library through mainly code
examples. For in-depth description of the modules, you'll want to read 
the :ref:`API <api>` documentation.

If you have not installed py-simplecouchdb yet, the :ref:`installation docs <installation>`
will get you started. Once you've done, you can write your first CouchDB
document:

.. code-block:: python

    from simplecouchdb import schema

    class Greeting(schema.Document):
        author = schema.StringProperty()
        content = schema.StringProperty()
        date = schema.DateTimeProperty()

.. seealso::

    :ref:`schema-ref`

Store the submitted Greetings
-----------------------------

.. code-block:: python
    
    import datetime
    from simplecouchdb import Server
    
    server = Server()
    
    try:
        db = server.create_db("greetings")
    except:
        db = server['greetings']

    
    greet = Greeting(
        author="Benoit",
        content="Welcome to simplecouchdb world",
        date=datetime.datetime.utcnow()
    )

    greet.save(db)

Now your greet document is saved in db. You could simple update it :

.. code-block:: python

    greet.content = "Welcome to CouchDB"
    greet.save(db)


Do you want to add a property ? Easy:

.. code-block:: python

    greet.homepage = "http://www.e-engura.org"
    greet.save(db)

Now you just add homepage property to your document.

Get all greetings
-----------------

First wee need to create a view and save it in db (see :ref:`view-ref` for more informations),
we add a :class:`simplecouchdb.schema.ViewProperty` to our Document schema:

.. code-block:: python

    class Greeting(schema.Document):
        ...
        all = schema.ViewProperty("greeting", map_fun="""function(doc)
            { if (doc.doc_type == "Greeting") emit(doc._id, doc); }""")

    greets = Greeting.all.get(db)

That's it you now get all greets.

To go further
-------------

Have a look on

* :ref:`creating-getting-deleting-ref`

* :ref:`schema-ref`

* :ref:`api`

Enjoy :)

