Skip to content

Commit

Permalink
create session and permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
t-a-g-o committed Jun 1, 2024
1 parent 47c8346 commit bd28ca7
Show file tree
Hide file tree
Showing 4 changed files with 358 additions and 138 deletions.
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,42 @@ This package provides a lightweight, encrypted JSON-based database with support
```sh
pip install pyjondb
```
### Creating a database
### Create a user with admin role
```python
# Initialize authentication
auth = session.start()
auth.create_user('admin', 'adminpass', roles=['admin'])

# Authenticate and get a session ID
session_id = auth.authenticate('admin', 'adminpass')
if not session_id:
raise PermissionError("Authentication failed")
```
### Initialize and create the database
```python
# Initialize the database
# The database function has optional values:
# pyjondb.database("database", "mypassword", debug=True/False, absolute=True/False)
db = pyjondb.database("database", "mypassword")
db = database.init("database", "mydatabasekey", auth)

# Create the database
# Note: you should only create the database if it doesn't already exist
db.create()
db.create(session_id)
```
### Writing data to the database
```python
data = {
"name": "John",
"age": 30,
"email": "john@example.com"
}
db.write(data)
# Create a collection
db.create_collection('my_collection', session_id)

# Add a document
db.add_document('my_collection', {'name': 'example'}, session_id)

```

### Read the data
```python
data = db.read()
print(data)
# Get all documents in a collection
print(db.read_collection('my_collection', session_id))
```

### PyJONDB gets way more advanced than writing simple data. To learn more about collections, documents, aggregation, linking, and tree structures read the [docs](https://github.com/t-a-g-o/PyJONDB)
Expand Down
44 changes: 27 additions & 17 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
import pyjondb
from pyjondb import database
from pyjondb import session

# Initialize authentication
auth = session.start()
auth.create_user('admin', 'adminpass', roles=['admin'])

# Authenticate and get a session ID
session_id = auth.authenticate('admin', 'adminpass')
if not session_id:
raise PermissionError("Authentication failed")

# Initialize the database
db = pyjondb.database("database", "mypassword")
db = database.init("database", "mydatabasekey", auth)

# Create the database
# Note: you should only create the database if it doesnt already exist
db.create()
# Note: you should only create the database if it doesn't already exist
db.create(session_id)

# Create a collection
db.create_collection("users")
db.create_collection("users", session_id)

# Add documents to the collection
document1 = {"_id": 1, "name": "John", "age": 30}
document2 = {"_id": 2, "name": "Jane", "age": 25}
db.add_document("users", document1)
db.add_document("users", document2)
db.add_document("users", document1, session_id)
db.add_document("users", document2, session_id)

# Read the collection
print("Reading collection 'users':")
print(db.read_collection("users"))
print(db.read_collection("users", session_id))

# Find documents matching a query
query = {"age": 30}
print("Finding documents in 'users' collection with age 30:")
print(db.find_document("users", query))
print(db.find_document("users", query, session_id))

# Update a document
update_data = {"age": 35}
print("Updating document in 'users' collection:")
db.update_document("users", 1, update_data)
print(db.read_collection("users"))
db.update_document("users", 1, update_data, session_id)
print(db.read_collection("users", session_id))

# Delete a document
print("Deleting document from 'users' collection:")
db.delete_document("users", 2)
print(db.read_collection("users"))
db.delete_document("users", 2, session_id)
print(db.read_collection("users", session_id))

# Link collections
print("Linking collections:")
db.link_collections("users", "orders", "user_id", "order_id")
print("Linking collections:")
db.link_collections("users", "orders", "user_id", "order_id", session_id)

# Create tree structure
print("Creating tree structure:")
root_query = {"type": "person"}
child_query = {"age": {"$gt": 18}}
print(db.create_tree("people", root_query, "children", child_query))
print(db.create_tree("people", root_query, "children", child_query, session_id))

# Aggregate collection
print("Aggregating collection:")
pipeline = [
{"$match": {"age": {"$gt": 18}}},
{"$group": {"_id": {"age": "$age"}, "$sum": {"count": 1}}}
]
print(db.aggregate("users", pipeline))
print(db.aggregate("users", pipeline, session_id))
Loading

0 comments on commit bd28ca7

Please sign in to comment.