Skip to content

Commit 13d412a

Browse files
Added docs
1 parent f0de69c commit 13d412a

9 files changed

Lines changed: 470 additions & 8 deletions

File tree

docs/code/add.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<h1 align="center"> Onstro DB </h1>
2+
3+
## Adding Data
4+
5+
Values can be added to the DB with the `.add()` method. It takes a list of values to add to the DB.
6+
7+
```python
8+
from onstrodb import OnstroDb
9+
10+
schema = {
11+
"name": {"type": "str"}
12+
}
13+
14+
db = OnstroDb(db_name="test", schema=schema)
15+
16+
db.add([
17+
{"name": "ad"}
18+
])
19+
db.commit() # save the changes
20+
21+
print(db)
22+
23+
# output
24+
{'70ba3370': {'name': 'ad'}}
25+
26+
```
27+
28+
> `db.commit()` stores all the current addition and updates in the `.db` file. If not the changes will not be permanent. The commit has no meaning if the DB is an in memory DB
29+
30+
### Adding multiple values.
31+
32+
```python
33+
from onstrodb import OnstroDb
34+
35+
schema = {
36+
"name": {"type": "str"}
37+
}
38+
39+
db = OnstroDb(db_name="test", schema=schema)
40+
41+
ids = db.add([
42+
{"name": "ad"},
43+
{"name": "fred"}
44+
], get_hash_id=True)
45+
db.commit() # save the changes
46+
47+
print(db)
48+
print(ids)
49+
50+
# output
51+
{'70ba3370': {'name': 'ad'}, 'd0cfc2e5': {'name': 'fred'}}
52+
['70ba3370', 'd0cfc2e5']
53+
```
54+
55+
> The `get_hash_id` param if set to `True` will return a list of all the hash id of the newly added data in the order in which they are inserted.
56+
57+
---

docs/code/chain.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<h1 align="center"> Onstro DB </h1>
2+
3+
## Multiple conditions in queries
4+
5+
As you might have noticed that all the methods that accept query as an argument, the query can only be one condition long.
6+
In order have queries that have multiple condition you can use that following technique
7+
8+
```python
9+
from onstrodb import OnstroDb
10+
11+
schema = {
12+
"name": {"type": "str", "required": True},
13+
"age": {"type": "int"}
14+
}
15+
16+
db = OnstroDb(db_name="test", schema=schema)
17+
18+
db.add([
19+
{"name": "ad", "age": 3},
20+
{"name": "fred", "age": 4},
21+
{"name": "dev", "age": 3}
22+
])
23+
24+
print(db)
25+
26+
hid = db.get_hash_id({"name": "ad", "age": 3})
27+
db.update_by_hash_id(hid[0], {"name": 'mike'})
28+
29+
print(db)
30+
```
31+
32+
{ '7b672af4': {'name': 'ad', 'age': 3},
33+
'93b626d2': {'name': 'fred', 'age': 4},
34+
'f3d32e1e': {'name': 'dev', 'age': 3}}
35+
36+
{ '110f1f27': {'name': 'mike', 'age': 3},
37+
'93b626d2': {'name': 'fred', 'age': 4},
38+
'f3d32e1e': {'name': 'dev', 'age': 3}}
39+
40+
> The `get_hash_id` method returns a list of all the hash ids of rows that matches all the conditions provided in the query.

docs/code/delete.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
## Deleting data from the DB
2+
3+
There are three ways to delete data from a DB
4+
5+
- delete_by_query
6+
- delete_by_hash_id
7+
- purge
8+
9+
### delete_by_query
10+
11+
The `delete_by_query` accepts a single condition which removes all the rows that mathes that condition
12+
13+
```python
14+
from onstrodb import OnstroDb
15+
16+
schema = {
17+
"name": {"type": "str", "required": True},
18+
"age": {"type": "int"}
19+
}
20+
21+
db = OnstroDb(db_name="test", schema=schema)
22+
23+
db.add([
24+
{"name": "ad", "age": 3},
25+
{"name": "fred", "age": 4},
26+
{"name": "dev", "age": 5}
27+
])
28+
29+
print(db)
30+
31+
db.delete_by_query({"name": "ad"}) # queries can only be one condition long(There is a work around for this)
32+
33+
print(db)
34+
```
35+
36+
{ '7b672af4': {'name': 'ad', 'age': 3},
37+
'93b626d2': {'name': 'fred', 'age': 4},
38+
'41907268': {'name': 'dev', 'age': 5}}
39+
{'93b626d2': {'name': 'fred', 'age': 4}, '41907268': {'name': 'dev', 'age': 5}}
40+
41+
### delete_by_hash_id
42+
43+
The `delete_by_hash_id` taskes the hash id of the row as an argument
44+
45+
```python
46+
from onstrodb import OnstroDb
47+
48+
schema = {
49+
"name": {"type": "str", "required": True},
50+
"age": {"type": "int"}
51+
}
52+
53+
db = OnstroDb(db_name="test", schema=schema)
54+
55+
db.add([
56+
{"name": "ad", "age": 3},
57+
{"name": "fred", "age": 4},
58+
{"name": "dev", "age": 5}
59+
])
60+
61+
print(db)
62+
63+
db.delete_by_hash_id("93b626d2")
64+
65+
print(db)
66+
```
67+
68+
{ '7b672af4': {'name': 'ad', 'age': 3},
69+
'93b626d2': {'name': 'fred', 'age': 4},
70+
'41907268': {'name': 'dev', 'age': 5}}
71+
{'7b672af4': {'name': 'ad', 'age': 3}, '41907268': {'name': 'dev', 'age': 5}}
72+
73+
### purge
74+
75+
Purging a DB will delete all the rows.
76+
77+
```python
78+
from onstrodb import OnstroDb
79+
80+
schema = {
81+
"name": {"type": "str", "required": True},
82+
"age": {"type": "int"}
83+
}
84+
85+
db = OnstroDb(db_name="test", schema=schema)
86+
87+
db.add([
88+
{"name": "ad", "age": 3},
89+
{"name": "fred", "age": 4},
90+
{"name": "dev", "age": 5}
91+
])
92+
93+
print(db)
94+
95+
db.purge()
96+
db.commit() # to save the changes
97+
98+
print(db)
99+
```
100+
101+
{ '7b672af4': {'name': 'ad', 'age': 3},
102+
'93b626d2': {'name': 'fred', 'age': 4},
103+
'41907268': {'name': 'dev', 'age': 5}}
104+
105+
{}

docs/code/get.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<h1 align="center"> Onstro DB </h1>
2+
3+
## Getting data from the DB
4+
5+
You can get data from a DB with the following methods
6+
7+
- get_by_query()
8+
- get_by_hash_id()
9+
- get_all()
10+
11+
### get_by_query
12+
13+
The `get_by_query` method takes a single condition query and return a dict of all the rows that matches that condition
14+
15+
```python
16+
from onstrodb import OnstroDb
17+
18+
schema = {
19+
"name": {"type": "str", "required": True},
20+
"age": {"type": "int"}
21+
}
22+
23+
db = OnstroDb(db_name="test", schema=schema)
24+
25+
db.add([
26+
{"name": "ad", "age": 3},
27+
{"name": "fred", "age": 4},
28+
{"name": "dev", "age": 3}
29+
])
30+
31+
data = db.get_by_query({"age": 3}) # queries can only be one condition long(There is a work around for this)
32+
33+
print(data)
34+
```
35+
36+
{'7b672af4': {'name': 'ad', 'age': 3}, 'f3d32e1e': {'name': 'dev', 'age': 3}}
37+
38+
### get_by_hash_id
39+
40+
The `get_by_hash_id` accepts the hash id of the row that you want to get as an argument.
41+
42+
```python
43+
from onstrodb import OnstroDb
44+
45+
schema = {
46+
"name": {"type": "str", "required": True},
47+
"age": {"type": "int"}
48+
}
49+
50+
db = OnstroDb(db_name="test", schema=schema)
51+
52+
db.add([
53+
{"name": "ad", "age": 3},
54+
{"name": "fred", "age": 4},
55+
{"name": "dev", "age": 3}
56+
])
57+
58+
print(db)
59+
60+
data = db.get_by_hash_id("7b672af4")
61+
62+
print(data)
63+
```
64+
65+
{ '7b672af4': {'name': 'ad', 'age': 3},
66+
'93b626d2': {'name': 'fred', 'age': 4},
67+
'f3d32e1e': {'name': 'dev', 'age': 3}}
68+
{'name': 'ad', 'age': 3}
69+
70+
### get_all
71+
72+
The `get_all` methods returns all the rows in the DB
73+
74+
```python
75+
from onstrodb import OnstroDb
76+
77+
schema = {
78+
"name": {"type": "str", "required": True},
79+
"age": {"type": "int"}
80+
}
81+
82+
db = OnstroDb(db_name="test", schema=schema)
83+
84+
db.add([
85+
{"name": "ad", "age": 3},
86+
{"name": "fred", "age": 4},
87+
{"name": "dev", "age": 3}
88+
])
89+
90+
data = db.get_all()
91+
92+
print(data)
93+
```
94+
95+
{'7b672af4': {'name': 'ad', 'age': 3}, '93b626d2': {'name': 'fred', 'age': 4}, 'f3d32e1e': {'name': 'dev', 'age': 3}}
96+
97+
```python
98+
99+
```

docs/code/initialize.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<h1 align="center"> Onstro DB </h1>
2+
3+
### Initializing the DB
4+
5+
```python
6+
from onstrodb import OnstroDb
7+
8+
schema = {
9+
"name": {"type": "str"}
10+
}
11+
12+
13+
db = OnstroDb(db_name="test", db_path="./test_db", schema=schema)
14+
```
15+
16+
The above code snippet will create a new DB with the name `test` in the directory `test_db`. The tree structure of the directory will look something like this.
17+
18+
```commandline
19+
`-- test_db
20+
`-- test
21+
|-- db.schema
22+
`-- test.db
23+
```
24+
25+
The `db.schema` file stores the initially provided schema, thus avoiding the need to provide the schema during the second run.
26+
27+
> The supported types for the schema are **int**, **str**, **bool**, **float**. and these must be in quotes.
28+
29+
> if **db_path** is not provided then it will default to `./onstro-db`
30+
31+
---
32+
33+
### In memory DB
34+
35+
To make an in memory DB, Initialize the DB as follows.
36+
37+
```python
38+
from onstrodb import OnstroDb
39+
40+
schema = {
41+
"name": {"type": "str"}
42+
}
43+
44+
db = OnstroDb(db_name="test", schema=schema, in_memory=True)
45+
46+
```
47+
48+
This will not create the files as shown in the above snippet.
49+
50+
---
51+
52+
### Allowing duplicate values.
53+
54+
The DB is designed not to have duplicate values, and if tried to insert one, will raise an error.
55+
56+
In order to handle duplicate values initialize the DB as follows.
57+
58+
```python
59+
60+
from onstrodb import OnstroDb
61+
62+
schema = {
63+
"name": {"type": "str"}
64+
}
65+
66+
db = OnstroDb(db_name="test", schema=schema, allow_duplicate_values=True)
67+
68+
```
69+
70+
This will work with both normal and in memory DB.
71+
72+
---

0 commit comments

Comments
 (0)