Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
"localforage-cordovasqlitedriver": "1.7.0"
},
"devDependencies": {
"@angular/compiler": "9",
"@angular/compiler-cli": "9",
"@angular/core": "9",
"@types/node": "12",
"@angular/common": "^9.1.12",
"@angular/compiler": "^9.1.12",
"@angular/compiler-cli": "^9.1.12",
"@angular/core": "^9.1.12",
"@types/node": "^12.12.54",
"canonical-path": "0.0.2",
"conventional-changelog-cli": "^2.0.1",
"cpr": "^2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { NgModule, ModuleWithProviders, PLATFORM_ID } from '@angular/core';
import {
getDefaultConfig,
provideStorage,
Expand All @@ -19,7 +19,7 @@ export class IonicStorageModule {
{
provide: Storage,
useFactory: provideStorage,
deps: [StorageConfigToken]
deps: [StorageConfigToken, PLATFORM_ID ]
}
]
};
Expand Down
43 changes: 25 additions & 18 deletions src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { InjectionToken } from '@angular/core';
import { InjectionToken, Inject, PLATFORM_ID } from '@angular/core';
import { isPlatformServer } from '@angular/common';

import * as LocalForage from 'localforage';

Expand Down Expand Up @@ -109,9 +110,12 @@ export class Storage {
* Possible driver options are: ['sqlite', 'indexeddb', 'websql', 'localstorage'] and the
* default is that exact ordering.
*/
constructor(config: StorageConfig) {
constructor(
config: StorageConfig,
@Inject(PLATFORM_ID) private platformId: Object
) {
this._dbPromise = new Promise((resolve, reject) => {
if (typeof process !== 'undefined') {
if (isPlatformServer(this.platformId)) {
const noopDriver = getNoopDriver();
resolve(noopDriver);
return;
Expand All @@ -133,7 +137,7 @@ export class Storage {
this._driver = db.driver();
resolve(db);
})
.catch(reason => reject(reason));
.catch((reason) => reject(reason));
});
}

Expand All @@ -154,8 +158,8 @@ export class Storage {
}

/** @hidden */
private _getDriverOrder(driverOrder) {
return driverOrder.map(driver => {
private _getDriverOrder(driverOrder: string[]) {
return driverOrder.map((driver: string) => {
switch (driver) {
case 'sqlite':
return CordovaSQLiteDriver._driver;
Expand All @@ -175,7 +179,7 @@ export class Storage {
* @returns Returns a promise with the value of the given key
*/
get(key: string): Promise<any> {
return this._dbPromise.then(db => db.getItem(key));
return this._dbPromise.then((db) => db.getItem(key));
}

/**
Expand All @@ -185,7 +189,7 @@ export class Storage {
* @returns Returns a promise that resolves when the key and value are set
*/
set(key: string, value: any): Promise<any> {
return this._dbPromise.then(db => db.setItem(key, value));
return this._dbPromise.then((db) => db.setItem(key, value));
}

/**
Expand All @@ -194,29 +198,29 @@ export class Storage {
* @returns Returns a promise that resolves when the value is removed
*/
remove(key: string): Promise<any> {
return this._dbPromise.then(db => db.removeItem(key));
return this._dbPromise.then((db) => db.removeItem(key));
}

/**
* Clear the entire key value store. WARNING: HOT!
* @returns Returns a promise that resolves when the store is cleared
*/
clear(): Promise<void> {
return this._dbPromise.then(db => db.clear());
return this._dbPromise.then((db) => db.clear());
}

/**
* @returns Returns a promise that resolves with the number of keys stored.
*/
length(): Promise<number> {
return this._dbPromise.then(db => db.length());
return this._dbPromise.then((db) => db.length());
}

/**
* @returns Returns a promise that resolves with the keys in the store.
*/
keys(): Promise<string[]> {
return this._dbPromise.then(db => db.keys());
return this._dbPromise.then((db) => db.keys());
}

/**
Expand All @@ -227,7 +231,7 @@ export class Storage {
forEach(
iteratorCallback: (value: any, key: string, iterationNumber: Number) => any
): Promise<void> {
return this._dbPromise.then(db => db.iterate(iteratorCallback));
return this._dbPromise.then((db) => db.iterate(iteratorCallback));
}
}

Expand All @@ -237,7 +241,7 @@ export function getDefaultConfig() {
name: '_ionicstorage',
storeName: '_ionickv',
dbKey: '_ionickey',
driverOrder: ['sqlite', 'indexeddb', 'websql', 'localstorage']
driverOrder: ['sqlite', 'indexeddb', 'websql', 'localstorage'],
};
}

Expand All @@ -258,9 +262,12 @@ export const StorageConfigToken = new InjectionToken<any>(
);

/** @hidden */
export function provideStorage(storageConfig: StorageConfig): Storage {
export function provideStorage(
storageConfig: StorageConfig,
platformID: Object
): Storage {
const config = !!storageConfig ? storageConfig : getDefaultConfig();
return new Storage(config);
return new Storage(config, platformID);
}

function getNoopDriver() {
Expand All @@ -273,7 +280,7 @@ function getNoopDriver() {
clear: noop,
length: () => 0,
keys: () => [],
iterate: noop
iterate: noop,
};
return driver;
}
}