Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
victorguarana committed Apr 30, 2024
1 parent f15e6bb commit b6df99c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- [ ] Implement History
- [ ] gomongo.Options
- [ ] Implement Validation Hooks
- [ ] Implement Cache
- [ ] Add Benchmarks
66 changes: 66 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"context"
"fmt"
"time"

"github.com/victorguarana/gomongo/gomongo"
)

type Movie struct {
ID gomongo.ID `bson:"_id"`
Name string
Year int
}

func main() {
// Setting up the connection to the database
connectionSettings := gomongo.ConnectionSettings{
URI: "mongodb://localhost:27017",
DatabaseName: "mydatabase",
ConnectionTimeout: 60 * time.Second,
}
database, err := gomongo.NewDatabase(context.Background(), connectionSettings)
if err != nil {
panic(err)
}

// Creating a collection
moviesCollection, err := gomongo.NewCollection[Movie](database, "mymovies")
if err != nil {
panic(err)
}

// Inserting a movie
starWarsIV := Movie{
Name: "Star Wars",
Year: 1977,
}
starWarsIV.ID, err = moviesCollection.Create(context.Background(), starWarsIV)
if err != nil {
panic(err)
}

// Updating a movie
starWarsIV.Name = "Star Wars: Episode IV - A New Hope"
err = moviesCollection.UpdateID(context.Background(), starWarsIV.ID, starWarsIV)
if err != nil {
panic(err)
}

// Listing all movies
allMovies, err := moviesCollection.All(context.Background())
if err != nil {
panic(err)
}

fmt.Println("All movies from Mongo: ", allMovies)

// Deleting a movie
err = moviesCollection.DeleteID(context.Background(), starWarsIV.ID)
if err != nil {
panic(err)
}

}

0 comments on commit b6df99c

Please sign in to comment.