Basic Usage (fdm-core
)
After setting up your database connection as described in the previous step, you can start interacting with the FDM using functions exported by @svenvw/fdm-core
. This section provides basic examples of common operations using direct function calls.
Note: Most functions require a principal_id
(representing the user performing the action) and the initialized fdm
instance (database connection, likely of type FdmServerType
) as arguments. Authorization checks are performed internally based on the principal_id
.
import {
createFdmServer,
addFarm,
addField,
addCultivation,
// Import other needed functions and types
FdmServerType,
PrincipalId
} from '@svenvw/fdm-core';
import { createId } from '@paralleldrive/cuid2';
// --- Assume Initialization ---
// Replace with your actual initialization logic
declare const fdm: FdmServerType;
declare const principalId: PrincipalId;
// --- End Initialization ---
async function runExamples() {
let farmId: string | undefined;
let fieldId: string | undefined;
// --- Example: Creating a Farm ---
console.log("Attempting to create farm...");
try {
const newFarmData = {
// b_id_farm is generated by addFarm if not provided
b_name_farm: 'My Example Farm ' + createId(), // Add unique part to name
b_businessid_farm: 'BUSINESS123',
b_address_farm: '123 Farm Lane',
b_postalcode_farm: '12345',
};
const createdFarm = await addFarm(fdm, principalId, newFarmData);
console.log('Farm created successfully:', createdFarm);
farmId = createdFarm.b_id_farm; // Capture the ID for next steps
} catch (error) {
console.error('Error creating farm:', error);
return; // Stop if farm creation fails
}
// --- Example: Adding a Field ---
if (farmId) {
console.log(`Attempting to add field to farm ${farmId}...`);
try {
const newFieldData = {
// b_id is generated by addField if not provided
b_name: 'North Field ' + createId(), // Add unique part to name
// b_geometry: { type: 'Polygon', coordinates: [...] }, // GeoJSON Polygon - Requires PostGIS
b_id_source: 'ExternalSystemID-' + createId(),
b_acquiring_method: 'owner', // 'owner', 'lease', or 'unknown'
b_start: new Date(), // Optional: When management started
};
const createdFieldResult = await addField(fdm, principalId, farmId, newFieldData);
console.log('Field added successfully:', createdFieldResult);
// Assuming the result structure includes the field ID, adjust if needed
fieldId = createdFieldResult.field?.b_id;
} catch (error) {
console.error('Error adding field:', error);
// Continue even if field adding fails, to show next example if possible
}
}
// --- Example: Adding a Cultivation ---
if (farmId && fieldId) {
console.log(`Attempting to add cultivation to field ${fieldId}...`);
try {
// You would typically get this ID from fdm-data or your own source
const catalogueId = 'REPLACE_WITH_VALID_CATALOGUE_ID'; // e.g., a wheat variety ID
const cultivationData = {
// b_lu (cultivation instance ID) is generated if not provided
b_lu_catalogue: catalogueId,
b_lu_start: new Date(), // Sowing date/time
b_sowing_amount: 150, // Optional: amount in kg/ha or other unit
b_sowing_method: 'Drilled', // Optional: method
};
const cultivationResult = await addCultivation(fdm, principalId, fieldId, cultivationData);
console.log('Cultivation started successfully:', cultivationResult);
} catch (error) {
console.error('Error starting cultivation:', error);
}
} else {
console.log("Skipping cultivation example as farmId or fieldId is missing.");
}
}
// To run these examples:
// runExamples();
These examples illustrate the direct usage pattern for @svenvw/fdm-core
functions. Remember to replace placeholder values (like REPLACE_WITH_VALID_CATALOGUE_ID
) and consult the API Reference or source code for exact function signatures and return types. The next sections cover using catalogues (fdm-data
) and calculations (fdm-calculator
).