Last active
May 7, 2019 20:59
-
-
Save ArmandoAssuncao/cc171fc2dfda5da9a9699ecbb3468c36 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
process.env.NODE_ENV = process.env.NODE_ENV || 'development' | |
require('dotenv-flow').config() | |
const url = process.env.MONGO_URL || 'mongodb://localhost:27017/DATABASE_NAME' | |
const MongoClient = require('mongodb').MongoClient | |
const index = (key, options = null) => ({ key: key, options: options }) | |
const indexesCollections = [ | |
{ | |
collectionName: '_Session', | |
indexes: [ | |
index({ _session_token: 1 }, { unique: true }), | |
index({ _p_user: 1 }), | |
], | |
}, | |
{ | |
collectionName: '_User', | |
indexes: [ | |
// username and email not necessary, parse-server already does it | |
], | |
}, | |
// Put others indexes | |
] | |
// collections _Join | |
const indexesCollJoins = [ | |
index({ owningId: 1 }), | |
index({ relatedId: 1 }), | |
] | |
const createIndexes = async (db, collName, indexes) => { | |
const arrayIndexes = indexes.map((objIndex) => ( | |
{ | |
key: objIndex.key, | |
...objIndex.options, | |
} | |
)) | |
if (arrayIndexes.length === 0) return | |
return db.collection(collName).createIndexes(arrayIndexes) | |
} | |
MongoClient.connect(url, { useNewUrlParser: true }) | |
.then(async function(client) { | |
const db = client.db() | |
console.log('Creating index to database: ' + db.databaseName) | |
// collections _Join | |
const collections = await db.listCollections().toArray() | |
for (const coll of collections) { | |
if (coll.name.startsWith('_Join')) { | |
await createIndexes(db, coll.name, indexesCollJoins) | |
} | |
} | |
for (const indexesColl of indexesCollections) { | |
await createIndexes(db, indexesColl.collectionName, indexesColl.indexes) | |
} | |
client.close() | |
process.exit(0) | |
}).catch(function(e) { | |
console.error(e.name, e.message) | |
process.exit(1) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment