Exemple #1
0
// GetById method
func (ds *mongodb) GetById(id string) (*domain.Account, error) {
	session, err := registry.CreateMongoDBClient()
	if err != nil {
		return nil, err
	}
	collection := session.DB(registry.GetDatabaseName()).C(provision.TABLE_ACCOUNTS)
	acc := &domain.Account{}
	collection.FindId(bson.M{"_id": id}).One(&acc)

	return acc, nil
}
Exemple #2
0
func DestroyMongoDB() {
	session, err := registry.CreateMongoDBClient()
	if err != nil {
		fmt.Printf("%s", err)
	}

	err = session.DB(registry.GetDatabaseName()).C(TABLE_ACCOUNTS).DropCollection()

	if err != nil {
		fmt.Printf("%s", err)
	}
}
Exemple #3
0
// NildevInitMongoDB init
func NildevInitMongoDB() {
	// In which environment we are
	//env := registry.GetEnv()
	//fmt.Printf("%s", env)

	session, err := registry.CreateMongoDBClient()
	if err != nil {
		fmt.Printf("%s", err)
	}

	err = session.DB(registry.GetDatabaseName()).C(TABLE_ACCOUNTS).Create(&mgo.CollectionInfo{})

	if err != nil {
		fmt.Printf("%s", err)
	}

	usernameIndex := mgo.Index{
		Key:        []string{"userName"},
		Unique:     true,
		DropDups:   false,
		Background: true,
		Sparse:     true,
	}

	err = session.DB(registry.GetDatabaseName()).C(TABLE_ACCOUNTS).EnsureIndex(usernameIndex)

	if err != nil {
		fmt.Printf("%s", err)
	}

	emailIndex := mgo.Index{
		Key:        []string{"email"},
		Unique:     true,
		DropDups:   false,
		Background: true,
		Sparse:     true,
	}

	err = session.DB(registry.GetDatabaseName()).C(TABLE_ACCOUNTS).EnsureIndex(emailIndex)

	if err != nil {
		fmt.Printf("%s", err)
	}
}
Exemple #4
0
// Save method
func (ds *mongodb) Save(acc *domain.Account) error {
	if acc == nil {
		return errors.Trace(errors.Errorf("acc is nil!"))
	}

	session, err := registry.CreateMongoDBClient()
	if err != nil {
		return err
	}

	collection := session.DB(registry.GetDatabaseName()).C(provision.TABLE_ACCOUNTS)
	_, err = collection.Upsert(bson.M{"email": acc.Email}, acc)

	if err != nil {
		return err
	}

	return nil
}