mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
chore(storage): Remove storage. Code moving to @ionic/storage
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
import { Component, NgModule } from '@angular/core';
|
||||
import { IonicApp, IonicModule, Storage, LocalStorage, SqlStorage } from '../../../..';
|
||||
|
||||
|
||||
@Component({
|
||||
templateUrl: 'main.html'
|
||||
})
|
||||
export class E2EApp {
|
||||
local: Storage;
|
||||
sql: Storage;
|
||||
|
||||
constructor() {
|
||||
this.local = new Storage(LocalStorage);
|
||||
this.sql = new Storage(SqlStorage);
|
||||
}
|
||||
|
||||
getLocal() {
|
||||
this.local.get('name').then(value => {
|
||||
alert('Your name is: ' + value);
|
||||
});
|
||||
}
|
||||
|
||||
setLocal() {
|
||||
let name = prompt('Your name?');
|
||||
|
||||
this.local.set('name', name);
|
||||
}
|
||||
|
||||
removeLocal() {
|
||||
this.local.remove('name');
|
||||
}
|
||||
|
||||
getSql() {
|
||||
this.sql.get('name').then(value => {
|
||||
alert('Your name is: ' + value);
|
||||
}, (errResult) => {
|
||||
console.error('Unable to get item from SQL db:', errResult);
|
||||
});
|
||||
}
|
||||
|
||||
setSql() {
|
||||
let name = prompt('Your name?');
|
||||
|
||||
this.sql.set('name', name);
|
||||
}
|
||||
|
||||
removeSql() {
|
||||
this.sql.remove('name');
|
||||
}
|
||||
}
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
E2EApp
|
||||
],
|
||||
imports: [
|
||||
IonicModule.forRoot(E2EApp)
|
||||
],
|
||||
bootstrap: [IonicApp]
|
||||
})
|
||||
export class AppModule {}
|
||||
@@ -1,11 +0,0 @@
|
||||
<ion-content padding>
|
||||
<h2>Local Storage</h2>
|
||||
<button ion-button primary (click)="getLocal()">Get</button>
|
||||
<button ion-button primary (click)="setLocal()">Set</button>
|
||||
<button ion-button primary (click)="removeLocal()">Remove</button>
|
||||
|
||||
<h2>SQL Storage</h2>
|
||||
<button ion-button primary (click)="getSql()">Get</button>
|
||||
<button ion-button primary (click)="setSql()">Set</button>
|
||||
<button ion-button primary (click)="removeSql()">Remove</button>
|
||||
</ion-content>
|
||||
@@ -8,10 +8,6 @@ export * from './gestures/slide-edge-gesture';
|
||||
export * from './gestures/slide-gesture';
|
||||
export * from './gestures/gesture-controller';
|
||||
|
||||
export * from './storage/storage';
|
||||
export * from './storage/sql';
|
||||
export * from './storage/local-storage';
|
||||
|
||||
export * from './util/click-block';
|
||||
export * from './util/events';
|
||||
export * from './util/keyboard';
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
import { StorageEngine } from './storage';
|
||||
|
||||
|
||||
/**
|
||||
* @name LocalStorage
|
||||
* @description
|
||||
* The LocalStorage storage engine uses the browser's local storage system for
|
||||
* storing key/value pairs.
|
||||
*
|
||||
* Note: LocalStorage should ONLY be used for temporary data that you can afford to lose.
|
||||
* Given disk space constraints on a mobile device, local storage might be "cleaned up"
|
||||
* by the operating system (iOS).
|
||||
*
|
||||
* For guaranteed, long-term storage, use the SqlStorage engine which stores data in a file.
|
||||
*
|
||||
* @usage
|
||||
* ```ts
|
||||
* import { Component } from '@angular/core';
|
||||
* import { Storage, LocalStorage } from 'ionic-angular';
|
||||
* @Component({
|
||||
* template: `<ion-content></ion-content>`
|
||||
* });
|
||||
* export class MyClass{
|
||||
* constructor(){
|
||||
* this.local = new Storage(LocalStorage);
|
||||
* this.local.set('didTutorial', 'true');
|
||||
* }
|
||||
*}
|
||||
*```
|
||||
* @demo /docs/v2/demos/src/local-storage/
|
||||
* @see {@link /docs/v2/platform/storage/ Storage Platform Docs}
|
||||
*/
|
||||
export class LocalStorage extends StorageEngine {
|
||||
constructor(options = {}) {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a key in LocalStorage
|
||||
* @param {string} key the key you want to lookup in LocalStorage
|
||||
* @returns {Promise} Returns a promise which is resolved when the value has been retrieved
|
||||
*/
|
||||
get(key: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
let value = window.localStorage.getItem(key);
|
||||
resolve(value);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a key value pair and save it to LocalStorage
|
||||
* @param {string} key the key you want to save to LocalStorage
|
||||
* @param {string} value the value of the key you're saving
|
||||
* @returns {Promise} Returns a promise which is resolved when the key value pair have been set
|
||||
*/
|
||||
set(key: string, value: string): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
window.localStorage.setItem(key, value);
|
||||
resolve();
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a key from LocalStorage
|
||||
* @param {string} key the key you want to remove from LocalStorage
|
||||
* @returns {Promise} Returns a promise which is resolved when the key has been removed
|
||||
*/
|
||||
remove(key: string): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
window.localStorage.removeItem(key);
|
||||
resolve();
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear data stored in LocalStorage
|
||||
* @returns {Promise} Returns a promise which is resolved when the data have been cleared
|
||||
*/
|
||||
clear(): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
window.localStorage.clear();
|
||||
resolve();
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
import { StorageEngine } from './storage';
|
||||
|
||||
import { defaults, assign } from '../util/util';
|
||||
|
||||
const DB_NAME: string = '__ionicstorage';
|
||||
const win: any = window;
|
||||
|
||||
/**
|
||||
* SqlStorage is a wrapper that uses SQLite when running natively (if available)
|
||||
* to store data in a persistent SQL store on the filesystem
|
||||
* or uses WebSQL when serving the app to the browser.
|
||||
*
|
||||
* This is the preferred storage engine, as data will be stored in appropriate
|
||||
* app storage, unlike Local Storage which is treated differently by the OS.
|
||||
*
|
||||
* For convenience, the engine supports key/value storage for simple get/set and blob
|
||||
* storage. The full SQL engine is exposed underneath through the `query` method.
|
||||
*
|
||||
* @usage
|
||||
```js
|
||||
* let storage = new Storage(SqlStorage, options);
|
||||
* storage.set('name', 'Max');
|
||||
* storage.get('name').then((name) => {
|
||||
* });
|
||||
*
|
||||
* // Sql storage also exposes the full engine underneath
|
||||
* storage.query('insert into projects(name, data) values("Cool Project", "blah")');
|
||||
* storage.query('select * from projects').then((resp) => {})
|
||||
* ```
|
||||
*
|
||||
* The `SqlStorage` service supports these options:
|
||||
* {
|
||||
* name: the name of the database (__ionicstorage by default)
|
||||
* backupFlag: // where to store the file, default is BACKUP_LOCAL which DOES NOT store to iCloud. Other options: BACKUP_LIBRARY, BACKUP_DOCUMENTS
|
||||
* existingDatabase: whether to load this as an existing database (default is false)
|
||||
* }
|
||||
*
|
||||
*/
|
||||
export class SqlStorage extends StorageEngine {
|
||||
static BACKUP_LOCAL = 2;
|
||||
static BACKUP_LIBRARY = 1;
|
||||
static BACKUP_DOCUMENTS = 0;
|
||||
|
||||
private _db: any;
|
||||
|
||||
constructor(options = {}) {
|
||||
super();
|
||||
|
||||
let dbOptions = defaults(options, {
|
||||
name: DB_NAME,
|
||||
backupFlag: SqlStorage.BACKUP_LOCAL,
|
||||
existingDatabase: false
|
||||
});
|
||||
|
||||
if (win.sqlitePlugin) {
|
||||
let location = this._getBackupLocation(dbOptions.backupFlag);
|
||||
|
||||
this._db = win.sqlitePlugin.openDatabase(assign({
|
||||
name: dbOptions.name,
|
||||
location: location,
|
||||
createFromLocation: dbOptions.existingDatabase ? 1 : 0
|
||||
}, dbOptions));
|
||||
|
||||
} else {
|
||||
console.warn('Storage: SQLite plugin not installed, falling back to WebSQL. Make sure to install cordova-sqlite-storage in production!');
|
||||
|
||||
this._db = win.openDatabase(dbOptions.name, '1.0', 'database', 5 * 1024 * 1024);
|
||||
}
|
||||
this._tryInit();
|
||||
}
|
||||
|
||||
_getBackupLocation(dbFlag: number): number {
|
||||
switch (dbFlag) {
|
||||
case SqlStorage.BACKUP_LOCAL:
|
||||
return 2;
|
||||
case SqlStorage.BACKUP_LIBRARY:
|
||||
return 1;
|
||||
case SqlStorage.BACKUP_DOCUMENTS:
|
||||
return 0;
|
||||
default:
|
||||
throw Error('Invalid backup flag: ' + dbFlag);
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the DB with our required tables
|
||||
_tryInit() {
|
||||
this.query('CREATE TABLE IF NOT EXISTS kv (key text primary key, value text)').catch(err => {
|
||||
console.error('Storage: Unable to create initial storage tables', err.tx, err.err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform an arbitrary SQL operation on the database. Use this method
|
||||
* to have full control over the underlying database through SQL operations
|
||||
* like SELECT, INSERT, and UPDATE.
|
||||
*
|
||||
* @param {string} query the query to run
|
||||
* @param {array} params the additional params to use for query placeholders
|
||||
* @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)}
|
||||
*/
|
||||
query(query: string, params: any[] = []): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
this._db.transaction((tx: any) => {
|
||||
tx.executeSql(query, params,
|
||||
(tx: any, res: any) => resolve({ tx: tx, res: res }),
|
||||
(tx: any, err: any) => reject({ tx: tx, err: err }));
|
||||
},
|
||||
(err: any) => reject({ err: err }));
|
||||
} catch (err) {
|
||||
reject({ err: err });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value in the database identified by the given key.
|
||||
* @param {string} key the key
|
||||
* @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)}
|
||||
*/
|
||||
get(key: string): Promise<any> {
|
||||
return this.query('select key, value from kv where key = ? limit 1', [key]).then(data => {
|
||||
if (data.res.rows.length > 0) {
|
||||
return data.res.rows.item(0).value;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value in the database for the given key. Existing values will be overwritten.
|
||||
* @param {string} key the key
|
||||
* @param {string} value The value (as a string)
|
||||
* @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)}
|
||||
*/
|
||||
set(key: string, value: string): Promise<any> {
|
||||
return this.query('insert or replace into kv(key, value) values (?, ?)', [key, value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the value in the database for the given key.
|
||||
* @param {string} key the key
|
||||
* @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)}
|
||||
*/
|
||||
remove(key: string): Promise<any> {
|
||||
return this.query('delete from kv where key = ?', [key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all keys/values of your database.
|
||||
* @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)}
|
||||
*/
|
||||
clear(): Promise<any> {
|
||||
return this.query('delete from kv');
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
/**
|
||||
* Storage is an easy way to store key/value pairs and other complicated
|
||||
* data in a way that uses a variety of storage engines underneath.
|
||||
*
|
||||
* For most cases, we recommend the SqlStorage system as it will store
|
||||
* data in a file in the app's sandbox. LocalStorage should ONLY be used
|
||||
* for temporary data as it may be 'cleaned up' by the operation system
|
||||
* during low disk space situations.
|
||||
*/
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
export class Storage {
|
||||
private _strategy: StorageEngine;
|
||||
|
||||
constructor(strategyCls: IStorageEngine, options?: any) {
|
||||
this._strategy = new strategyCls(options);
|
||||
}
|
||||
|
||||
get(key: string): Promise<any> {
|
||||
return this._strategy.get(key);
|
||||
}
|
||||
|
||||
getJson(key: string): Promise<any> {
|
||||
return this.get(key).then(value => {
|
||||
try {
|
||||
return JSON.parse(value);
|
||||
} catch (e) {
|
||||
console.warn('Storage getJson(): unable to parse value for key', key, ' as JSON');
|
||||
throw e; // rethrowing exception so it can be handled with .catch()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setJson(key: string, value: any): Promise<any> {
|
||||
try {
|
||||
return this.set(key, JSON.stringify(value));
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
set(key: string, value: any) {
|
||||
return this._strategy.set(key, value);
|
||||
}
|
||||
|
||||
remove(key: string) {
|
||||
return this._strategy.remove(key);
|
||||
}
|
||||
|
||||
query(query: string, params?: any) {
|
||||
return this._strategy.query(query, params);
|
||||
}
|
||||
|
||||
clear() {
|
||||
return this._strategy.clear();
|
||||
}
|
||||
}
|
||||
|
||||
export interface IStorageEngine {
|
||||
new (options: any): StorageEngine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
export class StorageEngine {
|
||||
constructor(options = {}) { }
|
||||
|
||||
get(key: string): Promise<any> {
|
||||
throw Error('get() not implemented for this storage engine');
|
||||
}
|
||||
set(key: string, value: any): Promise<any> {
|
||||
throw Error('set() not implemented for this storage engine');
|
||||
}
|
||||
remove(key: string): Promise<any> {
|
||||
throw Error('remove() not implemented for this storage engine');
|
||||
}
|
||||
query(query: string, params?: any): Promise<any> {
|
||||
throw Error('query() not implemented for this storage engine');
|
||||
}
|
||||
clear(): Promise<any> {
|
||||
throw Error('clear() not implemented for this storage engine');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user