Node.js Driver tutorial

Prerequisites

Make sure to install the Node.js Driver as per Installation page. The TypeDB server is also needs to be installed and running.

Importing

Use the require keyword with the typedb-client.

const { TypeDB } = require("typedb-client");

Connecting

Instantiate a TypeDB Core client and open a session to a database.

Find out how to create a database in TypeDB on the Quickstart guide page.

The name for the database should be social_network.
The database should have the following schema loaded: schema.tql.
The database should have the following data inserted: data.tql.

Instantiate a client and open a session.

const { TypeDB, SessionType } = require("typedb-client");

async function openSession (database) {
	const client = TypeDB.coreClient("localhost:1729");
	const session = await client.session(database, SessionType.DATA);
	// session is open
	await session.close();
	//session is closed
	client.close();
};

openSession("social_network");

Creating transactions

Create transactions to use for reading and writing data.

const { TypeDB, SessionType, TransactionType } = require("typedb-client");

async function createTransactions (database) {
	const client = TypeDB.coreClient("localhost:1729");
	const session = await client.session(database, SessionType.DATA);

	// creating a write transaction
	const writeTransaction = await session.transaction(TransactionType.WRITE); // write transaction is open
	// to persist changes, write transaction must always be committed/closed
	await writeTransaction.commit();

	// creating a read transaction
	const readTransaction = await session.transaction(TransactionType.READ); // read transaction is open
	// read transaction must always be closed
	await readTransaction.close();
	// a session must always be closed
	await session.close();
	// a client must always be closed
	client.close();
}

createTransactions("social_network");

Querying

Running basic retrieval and insertion queries.

const { TypeDB, SessionType, TransactionType } = require("typedb-client");

async function runBasicQueries(database) {
	const client = TypeDB.coreClient("localhost:1729");
	const session = await client.session(database, SessionType.DATA);

	// Insert a person using a WRITE transaction
	const writeTransaction = await session.transaction(TransactionType.WRITE);
	const insertStream = await writeTransaction.query.insert('insert $x isa person, has email "[email protected]";');
	const conceptMaps = await insertStream.collect();
	console.log("Inserted a person with ID: " + conceptMaps[0].get("x").iid);
	// to persist changes, a write transaction must always be committed (closed)
	await writeTransaction.commit();

	// Retrieve persons using a READ only transaction
	const readTransaction = await session.transaction(TransactionType.READ);

	// We can either query and consume the iterator lazily
	let answerStream = await readTransaction.query.match("match $x isa person; get $x; limit 10;");
	for await (const aConceptMapAnswer of answerStream) {
		const person = aConceptMapAnswer.get("x");
		console.log("Retrieved person with id " + person.iid);
	}

	// Or query and consume the iterator immediately collecting all the results
	answerStream = await readTransaction.query.match("match $x isa person; get $x; limit 10;");
	const persons = await answerStream.collect();
	persons.forEach( conceptMap => {
        person = conceptMap.get("x");
        console.log("Retrieved person with id " + person.iid);
    });

	// a read transaction must always be closed
	await readTransaction.close();
	// a session must always be closed
	await session.close();
	// a client must always be closed
	client.close();
}

runBasicQueries("social_network");

Remember that transactions always need to be closed. Committing a write transaction closes it. A read transaction, however, must be explicitly closed by calling the close() method on it.

Check out the API Reference to learn about the available methods on the concepts retrieved as the answers to TypeQL queries.

To view examples of running various queries using TypeQL, head over to their dedicated documentation pages as listed below:

Where to go next

Check out the Response section of API reference page to learn about the available methods on the concepts retrieved as the answers to queries.

To view examples of various TypeQL queries, head over to Writing data and Reading data pages.

For some more Python Driver examples — see the Python implementation on the Sample application page.