Skip to content
This repository was archived by the owner on Sep 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

- chore: Update dgraph dependency [#62](https://github.com/hypermodeinc/modusDB/pull/62)

- fix: add context to api functions [#69](https://github.com/hypermodeinc/modusDB/pull/69)

## 2025-01-02 - Version 0.1.0

Baseline for the changelog.
Expand Down
26 changes: 16 additions & 10 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package modusdb

import (
"context"
"fmt"

"github.com/hypermodeinc/dgraph/v24/dql"
Expand All @@ -14,13 +15,14 @@ import (
"github.com/hypermodeinc/modusdb/api/structreflect"
)

func Create[T any](engine *Engine, object T, nsId ...uint64) (uint64, T, error) {
func Create[T any](ctx context.Context, engine *Engine, object T,
nsId ...uint64) (uint64, T, error) {
engine.mutex.Lock()
defer engine.mutex.Unlock()
if len(nsId) > 1 {
return 0, object, fmt.Errorf("only one namespace is allowed")
}
ctx, ns, err := getDefaultNamespace(engine, nsId...)
ctx, ns, err := getDefaultNamespace(ctx, engine, nsId...)
if err != nil {
return 0, object, err
}
Expand Down Expand Up @@ -50,7 +52,8 @@ func Create[T any](engine *Engine, object T, nsId ...uint64) (uint64, T, error)
return getByGid[T](ctx, ns, gid)
}

func Upsert[T any](engine *Engine, object T, nsId ...uint64) (uint64, T, bool, error) {
func Upsert[T any](ctx context.Context, engine *Engine, object T,
nsId ...uint64) (uint64, T, bool, error) {

var wasFound bool
engine.mutex.Lock()
Expand All @@ -59,7 +62,7 @@ func Upsert[T any](engine *Engine, object T, nsId ...uint64) (uint64, T, bool, e
return 0, object, false, fmt.Errorf("only one namespace is allowed")
}

ctx, ns, err := getDefaultNamespace(engine, nsId...)
ctx, ns, err := getDefaultNamespace(ctx, engine, nsId...)
if err != nil {
return 0, object, false, err
}
Expand Down Expand Up @@ -122,14 +125,15 @@ func Upsert[T any](engine *Engine, object T, nsId ...uint64) (uint64, T, bool, e
return gid, object, wasFound, nil
}

func Get[T any, R UniqueField](engine *Engine, uniqueField R, nsId ...uint64) (uint64, T, error) {
func Get[T any, R UniqueField](ctx context.Context, engine *Engine, uniqueField R,
nsId ...uint64) (uint64, T, error) {
engine.mutex.Lock()
defer engine.mutex.Unlock()
var obj T
if len(nsId) > 1 {
return 0, obj, fmt.Errorf("only one namespace is allowed")
}
ctx, ns, err := getDefaultNamespace(engine, nsId...)
ctx, ns, err := getDefaultNamespace(ctx, engine, nsId...)
if err != nil {
return 0, obj, err
}
Expand All @@ -144,28 +148,30 @@ func Get[T any, R UniqueField](engine *Engine, uniqueField R, nsId ...uint64) (u
return 0, obj, fmt.Errorf("invalid unique field type")
}

func Query[T any](engine *Engine, queryParams QueryParams, nsId ...uint64) ([]uint64, []T, error) {
func Query[T any](ctx context.Context, engine *Engine, queryParams QueryParams,
nsId ...uint64) ([]uint64, []T, error) {
engine.mutex.Lock()
defer engine.mutex.Unlock()
if len(nsId) > 1 {
return nil, nil, fmt.Errorf("only one namespace is allowed")
}
ctx, ns, err := getDefaultNamespace(engine, nsId...)
ctx, ns, err := getDefaultNamespace(ctx, engine, nsId...)
if err != nil {
return nil, nil, err
}

return executeQuery[T](ctx, ns, queryParams, true)
}

func Delete[T any, R UniqueField](engine *Engine, uniqueField R, nsId ...uint64) (uint64, T, error) {
func Delete[T any, R UniqueField](ctx context.Context, engine *Engine, uniqueField R,
nsId ...uint64) (uint64, T, error) {
engine.mutex.Lock()
defer engine.mutex.Unlock()
var zeroObj T
if len(nsId) > 1 {
return 0, zeroObj, fmt.Errorf("only one namespace is allowed")
}
ctx, ns, err := getDefaultNamespace(engine, nsId...)
ctx, ns, err := getDefaultNamespace(ctx, engine, nsId...)
if err != nil {
return 0, zeroObj, err
}
Expand Down
3 changes: 1 addition & 2 deletions api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func WithNamespace(ns uint64) ModusDbOption {
}
}

func getDefaultNamespace(engine *Engine, nsId ...uint64) (context.Context, *Namespace, error) {
func getDefaultNamespace(ctx context.Context, engine *Engine, nsId ...uint64) (context.Context, *Namespace, error) {
dbOpts := &modusDbOptions{
ns: engine.db0.ID(),
}
Expand All @@ -93,7 +93,6 @@ func getDefaultNamespace(engine *Engine, nsId ...uint64) (context.Context, *Name
return nil, nil, err
}

ctx := context.Background()
ctx = x.AttachNamespace(ctx, d.ID())

return ctx, d, nil
Expand Down
Loading