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

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  	r "gopkg.in/rethinkdb/rethinkdb-go.v6"
     6  )
     7  
     8  // Insert a document into the table posts using a struct.
     9  func ExampleTerm_Insert_struct() {
    10  	type Post struct {
    11  		ID      int    `rethinkdb:"id"`
    12  		Title   string `rethinkdb:"title"`
    13  		Content string `rethinkdb:"content"`
    14  	}
    15  
    16  	resp, err := r.DB("examples").Table("posts").Insert(Post{
    17  		ID:      1,
    18  		Title:   "Lorem ipsum",
    19  		Content: "Dolor sit amet",
    20  	}).RunWrite(session)
    21  	if err != nil {
    22  		fmt.Print(err)
    23  		return
    24  	}
    25  
    26  	fmt.Printf("%d row inserted", resp.Inserted)
    27  
    28  	// Output:
    29  	// 1 row inserted
    30  }
    31  
    32  // Insert a document without a defined primary key into the table posts where
    33  // the primary key is id.
    34  func ExampleTerm_Insert_generatedKey() {
    35  	type Post struct {
    36  		Title   string `rethinkdb:"title"`
    37  		Content string `rethinkdb:"content"`
    38  	}
    39  
    40  	resp, err := r.DB("examples").Table("posts").Insert(map[string]interface{}{
    41  		"title":   "Lorem ipsum",
    42  		"content": "Dolor sit amet",
    43  	}).RunWrite(session)
    44  	if err != nil {
    45  		fmt.Print(err)
    46  		return
    47  	}
    48  
    49  	fmt.Printf("%d row inserted, %d key generated", resp.Inserted, len(resp.GeneratedKeys))
    50  
    51  	// Output:
    52  	// 1 row inserted, 1 key generated
    53  }
    54  
    55  // Insert a document into the table posts using a map.
    56  func ExampleTerm_Insert_map() {
    57  	resp, err := r.DB("examples").Table("posts").Insert(map[string]interface{}{
    58  		"id":      2,
    59  		"title":   "Lorem ipsum",
    60  		"content": "Dolor sit amet",
    61  	}).RunWrite(session)
    62  	if err != nil {
    63  		fmt.Print(err)
    64  		return
    65  	}
    66  
    67  	fmt.Printf("%d row inserted", resp.Inserted)
    68  
    69  	// Output:
    70  	// 1 row inserted
    71  }
    72  
    73  // Insert multiple documents into the table posts.
    74  func ExampleTerm_Insert_multiple() {
    75  	resp, err := r.DB("examples").Table("posts").Insert([]interface{}{
    76  		map[string]interface{}{
    77  			"title":   "Lorem ipsum",
    78  			"content": "Dolor sit amet",
    79  		},
    80  		map[string]interface{}{
    81  			"title":   "Lorem ipsum",
    82  			"content": "Dolor sit amet",
    83  		},
    84  	}).RunWrite(session)
    85  	if err != nil {
    86  		fmt.Print(err)
    87  		return
    88  	}
    89  
    90  	fmt.Printf("%d rows inserted", resp.Inserted)
    91  
    92  	// Output:
    93  	// 2 rows inserted
    94  }
    95  
    96  // Insert a document into the table posts, replacing the document if it already
    97  // exists.
    98  func ExampleTerm_Insert_upsert() {
    99  	resp, err := r.DB("examples").Table("posts").Insert(map[string]interface{}{
   100  		"id":    1,
   101  		"title": "Lorem ipsum 2",
   102  	}, r.InsertOpts{
   103  		Conflict: "replace",
   104  	}).RunWrite(session)
   105  	if err != nil {
   106  		fmt.Print(err)
   107  		return
   108  	}
   109  
   110  	fmt.Printf("%d row replaced", resp.Replaced)
   111  
   112  	// Output:
   113  	// 1 row replaced
   114  }
   115  
   116  // Update the status of the post with id of 1 to published.
   117  func ExampleTerm_Update() {
   118  	resp, err := r.DB("examples").Table("posts").Get(2).Update(map[string]interface{}{
   119  		"status": "published",
   120  	}).RunWrite(session)
   121  	if err != nil {
   122  		fmt.Print(err)
   123  		return
   124  	}
   125  
   126  	fmt.Printf("%d row replaced", resp.Replaced)
   127  
   128  	// Output:
   129  	// 1 row replaced
   130  }
   131  
   132  // Update bob's cell phone number.
   133  func ExampleTerm_Update_nested() {
   134  	resp, err := r.DB("examples").Table("users").Get("bob").Update(map[string]interface{}{
   135  		"contact": map[string]interface{}{
   136  			"phone": "408-555-4242",
   137  		},
   138  	}).RunWrite(session)
   139  	if err != nil {
   140  		fmt.Print(err)
   141  		return
   142  	}
   143  
   144  	fmt.Printf("%d row replaced", resp.Replaced)
   145  
   146  	// Output:
   147  	// 1 row replaced
   148  }
   149  
   150  // Update the status of all posts to published.
   151  func ExampleTerm_Update_all() {
   152  	resp, err := r.DB("examples").Table("posts").Update(map[string]interface{}{
   153  		"status": "published",
   154  	}).RunWrite(session)
   155  	if err != nil {
   156  		fmt.Print(err)
   157  		return
   158  	}
   159  
   160  	fmt.Printf("%d row replaced", resp.Replaced)
   161  
   162  	// Output:
   163  	// 4 row replaced
   164  }
   165  
   166  // Increment the field view of the post with id of 1. If the field views does not
   167  // exist, it will be set to 0.
   168  func ExampleTerm_Update_increment() {
   169  	resp, err := r.DB("examples").Table("posts").Get(1).Update(map[string]interface{}{
   170  		"views": r.Row.Field("views").Add(1).Default(0),
   171  	}).RunWrite(session)
   172  	if err != nil {
   173  		fmt.Print(err)
   174  		return
   175  	}
   176  
   177  	fmt.Printf("%d row replaced", resp.Replaced)
   178  
   179  	// Output:
   180  	// 1 row replaced
   181  }
   182  
   183  // Update the status of the post with id of 1 using soft durability.
   184  func ExampleTerm_Update_softDurability() {
   185  	resp, err := r.DB("examples").Table("posts").Get(2).Update(map[string]interface{}{
   186  		"status": "draft",
   187  	}, r.UpdateOpts{
   188  		Durability: "soft",
   189  	}).RunWrite(session)
   190  	if err != nil {
   191  		fmt.Print(err)
   192  		return
   193  	}
   194  
   195  	fmt.Printf("%d row replaced", resp.Replaced)
   196  
   197  	// Output:
   198  	// 1 row replaced
   199  }
   200  
   201  // Delete a single document from the table posts.
   202  func ExampleTerm_Delete() {
   203  	resp, err := r.DB("examples").Table("posts").Get(2).Delete().RunWrite(session)
   204  	if err != nil {
   205  		fmt.Print(err)
   206  		return
   207  	}
   208  
   209  	fmt.Printf("%d row deleted", resp.Deleted)
   210  
   211  	// Output:
   212  	// 1 row deleted
   213  }
   214  
   215  // Delete all comments where the field status is published
   216  func ExampleTerm_Delete_many() {
   217  	resp, err := r.DB("examples").Table("posts").Filter(map[string]interface{}{
   218  		"status": "published",
   219  	}).Delete().RunWrite(session)
   220  	if err != nil {
   221  		fmt.Print(err)
   222  		return
   223  	}
   224  
   225  	fmt.Printf("%d rows deleted", resp.Deleted)
   226  
   227  	// Output:
   228  	// 4 rows deleted
   229  }
   230  
   231  func ExampleTerm_SetWriteHook() {
   232  	resp, err := r.DB("test").Table("test").SetWriteHook(
   233  		func(id r.Term, oldVal r.Term, newVal r.Term) r.Term {
   234  			return r.Branch(oldVal.And(newVal),
   235  				newVal.Merge(map[string]r.Term{"write_counter": oldVal.Field("write_counter").Add(1)}),
   236  				newVal,
   237  				newVal.Merge(r.Expr(map[string]int{"write_counter": 1})),
   238  				nil,
   239  			)
   240  		}).RunWrite(session)
   241  
   242  	if err != nil {
   243  		fmt.Print(err)
   244  		return
   245  	}
   246  
   247  	fmt.Printf("%d hook created", resp.Created)
   248  	// Output:
   249  	// 1 hook created
   250  }