1 | import LocalForage from 'localforage';
|
2 | /** @hidden */
|
3 | export const Drivers = {
|
4 | SecureStorage: 'ionicSecureStorage',
|
5 | IndexedDB: LocalForage.INDEXEDDB,
|
6 | LocalStorage: LocalForage.LOCALSTORAGE
|
7 | };
|
8 | const defaultConfig = {
|
9 | name: '_ionicstorage',
|
10 | storeName: '_ionickv',
|
11 | dbKey: '_ionickey',
|
12 | driverOrder: [
|
13 | Drivers.SecureStorage,
|
14 | Drivers.IndexedDB,
|
15 | Drivers.LocalStorage
|
16 | ]
|
17 | };
|
18 | export class Storage {
|
19 | /**
|
20 | * Create a new Storage instance using the order of drivers and any additional config
|
21 | * options to pass to LocalForage.
|
22 | *
|
23 | * Possible default driverOrder options are: ['indexeddb', 'localstorage'] and the
|
24 | * default is that exact ordering.
|
25 | *
|
26 | * When using Ionic Secure Storage (enterprise only), use ['ionicSecureStorage', 'indexeddb', 'localstorage'] to ensure
|
27 | * Secure Storage is used when available, or fall back to IndexedDB or LocalStorage on the web.
|
28 | */
|
29 | constructor(config = defaultConfig) {
|
30 | this._db = null;
|
31 | this._secureStorageDriver = null;
|
32 | const actualConfig = Object.assign({}, defaultConfig, config || {});
|
33 | this._config = actualConfig;
|
34 | }
|
35 | async create() {
|
36 | const db = LocalForage.createInstance(this._config);
|
37 | this._db = db;
|
38 | await db.setDriver(this._config.driverOrder || []);
|
39 | return this;
|
40 | }
|
41 | /**
|
42 | * Define a new Driver. Must be called before
|
43 | * initializing the database. Example:
|
44 | *
|
45 | * await storage.defineDriver(myDriver);
|
46 | * await storage.create();
|
47 | */
|
48 | async defineDriver(driver) {
|
49 | if (driver._driver === Drivers.SecureStorage) {
|
50 | this._secureStorageDriver = driver;
|
51 | }
|
52 | return LocalForage.defineDriver(driver);
|
53 | }
|
54 | /**
|
55 | * Get the name of the driver being used.
|
56 | * @returns Name of the driver
|
57 | */
|
58 | get driver() {
|
59 | var _a;
|
60 | return ((_a = this._db) === null || _a === void 0 ? void 0 : _a.driver()) || null;
|
61 | }
|
62 | assertDb() {
|
63 | if (!this._db) {
|
64 | throw new Error('Database not created. Must call create() first');
|
65 | }
|
66 | return this._db;
|
67 | }
|
68 | /**
|
69 | * Get the value associated with the given key.
|
70 | * @param key the key to identify this value
|
71 | * @returns Returns a promise with the value of the given key
|
72 | */
|
73 | get(key) {
|
74 | const db = this.assertDb();
|
75 | return db.getItem(key);
|
76 | }
|
77 | /**
|
78 | * Set the value for the given key.
|
79 | * @param key the key to identify this value
|
80 | * @param value the value for this key
|
81 | * @returns Returns a promise that resolves when the key and value are set
|
82 | */
|
83 | set(key, value) {
|
84 | const db = this.assertDb();
|
85 | return db.setItem(key, value);
|
86 | }
|
87 | /**
|
88 | * Remove any value associated with this key.
|
89 | * @param key the key to identify this value
|
90 | * @returns Returns a promise that resolves when the value is removed
|
91 | */
|
92 | remove(key) {
|
93 | const db = this.assertDb();
|
94 | return db.removeItem(key);
|
95 | }
|
96 | /**
|
97 | * Clear the entire key value store. WARNING: HOT!
|
98 | * @returns Returns a promise that resolves when the store is cleared
|
99 | */
|
100 | clear() {
|
101 | const db = this.assertDb();
|
102 | return db.clear();
|
103 | }
|
104 | /**
|
105 | * @returns Returns a promise that resolves with the number of keys stored.
|
106 | */
|
107 | length() {
|
108 | const db = this.assertDb();
|
109 | return db.length();
|
110 | }
|
111 | /**
|
112 | * @returns Returns a promise that resolves with the keys in the store.
|
113 | */
|
114 | keys() {
|
115 | const db = this.assertDb();
|
116 | return db.keys();
|
117 | }
|
118 | /**
|
119 | * Iterate through each key,value pair.
|
120 | * @param iteratorCallback a callback of the form (value, key, iterationNumber)
|
121 | * @returns Returns a promise that resolves when the iteration has finished.
|
122 | */
|
123 | forEach(iteratorCallback) {
|
124 | const db = this.assertDb();
|
125 | return db.iterate(iteratorCallback);
|
126 | }
|
127 | setEncryptionKey(key) {
|
128 | var _a;
|
129 | if (!this._secureStorageDriver) {
|
130 | throw new Error('@ionic-enterprise/secure-storage not installed. Encryption support not available');
|
131 | }
|
132 | else {
|
133 | (_a = this._secureStorageDriver) === null || _a === void 0 ? void 0 : _a.setEncryptionKey(key);
|
134 | }
|
135 | }
|
136 | }
|
137 | //# sourceMappingURL=index.js.map |
\ | No newline at end of file |