-
Notifications
You must be signed in to change notification settings - Fork 20
/
create-indexes.js
30 lines (26 loc) · 983 Bytes
/
create-indexes.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
/* Import dependencies */
const async = require('async');
const db = require('./utils/db.js');
/* Load the indexes config */
const indexes = require('./config/indexes.json');
/* Attempt to get a database connection */
db( db => {
/* Loop through the list of required indexes and create them in the database */
async.each( indexes, ( item, callback ) => {
createDatabaseIndex( db, item, callback );
}, () => {
/* We have completed creating the indexes, close the database connection */
console.log( 'Completed all' );
db.close();
});
});
/* This function creates a database index with the specified information */
createDatabaseIndex = ( db, item, callback ) => {
const { collection, fields, name } = item;
console.log( `Creating index - ${name}` );
/* Create the index if it doesn't already exist */
db.collection(collection).ensureIndex( fields, { background: true }, () => {
console.log( `Created index - ${name}` );
callback();
});
}