0% found this document useful (0 votes)
12 views16 pages

Datastore Documentation & Users DB Design

Google Cloud Datastore is a fully managed NoSQL database ideal for scalable applications, storing data as entities with key-value pairs. The document outlines the steps to create a database, configure it, and perform CRUD operations using Cloud Functions and an API. It also provides a database design for user information and details on data retrieval methods with expected responses.

Uploaded by

yukkendran.dp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

Datastore Documentation & Users DB Design

Google Cloud Datastore is a fully managed NoSQL database ideal for scalable applications, storing data as entities with key-value pairs. The document outlines the steps to create a database, configure it, and perform CRUD operations using Cloud Functions and an API. It also provides a database design for user information and details on data retrieval methods with expected responses.

Uploaded by

yukkendran.dp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

DATASTORE

Basics to know about Datastore:

Why Datastore?

Google Cloud Datastore is a fully managed, NoSQL, document-based database designed


for scalability, flexibility, and high performance. It's part of the Google Cloud Platform
(GCP) and is especially well-suited for applications that require high availability and can
scale horizontally, like web and mobile apps.

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.

Steps to be followed to create table, kind and to do operations:-

On Creating a GCP account we are going to use the Datastore.


Create a New Project

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.

To create a kind and entity, click on create entity option


on clicking it, give namespace, kind, key Identifier(like Primary Key).

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

->give Cloud Run Functions(1st gen) in Environment


->Give function name and region
->Set trigger as HTTP
->save

->Write cloud funtion in desired language


->entry point should be same as function name in code like the below image
->test the function after deploying
->by default only POST method can be checked in GCP Console.
->Below is where you find the trigger URL(API link)

->Set Permissions to allow allUsers after clicking Grant access


->New principals = allUsers

->Select a role->Cloud functions->Cloud functions Invoker


Set the permissions like this in the below image.
->Save
->ReDeploy(Save and Deploy) and test the trigger in Postman(All the http methods can be
tested).
->cloud functions code which perfoms CRUD operations is given below
->requirements.txt also given
main.py :-

from google.cloud import datastore


from flask import jsonify, request

# Initialize Datastore client


datastore_client = datastore.Client()

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')

if not kind or not data or 'personid' not in data:


return jsonify({'error': 'Kind and data fields are required, including personid as
userName.'}), 400

# Set personid (userName) as the key


task_key = datastore_client.key(kind, data['personid'])
entity = datastore.Entity(key=task_key)
entity.update(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

elif request.method == 'GET':


# Handle GET: Retrieve one or all entities
kind = request.args.get('kind')
personid = request.args.get('personid') # Changed 'id' to 'personid' to match the key

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 not kind or not personid or not updated_data:


return jsonify({'error': 'Kind, personid, and data fields are required.'}), 400

task_key = datastore_client.key(kind, personid)


entity = datastore_client.get(task_key)

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

elif request.method == 'DELETE':


# Handle DELETE: Remove an entity
kind = request.args.get('kind')
personid = request.args.get('personid') # Changed 'id' to 'personid' to match the key

if not kind or not personid:


return jsonify({'error': 'Kind and personid are required'}), 400

task_key = datastore_client.key(kind, personid)

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 :-

# Function dependencies, for example:


# package>=version
google-cloud-datastore
flask

Database Design for storing Users Information:

1. personid: Unique identifier for the user (string).


2. name: User's name (string).
3. dateofbirth: User's date of birth (string, format YYYY-MM-DD).
4. gender: User's gender (string, e.g., "male").
5. email: User's email address (string).
6. phone_number: User's phone number (string, with country code).
7. languagePreference: User's language preference (string, e.g., "English").
8. police_station: Name of the user's police station, if applicable (string, possibly
empty).
9. specialization: User's area of specialization, if any (string, possibly empty).
10. experience: User's experience in years (integer).
11. role: User's role (string, e.g., "User",”Police”,”Admin”,”DGP”).
12. assignedCases: Number of cases assigned to the Police (integer).
13. finishedCases: Number of cases finished by the Police(integer).
14. factorValue: A numerical factor associated with the Police. To Determine the
number of cases that can be assigned to a particular investigator.
15. fakeAttempts: Count of fake attempts associated with the user (integer).
16. isDebarred: Whether the user is debarred (boolean).
17. debarredUntil: Date until which the user is debarred (string, possibly empty).

Data Retrieval/Manipulation:

1) Method: GET (All Users)

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"
}
_____________________________________________________________________________

2) Method: GET(Individual User)

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"}.

_____________________________________________________________________________

3) Method: DELETE(Individual User)

URL : https://hjnxjwv9kg.execute-api.eu-west-1.amazonaws.com/dev/user_id
Body:
{
"userName": "c8f193b0-5071-7078-770a-3791137cbaa0"
}

_____________________________________________________________________________

4) Method: PATCH (Individual User)


URL : https://hjnxjwv9kg.execute-api.eu-west-1.amazonaws.com/dev/user_id
Body:

{
"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"
}

You might also like