Skip to content

Commit

Permalink
feat: Add means to fetch schema (#2006)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #1993

## Description

Adds means to fetch schema.

Adds a bunch of funcs to the clients to allow fetching of
schema(versions) defined in the system. When the set-default stuff got
merged, users were no longer guaranteed (at least on init) to be able to
view the schema ids they have created, this change allows them to view
them when ever they like.
  • Loading branch information
AndrewSisley authored Oct 27, 2023
1 parent d49c860 commit 4427a16
Show file tree
Hide file tree
Showing 17 changed files with 1,152 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func NewDefraCommand(cfg *config.Config) *cobra.Command {
MakeSchemaAddCommand(),
MakeSchemaPatchCommand(),
MakeSchemaSetDefaultCommand(),
MakeSchemaDescribeCommand(),
schema_migrate,
)

Expand Down
82 changes: 82 additions & 0 deletions cli/schema_describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2023 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package cli

import (
"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/client"
)

func MakeSchemaDescribeCommand() *cobra.Command {
var name string
var root string
var versionID string

var cmd = &cobra.Command{
Use: "describe",
Short: "View schema descriptions.",
Long: `Introspect schema types.
Example: view all schemas
defradb client schema describe
Example: view schemas by name
defradb client schema describe --name User
Example: view schemas by root
defradb client schema describe --root bae123
Example: view a single schema by version id
defradb client schema describe --version bae123
`,
RunE: func(cmd *cobra.Command, args []string) error {
store := mustGetStoreContext(cmd)

var schemas []client.SchemaDescription
switch {
case versionID != "":
schema, err := store.GetSchemaByVersionID(cmd.Context(), versionID)
if err != nil {
return err
}
return writeJSON(cmd, schema)

case root != "":
s, err := store.GetSchemasByRoot(cmd.Context(), root)
if err != nil {
return err
}
schemas = s

case name != "":
s, err := store.GetSchemasByName(cmd.Context(), name)
if err != nil {
return err
}
schemas = s

default:
s, err := store.GetAllSchemas(cmd.Context())
if err != nil {
return err
}
schemas = s
}

return writeJSON(cmd, schemas)
},
}
cmd.PersistentFlags().StringVar(&name, "name", "", "Schema name")
cmd.PersistentFlags().StringVar(&root, "root", "", "Schema root")
cmd.PersistentFlags().StringVar(&versionID, "version", "", "Schema Version ID")
return cmd
}
16 changes: 16 additions & 0 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ type Store interface {
// this [Store].
GetAllCollections(context.Context) ([]Collection, error)

// GetSchemasByName returns the all schema versions with the given name.
GetSchemasByName(context.Context, string) ([]SchemaDescription, error)

// GetSchemaByVersionID returns the schema description for the schema version of the
// ID provided.
//
// Will return an error if it is not found.
GetSchemaByVersionID(context.Context, string) (SchemaDescription, error)

// GetSchemasByRoot returns the all schema versions for the given root.
GetSchemasByRoot(context.Context, string) ([]SchemaDescription, error)

// GetAllSchemas returns all schema versions that currently exist within
// this [Store].
GetAllSchemas(context.Context) ([]SchemaDescription, error)

// GetAllIndexes returns all the indexes that currently exist within this [Store].
GetAllIndexes(context.Context) (map[CollectionName][]IndexDescription, error)

Expand Down
217 changes: 217 additions & 0 deletions client/mocks/db.go

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

Loading

0 comments on commit 4427a16

Please sign in to comment.