In [1]:
from tinydb import TinyDB, Query
db = TinyDB('db.json')
In [2]:
db.insert({'type': 'apple', 'count': 7})
db.insert({'type': 'peach', 'count': 3})
Out[2]:
In [3]:
db.all()
Out[3]:
In [4]:
Fruit = Query()
db.search(Fruit.type == 'peach')
Out[4]:
In [5]:
db.search(Fruit.count > 5)
Out[5]:
In [6]:
db.update({'count': 10}, Fruit.type == 'apple')
db.all()
Out[6]:
In [7]:
db.remove(Fruit.count < 5)
db.all()
Out[7]:
In [8]:
db.purge()
db.all()
Out[8]:
In [9]:
from tinydb import Query
User = Query()
db.search(User.name == 'John')
Out[9]:
In [10]:
db.search(User.birthday.year == 1990)
Out[10]:
In [11]:
# db.search(User.country-code == 'foo') is invalid syntax! instead:
db.search(User['country-code'] == 'foo')
Out[11]:
In [12]:
from tinydb import where
db.search(where('field') == 'value')
Out[12]:
In [13]:
# db.search(where('field') == 'value') is shorthand for:
db.search(Query()['field'] == 'value')
Out[13]:
In [14]:
db.search(User.name.exists())
Out[14]:
In [15]:
db.search(User.name.matches('[aZ]*'))
db.search(User.name.search('b+'))
Out[15]:
In [16]:
test_func = lambda s: s == 'John'
db.search(User.name.test(test_func))
Out[16]:
In [17]:
def test_func(val, m, n):
return m <= val <= n
db.search(User.age.test(test_func, 0, 21))
db.search(User.age.test(test_func, 21, 99))
Out[17]:
In [18]:
db.search(User.groups.any(Group.name == 'admin')) # member of 1+ admin group
db.search(User.groups.all(Group.name == 'admin')) # member of just admin group
In [19]:
db.search(User.groups.any(['admin', 'user']))
db.search(User.groups.all(['admin', 'user']))
Out[19]:
In [22]:
db.search(~ User.name == 'John')
In [23]:
db.search((User.name == 'John') & (User.age <= 30)) # wrap conditions in parens!
Out[23]:
In [24]:
db.search((User.name == 'John') | (User.age <= 30)) # wrap conditions in parens!
Out[24]:
In [25]:
db.insert_multiple([{'name': 'John', 'age': 22}, {'name': 'John', 'age': 37}])
db.insert_multiple({'int': 1, 'value': i} for i in range(2))
Out[25]:
In [26]:
db.all()
Out[26]:
In [27]:
from tinydb.operations import delete
db.update(delete('key1'), User.name == 'John')
In [28]:
len(db)
Out[28]:
In [29]:
db.get(User.name == 'John')
Out[29]:
In [30]:
db.get(User.name == 'Bobby')
In [31]:
db.contains(User.name == 'John')
Out[31]:
In [32]:
db.count(User.name == 'John')
Out[32]:
In [33]:
db.insert({'name': 'John', 'age': 22})
Out[33]:
In [34]:
el = db.get(User.name == 'John')
el.eid
Out[34]:
In [35]:
el = db.all()[0]
el.eid
Out[35]:
In [36]:
db.update({'value': 2}, eids=[1,2])
db.contains(eids=[1])
Out[36]:
In [37]:
db.remove(eids=[1,2])
db.get(eid=3)
Out[37]:
In [38]:
table = db.table('table_name')
table.insert({'value': True})
table.all()
Out[38]:
In [39]:
db.purge_table('table_name')
# db.purge_tables()
In [40]:
db.tables()
Out[40]:
In [41]:
table = db.table('table_name')
db.tables()
Out[41]:
In [ ]:
# 1: for a single instance only
# TinyDb(storage=SomeStorage, default_table='my-default')
# 2: for all instances
# TinyDB.DEFAULT_TABLE = 'my-default'
# table = db.table('table_name', cache_size=30)
# NOTE: cache_size set to None indicated unlimited cache
In [ ]:
# TinyDB can use JSON and in-memory, stored in JSON by default
# to use in-memory storage:
# from tinydb.storages import MemoryStorage
# db = TinyDB(storage=MemoryStorage)
#
# can also set DEFAULT_STORAGE class variable, i.e.
# TinyDB.DEFAULT_STORAGE = MemoryStorage
In [ ]: