69 lines
1.8 KiB
JavaScript
69 lines
1.8 KiB
JavaScript
/**
|
|
* Basic usage example of Tryton RPC Client
|
|
* Simple example similar to the README
|
|
*/
|
|
|
|
const { TrytonClient } = require("../src/client");
|
|
|
|
async function basicExample() {
|
|
console.log("📖 Basic Tryton RPC Client Example");
|
|
console.log("==================================\n");
|
|
|
|
// Create client
|
|
const client = new TrytonClient({
|
|
hostname: "https://demo7.6.tryton.org", // Using HTTPS demo server
|
|
database: "demo7.6",
|
|
username: "admin",
|
|
password: "admin",
|
|
port: 8000,
|
|
language: "en",
|
|
});
|
|
|
|
try {
|
|
// Connect
|
|
console.log("Connecting...");
|
|
await client.connect();
|
|
console.log("✅ Connected!\n");
|
|
|
|
// Read a party
|
|
console.log("Reading party with ID 1...");
|
|
const party = await client.read(
|
|
"party.party",
|
|
[1],
|
|
["id", "name", "code"]
|
|
);
|
|
console.log("Party:", party[0]);
|
|
console.log();
|
|
|
|
// Create a new party
|
|
console.log("Creating new party...");
|
|
const newIds = await client.create("party.party", [
|
|
{ name: "Test Party from JS" },
|
|
]);
|
|
console.log("Created party with ID:", newIds[0]);
|
|
console.log();
|
|
|
|
// Search for parties
|
|
console.log("Searching for parties...");
|
|
const searchResults = await client.searchRead(
|
|
"party.party",
|
|
[["name", "like", "Test%"]],
|
|
["id", "name"],
|
|
0, // offset
|
|
5 // limit
|
|
);
|
|
console.log("Found parties:", searchResults);
|
|
} catch (error) {
|
|
console.error("❌ Error:", error.message);
|
|
} finally {
|
|
client.close();
|
|
console.log("\n👋 Connection closed");
|
|
}
|
|
}
|
|
|
|
if (require.main === module) {
|
|
basicExample().catch(console.error);
|
|
}
|
|
|
|
module.exports = { basicExample };
|