Skip to content

Commit 9da6503

Browse files
committed
data fetching
1 parent 5206608 commit 9da6503

File tree

7 files changed

+213
-31
lines changed

7 files changed

+213
-31
lines changed

modus/data-fetching.mdx

Lines changed: 189 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,199 @@
11
---
22
title: Data Fetching
3-
description: ""
3+
description: "Pull data into your app"
44
---
55

6-
PostgreSQL Example
6+
Modus makes it simple to fetch data from external sources. The specific data source you're retrieving from determines
7+
the method you use to interact with it.
8+
9+
## Fetching from databases
10+
11+
### PostgreSQL
12+
13+
PostgreSQL is a powerful, open source relational database system. Modus provides a simple way to interact with
14+
PostgreSQL databases.
15+
16+
Here is an example of fetching a person from a PostgreSQL database using the Modus SDK:
17+
18+
<CodeGroup>
19+
20+
```go Go
21+
import github.com/hypermodeinc/modus/sdk/go/pkg/postgresql
22+
23+
// The name of the PostgreSQL host, as specified in the modus.json manifest
24+
const host = "my-database"
25+
26+
type Person struct {
27+
Id int `json:"id"`
28+
Name string `json:"name"`
29+
Age int `json:"age"`
30+
Home *postgresql.Location `json:"home"`
31+
}
32+
33+
func GetPerson(id int) (*Person, error) {
34+
const query = "select * from people where id = $1"
35+
rows, _, err := postgresql.Query[Person](host, query, id)
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
if len(rows) == 0 {
41+
return nil, nil // Person not found
42+
}
43+
44+
return &rows[0], nil
45+
}
46+
```
747

848
```ts AssemblyScript
9-
const params = new postgresql.Params()
10-
params.push(firstName)
11-
params.push(lastName)
49+
import { postgresql } from "@hypermode/modus-sdk-as"
50+
51+
// The name of the PostgreSQL host, as specified in the modus.json manifest
52+
const host = "my-database"
53+
54+
@json
55+
class Person {
56+
id: i32 = 0
57+
name!: string
58+
age!: i32
59+
home!: postgresql.Location | null
60+
}
61+
62+
export function getPerson(id: i32): Person | null {
63+
const query = "select * from people where id = $1"
1264

13-
const response = postgresql.query<Person>(host, query, params)
65+
const params = new postgresql.Params()
66+
params.push(id)
67+
68+
const response = postgresql.query<Person>(host, query, params)
69+
return response.rows.length > 0 ? response.rows[0] : null
70+
}
1471
```
1572

16-
GraphQL Example
73+
</CodeGroup>
74+
75+
### Dgraph
76+
77+
Dgraph is a distributed, transactional graph database. Modus offers an easy way to query and mutate data in Dgraph.
78+
79+
Here is an example of fetching a person from a Dgraph database using the Modus SDK:
80+
81+
<CodeGroup>
82+
83+
```go Go
84+
import github.com/hypermodeinc/modus/sdk/go/pkg/dgraph
85+
86+
const hostName = "my-database"
87+
88+
func QuerySpecificPerson(firstName, lastName string) (*Person, error) {
89+
statement := `query queryPerson($firstName: string, $lastName: string) {
90+
people(func: eq(firstName, $firstName)) @filter(eq(lastName, $lastName)) {
91+
uid
92+
firstName
93+
lastName
94+
dgraph.type
95+
}
96+
}`
97+
98+
variables := map[string]string{
99+
"$firstName": firstName,
100+
"$lastName": lastName,
101+
}
102+
103+
response, err := dgraph.Execute(hostName, &dgraph.Request{
104+
Query: &dgraph.Query{
105+
Query: statement,
106+
Variables: variables,
107+
},
108+
})
109+
110+
if err != nil {
111+
return nil, err
112+
}
113+
114+
var peopleData PeopleData
115+
if err := json.Unmarshal([]byte(response.Json), &peopleData); err != nil {
116+
return nil, err
117+
}
118+
119+
if len(peopleData.People) == 0 {
120+
return nil, nil // Person not found
121+
}
122+
123+
return peopleData.People[0], nil
124+
}
125+
```
126+
127+
```ts AssemblyScript
128+
import { dgraph } from "@hypermode/modus-sdk-as"
129+
import { JSON } from "json-as"
130+
131+
// This host name should match one defined in the modus.json manifest file.
132+
const hostName: string = "my-database"
133+
134+
@json
135+
export class Person {
136+
constructor(uid: string = "", firstName: string = "", lastName: string = "", dType: string[] = []) {
137+
this.uid = uid
138+
this.firstName = firstName
139+
this.lastName = lastName
140+
this.dType = dType
141+
}
142+
uid: string = ""
143+
firstName: string = ""
144+
lastName: string = ""
145+
146+
@alias("dgraph.type")
147+
dType: string[] = []
148+
}
149+
150+
export function querySpecificPerson(firstName: string, lastName: string): Person | null {
151+
const statement = ` query queryPerson($firstName: string, $lastName: string) {
152+
people(func: eq(firstName, $firstName)) @filter(eq(lastName, $lastName)) {
153+
uid
154+
firstName
155+
lastName
156+
}
157+
}`
158+
159+
const vars = new dgraph.Variables()
160+
vars.set("$firstName", firstName)
161+
vars.set("$lastName", lastName)
162+
163+
const resp = dgraph.execute(hostName, new dgraph.Request(new dgraph.Query(statement, vars)))
164+
165+
const people = JSON.parse<PeopleData>(resp.Json).people
166+
167+
if (people.length === 0) return null
168+
return people[0]
169+
}
170+
```
171+
172+
</CodeGroup>
173+
174+
## Fetching from APIs
175+
176+
### HTTP
177+
178+
<CodeGroup>
179+
180+
```go Go
181+
182+
```
183+
184+
```ts AssemblyScript
185+
186+
```
187+
188+
</CodeGroup>
189+
190+
### GraphQL
191+
192+
<CodeGroup>
193+
194+
```go Go
195+
196+
```
17197

18198
```ts AssemblyScript
19199
const vars = new graphql.Variables()
@@ -22,3 +202,5 @@ vars.set("lastName", lastName)
22202

23203
const response = graphql.execute<PeopleData>(hostName, statement, vars)
24204
```
205+
206+
</CodeGroup>

modus/sdk/collections.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ To begin, import the `collections` namespace from the SDK:
1111

1212
<CodeGroup>
1313

14-
```ts AssemblyScript
15-
import { collections } from "@hypermode/functions-as"
16-
```
17-
1814
```go Go
1915
import github.com/hypermodeinc/modus/sdk/go/pkg/collections
2016
```
2117

18+
```ts AssemblyScript
19+
import { collections } from "@hypermode/functions-as"
20+
```
21+
2222
</CodeGroup>
2323

2424
## Collections APIs

modus/sdk/dgraph.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ To begin, import the `dgraph` namespace from the SDK:
1111

1212
<CodeGroup>
1313

14-
```ts AssemblyScript
15-
import { dgraph } from "@hypermode/modus-sdk-as"
16-
```
17-
1814
```go Go
1915
import github.com/hypermodeinc/modus/sdk/go/pkg/dgraph
2016
```
2117

18+
```ts AssemblyScript
19+
import { dgraph } from "@hypermode/modus-sdk-as"
20+
```
21+
2222
</CodeGroup>
2323

2424
## Dgraph APIs

modus/sdk/graphql.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ To begin, import the `graphql` namespace from the SDK:
1111

1212
<CodeGroup>
1313

14-
```ts AssemblyScript
15-
import { http } from "@hypermode/modus-sdk-as"
16-
```
17-
1814
```go Go
1915
import github.com/hypermodeinc/modus/sdk/go/pkg/graphql
2016
```
2117

18+
```ts AssemblyScript
19+
import { http } from "@hypermode/modus-sdk-as"
20+
```
21+
2222
</CodeGroup>
2323

2424
## GraphQL APIs

modus/sdk/http.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ To begin, import the `http` namespace from the SDK:
2424

2525
<CodeGroup>
2626

27-
```ts AssemblyScript
28-
import { http } from "@hypermode/modus-sdk-as"
29-
```
30-
3127
```go Go
3228
import github.com/hypermodeinc/modus/sdk/go/pkg/http
3329
```
3430

31+
```ts AssemblyScript
32+
import { http } from "@hypermode/modus-sdk-as"
33+
```
34+
3535
</CodeGroup>
3636

3737
## HTTP APIs

modus/sdk/models.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ To begin, import the `models` namespace from the SDK:
3636

3737
<CodeGroup>
3838

39-
```ts AssemblyScript
40-
import { models } from "@hypermode/modus-sdk-as"
41-
```
42-
4339
```go Go
4440
import github.com/hypermodeinc/modus/sdk/go/pkg/models
4541
```
4642

43+
```ts AssemblyScript
44+
import { models } from "@hypermode/modus-sdk-as"
45+
```
46+
4747
</CodeGroup>
4848

4949
You'll also need to import one or more classes for the model you are working with.

modus/sdk/postgresql.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ To begin, import the `postgresql` namespace from the SDK:
1111

1212
<CodeGroup>
1313

14-
```ts AssemblyScript
15-
import { postgresql } from "@hypermode/modus-sdk-as"
16-
```
17-
1814
```go Go
1915
import github.com/hypermodeinc/modus/sdk/go/pkg/postgresql
2016
```
2117

18+
```ts AssemblyScript
19+
import { postgresql } from "@hypermode/modus-sdk-as"
20+
```
21+
2222
</CodeGroup>
2323

2424
## PostgreSQL APIs

0 commit comments

Comments
 (0)