// WhereAndFetchOne returns one document that match the filter func (d *Default) WhereAndFetchOne(filter interface{}, result interface{}) error { cursor, err := r.Table(d.table).Filter(filter).Run(d.session) if err != nil { return api.NewDatabaseError(d, err, "") } if err := cursor.One(result); err != nil { if err == r.ErrEmptyResult { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// WhereAndFetchLimit returns paginated list of document func (d *Default) WhereAndFetchLimit(filter interface{}, paginator *api.Pagination, results interface{}) error { cursor, err := r.Table(d.table).Filter(filter).Run(d.session) if err != nil { return api.NewDatabaseError(d, err, "") } if err := cursor.All(results); err != nil { if err == r.ErrEmptyResult { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// Find a document match given id func (d *Default) Find(id interface{}, value interface{}) error { cursor, err := r.Table(d.table).Get(id).Run(d.session) if err != nil { return api.NewDatabaseError(d, err, "") } if err := cursor.One(value); err != nil { if err == r.ErrEmptyResult { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// FindOneBy a couple (k = v) in the database func (d *Default) FindOneBy(key string, value interface{}, result interface{}) error { cursor, err := r.Table(d.table).GetAllByIndex(key, value).Run(d.session) if err != nil { return api.NewDatabaseError(d, err, "") } if err := cursor.One(result); err != nil { if err == r.ErrEmptyResult { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// FindBy all couples (k = v) in the database func (d *Default) FindBy(key string, value interface{}, results interface{}) error { cursor, err := r.Table(d.table).Filter(func(row r.Term) r.Term { return row.Field(key).Eq(value) }).Run(d.session) if err != nil { return api.NewDatabaseError(d, err, "") } if err := cursor.All(results); err != nil { if err == r.ErrEmptyResult { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// Search all entities in the database func (d *Default) Search(results interface{}, filter interface{}, sortParams *api.SortParameters, pagination *api.Pagination) error { term := r.Table(d.table) // Filter if filter != nil { term = term.Filter(filter) } // Get total if pagination != nil { total, err := d.WhereCount(filter) if err != nil { return api.NewDatabaseError(d, err, "") } pagination.SetTotal(uint(total)) } // Sort if sortParams != nil { term = term.OrderBy(ConvertSortParameters(*sortParams)...) } // Slice result if pagination != nil { term = term.Slice(pagination.Offset(), pagination.Offset()+pagination.PerPage) } // Run the query cursor, err := term.Run(d.session) if err != nil { return api.NewDatabaseError(d, err, "") } // Fetch cursor err = cursor.All(results) if err != nil { if err == r.ErrEmptyResult { return api.ErrNoResult } return err } return nil }
// InsertOrUpdate a document occording to ID presence in database func (d *Default) InsertOrUpdate(id interface{}, data interface{}) error { _, err := r.Table(d.table).Insert(data, r.InsertOpts{Conflict: "update"}).RunWrite(d.session) if err != nil { if err == r.ErrEmptyResult { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// Insert inserts a document into the database func (d *Default) Insert(data interface{}) error { _, err := r.Table(d.table).Insert(data).RunWrite(d.session) if err != nil { if err == r.ErrEmptyResult { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// Delete a document from the database func (d *Default) Delete(id interface{}) error { _, err := r.Table(d.table).Get(id).Delete().RunWrite(d.session) if err != nil { if err == r.ErrEmptyResult { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// Update a document that match the selector func (d *Default) Update(selector interface{}, data interface{}) error { _, err := r.Table(d.table).Filter(selector).Update(data).RunWrite(d.session) if err != nil { if err == r.ErrEmptyResult { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// Search all entities from the database func (d *Default) Search(results interface{}, filter interface{}, sortParams *api.SortParameters, pagination *api.Pagination) error { session := d.session.Clone() defer session.Close() // Apply Filter if filter == nil { filter = bson.M{} } // Get total if pagination != nil { total, err := d.WhereCount(filter) if err != nil { return api.NewDatabaseError(d, err, "") } pagination.SetTotal(uint(total)) } // Prepare the query query := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filter) // Apply sorts if sortParams != nil { sort := ConvertSortParameters(*sortParams) if len(sort) > 0 { query = query.Sort(sort...) } } // Paginate if pagination != nil { query = query.Limit(int(pagination.PerPage)).Skip(int(pagination.Offset())) } err := query.All(results) if err != nil { return api.NewDatabaseError(d, err, "") } return err }
// UpdateID performs an update on an existing resource with ID that equals the id argument func (d *Default) UpdateID(id interface{}, data interface{}) error { session := d.session.Clone() defer session.Close() err := session.DB(d.GetDBName()).C(d.GetTableName()).UpdateId(id.(string), data) if err != nil { if err == mgo.ErrNotFound { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// WhereAndFetchOne filters with multiple fields and then fills result with the first found resource func (d *Default) WhereAndFetchOne(filter interface{}, result interface{}) error { session := d.session.Clone() defer session.Close() err := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filter).One(result) if err != nil { if err == mgo.ErrNotFound { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// WhereAndFetchLimit filters with multiple fields and then fills results with all found resources func (d *Default) WhereAndFetchLimit(filter interface{}, paginator *api.Pagination, results interface{}) error { session := d.session.Clone() defer session.Close() err := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filter).Limit(int(paginator.PerPage)).Skip(int(paginator.Offset())).All(results) if err != nil { if err == mgo.ErrNotFound { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// WhereCount allows counting with multiple fields func (d *Default) WhereCount(filter interface{}) (int, error) { session := d.session.Clone() defer session.Close() count, err := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filter).Count() if err != nil { if err == mgo.ErrNotFound { return 0, api.ErrNoResult } return 0, api.NewDatabaseError(d, err, "") } return count, nil }
// FindFetchOne searches for a resource and then unmarshals the first row into value func (d *Default) FindFetchOne(id string, value interface{}) error { session := d.session.Clone() defer session.Close() err := session.DB(d.GetDBName()).C(d.GetTableName()).Find(bson.M{"_id": id}).One(value) if err != nil { if err == mgo.ErrNotFound { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// DeleteAll deletes resources that match the passed filter func (d *Default) DeleteAll(pred interface{}) error { session := d.session.Clone() defer session.Close() _, err := session.DB(d.GetDBName()).C(d.GetTableName()).RemoveAll(pred) if err != nil { if err == mgo.ErrNotFound { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// WhereCount returns the document count that match the filter func (d *Default) WhereCount(filter interface{}) (int, error) { cursor, err := r.Table(d.table).Filter(filter).Count().Run(d.session) if err != nil { return 0, err } var count int if err := cursor.One(&count); err != nil { if err == r.ErrEmptyResult { return 0, api.ErrNoResult } return 0, api.NewDatabaseError(d, err, "") } return count, nil }
// FindByAndFetch retrieves a value by key and then fills results with the result. func (d *Default) FindByAndFetch(key string, value interface{}, results interface{}) error { filterMap := bson.M{ key: value, } session := d.session.Clone() defer session.Close() err := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filterMap).All(results) if err != nil { if err == mgo.ErrNotFound { return api.ErrNoResult } return api.NewDatabaseError(d, err, "") } return nil }
// FindByAndCount returns the number of elements that match the filter func (d *Default) FindByAndCount(key string, value interface{}) (int, error) { filterMap := bson.M{ key: value, } session := d.session.Clone() defer session.Close() n, err := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filterMap).Count() if err != nil { if err == mgo.ErrNotFound { return 0, api.ErrNoResult } return 0, api.NewDatabaseError(d, err, "") } return n, nil }
// FindByAndCount is used to count object that matchs the (key = value) predicate func (d *Default) FindByAndCount(key string, value interface{}) (int, error) { cursor, err := r.Table(d.table).Filter(func(row r.Term) r.Term { return row.Field(key).Eq(value) }).Count().Run(d.session) if err != nil { return 0, err } var count int if err := cursor.One(&count); err != nil { if err == r.ErrEmptyResult { return 0, api.ErrNoResult } return 0, api.NewDatabaseError(d, err, "") } return count, nil }