To query data, users interface with OrbisDB nodes directly. This can be done using a query builder, raw SQL or GraphQL. Node owners also have the option of using our Data dashboard (link) and the provided SQL or GraphQL editors (link).

Check out our Querying data (link) to find out more.

Before using the methods, make sure to initialize (link).

Syntax

OrbisDB SDK provides 2 main ways to query data, ORM-like query builder and raw SQL.

Queries ran using the SDK go through the public API interface and are ran using a read-only PostgreSQL user. This prevents any malicious query attempts at the DB level.

ORM-like (Query builder)

This syntax matches the way INSERTs and UPDATEs work. You build statements by chaining methods and eventually executing them.

const { columns, rows } = await orbis
    .select()
    .from("MODEL_ID" | "MODEL_ALIAS" | "VIEW")
    .where(
        {
            column_name: "value"
        }
    )
    .run()

// SELECT * FROM table WHERE column_name = 'value';
console.log("Retrieved data", rows)

Raw SQL

While still utilizing the same method, raw SQL statements do not have additional chained methods to build the statement itself.

const { columns, rows } = await orbis
    .select()
    .raw("SELECT * FROM table WHERE column_name = $1", ["value"])
    .run()

// SELECT * FROM table WHERE column_name = 'value';
console.log("Retrieved data", rows)

GraphQL

Execute GraphQL queries with any of the available GraphQL clients for your language of choice. This functionality is not built into the SDK.

// SELECT columnName, anotherColumn FROM table WHERE columnName = 'value';
{
	table(filter:{ column_name: "value" }) {
		column_name,
		another_column	
	}
}

Contexts

Query builder

Only SELECT statements built using the query builder can be scoped to a Context (link) by using .context(). You can also run the search across multiple Contexts.

Single context

const { columns, rows } = await orbis
    .select()
    .from("MODEL_ID" | "MODEL_ALIAS" | "VIEW")
    .where(
        {
            column_name: "value"
        }
    )
    .context("CONTEXT_ID")
    .run()