Last active
November 2, 2022 10:54
-
-
Save ainsleyclark/9ead14d64edad3381e7c68b4433dc79d to your computer and use it in GitHub Desktop.
Mongo Page Update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const ( | |
| // DatabaseName is the database table name for Pages. | |
| DatabaseName = "tasks" | |
| // Collection is the collection in which scrapes | |
| // and meta of a URL is stored. | |
| Collection = "scrapes" | |
| ) | |
| type ScrapeStore struct { | |
| *mg.Database | |
| } | |
| // New returns a new ScrapeStore. | |
| func New(ctx context.Context, client *mg.Client) *ScrapeStore { | |
| db := client.Database(DatabaseName) | |
| p := &scrapesStore{ | |
| Database: db, | |
| } | |
| return p | |
| } | |
| // Update persists an updated Scrape to the datastore. | |
| // | |
| // Returns errors.NOTFOUND if no rows were effected. | |
| // Returns errors.INTERNAL if the entity could not be updated. | |
| func (s *ScrapeStore) Update(ctx context.Context, url string, in krangio.ScrapeMetrics) error { | |
| const op = "ScrapeStore.Update" | |
| result, err := s.Collection(Collection).UpdateOne(ctx, | |
| bson.D{{Key: "url", Value: url}}, | |
| bson.M{"$set": bson.D{ | |
| {"metrics", in}, | |
| }}, | |
| ) | |
| if err != nil { | |
| return errors.NewInternal(err, mongo.ErrQueryMessage, op) | |
| } else if result.MatchedCount == 0 { | |
| return errors.NewNotFound(mongo.ErrNoDocuments, "Error, no Scrape exists with the URL: " + url), op) | |
| } | |
| return nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment