@ -125,12 +125,17 @@ class Datastore extends EventEmitter {
* @ param { Function } callback Optional callback , signature : err
* @ param { Function } callback Optional callback , signature : err
* /
* /
ensureIndex ( options = { } , callback = ( ) => { } ) {
ensureIndex ( options = { } , callback = ( ) => { } ) {
callbackify ( this . ensureIndexAsync . bind ( this ) ) ( options , callback )
}
async ensureIndexAsync ( options = { } ) {
console . log ( 'exec now' )
if ( ! options . fieldName ) {
if ( ! options . fieldName ) {
const err = new Error ( 'Cannot create an index without a fieldName' )
const err = new Error ( 'Cannot create an index without a fieldName' )
err . missingFieldName = true
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 )
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
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 ( ) )
this . indexes [ options . fieldName ] . insert ( this . getAllData ( ) )
} catch ( e ) {
} catch ( e ) {
delete this . indexes [ options . fieldName ]
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
// 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 => {
await this . persistence . persistNewStateAsync ( [ { $$indexCreated : options } ] )
if ( err ) return callback ( err )
return callback ( null )
} )
}
}
/ * *
/ * *