Exemplo n.º 1
0
func TableSetup(db, table string, optArgs ...r.TableCreateOpts) (r.WriteResponse, error) {
	wr, err := r.DBCreate(db).RunWrite(Session())
	if err != nil {
		return wr, err
	}

	return r.DB(db).TableCreate(table, optArgs...).RunWrite(Session())
}
Exemplo n.º 2
0
func setupHandler(response http.ResponseWriter, request *http.Request) {

	err := r.DBCreate("notifications").Exec(session)
	if err != nil {
		http.Error(response, err.Error(), 500)
		log.Println(err)

		err = r.DB("notifications").TableCreate("notification").Exec(session)
		if err != nil {
			http.Error(response, err.Error(), 500)
			log.Println(err)
		}
	}
}
Exemplo n.º 3
0
func main() {
	fmt.Println("Connecting to RethinkDB")

	session, err := r.Connect(r.ConnectOpts{
		Address:  "localhost:7704",
		Database: "players",
	})

	session.SetMaxOpenConns(10)

	if err != nil {
		log.Fatal("Could not connect")
	}

	r.DBDrop("players").Exec(session)
	err = r.DBCreate("players").Exec(session)
	if err != nil {
		panic(err)
	}

	err = r.DB("players").TableDrop("scores").Exec(session)
	err = r.DB("players").TableCreate("scores").Exec(session)
	if err != nil {
		log.Fatal("Could not create table", err)
	}

	err = r.DB("players").Table("scores").IndexCreate("Score").Exec(session)
	if err != nil {
		log.Fatal("Could not create index")
	}

	fmt.Println("Inserting new records")
	for i := 0; i < 10000; i++ {
		player := new(ScoreEntry)
		player.ID = strconv.Itoa(i)
		player.PlayerName = fmt.Sprintf("Player %d", i)
		player.Score = rand.Intn(100)
		_, err := r.Table("scores").Insert(player).RunWrite(session)
		if err != nil {
			log.Fatal(err)
		}
	}
}
Exemplo n.º 4
0
func TestDial(t *testing.T) {
	Convey("Given rethinkdb host, db name, table name", t, func() {
		host := "localhost:28015"
		dbName := "rethinker_test"
		tableName := "contacts"

		Printf("host[%s] db[%s] table[%s] \n", host, dbName, tableName)

		Convey("Startup connection", func() {
			var err error
			err = Startup(r.ConnectOpts{
				Address: host,
			})

			So(err, ShouldBeNil)

			Convey("Create db", func() {
				_, err = r.DBCreate(dbName).RunWrite(Session())
				So(err, ShouldBeNil)

				Convey("Create table", func() {
					_, err = r.DB(dbName).TableCreate(tableName).RunWrite(Session())
					So(err, ShouldBeNil)

					Convey("Insert item", func() {
						_, err := RunWrite(dbName, tableName, func(table r.Term) r.Term {
							return table.Insert(map[string]interface{}{
								"first_name": "tester",
							})
						})
						So(err, ShouldBeNil)
					})

					//					Convey("Find item", func() {
					//						c, err := Run(dbName, tableName, func(table r.Term) r.Term {
					//							return table.GetAllByIndex(0)
					//						})

					//						So(err, ShouldBeNil)

					//						item := make(map[string]interface{})
					//						c.One(&item)

					//						So(item["first_name"], ShouldEqual, "tester")
					//					})

					//					Convey("Delete item", func() {
					//						_, err := RunWrite(dbName, tableName, func(table r.Term) r.Term {
					//							return table.Delete()
					//						})
					//						So(err, ShouldBeNil)
					//					})

					Reset(func() {
						r.DB(dbName).TableDrop(tableName).RunWrite(Session())
					})
				})

			})

			Reset(func() {
				r.DBDrop(dbName).RunWrite(Session())
			})
		})
	})
}
func createTestDatabase() {
	r.DBCreate(RethinkDBName).RunWrite(Rethink)
}