@ -125,12 +125,17 @@ class Datastore extends EventEmitter {
* @ param { Function } callback Optional callback , signature : err
* /
ensureIndex ( options = { } , callback = ( ) => { } ) {
callbackify ( this . ensureIndexAsync . bind ( this ) ) ( options , callback )
}
async ensureIndexAsync ( options = { } ) {
console . log ( 'exec now' )
if ( ! options . fieldName ) {
const err = new Error ( 'Cannot create an index without a fieldName' )
err . missingFieldName = true
return callback ( err )
throw err
}
if ( this . indexes [ options . fieldName ] ) return callback ( null )
if ( this . indexes [ options . fieldName ] ) return
this . indexes [ options . fieldName ] = new Index ( options )
if ( options . expireAfterSeconds !== undefined ) this . ttlIndexes [ options . fieldName ] = options . expireAfterSeconds // With this implementation index creation is not necessary to ensure TTL but we stick with MongoDB's API here
@ -139,14 +144,11 @@ class Datastore extends EventEmitter {
this . indexes [ options . fieldName ] . insert ( this . getAllData ( ) )
} catch ( e ) {
delete this . indexes [ options . fieldName ]
return callback ( e )
throw e
}
// We may want to force all options to be persisted including defaults, not just the ones passed the index creation function
this . persistence . persistNewState ( [ { $$indexCreated : options } ] , err => {
if ( err ) return callback ( err )
return callback ( null )
} )
await this . persistence . persistNewStateAsync ( [ { $$indexCreated : options } ] )
}
/ * *