Datastore Documentation & Users DB Design
Datastore Documentation & Users DB Design
Why Datastore?
Structure:
• Document-Based: Datastore stores data in the form of entities, each of which can
have multiple key-value pairs known as properties.
• Entities and Kinds: Similar to tables in relational databases, entities are grouped by
“kind,” which is like a collection or table but without a rigid schema.
• Properties: These are the fields of each entity. Datastore supports various data
types for properties, such as strings, numbers, dates, lists, and more.
On Creating a New Project after selecting location and stuff. Create a new database
with the Datastore mode.
Configure your database. Encryption options keep as it is(If needed can be chnaged. Not
required in our project)
In the created database, (I have given default). We can create a kind and add entities to it
with multiple properties.
After adding properties to entity. It will look like the below image. You can see the kind and
Key value pairs in it
After that We need a trigger(API link) to perform CRUD operations on it. to generate API
enable CLOUD FUNCTIONS API
After enabling CLOUD Function API. Search for Cloud RUN functions and Create a
Function
def datastore_api(request):
if request.method == 'POST':
# Handle POST: Create a new entity
request_json = request.get_json(silent=True)
if not request_json:
return jsonify({'error': 'Invalid JSON'}), 400
kind = request_json.get('kind')
data = request_json.get('data')
try:
datastore_client.put(entity)
return jsonify({'message': f'Entity created with key {data["personid"]}'})
except Exception as e:
return jsonify({'error': str(e)}), 500
if not kind:
return jsonify({'error': 'Kind is required'}), 400
if personid:
# Retrieve a specific entity by personid
task_key = datastore_client.key(kind, personid)
entity = datastore_client.get(task_key)
if entity is None:
return jsonify({'error': 'Entity not found'}), 404
return jsonify(dict(entity))
else:
# Retrieve all entities of the specified kind
query = datastore_client.query(kind=kind)
entities = list(query.fetch())
return jsonify([dict(entity) for entity in entities])
elif request.method == 'PATCH':
# Handle PATCH: Update an entity
request_json = request.get_json(silent=True)
if not request_json:
return jsonify({'error': 'Invalid JSON'}), 400
kind = request_json.get('kind')
personid = request_json.get('personid') # Use personid instead of id
updated_data = request_json.get('data')
if entity is None:
return jsonify({'error': 'Entity not found'}), 404
entity.update(updated_data)
try:
datastore_client.put(entity)
return jsonify({'message': 'Entity updated successfully'})
except Exception as e:
return jsonify({'error': str(e)}), 500
try:
datastore_client.delete(task_key)
return jsonify({'message': 'Entity deleted successfully'})
except Exception as e:
return jsonify({'error': str(e)}), 500
else:
# Method not allowed
return jsonify({'error': 'Method Not Allowed'}), 405
requirements.txt :-
Data Retrieval/Manipulation:
URL: https://tejyy1g6bg.execute-api.eu-west-1.amazonaws.com/dev/getall
Expected Response
• Successful Response: If the request successfully retrieves user data, you should receive a
200 OK status with the details of all users. The response might look like:
[
{
"name": "John Doe",
"email": "[email protected]",
"phone_number": "1234567890",
...
},
{
"name": "Jane Smith",
"email": "[email protected]",
"phone_number": "0987654321",
...
}
]
• No Users Found: If there are no users in the datastore, you should receive a 404 response
with an error message:
{
"error": "No users found"
}
• Error Response: If an error occurs, such as a network issue or internal error, you will
receive a 500 status with a detailed error message:
{
"error": "An error occurred",
"details": "Error details here"
}
_____________________________________________________________________________
URL : https://hjnxjwv9kg.execute-api.eu-west-1.amazonaws.com/dev/user_id
Body:
{
"userName": "c8f193b0-5071-7078-770a-3791137cbaa0"
}
Expected Response
• If the userName exists, you should get a 200 OK status with the user's details in the
response.
• If the userName does not exist, you should get a 404 status with an error message:
{"error": "User not found"}.
• If userName is missing in the request body, the function will return a 400 status with an
error message: {"error": "User ID (User name) is required"}.
_____________________________________________________________________________
URL : https://hjnxjwv9kg.execute-api.eu-west-1.amazonaws.com/dev/user_id
Body:
{
"userName": "c8f193b0-5071-7078-770a-3791137cbaa0"
}
_____________________________________________________________________________
{
"userName": "exampleUserID",
"name": "New Name",
"phone_number": "1234567890",
"languagePreference": "Spanish",
"isDebarred": true,
"debarredUntil": "2024-12-31",
"fakeAttempts": 2,
"police_station": "Central Station",
"specialization": "Cybersecurity",
"experience": 10,
"assignedCases": 5,
"finishedCases": 3,
"factorValue": 1.5,
"isApproved": true,
"role": "police"
}
Expected Response
• Successful Update: If the data is successfully updated, you should get a 200 OK
response with a message like:
{
"message": "User data updated successfully",
"user_id": "exampleUserID"
}
• Failed Update: If the update fails, for example, due to an error in the request or
Datastore, you will get a 500 response with an error message similar to:
{
"error": "Failed to update user data in Datastore",
"details": "Additional error details"
}
• Missing userName Field: If userName is missing, a 400 error response will return:
{
"error": "userName (personid) is required"
}