mirror of
https://github.com/bestnite/igdb-database.git
synced 2025-06-17 10:13:18 +08:00
u
This commit is contained in:
24
db/db.go
24
db/db.go
@ -20,10 +20,9 @@ var (
|
||||
)
|
||||
|
||||
type MongoDB struct {
|
||||
client *mongo.Client
|
||||
Collections map[endpoint.Name]*mongo.Collection
|
||||
GameCollection *mongo.Collection
|
||||
CountCollection *mongo.Collection
|
||||
client *mongo.Client
|
||||
Collections map[endpoint.Name]*mongo.Collection
|
||||
GameCollection *mongo.Collection
|
||||
}
|
||||
|
||||
func GetInstance() *MongoDB {
|
||||
@ -54,7 +53,6 @@ func GetInstance() *MongoDB {
|
||||
}
|
||||
|
||||
instance.GameCollection = client.Database(config.C().Database.Database).Collection("game_details")
|
||||
instance.CountCollection = client.Database(config.C().Database.Database).Collection("count")
|
||||
instance.createIndex()
|
||||
})
|
||||
|
||||
@ -115,7 +113,7 @@ func (m *MongoDB) createIndex() {
|
||||
}
|
||||
|
||||
for _, e := range endpoint.AllNames {
|
||||
if e == endpoint.EPWebhooks || e == endpoint.EPSearch || e == endpoint.EPPopularityPrimitives {
|
||||
if e == endpoint.EPWebhooks || e == endpoint.EPSearch {
|
||||
continue
|
||||
}
|
||||
_, err := m.Collections[e].Indexes().CreateOne(ctx, mongo.IndexModel{
|
||||
@ -140,6 +138,20 @@ func (m *MongoDB) createIndex() {
|
||||
}
|
||||
}
|
||||
|
||||
func CountDocuments(e endpoint.Name) (int64, error) {
|
||||
coll := GetInstance().Collections[e]
|
||||
if coll == nil {
|
||||
return 0, fmt.Errorf("collection not found")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
count, err := coll.CountDocuments(ctx, bson.M{})
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to count %s: %w", string(e), err)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func EstimatedDocumentCount(e endpoint.Name) (int64, error) {
|
||||
coll := GetInstance().Collections[e]
|
||||
if coll == nil {
|
||||
|
Reference in New Issue
Block a user