import Dexie , { DexieOptions } from 'dexie' ; import setGlobalVars from 'indexeddbshim' ; import { changesAddon } from '../middleware/changes' ; import '../middleware/mongo' ; import { schemaAddon } from '../middleware/schemas' ; import { ulidMiddleware } from '../middleware/ulid' ; let indexedDB , IDBKeyRange ; if ( setGlobalVars instanceof Function ) { const globals = setGlobalVars ( { } , { checkOrigin : false } ) ; indexedDB = globals . indexedDB ; IDBKeyRange = globals . IDBKeyRange ; } param databaseName export function NewDexie ( databaseName ) { const options = { addons : [ ... Dexie . addons , changesAddon , schemaAddon ] , } ; if ( indexedDB ) options . indexedDB = indexedDB ; if ( IDBKeyRange ) options . IDBKeyRange = IDBKeyRange ; const dexie = new Dexie ( databaseName , options ) ; dexie . use ( ulidMiddleware ) ; dexie . on ( 'versionchange' , ( ) => { dexie . close ( ) ; dexie . open ( ) . then ( ( ) => { console . log ( 'Database reloaded with updated version' ) ; } ) . catch ( ( err ) => { console . log ( 'Failed to reload database with updated version' ) ; throw err ; } ) ; return false ; } ) ; return dexie ; } export function nameFromKeyPath ( keyPath ? ) { return typeof keyPath === 'string' ? keyPath : keyPath ? '[' + [ ] . join . call ( keyPath , '+' ) + ']' : '' ; } export function createIndexString ( { path , unique , multi , auto , } , isPrimKey = false ) { return ( ( unique && ! isPrimKey ? '&' : '' ) + ( multi ? '*' : '' ) + ( auto ? '++' : '' ) + nameFromKeyPath ( path ) ) ; } param s link export const hashString = ( s ) : number => { let i , h ; for ( i = 0 , h = 9 ; i < s . length ; ) { h = Math . imul ( h ^ s . charCodeAt ( i ++ ) , 9 ** 9 ) ; } return ( h ^ ( h >>> 9 ) ) + 0x7fffffff + 1 ; } ;