1
+ /**
2
+ * Copyright 2018 Google Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import {
17
+ DbDocumentMutation , DbInstance ,
18
+ DbMutationBatch , DbMutationQueue , DbOwner , DbRemoteDocument , DbTarget ,
19
+ DbTargetDocument , DbTargetGlobal
20
+ } from './indexeddb_schema' ;
21
+ import { fail } from '../util/assert' ;
22
+
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
+ ) ;
35
+
36
+ const targetDocumentsStore = db . createObjectStore (
37
+ DbTargetDocument . store ,
38
+ // tslint:disable-next-line:no-any
39
+ { keyPath : DbTargetDocument . keyPath as any }
40
+ ) ;
41
+ targetDocumentsStore . createIndex (
42
+ DbTargetDocument . documentTargetsIndex ,
43
+ DbTargetDocument . documentTargetsKeyPath ,
44
+ { unique : true }
45
+ ) ;
46
+
47
+ const targetStore = db . createObjectStore ( DbTarget . store , {
48
+ keyPath : DbTarget . keyPath
49
+ } ) ;
50
+ // NOTE: This is unique only because the TargetId is the suffix.
51
+ targetStore . createIndex (
52
+ DbTarget . queryTargetsIndexName ,
53
+ DbTarget . queryTargetsKeyPath ,
54
+ { unique : true }
55
+ ) ;
56
+
57
+ const instanceStore = db . createObjectStore ( DbInstance . store , {
58
+ keyPath : DbInstance . keyPath as any
59
+ } ) ;
60
+
61
+ // NOTE: keys for these stores are specified explicitly rather than using a
62
+ // keyPath.
63
+ db . createObjectStore ( DbDocumentMutation . store ) ;
64
+ db . createObjectStore ( DbRemoteDocument . store ) ;
65
+ db . createObjectStore ( DbOwner . store ) ;
66
+ db . createObjectStore ( DbTargetGlobal . store ) ;
67
+ }
68
+
69
+ export function upgradeDbFromV1 ( db : IDBDatabase ) : void {
70
+
71
+ }
72
+
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
+ }
83
+
84
+ if ( oldVersion == 1 ) {
85
+ upgradeDbFromV1 ( db , oldVersion ) ;
86
+ return ;
87
+ }
88
+
89
+ fail ( 'Unexpected upgrade from version ' + oldVersion ) ;
90
+ }
91
+ }
0 commit comments