gmongo 3.0.2 → 3.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/collectionCopy.js +37 -5
- package/package.json +2 -1
- package/services/connect.js +21 -7
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require('dotenv').config()
|
|
1
|
+
require('dotenv').config({ path: __dirname + '/.env' })
|
|
2
2
|
const MGO = require('../mongo.js')
|
|
3
3
|
|
|
4
4
|
function splitIntoBatches(array, batchSize) {
|
|
@@ -10,6 +10,12 @@ function splitIntoBatches(array, batchSize) {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
async function copyCollections() {
|
|
13
|
+
// Validate required environment variables
|
|
14
|
+
if (!process.env.COLLECTIONS_TO_COPY) {
|
|
15
|
+
console.error('Error: COLLECTIONS_TO_COPY environment variable is required')
|
|
16
|
+
process.exit(1)
|
|
17
|
+
}
|
|
18
|
+
|
|
13
19
|
const Source = {
|
|
14
20
|
ATLAS: process.env.FROM_ATLAS === 'true',
|
|
15
21
|
DB: process.env.FROM_DB,
|
|
@@ -31,6 +37,7 @@ async function copyCollections() {
|
|
|
31
37
|
const timeoutInMS = parseInt(process.env.CONNECTION_TIMEOUT_MS) || 10000
|
|
32
38
|
const insertBatchSize = parseInt(process.env.INSERT_BATCH_SIZE) || 1000
|
|
33
39
|
|
|
40
|
+
console.log('Connecting to source database...')
|
|
34
41
|
await MGO.start(
|
|
35
42
|
Source.ATLAS,
|
|
36
43
|
Source.DB,
|
|
@@ -41,6 +48,9 @@ async function copyCollections() {
|
|
|
41
48
|
Source.X509,
|
|
42
49
|
timeoutInMS,
|
|
43
50
|
)
|
|
51
|
+
console.log('✓ Connected to source database')
|
|
52
|
+
|
|
53
|
+
console.log('Connecting to destination database...')
|
|
44
54
|
await MGO.start(
|
|
45
55
|
Destination.ATLAS,
|
|
46
56
|
Destination.DB,
|
|
@@ -51,24 +61,46 @@ async function copyCollections() {
|
|
|
51
61
|
Destination.X509,
|
|
52
62
|
timeoutInMS,
|
|
53
63
|
)
|
|
64
|
+
console.log('✓ Connected to destination database')
|
|
54
65
|
|
|
55
66
|
const CollectionsToCopy = process.env.COLLECTIONS_TO_COPY.split(',').map(
|
|
56
67
|
(c) => c.trim(),
|
|
57
68
|
)
|
|
58
69
|
|
|
70
|
+
console.log('Starting collection copy process...')
|
|
71
|
+
console.log(`Collections to copy: ${CollectionsToCopy.join(', ')}`)
|
|
72
|
+
|
|
73
|
+
let totalDocuments = 0
|
|
74
|
+
|
|
59
75
|
try {
|
|
60
76
|
for (const collectionName of CollectionsToCopy) {
|
|
77
|
+
console.log(`\nCopying collection: ${collectionName}`)
|
|
61
78
|
const fromCollection = await MGO.query(Source.DB, collectionName, {})
|
|
79
|
+
console.log(` Found ${fromCollection.length} documents`)
|
|
80
|
+
|
|
81
|
+
if (fromCollection.length === 0) {
|
|
82
|
+
console.log(` Skipping empty collection`)
|
|
83
|
+
continue
|
|
84
|
+
}
|
|
85
|
+
|
|
62
86
|
const insertList = splitIntoBatches(fromCollection, insertBatchSize)
|
|
63
|
-
|
|
64
|
-
|
|
87
|
+
console.log(` Inserting in ${insertList.length} batches...`)
|
|
88
|
+
|
|
89
|
+
for (let i = 0; i < insertList.length; i++) {
|
|
90
|
+
await MGO.insert(Destination.DB, collectionName, insertList[i])
|
|
91
|
+
console.log(` Batch ${i + 1}/${insertList.length} completed`)
|
|
65
92
|
}
|
|
93
|
+
|
|
94
|
+
totalDocuments += fromCollection.length
|
|
66
95
|
console.log(
|
|
67
|
-
|
|
96
|
+
`✓ Copied collection: ${collectionName} (${fromCollection.length} documents)`,
|
|
68
97
|
)
|
|
69
98
|
}
|
|
99
|
+
console.log(`\n✓ All collections copied successfully! (Total: ${totalDocuments} documents)`)
|
|
100
|
+
process.exit(0)
|
|
70
101
|
} catch (error) {
|
|
71
|
-
console.error('Error copying collections:', error)
|
|
102
|
+
console.error('\n✗ Error copying collections:', error)
|
|
103
|
+
process.exit(1)
|
|
72
104
|
}
|
|
73
105
|
}
|
|
74
106
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gmongo",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"description": "MongoDB Connection Class",
|
|
5
5
|
"main": "mongo.js",
|
|
6
6
|
"scripts": {
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"homepage": "https://github.com/GnomeGroup/gmongo#readme",
|
|
19
19
|
"dependencies": {
|
|
20
|
+
"dotenv": "^17.3.1",
|
|
20
21
|
"eslint": "^8.57.0",
|
|
21
22
|
"mongodb": "^6.2.0",
|
|
22
23
|
"prettier": "^3.2.5",
|
package/services/connect.js
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
const mongo = require('mongodb').MongoClient
|
|
2
2
|
|
|
3
|
+
const DEFAULT_TIMEOUT_MS = 30000
|
|
4
|
+
const DEFAULT_PORT = 27017
|
|
5
|
+
|
|
3
6
|
module.exports = (isAtlas, dbName, ip, port, user, pass, x509, timeoutInMS) =>
|
|
4
7
|
new Promise(function (resolve, reject) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
const parsedTimeout = Number.parseInt(timeoutInMS, 10)
|
|
9
|
+
const parsedPort = Number.parseInt(port, 10)
|
|
10
|
+
const effectivePort = Number.isFinite(parsedPort)
|
|
11
|
+
? parsedPort
|
|
12
|
+
: DEFAULT_PORT
|
|
13
|
+
const authSource = process.env.MONGO_AUTH_SOURCE || dbName
|
|
14
|
+
|
|
15
|
+
const connectOptions = {
|
|
16
|
+
serverSelectionTimeoutMS: Number.isFinite(parsedTimeout)
|
|
17
|
+
? parsedTimeout
|
|
18
|
+
: DEFAULT_TIMEOUT_MS,
|
|
9
19
|
}
|
|
20
|
+
|
|
10
21
|
try {
|
|
11
22
|
const url =
|
|
12
23
|
'mongodb' +
|
|
@@ -17,10 +28,11 @@ module.exports = (isAtlas, dbName, ip, port, user, pass, x509, timeoutInMS) =>
|
|
|
17
28
|
(pass ? encodeURIComponent(pass) : '') +
|
|
18
29
|
(user || pass ? '@' : '') +
|
|
19
30
|
encodeURIComponent(ip) +
|
|
20
|
-
(isAtlas ? '' : ':' +
|
|
31
|
+
(isAtlas ? '' : ':' + effectivePort.toString()) +
|
|
21
32
|
'/' +
|
|
22
33
|
encodeURIComponent(dbName) +
|
|
23
34
|
'?retryWrites=true&w=majority' +
|
|
35
|
+
`&authSource=${encodeURIComponent(authSource)}` +
|
|
24
36
|
(x509
|
|
25
37
|
? '&authMechanism=MONGODB-X509&tls=true&tlsCertificateKeyFile=' +
|
|
26
38
|
encodeURIComponent(x509)
|
|
@@ -32,6 +44,8 @@ module.exports = (isAtlas, dbName, ip, port, user, pass, x509, timeoutInMS) =>
|
|
|
32
44
|
resolve(dbObject)
|
|
33
45
|
return
|
|
34
46
|
}
|
|
35
|
-
|
|
36
|
-
|
|
47
|
+
reject(new Error(`Failed to initialize Mongo client for ${dbName}`))
|
|
48
|
+
} catch (err) {
|
|
49
|
+
reject(err)
|
|
50
|
+
}
|
|
37
51
|
})
|