Query

You can query Shelf DB database using shelfquery module, which can execute as sync or async client.

import shelfquery

db = shelfquery.db() # sync client point to host 127.0.0.1:17000
db.asyncio() # make it async client.
db.sync()  # make it sync client again.

Try it

The best way to try shelfquery is using ipython or native python shell then run the codes in this guide.

sync client is recommened if you want to try and run simple codes in python shell.

Query

shelfquery use method chaining as a syntax to query database. Every query will have a sequence steps as
select database -> select shelf -> chained query -> run. For example,

python
import shelfquery

shelfquery.db()\
    .shelf('note')\
    .insert({
        'title': 'Shelf DB',
        'content': 'Simple note',
        'datetime': datetime.utcnow()})\
    .run()

# Explanation ::
# .db()  : select database, default to 127.0.0.1:17000
# .shelf('note')  : select shelf 'note'
# .insert({..})  : insert entry to database.
# .run()  : send query to database.

Shelf DB will store entries only as python dict instances with UUID1 as entries hash key in database. It will return errors if store other instances or not use UUID1 as IDs.

There're 2 query APIs to store entries to database, insert and put.

To query entries from database, you can use APIs such as get, filter, sort, map, slice, etc. For example, to run chain query in the following order.

  1. notes which have title start with 'Shelf'
  2. then, sort by datetime
  3. then, get the first 10 items
python
entries = shelfquery.db().shelf('note')\
    .filter(lambda note: re.match('Shelf.*', note['title']))\
    .sort(key=lambda note: note['datetime'])\
    .slice(0,10)\
    .run()
Shelf DB
Guide Database Query