Examples
Server client - TypeScript
agdb example: managing users and databases and running queries on agdb_server from TypeScript
What it shows
The TypeScript version of the Rust server client example using the @agnesoft/agdb_api package: it logs in as the admin, creates a user and a database and runs two batches of queries. Plain JavaScript objects are inserted as node values and the result of the first batch is passed directly to the second one.
Run it
From the repository root:
pnpm run examples --filter @agnesoft/agdb_api
The script compiles the example, builds the agdb_server and runs the example against a freshly started server, so you do not need to start one yourself. The port 3000 must be free (stop any agdb_server you started yourself).
Users and databases
The TypeScript client takes named arguments as objects:
const client = AgdbApi.new(new FetchClient(), "localhost:3000");
// Log in as admin and create a new user.
await client.user_login({ username: "admin", password: "admin" });
await client.admin_user_add({
username: "client",
password: "password111",
});
// Switch to the new user and create a database.
await client.user_login({ username: "client", password: "password111" });
await client.db_add({ owner: "client", db: "db", db_type: "memory" });
Running queries
const users = [
{ username: "user1", password: "password123" },
{ username: "user2", password: "password456" },
];
// Insert a root "users" alias and the user nodes.
let queries = [
QueryBuilder.insert().nodes().aliases(["users"]).query(),
QueryBuilder.insert().nodes().values(users).query(),
];
let results = (
await client.db_exec_mut({ owner: "client", db: "db", queries })
)[1];
// Link users to root and search for one by username.
queries = [
QueryBuilder.insert().edges().from("users").to(results[1]).query(),
QueryBuilder.select()
.search()
.depth_first()
.from("users")
.where()
.key("username")
.value("user1")
.query(),
];
results = (await client.db_exec_mut({ owner: "client", db: "db", queries }))[1];
console.log(`User: ${results[1].elements[0].id}`);
for (const { key, value } of results[1].elements[0].values) {
console.log(` ${JSON.stringify(key)}: ${JSON.stringify(value)}`);
}
Note that where_() from Rust is where() in TypeScript.
Learn more
- TypeScript client - installing and using
@agnesoft/agdb_apiin your project. - Server reference