Using a wrapper for your RethinkDB tables in Python

Using a wrapper for your RethinkDB tables in Python

Since I’ve been using Django a lot recently I’ve become sort of spoiled by the Database abstraction that comes with it, but unfortunately using that with RDB just isn’t an option so I tried to find a nice middle ground. This wrapper allows me to emulate the most common usages from Django’s DB abstraction — for anything more complex I believe it would be best to do a manual query.

The Source

Is available here on GitHub

Examples


Example Table #1

Example Table #2

Example Table #3

Field Types


Global Options

These options are available to every field type.
Param Default Description
required True Is this field required for every entry?
convert_type True Should the field controller should try to convert the type for consistency?

BooleanField

* Global Options only

CharField

Param Default Description
max_length None The maximum number of characters this field should store.
min_length None The minimum number of characters this field should store.
utf8 True Should this field try to enforce utf8 conversion?

LongField

Param Default Description
positive_only False Should this field contain positive values only?
negative_only False Should this field contain negative values only?
max_digits None The maximum number of digits this field should have.

IntegerField

* Same as LongField

FloatField

Param Default Description
positive_only False Should this field contain positive values only?
negative_only False Should this field contain negative values only?
max_digits None The maximum number of digits this field should have.
max_decimals None The maximum number of decimal places this field should have.
round_decimals False Should this field be rounded to the max_decimal length?

Save Documents

save() is responsible for new record creation and updating existing records

Examples


new document
After save() will update the id field to reflect the id of the newly generated document.
update document
Note: save() will only update if the fields ACTUALLY change, otherwise it will not bother trying.

Get Documents

all() Will will return every result found. get() Will append .limit(1) to any query and attempt to return the result.

Examples


get documents
This will return a list containing the dictionary response for each document. Which means, that if you needed to json serialize the return you do not need to loop the records, you can simply do: json.dumps(results) This is the same as running [row for row in results]
get documents as an object list
This will return a list containing the passed object with the response data already parsed. You cannot json serialize this type of call. This is the same as running [MyTable(**row) for row in results]
get document record
This will return the first record from a query. This is the same as running result[0]. In this instance, if 0 index is not found then None is returned.

Count Documents

Delete Documents

Order Documents

Ascending

Descending

 Dave

About David Parlevliet

Dave is long time developer with a passion toward teaching. He divides his time between his wife, her cat and his projects. He recently started using twitter so make sure to follow him!