-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e38091
commit 6797577
Showing
22 changed files
with
536 additions
and
382 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,67 @@ | ||
// db/db.go | ||
package db | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"github.com/jinzhu/gorm" | ||
_ "github.com/jinzhu/gorm/dialects/sqlite" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
var instance *gorm.DB | ||
var instance *mongo.Client | ||
var dbName = "testDB" | ||
|
||
func InitDB(uri string) { | ||
|
||
if instance != nil { | ||
return | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
func InitDB(localDBPath string) { | ||
var err error | ||
instance, err = gorm.Open("sqlite3", localDBPath) | ||
instance, err = mongo.Connect(ctx, options.Client().ApplyURI(uri)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// defer instance.Disconnect(ctx) | ||
|
||
err = instance.Ping(ctx, nil) | ||
if err != nil { | ||
log.Fatalf("Failed to ping: %s", err) | ||
} | ||
//fmt.Println("Successfully connected to MongoDB!") | ||
} | ||
|
||
func GetDB() *gorm.DB { | ||
return instance | ||
func GetDB() *mongo.Database { | ||
return instance.Database(dbName) | ||
} | ||
|
||
func CloseDB() { | ||
instance.Close() | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
if err := instance.Disconnect(ctx); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func EnsureCollectionExists(client *mongo.Client, dbName, collectionName string) error { | ||
// コレクションの一覧を取得 | ||
collections, err := client.Database(dbName).ListCollectionNames(context.TODO(), bson.M{"name": collectionName}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// コレクションが存在しない場合、新しいコレクションを作成 | ||
if len(collections) == 0 { | ||
err := client.Database(dbName).CreateCollection(context.TODO(), collectionName, options.CreateCollection().SetCapped(false)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.