Skip to content

Commit c172857

Browse files
chore: add examples (#89)
**Description** This PR adds a few examples and updates the README **Checklist** - [x] Code compiles correctly and linting passes locally --------- Co-authored-by: Copilot <[email protected]>
1 parent 20fd6d1 commit c172857

File tree

4 files changed

+416
-1
lines changed

4 files changed

+416
-1
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ new apps, paired with support for advanced use cases through the Dgraph Query La
2121
dynamic schema allows for natural relations to be expressed in your data with performance that
2222
scales with your use case.
2323

24-
ModusGraph is available as a Go package for running in-process, providing low-latency reads, writes,
24+
modusGraph is available as a Go package for running in-process, providing low-latency reads, writes,
2525
and vector searches. We’ve made trade-offs to prioritize speed and simplicity. When runnning
2626
in-process, modusGraph internalizes Dgraph's server components, and data is written to a local
2727
file-based database. modusGraph also supports remote Dgraph servers, allowing you deploy your apps
@@ -92,6 +92,19 @@ func main() {
9292
}
9393
```
9494

95+
## Limitations
96+
97+
modusGraph has a few limitations to be aware of:
98+
99+
- **Unique constraints in file-based mode**: Due to the intricacies of how Dgraph handles unique
100+
fields and upserts in its core package, unique field checks and upsert operations are not
101+
supported (yet) when using the local (file-based) mode. These operations work properly when using
102+
a full Dgraph cluster, but the simplified file-based mode does not support the constraint
103+
enforcement mechanisms required for uniqueness guarantees.
104+
105+
- **Schema evolution**: While modusGraph supports schema inference through tags, evolving an
106+
existing schema with new fields requires careful consideration to avoid data inconsistencies.
107+
95108
## Open Source
96109

97110
The modus framework, including modusGraph, is developed by [Hypermode](https://hypermode.com/) as an

examples/basic/README.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# modusGraph Basic CLI Example
2+
3+
This command-line application demonstrates basic operations with modusGraph, a graph database
4+
library. The example implements CRUD operations (Create, Read, Update, Delete) for a simple `Thread`
5+
entity type.
6+
7+
## Requirements
8+
9+
- Go 1.24 or higher
10+
- Access to either:
11+
- A local filesystem (for file-based storage)
12+
- A Dgraph cluster (for distributed storage)
13+
14+
## Installation
15+
16+
```bash
17+
# Navigate to the examples/basic directory
18+
cd examples/basic
19+
20+
# Run directly
21+
go run main.go [options]
22+
23+
# Or build and then run
24+
go build -o modusgraph-cli
25+
./modusgraph-cli [options]
26+
```
27+
28+
## Usage
29+
30+
```sh
31+
Usage of ./main:
32+
--dir string Directory where modusGraph will initialize, note the directory must exist and you must have write access
33+
--addr string Hostname/port where modusGraph will access for I/O (if not using the dir flag)
34+
--cmd string Command to execute: create, update, delete, get, list (default "create")
35+
--author string Created by (for create and update)
36+
--name string Name of the Thread (for create and update)
37+
--uid string UID of the Thread (required for update, delete, and get)
38+
--workspace string Workspace ID (for create, update, and filter for list)
39+
```
40+
41+
**Note**: You must provide either `--dir` (for file-based storage) or `--addr` (for Dgraph cluster)
42+
parameter.
43+
44+
## Commands
45+
46+
### Create a Thread
47+
48+
```bash
49+
go run main.go --dir /tmp/modusgraph-data --cmd create --name "New Thread" --workspace "workspace-123" --author "user-456"
50+
```
51+
52+
**Note**: Due to the intricacies of how Dgraph handles unique fields and upserts in its core
53+
package, unique field checks and upsert operations are not supported (yet) when using the local
54+
(file-based) mode. These operations work properly when using a full Dgraph cluster (--addr option),
55+
but the simplified file-based mode does not support the constraint enforcement mechanisms required
56+
for uniqueness guarantees. The workaround here would be to check for the Thread name and workspace
57+
ID before creating a new Thread.
58+
59+
### Update a Thread
60+
61+
```bash
62+
go run main.go --dir /tmp/modusgraph-data --cmd update --uid "0x123" --name "Updated Thread" --workspace "workspace-123" --author "user-456"
63+
```
64+
65+
### Get a Thread by UID
66+
67+
```bash
68+
go run main.go --dir /tmp/modusgraph-data --cmd get --uid "0x123"
69+
```
70+
71+
### Delete a Thread
72+
73+
```bash
74+
go run main.go --dir /tmp/modusgraph-data --cmd delete --uid "0x123"
75+
```
76+
77+
### List All Threads
78+
79+
```bash
80+
go run main.go --dir /tmp/modusgraph-data --cmd list
81+
```
82+
83+
### List Threads by Workspace
84+
85+
```bash
86+
go run main.go --dir /tmp/modusgraph-data --cmd list --workspace "workspace-123"
87+
```
88+
89+
## Using with Dgraph
90+
91+
To use with a Dgraph cluster instead of file-based storage:
92+
93+
```bash
94+
go run main.go --addr localhost:9080 --cmd list
95+
```
96+
97+
## Output Format
98+
99+
The application displays data in a tabular format:
100+
101+
- For single Thread retrieval (`get`), fields are displayed in a vertical layout
102+
- For multiple Thread retrieval (`list`), records are displayed in a horizontal table
103+
104+
## Logging
105+
106+
The application uses structured logging with different verbosity levels. To see more detailed logs
107+
including query execution, you can modify the `stdr.SetVerbosity(1)` line in the code to a higher
108+
level.

0 commit comments

Comments
 (0)