15
15
*/
16
16
import {
17
17
DbDocumentMutation , DbInstance ,
18
- DbMutationBatch , DbMutationQueue , DbOwner , DbRemoteDocument , DbTarget ,
18
+ DbMutationBatch , DbMutationQueue , DbOwner ,
19
+ DbRemoteDocument , DbTarget ,
19
20
DbTargetDocument , DbTargetGlobal
20
21
} from './indexeddb_schema' ;
21
- import { fail } from '../util/assert' ;
22
+ import { assert , fail } from '../util/assert' ;
22
23
23
- export function createDb ( db : IDBDatabase ) : void {
24
- db . createObjectStore ( DbMutationQueue . store , {
25
- keyPath : DbMutationQueue . keyPath
26
- } ) ;
27
-
28
- // TODO(mikelehen): Get rid of "as any" if/when TypeScript fixes their
29
- // types. https://github.com/Microsoft/TypeScript/issues/14322
30
- db . createObjectStore (
31
- DbMutationBatch . store ,
32
- // tslint:disable-next-line:no-any
33
- { keyPath : DbMutationBatch . keyPath as any }
34
- ) ;
24
+ // TODO(mikelehen): Get rid of "as any" if/when TypeScript fixes their types.
25
+ // https://github.com/Microsoft/TypeScript/issues/14322
26
+ type KeyPath = any ; // tslint:disable-line:no-any
35
27
28
+ function createCache ( db : IDBDatabase ) : void {
36
29
const targetDocumentsStore = db . createObjectStore (
37
30
DbTargetDocument . store ,
38
- // tslint:disable-next-line:no-any
39
- { keyPath : DbTargetDocument . keyPath as any }
31
+ { keyPath : DbTargetDocument . keyPath as KeyPath }
40
32
) ;
41
33
targetDocumentsStore . createIndex (
42
34
DbTargetDocument . documentTargetsIndex ,
@@ -47,45 +39,72 @@ export function createDb(db: IDBDatabase): void {
47
39
const targetStore = db . createObjectStore ( DbTarget . store , {
48
40
keyPath : DbTarget . keyPath
49
41
} ) ;
42
+
50
43
// NOTE: This is unique only because the TargetId is the suffix.
51
44
targetStore . createIndex (
52
45
DbTarget . queryTargetsIndexName ,
53
46
DbTarget . queryTargetsKeyPath ,
54
47
{ unique : true }
55
48
) ;
49
+ db . createObjectStore ( DbRemoteDocument . store ) ;
50
+ db . createObjectStore ( DbTargetGlobal . store ) ;
51
+ }
52
+
53
+ function dropCache ( db : IDBDatabase ) : void {
54
+ db . deleteObjectStore ( DbTargetDocument . store ) ;
55
+ db . deleteObjectStore ( DbTarget . keyPath ) ;
56
+ db . deleteObjectStore ( DbRemoteDocument . store ) ;
57
+ db . deleteObjectStore ( DbTargetGlobal . store ) ;
58
+ }
56
59
57
- const instanceStore = db . createObjectStore ( DbInstance . store , {
58
- keyPath : DbInstance . keyPath as any
60
+ function createOwnerStore ( db : IDBDatabase ) : void {
61
+ db . createObjectStore ( DbOwner . store ) ;
62
+ }
63
+
64
+ function createInstanceStore ( db : IDBDatabase ) : void {
65
+ db . createObjectStore ( DbInstance . store , {
66
+ keyPath : DbInstance . keyPath as KeyPath
59
67
} ) ;
68
+ }
69
+
70
+ function createMutationQueue ( db : IDBDatabase ) : void {
71
+ db . createObjectStore ( DbMutationQueue . store , {
72
+ keyPath : DbMutationQueue . keyPath
73
+ } ) ;
74
+
75
+ db . createObjectStore (
76
+ DbMutationBatch . store ,
77
+ { keyPath : DbMutationBatch . keyPath as KeyPath }
78
+ ) ;
60
79
61
80
// NOTE: keys for these stores are specified explicitly rather than using a
62
81
// keyPath.
63
82
db . createObjectStore ( DbDocumentMutation . store ) ;
64
- db . createObjectStore ( DbRemoteDocument . store ) ;
65
- db . createObjectStore ( DbOwner . store ) ;
66
- db . createObjectStore ( DbTargetGlobal . store ) ;
67
83
}
68
84
69
- export function upgradeDbFromV1 ( db : IDBDatabase ) : void {
85
+ /**
86
+ * Runs any migrations needed to bring the given database up to the current
87
+ * schema version.
88
+ */
89
+ export function createOrUpgradeDb ( db : IDBDatabase , oldVersion : number , newVersion : number ) : void {
90
+ assert ( oldVersion >= 0 || oldVersion <= 1 , 'Unexpected upgrade from version ' + oldVersion ) ;
91
+ assert ( newVersion >= 1 || newVersion <= 2 , 'Unexpected upgrade to version ' + newVersion ) ;
70
92
71
- }
93
+ const createV1 = newVersion >= 1 && oldVersion <= 1 ;
94
+ const dropV1 = oldVersion >= 1 ;
95
+ const createV2 = newVersion >= 2 && oldVersion <= 2 ;
72
96
73
- export class IndexedDbMigrations {
74
- /**
75
- * Runs any migrations needed to bring the given database up to the current
76
- * schema version.
77
- */
78
- static runMigrations ( db : IDBDatabase , oldVersion : number ) {
79
- if ( oldVersion == 0 ) {
80
- createDb ( db , oldVersion ) ;
81
- return ;
82
- }
97
+ if ( dropV1 ) {
98
+ dropCache ( db ) ;
99
+ }
83
100
84
- if ( oldVersion == 1 ) {
85
- upgradeDbFromV1 ( db , oldVersion ) ;
86
- return ;
87
- }
101
+ if ( createV1 ) {
102
+ createOwnerStore ( db ) ;
103
+ createMutationQueue ( db ) ;
104
+ createCache ( db ) ;
105
+ }
88
106
89
- fail ( 'Unexpected upgrade from version ' + oldVersion ) ;
107
+ if ( createV2 ) {
108
+ createInstanceStore ( db ) ;
90
109
}
91
110
}
0 commit comments