Using a wrapper for your RethinkDB tables in Python
Table of Contents
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
|
1 2 3 4 |
class MyTable(rwrapper): field1 = None field2 = None _db_table = 'my_table' |
Example Table #2
|
1 2 3 4 |
class MyTable(rwrapper): field1 = CharField() field2 = CharField() _db_table = 'my_table' |
Example Table #3
|
1 2 3 4 |
class MyTable(rwrapper): field1 = FloatField(max_decimals=2, round_decimals=True) field2 = CharField() _db_table = 'my_table' |
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
|
1 |
save([object=False]) |
save() is responsible for new record creation and updating existing records
Examples
new document
|
1 2 |
table = MyTable(field1='something', field2='something else') table.save() |
After save() will update the id field to reflect the id of the newly generated document.
update document
|
1 2 3 |
# if the id field is set, the class will attempt to update table = MyTable(id=1, field1='something new') table.save() |
Note: save() will only update if the fields ACTUALLY change, otherwise it will not bother trying.
Get Documents
|
1 2 |
all([object=False]) get([object=False, [return_exception=False]]) |
all() Will will return every result found. get() Will append .limit(1) to any query and attempt to return the result.
Examples
get documents
|
1 2 |
table = MyTable(field1='something') results = table.all() |
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
|
1 2 |
table = MyTable(field1='something') results = table.all(MyTable) |
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
|
1 2 |
table = MyTable(id=1) result = table.get() |
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
|
1 2 |
table = MyTable(field1='something') count = table.count() |
Delete Documents
|
1 2 |
table = MyTable(field1='something') result = table.delete() |
Order Documents
Ascending
|
1 2 |
table = MyTable(field1='something') results = table.order_by('field1').all() |
Descending
|
1 2 |
table = MyTable(field1='something') results = table.order_by('-field1').all() |
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!