|
|
@ -3,7 +3,7 @@ class app.DB
|
|
|
|
|
|
|
|
|
|
|
|
constructor: ->
|
|
|
|
constructor: ->
|
|
|
|
@useIndexedDB = @useIndexedDB()
|
|
|
|
@useIndexedDB = @useIndexedDB()
|
|
|
|
@indexedDBVersion = @indexedDBVersion()
|
|
|
|
@appVersion = @appVersion()
|
|
|
|
@callbacks = []
|
|
|
|
@callbacks = []
|
|
|
|
|
|
|
|
|
|
|
|
db: (fn) ->
|
|
|
|
db: (fn) ->
|
|
|
@ -13,7 +13,7 @@ class app.DB
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
try
|
|
|
|
@open = true
|
|
|
|
@open = true
|
|
|
|
req = indexedDB.open(NAME, @indexedDBVersion)
|
|
|
|
req = indexedDB.open(NAME, @schemaVersion())
|
|
|
|
req.onsuccess = @onOpenSuccess
|
|
|
|
req.onsuccess = @onOpenSuccess
|
|
|
|
req.onerror = @onOpenError
|
|
|
|
req.onerror = @onOpenError
|
|
|
|
req.onupgradeneeded = @onUpgradeNeeded
|
|
|
|
req.onupgradeneeded = @onUpgradeNeeded
|
|
|
@ -59,14 +59,16 @@ class app.DB
|
|
|
|
onUpgradeNeeded: (event) ->
|
|
|
|
onUpgradeNeeded: (event) ->
|
|
|
|
return unless db = event.target.result
|
|
|
|
return unless db = event.target.result
|
|
|
|
|
|
|
|
|
|
|
|
unless db.objectStoreNames.contains('docs')
|
|
|
|
objectStoreNames = $.makeArray(db.objectStoreNames)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unless $.arrayDelete(objectStoreNames, 'docs')
|
|
|
|
db.createObjectStore('docs')
|
|
|
|
db.createObjectStore('docs')
|
|
|
|
|
|
|
|
|
|
|
|
for doc in app.docs.all() when not db.objectStoreNames.contains(doc.slug)
|
|
|
|
for doc in app.docs.all() when not $.arrayDelete(objectStoreNames, doc.slug)
|
|
|
|
db.createObjectStore(doc.slug)
|
|
|
|
db.createObjectStore(doc.slug)
|
|
|
|
|
|
|
|
|
|
|
|
for doc in app.disabledDocs.all() when not db.objectStoreNames.contains(doc.slug)
|
|
|
|
for name in objectStoreNames
|
|
|
|
db.createObjectStore(doc.slug)
|
|
|
|
db.deleteObjectStore(name)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
store: (doc, data, onSuccess, onError) ->
|
|
|
|
store: (doc, data, onSuccess, onError) ->
|
|
|
@ -201,6 +203,11 @@ class app.DB
|
|
|
|
onError()
|
|
|
|
onError()
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unless db.objectStoreNames.contains(entry.doc.slug)
|
|
|
|
|
|
|
|
onError()
|
|
|
|
|
|
|
|
@loadDocsCache(db)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
txn = @idbTransaction db, stores: [entry.doc.slug], mode: 'readonly'
|
|
|
|
txn = @idbTransaction db, stores: [entry.doc.slug], mode: 'readonly'
|
|
|
|
store = txn.objectStore(entry.doc.slug)
|
|
|
|
store = txn.objectStore(entry.doc.slug)
|
|
|
|
|
|
|
|
|
|
|
@ -212,10 +219,11 @@ class app.DB
|
|
|
|
event.preventDefault()
|
|
|
|
event.preventDefault()
|
|
|
|
onError()
|
|
|
|
onError()
|
|
|
|
return
|
|
|
|
return
|
|
|
|
@loadDocsCache(db) unless @cachedDocs
|
|
|
|
@loadDocsCache(db)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
loadDocsCache: (db) ->
|
|
|
|
loadDocsCache: (db) ->
|
|
|
|
|
|
|
|
return if @cachedDocs
|
|
|
|
@cachedDocs = {}
|
|
|
|
@cachedDocs = {}
|
|
|
|
|
|
|
|
|
|
|
|
txn = @idbTransaction db, stores: ['docs'], mode: 'readonly'
|
|
|
|
txn = @idbTransaction db, stores: ['docs'], mode: 'readonly'
|
|
|
@ -261,5 +269,15 @@ class app.DB
|
|
|
|
catch
|
|
|
|
catch
|
|
|
|
false
|
|
|
|
false
|
|
|
|
|
|
|
|
|
|
|
|
indexedDBVersion: ->
|
|
|
|
migrate: ->
|
|
|
|
if app.config.env is 'production' then parseInt(app.config.version, 10) else Date.now() / 1000
|
|
|
|
app.settings.set('schema', @userVersion() + 1)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
schemaVersion: ->
|
|
|
|
|
|
|
|
@appVersion * 10 + @userVersion()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
userVersion: ->
|
|
|
|
|
|
|
|
app.settings.get('schema')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
appVersion: ->
|
|
|
|
|
|
|
|
if app.config.env is 'production' then parseInt(app.config.version, 10) else Math.floor(Date.now() / 1000)
|
|
|
|