-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (80 loc) · 2.49 KB
/
index.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const mongoose = require('mongoose');
const fs = require('fs');
const async = require('async');
const mongoUrl = 'mongodb://127.0.0.1:27017';
const databaseName = 'based_fellas';
const collectionName = 'metadata';
const jsonDirectory = './meta';
mongoose.connect(`${mongoUrl}/${databaseName}`, { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;
const schema = new mongoose.Schema({
_id: Number,
description: String,
name: String,
image: String,
attributes: [{
trait_type: String,
value: String,
}],
});
const Model = mongoose.model('FellaSchema', schema, collectionName);
function insertJsonFile(jsonFilePath, callback) {
fs.readFile(jsonFilePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading ${jsonFilePath}: ${err}`);
return callback(err);
}
try {
const jsonData = JSON.parse(data);
const filenameWithoutExtension = jsonFilePath.split('/').pop().replace('.json', '');
jsonData._id = parseInt(filenameWithoutExtension);
const newDocument = new Model(jsonData);
newDocument.save((saveErr) => {
if (saveErr) {
console.error(`Error inserting data from ${jsonFilePath}: ${saveErr}`);
callback(saveErr);
} else {
console.log(`Inserted data from ${jsonFilePath}`);
callback();
}
});
} catch (parseErr) {
console.error(`Error parsing JSON from ${jsonFilePath}: ${parseErr}`);
callback(parseErr);
}
});
}
function indexAllJsonFiles() {
const jsonFiles = fs.readdirSync(jsonDirectory).filter((filename) => filename.endsWith('.json'));
async.eachLimit(
jsonFiles,
10,
(filename, callback) => {
const jsonFilePath = `${jsonDirectory}/${filename}`;
insertJsonFile(jsonFilePath, callback);
},
(err) => {
if (err) {
console.error('Error indexing JSON files:', err);
} else {
console.log('All JSON files indexed successfully.');
}
mongoose.disconnect();
}
);
}
db.on('error', (err) => {
console.error('MongoDB connection error:', err);
});
db.once('open', () => {
console.log('Connected to MongoDB successfully.');
db.db.createCollection(collectionName, (createErr) => {
if (createErr) {
console.error(`Error creating collection ${collectionName}: ${createErr}`);
mongoose.disconnect();
} else {
console.log(`Database and collection ${collectionName} created successfully.`);
indexAllJsonFiles();
}
});
});