11---
22title : 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
19199const vars = new graphql .Variables ()
@@ -22,3 +202,5 @@ vars.set("lastName", lastName)
22202
23203const response = graphql .execute <PeopleData >(hostName , statement , vars )
24204```
205+
206+ </CodeGroup >
0 commit comments