The getRoleById tool retrieves a single role record by its document ID from the Firebase/Firestore database.
tools/getRoleById.js
Retrieves a specific role by document ID. The role is retrieved from the Firestore path: /Accounts/{accountId}/Roles/{roleId}.
{
roleId: string; // REQUIRED: Role document ID
}
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
roleId |
string | Yes | - | Firestore document ID of the role |
{
role: Role | null;
}
{
id: string; // Firestore document ID
path: string; // Full Firestore path
roleId: number; // Unique role ID
name: string; // Role name
updatedAt: string | Date; // Last update timestamp
permissions: object; // Role permissions configuration
// ... other role fields
}
// Get a specific role by ID
const result = await getRoleById(context, {
roleId: "role123"
});
if (result.role) {
console.log(result.role.name); // Role name
console.log(result.role.permissions); // Role permissions
} else {
console.log("Role not found");
}
const result = await getRoleById(context, {
roleId: "role123"
});
if (result.role) {
console.log(`Role: ${result.role.name}`);
console.log("Permissions:", result.role.permissions);
} else {
console.log("Role does not exist");
}
The tool transforms the Firestore document by:
id and full pathupdatedAt timestamp to Date objectconst data = docSnap.data();
return {
role: {
id: docSnap.id,
path: docSnap.ref.path,
...data,
updatedAt: safeToDate(data.updatedAt) || data.updatedAt,
},
};
The tool validates that roleId is provided:
if (!roleId) {
throw new Error("The 'roleId' parameter is required.");
}
If the role document doesn’t exist, the tool returns { role: null } instead of throwing an error.
| Error | Cause | Solution |
|---|---|---|
| “The ‘roleId’ parameter is required.” | Missing roleId | Provide roleId in params |
| “Failed to fetch role with id [id].” | Firestore error | Check Firebase connection and permissions |
Errors are logged with the role ID and re-thrown:
catch (error) {
console.error(`Error fetching role with id ${roleId}:`, error);
throw new Error(`Failed to fetch role with id ${roleId}.`);
}
getDoc() for efficient single document retrieval/Accounts/{accountId}/Roles/{roleId}
- roleId: number
- name: string
- updatedAt: timestamp
- permissions: object