-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
4,658 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
modus_runtime | ||
runtime | ||
data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["assemblyscript-prettier"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Modus Dgraph Example | ||
|
||
This example shows how to use the integrated Dgraph client. | ||
|
||
It uses Dgraph at the connection configured in the `modus.json` manifest. | ||
|
||
See [./assembly/index.ts](./assembly/index.ts) for the implementation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "./node_modules/@hypermode/modus-sdk-as/plugin.asconfig.json", | ||
"options": { | ||
"transform": ["@hypermode/modus-sdk-as/transform", "json-as/transform"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* This example is part of the Modus project, licensed under the Apache License 2.0. | ||
* You may modify and use this example in accordance with the license. | ||
* See the LICENSE file that accompanied this code for further details. | ||
*/ | ||
|
||
// These classes are used by the example functions in the index.ts file. | ||
|
||
@json | ||
export class Person { | ||
constructor( | ||
uid: string = "", | ||
firstName: string = "", | ||
lastName: string = "", | ||
dType: string[] = [], | ||
) { | ||
this.uid = uid; | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
this.dType = dType; | ||
} | ||
uid: string = ""; | ||
firstName: string = ""; | ||
lastName: string = ""; | ||
|
||
|
||
@alias("dgraph.type") | ||
dType: string[] = []; | ||
} | ||
|
||
|
||
@json | ||
export class PeopleData { | ||
people!: Person[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* This example is part of the Modus project, licensed under the Apache License 2.0. | ||
* You may modify and use this example in accordance with the license. | ||
* See the LICENSE file that accompanied this code for further details. | ||
*/ | ||
|
||
import { modusdb } from "@hypermode/modus-sdk-as"; | ||
import { PeopleData, Person } from "./classes"; | ||
import { JSON } from "json-as"; | ||
import { MutationRequest } from "@hypermode/modus-sdk-as/assembly/modusdb"; | ||
|
||
export function dropAll(): string { | ||
return modusdb.dropAll(); | ||
} | ||
|
||
export function dropData(): string { | ||
return modusdb.dropData(); | ||
} | ||
|
||
export function alterSchema(): string { | ||
const schema = ` | ||
firstName: string @index(term) . | ||
lastName: string @index(term) . | ||
dgraph.type: [string] @index(exact) . | ||
type Person { | ||
firstName | ||
lastName | ||
} | ||
`; | ||
return modusdb.alterSchema(schema); | ||
} | ||
|
||
// This function returns the results of querying for all people in the database. | ||
export function queryPeople(): Person[] { | ||
const query = ` | ||
{ | ||
people(func: type(Person)) { | ||
uid | ||
firstName | ||
lastName | ||
} | ||
} | ||
`; | ||
|
||
const resp = modusdb.query(query); | ||
|
||
return JSON.parse<PeopleData>(resp.Json).people; | ||
} | ||
|
||
// This function returns the results of querying for a specific person in the database. | ||
export function querySpecificPerson( | ||
firstName: string, | ||
lastName: string, | ||
): Person | null { | ||
const query = ` | ||
query queryPerson { | ||
people(func: eq(firstName, "${firstName}")) @filter(eq(lastName, "${lastName}")) { | ||
uid | ||
firstName | ||
lastName | ||
} | ||
} | ||
`; | ||
|
||
const resp = modusdb.query(query); | ||
|
||
const people = JSON.parse<PeopleData>(resp.Json).people; | ||
|
||
if (people.length === 0) return null; | ||
return people[0]; | ||
} | ||
|
||
// This function adds a new person to the database and returns that person. | ||
export function addPersonAsRDF( | ||
firstName: string, | ||
lastName: string, | ||
): Map<string, u64> | null { | ||
const mutation = ` | ||
_:user1 <firstName> "${firstName}" . | ||
_:user1 <lastName> "${lastName}" . | ||
_:user1 <dgraph.type> "Person" . | ||
`; | ||
|
||
const mutations: modusdb.Mutation[] = [ | ||
new modusdb.Mutation("", "", mutation), | ||
]; | ||
|
||
return modusdb.mutate(new MutationRequest(mutations)); | ||
} | ||
|
||
export function addPersonAsJSON( | ||
firstName: string, | ||
lastName: string, | ||
): Map<string, u64> | null { | ||
const person = new Person("_:user1", firstName, lastName, ["Person"]); | ||
|
||
const mutation = JSON.stringify(person); | ||
|
||
const mutations: modusdb.Mutation[] = [new modusdb.Mutation(mutation)]; | ||
|
||
return modusdb.mutate(new MutationRequest(mutations)); | ||
} | ||
|
||
export function deletePerson(uid: string): Map<string, u64> | null { | ||
const mutation = `<${uid}> * * .`; | ||
|
||
const mutations: modusdb.Mutation[] = [ | ||
new modusdb.Mutation("", "", mutation), | ||
]; | ||
|
||
return modusdb.mutate(new MutationRequest(mutations)); | ||
} | ||
|
||
// This function demonstrates what happens when a bad query is executed. | ||
export function testBadQuery(): Person[] { | ||
const query = "this is a bad query"; | ||
|
||
const resp = modusdb.query(query); | ||
|
||
return JSON.parse<PeopleData>(resp.Json).people; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "assemblyscript/std/assembly.json", | ||
"include": ["./**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// @ts-check | ||
|
||
import eslint from "@eslint/js"; | ||
import tseslint from "typescript-eslint"; | ||
import aseslint from "@hypermode/modus-sdk-as/tools/assemblyscript-eslint"; | ||
|
||
export default tseslint.config( | ||
eslint.configs.recommended, | ||
...tseslint.configs.recommended, | ||
aseslint.config, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"$schema": "https://schema.hypermode.com/modus.json", | ||
"endpoints": { | ||
"default": { | ||
"type": "graphql", | ||
"path": "/graphql", | ||
"auth": "bearer-token" | ||
} | ||
} | ||
} |
Oops, something went wrong.