gopkg.in/rethinkdb/rethinkdb-go.v6@v6.2.2/internal/integration/tests/example_query_table_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  	r "gopkg.in/rethinkdb/rethinkdb-go.v6"
     6  )
     7  
     8  // Create a table named "table" with the default settings.
     9  func ExampleTerm_TableCreate() {
    10  	// Setup database
    11  	r.DB("examples").TableDrop("table").Run(session)
    12  
    13  	response, err := r.DB("examples").TableCreate("table").RunWrite(session)
    14  	if err != nil {
    15  		r.Log.Fatalf("Error creating table: %s", err)
    16  	}
    17  
    18  	fmt.Printf("%d table created", response.TablesCreated)
    19  
    20  	// Output:
    21  	// 1 table created
    22  }
    23  
    24  // Create a simple index based on the field name.
    25  func ExampleTerm_IndexCreate() {
    26  	// Setup database
    27  	r.DB("examples").TableDrop("table").Run(session)
    28  	r.DB("examples").TableCreate("table").Run(session)
    29  
    30  	response, err := r.DB("examples").Table("table").IndexCreate("name").RunWrite(session)
    31  	if err != nil {
    32  		r.Log.Fatalf("Error creating index: %s", err)
    33  	}
    34  
    35  	fmt.Printf("%d index created", response.Created)
    36  
    37  	// Output:
    38  	// 1 index created
    39  }
    40  
    41  // Create a compound index based on the fields first_name and last_name.
    42  func ExampleTerm_IndexCreate_compound() {
    43  	// Setup database
    44  	r.DB("examples").TableDrop("table").Run(session)
    45  	r.DB("examples").TableCreate("table").Run(session)
    46  
    47  	response, err := r.DB("examples").Table("table").IndexCreateFunc("full_name", func(row r.Term) interface{} {
    48  		return []interface{}{row.Field("first_name"), row.Field("last_name")}
    49  	}).RunWrite(session)
    50  	if err != nil {
    51  		r.Log.Fatalf("Error creating index: %s", err)
    52  	}
    53  
    54  	fmt.Printf("%d index created", response.Created)
    55  
    56  	// Output:
    57  	// 1 index created
    58  }