#!/usr/bin/env node
/**
* Simple direct test of Datastore connection
* This bypasses MCP and tests the Datastore client directly
*/
import { Datastore } from '@google-cloud/datastore';
console.log('๐งช Testing Google Cloud Datastore Connection\n');
// Check environment variables
console.log('Environment Check:');
console.log(' GOOGLE_CLOUD_PROJECT:', process.env.GOOGLE_CLOUD_PROJECT || 'โ Not set');
console.log(' GOOGLE_APPLICATION_CREDENTIALS:', process.env.GOOGLE_APPLICATION_CREDENTIALS || 'โ Not set');
console.log('');
if (!process.env.GOOGLE_CLOUD_PROJECT) {
console.error('โ Error: GOOGLE_CLOUD_PROJECT environment variable not set');
console.log('\nSet it with:');
console.log(' $env:GOOGLE_CLOUD_PROJECT="your-project-id"');
process.exit(1);
}
if (!process.env.GOOGLE_APPLICATION_CREDENTIALS) {
console.error('โ Error: GOOGLE_APPLICATION_CREDENTIALS environment variable not set');
console.log('\nSet it with:');
console.log(' $env:GOOGLE_APPLICATION_CREDENTIALS="C:\\path\\to\\key.json"');
process.exit(1);
}
// Initialize Datastore
const datastore = new Datastore({
projectId: process.env.GOOGLE_CLOUD_PROJECT,
keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS,
});
async function runTests() {
try {
console.log('๐ Connecting to Datastore...');
// Test 1: Insert an entity
console.log('\n๐ Test 1: Insert Entity');
const testId = `test-${Date.now()}`;
const key = datastore.key(['TestUser', testId]);
const entity = {
key: key,
data: {
name: 'Direct Test User',
email: `direct-test-${Date.now()}@example.com`,
age: 28,
createdAt: new Date(),
testRun: true
}
};
await datastore.save(entity);
console.log('โ
Entity inserted successfully');
console.log(' Kind: TestUser');
console.log(' ID:', testId);
// Test 2: Get the entity back
console.log('\n๐ Test 2: Get Entity');
const [retrievedEntity] = await datastore.get(key);
if (retrievedEntity) {
console.log('โ
Entity retrieved successfully');
console.log(' Data:', JSON.stringify(retrievedEntity, null, 2));
} else {
console.log('โ Entity not found');
}
// Test 3: Query entities
console.log('\n๐ Test 3: Query Entities');
const query = datastore.createQuery('TestUser')
.filter('testRun', '=', true)
.limit(5);
const [entities] = await datastore.runQuery(query);
console.log(`โ
Found ${entities.length} test entities`);
if (entities.length > 0) {
console.log(' First entity:', JSON.stringify(entities[0], null, 2));
}
// Test 4: Update entity
console.log('\nโ๏ธ Test 4: Update Entity');
entity.data.age = 29;
entity.data.updatedAt = new Date();
await datastore.update(entity);
console.log('โ
Entity updated successfully');
// Test 5: Delete entity
console.log('\n๐๏ธ Test 5: Delete Entity');
await datastore.delete(key);
console.log('โ
Entity deleted successfully');
// Test 6: Verify deletion
console.log('\nโ๏ธ Test 6: Verify Deletion');
const [deletedEntity] = await datastore.get(key);
if (!deletedEntity) {
console.log('โ
Entity successfully deleted (not found)');
} else {
console.log('โ ๏ธ Entity still exists after deletion');
}
console.log('\nโจ All tests passed!');
console.log('\n๐ Next steps:');
console.log(' 1. Check Datastore console: https://console.cloud.google.com/datastore');
console.log(' 2. Run the full MCP test: node test-client.js');
console.log(' 3. Configure Claude Desktop with claude_desktop_config.json');
} catch (error) {
console.error('\nโ Test failed:', error.message);
if (error.code) {
console.error(' Error code:', error.code);
}
if (error.code === 7) {
console.error('\n๐ก Tip: Make sure:');
console.error(' 1. Firestore/Datastore API is enabled in your project');
console.error(' 2. Your service account has "Cloud Datastore User" role');
console.error(' 3. You\'re using Datastore mode (not Firestore Native mode)');
}
process.exit(1);
}
}
runTests();