Skip to content

Commit

Permalink
add sdk support for modusdb
Browse files Browse the repository at this point in the history
  • Loading branch information
jairad26 committed Nov 6, 2024
1 parent 3cc343e commit ab88d85
Show file tree
Hide file tree
Showing 29 changed files with 4,658 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@
"label": "HTTP Client Example",
"value": "http"
},
{
"label": "ModusDB Example",
"value": "modusdb"
},
{
"label": "PostgreSQL Client Example",
"value": "postgresql"
Expand Down
1 change: 1 addition & 0 deletions runtime/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
modus_runtime
runtime
data
2 changes: 1 addition & 1 deletion runtime/config/commandline.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func parseCommandLineFlags() {
var showVersion bool
const versionUsage = "Show the Runtime version number and exit."
flag.BoolVar(&showVersion, "version", false, versionUsage)
flag.BoolVar(&showVersion, "v", false, versionUsage+" (shorthand)")
// flag.BoolVar(&showVersion, "v", false, versionUsage+" (shorthand)")

flag.Parse()

Expand Down
16 changes: 8 additions & 8 deletions runtime/hostfunctions/modusdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (
)

func init() {
const module_name = "modus_db"
const module_name = "modus_modusdb_client"

registerHostFunction(module_name, "dropAll", modusdb.DropAll,
withStartingMessage("Dropping all data."),
withCompletedMessage("Completed dropping all data."),
withCancelledMessage("Cancelled dropping all data."),
withErrorMessage("Error dropping all data."),
withStartingMessage("Dropping all."),
withCompletedMessage("Completed dropping all."),
withCancelledMessage("Cancelled dropping all."),
withErrorMessage("Error dropping all."),
withMessageDetail(func() string {
return "Dropping all data."
return "Dropping all."
}))

registerHostFunction(module_name, "dropData", modusdb.DropData,
Expand All @@ -50,8 +50,8 @@ func init() {
withCompletedMessage("Completed mutation."),
withCancelledMessage("Cancelled mutation."),
withErrorMessage("Error executing mutation."),
withMessageDetail(func(clientMus []*modusdb.Mutation) string {
return fmt.Sprintf("Mutations: %s", fmt.Sprint(clientMus))
withMessageDetail(func(mutationReq modusdb.MutationRequest) string {
return fmt.Sprintf("Mutations: %s", fmt.Sprint(mutationReq))
}))

registerHostFunction(module_name, "query", modusdb.Query,
Expand Down
33 changes: 23 additions & 10 deletions runtime/modusdb/modusdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var mdb *modusdb.DB

func Init(ctx context.Context) {
var err error
mdb, err = modusdb.New(modusdb.NewDefaultConfig())
mdb, err = modusdb.New(modusdb.NewDefaultConfig().WithDataDir("data"))
if err != nil {
logger.Fatal(ctx).Err(err).Msg("Error initializing modusdb")
}
Expand All @@ -33,21 +33,34 @@ func Close(ctx context.Context) {
}
}

func DropAll(ctx context.Context) error {
return mdb.DropAll(ctx)
func DropAll(ctx context.Context) (string, error) {
err := mdb.DropAll(ctx)
if err != nil {
return "", err
}
return "success", nil
}

func DropData(ctx context.Context) error {
return mdb.DropData(ctx)
func DropData(ctx context.Context) (string, error) {
err := mdb.DropData(ctx)
if err != nil {
return "", err
}
return "success", nil
}

func AlterSchema(ctx context.Context, schema string) error {
return mdb.AlterSchema(ctx, schema)
func AlterSchema(ctx context.Context, schema string) (string, error) {
err := mdb.AlterSchema(ctx, schema)
if err != nil {
return "", err
}
return "success", nil
}

func Mutate(ctx context.Context, clientMus []*Mutation) (map[string]uint64, error) {
mus := make([]*api.Mutation, 0, len(clientMus))
for _, m := range clientMus {
func Mutate(ctx context.Context, mutationReq MutationRequest) (map[string]uint64, error) {
mutations := mutationReq.Mutations
mus := make([]*api.Mutation, 0, len(mutations))
for _, m := range mutations {
mu := &api.Mutation{}
if m.SetJson != "" {
mu.SetJson = []byte(m.SetJson)
Expand Down
6 changes: 5 additions & 1 deletion runtime/modusdb/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

package modusdb

type MutationRequest struct {
Mutations []*Mutation
}

type Mutation struct {
SetJson string
DelJson string
Expand All @@ -23,7 +27,7 @@ type Response struct {
Latency *Latency
Metrics *Metrics
Uids map[string]string
Rdf []byte
Rdf []uint8
Hdrs map[string]*ListOfString
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/assemblyscript/examples/http/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions sdk/assemblyscript/examples/modusdb/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["assemblyscript-prettier"]
}
7 changes: 7 additions & 0 deletions sdk/assemblyscript/examples/modusdb/README.md
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.
6 changes: 6 additions & 0 deletions sdk/assemblyscript/examples/modusdb/asconfig.json
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"]
}
}
35 changes: 35 additions & 0 deletions sdk/assemblyscript/examples/modusdb/assembly/classes.ts
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[];
}
122 changes: 122 additions & 0 deletions sdk/assemblyscript/examples/modusdb/assembly/index.ts
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;
}
4 changes: 4 additions & 0 deletions sdk/assemblyscript/examples/modusdb/assembly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "assemblyscript/std/assembly.json",
"include": ["./**/*.ts"]
}
11 changes: 11 additions & 0 deletions sdk/assemblyscript/examples/modusdb/eslint.config.js
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,
);
10 changes: 10 additions & 0 deletions sdk/assemblyscript/examples/modusdb/modus.json
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"
}
}
}
Loading

0 comments on commit ab88d85

Please sign in to comment.