Skip to content

Commit

Permalink
Merge pull request #31 from tigrisdata/main
Browse files Browse the repository at this point in the history
Alpha release
  • Loading branch information
efirs authored Apr 26, 2022
2 parents 807a614 + 5213d8c commit cc4e860
Show file tree
Hide file tree
Showing 20 changed files with 409 additions and 89 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*.so
*.dylib
tigrisdb-cli
tigris

# JSON files
*.json

# Test binary, built with `go test -c`
*.test
Expand Down
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@

# Install

## macOS

```sh
go install github.com/tigrisdata/tigrisdb-cli@latest
curl -sSL https://tigris.dev/cli-macos | sudo tar -xz -C /usr/local/bin
```

## Linux

```sh
curl -sSL https://tigris.dev/cli-linux | sudo tar -xz -C /usr/local/bin
```

# Example

```sh
tigris db local up # brings local TigrisDB up on localhost:8081

tigris db create database db1
tigris db create collection db1 \
'{ "name" : "coll1", "properties": {
"Key1": { "type": "string" },
"Field1": { "type": "int" } },
"primary_key": ["Key1"]
}'
tigris db create collection db1 '{
"name" : "coll1",
"properties": {
"Key1": { "type": "string" },
"Field1": { "type": "integer" }
},
"primary_key": ["Key1"]
}'

tigris db list databases
tigris db list collections db1
Expand Down Expand Up @@ -52,6 +64,8 @@ tigris db read "db1" "coll1" '{}'

tigris db drop collection db1 coll1
tigris db drop database db1

tigris db local down
```

# License
Expand Down
50 changes: 43 additions & 7 deletions cmd/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
"github.com/spf13/cobra"
"github.com/tigrisdata/tigrisdb-cli/client"
"github.com/tigrisdata/tigrisdb-cli/util"
api "github.com/tigrisdata/tigrisdb-client-go/api/server/v1"
"github.com/tigrisdata/tigrisdb-client-go/driver"
)

func createCollection(ctx context.Context, tx driver.Tx, raw driver.Schema) {
type Schema struct {
Name string
Name string `json:"title"`
}
var schema Schema
if err := json.Unmarshal(raw, &schema); err != nil {
Expand All @@ -36,12 +37,46 @@ func createCollection(ctx context.Context, tx driver.Tx, raw driver.Schema) {
if schema.Name == "" {
util.Error(fmt.Errorf("schema name is missing"), "create collection failed")
}
err := tx.CreateOrUpdateCollection(ctx, schema.Name, driver.Schema(raw))
err := tx.CreateOrUpdateCollection(ctx, schema.Name, raw)
if err != nil {
util.Error(err, "create collection failed")
}
}

type DescribeCollectionResponse struct {
Collection string `json:"collection,omitempty"`
Metadata *api.CollectionMetadata `json:"metadata,omitempty"`
Schema json.RawMessage `json:"schema,omitempty"`
}

var describeCollectionCmd = &cobra.Command{
Use: "collection {db} {collection}",
Short: "describe collection",
Long: "describe collection returns collection metadata, including schema",
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := util.GetContext(cmd.Context())
defer cancel()
resp, err := client.Get().DescribeCollection(ctx, args[0], args[1])
if err != nil {
util.Error(err, "describe collection failed")
}

tr := DescribeCollectionResponse{
Collection: resp.Collection,
Metadata: resp.Metadata,
Schema: resp.Schema,
}

b, err := json.Marshal(tr)
if err != nil {
util.Error(err, "describe collection failed")
}

util.Stdout("%s\n", string(b))
},
}

var listCollectionsCmd = &cobra.Command{
Use: "collections {db}",
Short: "list database collections",
Expand All @@ -62,10 +97,10 @@ var listCollectionsCmd = &cobra.Command{
var createCollectionCmd = &cobra.Command{
Use: "collection {db} {schema}...|-",
Short: "create collection(s)",
Args: cobra.MinimumNArgs(2),
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
client.Transact(cmd.Context(), args[0], func(ctx context.Context, tx driver.Tx) {
iterateInput(ctx, 1, args, func(ctx context.Context, args []string, docs []json.RawMessage) {
iterateInput(ctx, cmd, 1, args, func(ctx context.Context, args []string, docs []json.RawMessage) {
for _, v := range docs {
createCollection(ctx, tx, driver.Schema(v))
}
Expand All @@ -80,7 +115,7 @@ var dropCollectionCmd = &cobra.Command{
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
client.Transact(cmd.Context(), args[0], func(ctx context.Context, tx driver.Tx) {
iterateInput(ctx, 1, args, func(ctx context.Context, args []string, docs []json.RawMessage) {
iterateInput(ctx, cmd, 1, args, func(ctx context.Context, args []string, docs []json.RawMessage) {
for _, v := range docs {
err := tx.DropCollection(ctx, string(v))
if err != nil {
Expand All @@ -95,10 +130,10 @@ var dropCollectionCmd = &cobra.Command{
var alterCollectionCmd = &cobra.Command{
Use: "collection {db} {collection} {schema}",
Short: "update collection schema",
Args: cobra.MinimumNArgs(3),
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
client.Transact(cmd.Context(), args[0], func(ctx context.Context, tx driver.Tx) {
iterateInput(ctx, 1, args, func(ctx context.Context, args []string, docs []json.RawMessage) {
iterateInput(ctx, cmd, 1, args, func(ctx context.Context, args []string, docs []json.RawMessage) {
for _, v := range docs {
createCollection(ctx, tx, driver.Schema(v))
}
Expand All @@ -112,4 +147,5 @@ func init() {
createCmd.AddCommand(createCollectionCmd)
listCmd.AddCommand(listCollectionsCmd)
alterCmd.AddCommand(alterCollectionCmd)
describeCmd.AddCommand(describeCollectionCmd)
}
61 changes: 59 additions & 2 deletions cmd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package cmd

import (
"encoding/json"

"github.com/spf13/cobra"
"github.com/tigrisdata/tigrisdb-cli/client"
"github.com/tigrisdata/tigrisdb-cli/util"
api "github.com/tigrisdata/tigrisdb-client-go/api/server/v1"
)

var listDatabasesCmd = &cobra.Command{
Expand All @@ -36,10 +39,62 @@ var listDatabasesCmd = &cobra.Command{
},
}

type DescribeDatabaseResponse struct {
Db string `json:"db,omitempty"`
Metadata *api.DatabaseMetadata `json:"metadata,omitempty"`
Collections []*DescribeCollectionResponse `json:"collections,omitempty"`
}

var describeDatabaseCmd = &cobra.Command{
Use: "database {db}",
Short: "describe database",
Long: "describe database returns metadata for all the collections in the database",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := util.GetContext(cmd.Context())
defer cancel()
resp, err := client.Get().DescribeDatabase(ctx, args[0])
if err != nil {
util.Error(err, "describe collection failed")
}

schemaOnly, err := cmd.Flags().GetBool("schema-only")
if err != nil {
util.Error(err, "error reading the 'schema-only' option")
}

if schemaOnly {
for _, v := range resp.Collections {
util.Stdout("%s\n", string(v.Schema))
}
} else {
tr := DescribeDatabaseResponse{
Db: resp.Db,
Metadata: resp.Metadata,
}

for _, v := range resp.Collections {
tr.Collections = append(tr.Collections, &DescribeCollectionResponse{
Collection: v.Collection,
Metadata: v.Metadata,
Schema: v.Schema,
})
}

b, err := json.Marshal(tr)
if err != nil {
util.Error(err, "describe database failed")
}

util.Stdout("%s\n", string(b))
}
},
}

var createDatabaseCmd = &cobra.Command{
Use: "database {db}",
Short: "create database",
Args: cobra.MinimumNArgs(1),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := util.GetContext(cmd.Context())
defer cancel()
Expand All @@ -53,7 +108,7 @@ var createDatabaseCmd = &cobra.Command{
var dropDatabaseCmd = &cobra.Command{
Use: "database {db}",
Short: "drop database",
Args: cobra.MinimumNArgs(1),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := util.GetContext(cmd.Context())
defer cancel()
Expand All @@ -65,7 +120,9 @@ var dropDatabaseCmd = &cobra.Command{
}

func init() {
describeDatabaseCmd.Flags().BoolP("schema-only", "s", false, "dump only schema of all database collections")
dropCmd.AddCommand(dropDatabaseCmd)
createCmd.AddCommand(createDatabaseCmd)
listCmd.AddCommand(listDatabasesCmd)
describeCmd.AddCommand(describeDatabaseCmd)
}
11 changes: 5 additions & 6 deletions cmd/db.go → cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ import (
"github.com/spf13/cobra"
)

var dbCmd = &cobra.Command{
Use: "db",
Short: "Database related commands",
Long: `Database related commands.
Inluding database, collection and documents management`,
var describeCmd = &cobra.Command{
Use: "describe",
Short: "describe database or collection",
Args: cobra.MinimumNArgs(1),
}

func init() {
rootCmd.AddCommand(dbCmd)
dbCmd.AddCommand(describeCmd)
}
Loading

0 comments on commit cc4e860

Please sign in to comment.