Skip to content
Permalink
Browse files

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
  • Loading branch information...
jgw96 committed Mar 3, 2017
1 parent d78c2ff commit ba07581e82d20be4687e0930c349116655f8728b
Showing with 55 additions and 23 deletions.
  1. +8 −5 package.json
  2. +18 −2 src/index.ts
  3. +29 −16 src/storage.ts
@@ -1,16 +1,17 @@
{
"name": "@ionic/storage",
"version": "1.1.9",
"version": "2.0.0-0",
"description": "Ionic Storage utility",
"main": "es2015/index.js",
"module": "es2015/index.js",
"typings": "es2015/index.d.ts",
"scripts": {
"clean": "rm -rf dist",
"build-cjs": "tsc -p .",
"build-es2015": "tsc -p ./tsconfig-es2015.json",
"clean-generated": "rm -rf src/*.ngfactory.ts && rm -rf src/*.ngsummary.json",
"build-cjs": "ngc -p .",
"build-es2015": "ngc -p ./tsconfig-es2015.json",
"preparePackage": "node ./scripts/copy-package",
"build": "npm run clean && npm run build-cjs && npm run build-es2015 && npm run preparePackage",
"build": "npm run clean && npm run build-cjs && npm run build-es2015 && npm run preparePackage && npm run clean-generated",
"publishPackage": "npm run build && cd dist && npm publish"
},
"repository": {
@@ -36,7 +37,9 @@
"localforage-cordovasqlitedriver": "~1.5.0"
},
"devDependencies": {
"@angular/core": "2.2.1",
"@angular/core": "2.4.8",
"@angular/compiler": "2.4.8",
"@angular/compiler-cli": "2.4.8",
"@types/node": "^6.0.41",
"canonical-path": "0.0.2",
"cpr": "^2.0.0",
@@ -1,3 +1,19 @@
import { Storage } from './storage';
import { NgModule, ModuleWithProviders } from '@angular/core';

export { Storage };
import { getDefaultConfig, provideStorage, Storage, StorageConfig, StorageConfigToken } from './storage';

export { Storage, StorageConfig, StorageConfigToken };

@NgModule({
})
export class IonicStorageModule {
static forRoot(storageConfig: StorageConfig = null): ModuleWithProviders {
return {
ngModule: IonicStorageModule,
providers: [
{ provide: StorageConfigToken, useValue: storageConfig },
{ provide: Storage, useFactory: provideStorage, deps: [StorageConfigToken]},
]
}
}
}
@@ -1,4 +1,4 @@
import { Injectable } from '@angular/core';
import { Injectable, OpaqueToken, Optional } from '@angular/core';

import LocalForage from 'localforage';

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

let dbConfig = {
name : '_ionicstorage',
storeName : '_ionickv'
};

// Merge any custom config options they have
if(config) {
for(let k in config) {
dbConfig[k] = config[k];
}
}
const defaultConfig = getDefaultConfig();
const actualConfig = Object.assign(defaultConfig, config || {});

LocalForage.defineDriver(CordovaSQLiteDriver).then(() => {
db = LocalForage.createInstance(dbConfig);
db = LocalForage.createInstance(actualConfig);
})
.then(() => db.setDriver(this._getDriverOrder(driverOrder)))
.then(() => db.setDriver(this._getDriverOrder(actualConfig.driverOrder)))
.then(() => {
this._driver = db.driver();
resolve(db);
@@ -229,3 +221,24 @@ export class Storage {
return this._dbPromise.then(db => db.iterate(iteratorCallback));
}
}

export function getDefaultConfig() {
return {
name : '_ionicstorage',
storeName : '_ionickv',
driverOrder: ['sqlite', 'indexeddb', 'websql', 'localstorage']
};
}

export interface StorageConfig {
name?: string;
storeName?: string;
driverOrder?: string[];
};

export function provideStorage(storageConfig?: StorageConfig) {
const config = !!storageConfig ? storageConfig : getDefaultConfig();
return new Storage(config);
}

export const StorageConfigToken = new OpaqueToken('STORAGE_CONFIG_TOKEN');

0 comments on commit ba07581

Please sign in to comment.
You can’t perform that action at this time.