Skip to content

Commit ba07581

Browse files
authored
fix(storage): add NgModule to work with Angular 2.4.x
* wip * fix(angular update): migrate to NgModule instead of pure provider to avoid dependency injection issues * chore(dependencies): upgrade to angular 2.4.8 * 2.0.0-0
1 parent d78c2ff commit ba07581

File tree

3 files changed

+55
-23
lines changed

3 files changed

+55
-23
lines changed

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
22
"name": "@ionic/storage",
3-
"version": "1.1.9",
3+
"version": "2.0.0-0",
44
"description": "Ionic Storage utility",
55
"main": "es2015/index.js",
66
"module": "es2015/index.js",
77
"typings": "es2015/index.d.ts",
88
"scripts": {
99
"clean": "rm -rf dist",
10-
"build-cjs": "tsc -p .",
11-
"build-es2015": "tsc -p ./tsconfig-es2015.json",
10+
"clean-generated": "rm -rf src/*.ngfactory.ts && rm -rf src/*.ngsummary.json",
11+
"build-cjs": "ngc -p .",
12+
"build-es2015": "ngc -p ./tsconfig-es2015.json",
1213
"preparePackage": "node ./scripts/copy-package",
13-
"build": "npm run clean && npm run build-cjs && npm run build-es2015 && npm run preparePackage",
14+
"build": "npm run clean && npm run build-cjs && npm run build-es2015 && npm run preparePackage && npm run clean-generated",
1415
"publishPackage": "npm run build && cd dist && npm publish"
1516
},
1617
"repository": {
@@ -36,7 +37,9 @@
3637
"localforage-cordovasqlitedriver": "~1.5.0"
3738
},
3839
"devDependencies": {
39-
"@angular/core": "2.2.1",
40+
"@angular/core": "2.4.8",
41+
"@angular/compiler": "2.4.8",
42+
"@angular/compiler-cli": "2.4.8",
4043
"@types/node": "^6.0.41",
4144
"canonical-path": "0.0.2",
4245
"cpr": "^2.0.0",

src/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1-
import { Storage } from './storage';
1+
import { NgModule, ModuleWithProviders } from '@angular/core';
22

3-
export { Storage };
3+
import { getDefaultConfig, provideStorage, Storage, StorageConfig, StorageConfigToken } from './storage';
4+
5+
export { Storage, StorageConfig, StorageConfigToken };
6+
7+
@NgModule({
8+
})
9+
export class IonicStorageModule {
10+
static forRoot(storageConfig: StorageConfig = null): ModuleWithProviders {
11+
return {
12+
ngModule: IonicStorageModule,
13+
providers: [
14+
{ provide: StorageConfigToken, useValue: storageConfig },
15+
{ provide: Storage, useFactory: provideStorage, deps: [StorageConfigToken]},
16+
]
17+
}
18+
}
19+
}

src/storage.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from '@angular/core';
1+
import { Injectable, OpaqueToken, Optional } from '@angular/core';
22

33
import LocalForage from 'localforage';
44

@@ -79,12 +79,13 @@ import CordovaSQLiteDriver from 'localforage-cordovasqlitedriver';
7979
* The Storage engine can be configured both with specific storage engine priorities, or custom configuration
8080
* options to pass to localForage. See the localForage config docs for possible options: https://github.com/localForage/localForage#configuration
8181
*
82+
* Note: Any custom configurations will be merged with the default configuration
8283
*
8384
* ```typescript
8485
* import { Storage } from '@ionic/storage';
8586
*
8687
* export function provideStorage() {
87-
* return new Storage(['sqlite', 'websql', 'indexeddb'], { name: '__mydb' });
88+
* return new Storage({ name: '__mydb' });
8889
* }
8990
*
9091
* @NgModule({
@@ -111,26 +112,17 @@ export class Storage {
111112
* Possible driver options are: ['sqlite', 'indexeddb', 'websql', 'localstorage'] and the
112113
* default is that exact ordering.
113114
*/
114-
constructor(driverOrder: [string] = ['sqlite', 'indexeddb', 'websql', 'localstorage'], config?: any) {
115+
constructor(@Optional() config?: any) {
115116
this._dbPromise = new Promise((resolve, reject) => {
116117
let db: LocalForage;
117118

118-
let dbConfig = {
119-
name : '_ionicstorage',
120-
storeName : '_ionickv'
121-
};
122-
123-
// Merge any custom config options they have
124-
if(config) {
125-
for(let k in config) {
126-
dbConfig[k] = config[k];
127-
}
128-
}
119+
const defaultConfig = getDefaultConfig();
120+
const actualConfig = Object.assign(defaultConfig, config || {});
129121

130122
LocalForage.defineDriver(CordovaSQLiteDriver).then(() => {
131-
db = LocalForage.createInstance(dbConfig);
123+
db = LocalForage.createInstance(actualConfig);
132124
})
133-
.then(() => db.setDriver(this._getDriverOrder(driverOrder)))
125+
.then(() => db.setDriver(this._getDriverOrder(actualConfig.driverOrder)))
134126
.then(() => {
135127
this._driver = db.driver();
136128
resolve(db);
@@ -229,3 +221,24 @@ export class Storage {
229221
return this._dbPromise.then(db => db.iterate(iteratorCallback));
230222
}
231223
}
224+
225+
export function getDefaultConfig() {
226+
return {
227+
name : '_ionicstorage',
228+
storeName : '_ionickv',
229+
driverOrder: ['sqlite', 'indexeddb', 'websql', 'localstorage']
230+
};
231+
}
232+
233+
export interface StorageConfig {
234+
name?: string;
235+
storeName?: string;
236+
driverOrder?: string[];
237+
};
238+
239+
export function provideStorage(storageConfig?: StorageConfig) {
240+
const config = !!storageConfig ? storageConfig : getDefaultConfig();
241+
return new Storage(config);
242+
}
243+
244+
export const StorageConfigToken = new OpaqueToken('STORAGE_CONFIG_TOKEN');

0 commit comments

Comments
 (0)