#!/usr/bin/env node
/**
* Create a test user in the LiteFarm database
* This script directly inserts a user, password, farm, and links them together
*/
import pg from 'pg';
import bcrypt from 'bcrypt';
import { randomUUID } from 'crypto';
const { Client } = pg;
// Database configuration
const client = new Client({
host: 'localhost',
port: 5433,
database: 'pg-litefarm',
user: 'postgres',
password: 'postgres'
});
// User configuration
const USER_EMAIL = 'your-email@example.com';
const USER_PASSWORD = 'your-secure-password';
const USER_FIRST_NAME = 'Florian';
const USER_LAST_NAME = 'Test';
const FARM_NAME = 'Test Farm';
async function createTestUser() {
try {
await client.connect();
console.log('✅ Connected to database');
// Check if user already exists
const existingUser = await client.query(
'SELECT user_id FROM users WHERE email = $1',
[USER_EMAIL]
);
if (existingUser.rows.length > 0) {
console.log(`⚠️ User ${USER_EMAIL} already exists with ID: ${existingUser.rows[0].user_id}`);
// Check if they have a farm
const userFarms = await client.query(
`SELECT f.farm_id, f.farm_name FROM farm f
JOIN "userFarm" uf ON f.farm_id = uf.farm_id
WHERE uf.user_id = $1`,
[existingUser.rows[0].user_id]
);
if (userFarms.rows.length > 0) {
console.log('✅ User already has farms:');
userFarms.rows.forEach(farm => {
console.log(` - ${farm.farm_name} (${farm.farm_id})`);
});
}
await client.end();
return;
}
// Generate IDs
const userId = randomUUID();
const farmId = randomUUID();
console.log(`\n📝 Creating user with ID: ${userId}`);
console.log(`📝 Creating farm with ID: ${farmId}`);
// Hash password
console.log('🔐 Hashing password...');
const salt = await bcrypt.genSalt(10);
const passwordHash = await bcrypt.hash(USER_PASSWORD, salt);
// Start transaction
await client.query('BEGIN');
try {
// 1. Create user
console.log('👤 Creating user...');
await client.query(
`INSERT INTO users (user_id, email, first_name, last_name, language_preference, status_id, gender, do_not_email)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
[userId, USER_EMAIL, USER_FIRST_NAME, USER_LAST_NAME, 'en', 1, 'PREFER_NOT_TO_SAY', false]
);
// 2. Create password
console.log('🔑 Creating password...');
await client.query(
`INSERT INTO password (user_id, password_hash, reset_token_version)
VALUES ($1, $2, $3)`,
[userId, passwordHash, 0]
);
// 3. Create farm
console.log('🌾 Creating farm...');
await client.query(
`INSERT INTO farm (farm_id, farm_name, created_by_user_id, updated_by_user_id, deleted, sandbox_farm)
VALUES ($1, $2, $3, $4, $5, $6)`,
[farmId, FARM_NAME, userId, userId, false, true]
);
// 4. Link user to farm (owner role = 1)
console.log('🔗 Linking user to farm...');
await client.query(
`INSERT INTO "userFarm" (user_id, farm_id, role_id, has_consent, status, consent_version)
VALUES ($1, $2, $3, $4, $5, $6)`,
[userId, farmId, 1, true, 'Active', '1.0']
);
// Commit transaction
await client.query('COMMIT');
console.log('\n✅ Test user created successfully!');
console.log('\n📋 Details:');
console.log(` Email: ${USER_EMAIL}`);
console.log(` Password: ${USER_PASSWORD}`);
console.log(` User ID: ${userId}`);
console.log(` Farm ID: ${farmId}`);
console.log(` Farm Name: ${FARM_NAME}`);
console.log('\n🚀 You can now test the MCP server!');
} catch (error) {
await client.query('ROLLBACK');
throw error;
}
} catch (error) {
console.error('❌ Error creating test user:', error.message);
process.exit(1);
} finally {
await client.end();
}
}
createTestUser();