-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
65 lines (54 loc) · 1.67 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
"fmt"
"log"
"path/filepath"
"strings"
"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/core/fileaccess"
"github.com/pixlise/core/v4/core/utils"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func importJSONFiles(jsonImportDir string, destDB *mongo.Database) error {
if len(jsonImportDir) <= 0 {
fmt.Println("SKIPPED - jsonImportDir is empty")
return nil
}
fs := fileaccess.FSAccess{}
files, err := fs.ListObjects(jsonImportDir, "")
if err != nil {
return err
}
// Run through and make sure each is a collection
collNames := dbCollections.GetAllCollections()
for _, f := range files {
if !strings.HasSuffix(f, ".json") {
return fmt.Errorf("File type not valid: %v", f)
}
collName := f[0 : len(f)-5]
if !utils.ItemInSlice(collName, collNames) {
return fmt.Errorf("File name is not a collection name: %v", f)
}
// Read it into the specified collection
v := []interface{}{}
err = fs.ReadJSON(filepath.Join(jsonImportDir, f), "", &v, false)
if err != nil {
return fmt.Errorf("Failed to read json file %v. Error: %v", f, err)
}
// We have it as an object, should be able to add to DB
coll := destDB.Collection(collName)
result, err := coll.InsertMany(context.TODO(), v, options.InsertMany())
if err != nil || len(result.InsertedIDs) != len(v) {
if mongo.IsDuplicateKeyError(err) {
log.Printf("WARNING: Importing %v caused duplicate key error: %v. Continuing...", f, err)
} else {
return fmt.Errorf("Failed to insert into DB from json file %v. Error: %v", f, err)
}
} else {
fmt.Printf("Imported: %v\n", f)
}
}
return nil
}