|
| 1 | +--- |
| 2 | +title: How to use the terminusdb CLI query interface |
| 3 | +nextjs: |
| 4 | + metadata: |
| 5 | + title: How to use the terminusdb CLI query interface |
| 6 | + description: Quick introduction to the terminusdb CLI WOQL Query interface which has a distinct syntax to other flavours. |
| 7 | + openGraph: |
| 8 | + images: https://assets.terminusdb.com/docs/technical-documentation-terminuscms-og.png |
| 9 | + alternates: |
| 10 | + canonical: https://terminusdb.org/docs/terminusdb-db-cli-querying/ |
| 11 | +media: [] |
| 12 | +--- |
| 13 | + |
| 14 | +This is a tutorial to learn the command line CLI interface to start exploring the local TerminusDB _system database. Be careful in not making incorrect or uncontrolled changes to the _system database as it can lock you out of your data products. |
| 15 | + |
| 16 | +*Disclaimer*: Do not run this tutorial with data that you do care about. Here be dragons. You have hereby been warned! |
| 17 | + |
| 18 | +The local TerminusDB database CLI has a WOQL query interface built in, with a distinct syntax to other flavours. When working with a local TerminusDB instance, it is often handy to perform offline commands against it, or extract information in a data pipeline for ML/Ops. |
| 19 | + |
| 20 | +This syntax is for advanced users who have significant experience with TerminusDB and WOQL. If you are new to TerminusDB, we recommend starting with the [TerminusDB Quickstart](/docs/get-started-with-terminusdb/). |
| 21 | + |
| 22 | +## Syntax |
| 23 | + |
| 24 | +The syntax of the WOQL query interface is distinct from other flavours, and is based more closely on the Prolog syntax. The WOQL logic is the same, but the syntax is different, especially for and, triple and other predicates expected in WOQL. |
| 25 | + |
| 26 | +This documentation is incomplete and a stub. PRs are welcome to strengthen it and make it more complete. It only covers the syntax differences. Refer to the [WOQL Javascript syntax](/docs/javascript-woql/) as the main language interface. |
| 27 | + |
| 28 | +We will use the system database as the example database for this guide as it's available on the local system. |
| 29 | + |
| 30 | +### Getting started with a simple query |
| 31 | + |
| 32 | +An example for finding the databases in the system: |
| 33 | + |
| 34 | +```bash |
| 35 | +./terminusdb query _system "( |
| 36 | + t(Database,rdf:type,'@schema':'UserDatabase') |
| 37 | +)" |
| 38 | +``` |
| 39 | + |
| 40 | +A couple of notes: |
| 41 | +* All variables must start with a capital letter |
| 42 | +* The string values must be in single quotes |
| 43 | +* IRIs with prefixes must have the two portions quoted independently with a colon inbetween due to the `@` sign |
| 44 | +* the `triple` syntax is `t(A,B,C)` instead of `triple(A,B,C)` |
| 45 | +* the `quad` syntax is `t(A,B,C,schema)` instead of `quad(A,B,C,schema)` |
| 46 | +* the `and` syntax is `(statementA,statementB)` instead of `and(A,B)` |
| 47 | +* the `or` syntax is `(statementA;statementB)` instead of `or(A,B)` |
| 48 | + |
| 49 | +### Finding which capabilities users have |
| 50 | + |
| 51 | +The terminusdb client does not yet have the ability to find which capabilities a user has on which databases or organizations. This command queries them. |
| 52 | + |
| 53 | +```bash |
| 54 | +./terminusdb query _system "select([User,Role,Scope],( |
| 55 | + t(Capability,rdf:type,'@schema':'Capability'), |
| 56 | + t(Capability,role,Role), |
| 57 | + t(Capability,scope,Scope), |
| 58 | + t(User,capability,Capability), |
| 59 | + |
| 60 | +))" |
| 61 | +``` |
| 62 | + |
| 63 | +Some additional notes: |
| 64 | + |
| 65 | +* select works by binding variables to be returned |
| 66 | +* `t()` is used to bind triples |
| 67 | +* to bind a specific value, use bind variables to specific values using `=` |
| 68 | +* Note that the variables are unified by the values stored in the database |
| 69 | + |
| 70 | +Or, to see the capabilities of all users, don't check for a specific user: |
| 71 | + |
| 72 | +```bash |
| 73 | +./terminusdb query _system "select([User,Role,Scope],( |
| 74 | + t(Capability,rdf:type,'@schema':'Capability'), |
| 75 | + t(Capability,role,Role), |
| 76 | + t(Capability,scope,Scope), |
| 77 | + t(User,capability,Capability) |
| 78 | +))" |
| 79 | +``` |
| 80 | + |
| 81 | +### Graph Specifications, GRAPH_SPEC |
| 82 | + |
| 83 | +To specify a graph to interact with, use the graph_spec before the query. Here are a couple of ways to specify a graph. The admin team is the default team on local TerminusDB instances. |
| 84 | + |
| 85 | +* `_system`: the system database |
| 86 | +* `org/data_product`: main of a specific data product |
| 87 | +* `admin/data_product/local/branch/main` the main branch of the data product data_product in the admin organisation/team |
| 88 | +* `admin/data_product/local/commit/9w8hk3y6rb8tjdy961ed3i536ntkqd8` a specific commit in the data product data_product |
| 89 | + |
0 commit comments