strata-storage 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/README.md +116 -0
- package/dist/adapters/capacitor/FilesystemAdapter.js +4 -4
- package/dist/adapters/capacitor/PreferencesAdapter.js +4 -4
- package/dist/adapters/capacitor/SecureAdapter.js +4 -4
- package/dist/adapters/capacitor/SqliteAdapter.js +4 -4
- package/dist/adapters/capacitor/index.js +4 -4
- package/dist/adapters/web/CacheAdapter.js +3 -3
- package/dist/adapters/web/CookieAdapter.js +2 -2
- package/dist/adapters/web/IndexedDBAdapter.js +3 -3
- package/dist/adapters/web/LocalStorageAdapter.js +3 -3
- package/dist/adapters/web/MemoryAdapter.js +2 -2
- package/dist/adapters/web/SessionStorageAdapter.js +1 -1
- package/dist/adapters/web/index.js +6 -6
- package/dist/android/src/main/java/com/strata/storage/EncryptedStorage.java +65 -0
- package/dist/android/src/main/java/com/strata/storage/SQLiteStorage.java +147 -0
- package/dist/android/src/main/java/com/strata/storage/SharedPreferencesStorage.java +74 -0
- package/dist/android/src/main/java/com/stratastorage/StrataStoragePlugin.java +256 -0
- package/dist/core/AdapterRegistry.js +1 -1
- package/dist/core/BaseAdapter.js +3 -3
- package/dist/core/StorageStrategy.js +1 -0
- package/dist/core/Strata.js +17 -17
- package/dist/features/compression.js +1 -1
- package/dist/features/encryption.d.ts.map +1 -1
- package/dist/features/encryption.js +6 -5
- package/dist/features/sync.js +1 -1
- package/dist/features/ttl.js +1 -1
- package/dist/index.js +24 -24
- package/dist/ios/Plugin/KeychainStorage.swift +87 -0
- package/dist/ios/Plugin/SQLiteStorage.swift +167 -0
- package/dist/ios/Plugin/StrataStoragePlugin.swift +204 -0
- package/dist/ios/Plugin/UserDefaultsStorage.swift +44 -0
- package/dist/package.json +14 -4
- package/dist/plugin/index.js +2 -2
- package/package.json +8 -26
- package/scripts/build.js +119 -14
- package/scripts/cli.js +6 -2
- package/scripts/configure.js +7 -3
- package/scripts/postinstall.js +6 -2
package/dist/README.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# Strata Storage
|
|
2
|
+
|
|
3
|
+
## 📚 Documentation
|
|
4
|
+
|
|
5
|
+
- **[Getting Started](./docs/getting-started/installation.md)** - Installation and setup
|
|
6
|
+
- **[Quick Start Guide](./docs/getting-started/quick-start.md)** - Get running in minutes
|
|
7
|
+
- **[API Reference](./docs/api/README.md)** - Complete API documentation
|
|
8
|
+
- **[Configuration](./docs/getting-started/configuration.md)** - Configuration options
|
|
9
|
+
- **[Platform Guides](./docs/guides/platforms/web.md)** - Platform-specific guides
|
|
10
|
+
- **[Examples](./docs/examples/README.md)** - Code examples and recipes
|
|
11
|
+
- **[GitHub](https://github.com/aoneahsan/strata-storage)** - Source code
|
|
12
|
+
- **[NPM](https://www.npmjs.com/package/strata-storage)** - Package registry
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
**One API. Every Storage. Everywhere.**
|
|
17
|
+
|
|
18
|
+
Zero-dependency universal storage plugin providing a unified API for all storage operations across web, Android, and iOS platforms.
|
|
19
|
+
|
|
20
|
+
## 🚀 Quick Start
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install strata-storage
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { Strata } from 'strata-storage';
|
|
28
|
+
|
|
29
|
+
const storage = new Strata();
|
|
30
|
+
await storage.initialize();
|
|
31
|
+
|
|
32
|
+
// Works everywhere - web, iOS, Android
|
|
33
|
+
await storage.set('user', { name: 'John', age: 30 });
|
|
34
|
+
const user = await storage.get('user');
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## ✨ Features
|
|
38
|
+
|
|
39
|
+
### Core Features
|
|
40
|
+
- ✅ **Zero Dependencies** - No external packages, pure implementation
|
|
41
|
+
- ✅ **Universal API** - Single interface for all storage types
|
|
42
|
+
- ✅ **Cross-Platform** - Web, iOS, Android support
|
|
43
|
+
- ✅ **TypeScript** - Full type safety and IntelliSense
|
|
44
|
+
- ✅ **Auto Fallback** - Intelligent storage selection
|
|
45
|
+
|
|
46
|
+
### Storage Adapters
|
|
47
|
+
- ✅ **Memory** - Fast in-memory storage
|
|
48
|
+
- ✅ **LocalStorage** - Persistent browser storage
|
|
49
|
+
- ✅ **SessionStorage** - Session-based browser storage
|
|
50
|
+
- ✅ **IndexedDB** - Large-scale browser database
|
|
51
|
+
- ✅ **Cookies** - HTTP cookie storage
|
|
52
|
+
- ✅ **Cache API** - Service worker cache storage
|
|
53
|
+
- ✅ **Capacitor Preferences** - Native mobile preferences
|
|
54
|
+
- ✅ **SQLite** - Mobile SQL database
|
|
55
|
+
- ✅ **Secure Storage** - Keychain (iOS) / Encrypted SharedPreferences (Android)
|
|
56
|
+
- ✅ **Filesystem** - File-based storage
|
|
57
|
+
|
|
58
|
+
### Advanced Features
|
|
59
|
+
- ✅ **Encryption** - AES-GCM encryption with Web Crypto API
|
|
60
|
+
- ✅ **Compression** - LZ-string compression algorithm
|
|
61
|
+
- ✅ **Cross-Tab Sync** - Real-time synchronization across tabs
|
|
62
|
+
- ✅ **Query Engine** - MongoDB-like queries for filtering data
|
|
63
|
+
- ✅ **TTL Support** - Automatic expiration with sliding TTL
|
|
64
|
+
- ✅ **Migration System** - Version-based data migrations
|
|
65
|
+
- 🚧 **Framework Integrations** - React, Vue, Angular (coming soon)
|
|
66
|
+
|
|
67
|
+
## 📖 Basic Usage
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
// Initialize with configuration
|
|
71
|
+
const storage = new Strata({
|
|
72
|
+
defaultStorages: ['indexedDB', 'localStorage', 'memory'],
|
|
73
|
+
encryption: { enabled: true },
|
|
74
|
+
compression: { enabled: true },
|
|
75
|
+
ttl: { defaultTTL: 3600000 } // 1 hour
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
await storage.initialize();
|
|
79
|
+
|
|
80
|
+
// Store with options
|
|
81
|
+
await storage.set('key', value, {
|
|
82
|
+
ttl: 3600000, // Expire in 1 hour
|
|
83
|
+
encrypt: true, // Encrypt this value
|
|
84
|
+
compress: true, // Compress if beneficial
|
|
85
|
+
tags: ['user-data'] // Tag for grouping
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Query data
|
|
89
|
+
const results = await storage.query({
|
|
90
|
+
tags: { $in: ['user-data'] },
|
|
91
|
+
'value.age': { $gte: 18 }
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Subscribe to changes
|
|
95
|
+
storage.subscribe((change) => {
|
|
96
|
+
console.log(`${change.key} changed from ${change.oldValue} to ${change.newValue}`);
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## 🏗 Project Status
|
|
101
|
+
|
|
102
|
+
Currently in active development. Phase 1-5 completed:
|
|
103
|
+
- ✅ Project setup and core architecture
|
|
104
|
+
- ✅ Memory and web storage adapters
|
|
105
|
+
- ✅ Capacitor plugin structure
|
|
106
|
+
- ✅ Advanced features (encryption, compression, sync, query, TTL)
|
|
107
|
+
- 🚧 Native implementations (iOS/Android)
|
|
108
|
+
- 🚧 Testing and documentation
|
|
109
|
+
|
|
110
|
+
## 📄 License
|
|
111
|
+
|
|
112
|
+
MIT
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
Created by Ahsan Mahmood
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
* Filesystem Adapter - Native file system storage
|
|
3
3
|
* Direct file access on iOS and Android
|
|
4
4
|
*/
|
|
5
|
-
import { BaseAdapter } from
|
|
6
|
-
import { StrataStorage } from
|
|
7
|
-
import { StorageError } from
|
|
8
|
-
import { isCapacitor } from
|
|
5
|
+
import { BaseAdapter } from "../../core/BaseAdapter.js";
|
|
6
|
+
import { StrataStorage } from "../../plugin/index.js";
|
|
7
|
+
import { StorageError } from "../../utils/errors.js";
|
|
8
|
+
import { isCapacitor } from "../../utils/index.js";
|
|
9
9
|
/**
|
|
10
10
|
* Native filesystem adapter using Capacitor plugin
|
|
11
11
|
*/
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
* Preferences Adapter - Native preferences storage
|
|
3
3
|
* iOS: UserDefaults, Android: SharedPreferences
|
|
4
4
|
*/
|
|
5
|
-
import { BaseAdapter } from
|
|
6
|
-
import { StrataStorage } from
|
|
7
|
-
import { StorageError } from
|
|
8
|
-
import { isCapacitor } from
|
|
5
|
+
import { BaseAdapter } from "../../core/BaseAdapter.js";
|
|
6
|
+
import { StrataStorage } from "../../plugin/index.js";
|
|
7
|
+
import { StorageError } from "../../utils/errors.js";
|
|
8
|
+
import { isCapacitor } from "../../utils/index.js";
|
|
9
9
|
/**
|
|
10
10
|
* Native preferences adapter using Capacitor plugin
|
|
11
11
|
*/
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
* Secure Adapter - Native secure storage
|
|
3
3
|
* iOS: Keychain, Android: EncryptedSharedPreferences
|
|
4
4
|
*/
|
|
5
|
-
import { BaseAdapter } from
|
|
6
|
-
import { StrataStorage } from
|
|
7
|
-
import { StorageError } from
|
|
8
|
-
import { isCapacitor } from
|
|
5
|
+
import { BaseAdapter } from "../../core/BaseAdapter.js";
|
|
6
|
+
import { StrataStorage } from "../../plugin/index.js";
|
|
7
|
+
import { StorageError } from "../../utils/errors.js";
|
|
8
|
+
import { isCapacitor } from "../../utils/index.js";
|
|
9
9
|
/**
|
|
10
10
|
* Native secure storage adapter using Capacitor plugin
|
|
11
11
|
*/
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
* SQLite Adapter - Native SQLite database storage
|
|
3
3
|
* Available on iOS and Android
|
|
4
4
|
*/
|
|
5
|
-
import { BaseAdapter } from
|
|
6
|
-
import { StrataStorage } from
|
|
7
|
-
import { StorageError, TransactionError } from
|
|
8
|
-
import { isCapacitor } from
|
|
5
|
+
import { BaseAdapter } from "../../core/BaseAdapter.js";
|
|
6
|
+
import { StrataStorage } from "../../plugin/index.js";
|
|
7
|
+
import { StorageError, TransactionError } from "../../utils/errors.js";
|
|
8
|
+
import { isCapacitor } from "../../utils/index.js";
|
|
9
9
|
/**
|
|
10
10
|
* Native SQLite adapter using Capacitor plugin
|
|
11
11
|
*/
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Capacitor Native Storage Adapters
|
|
3
3
|
* Export all native platform storage implementations
|
|
4
4
|
*/
|
|
5
|
-
export { PreferencesAdapter } from
|
|
6
|
-
export { SqliteAdapter } from
|
|
7
|
-
export { SecureAdapter } from
|
|
8
|
-
export { FilesystemAdapter } from
|
|
5
|
+
export { PreferencesAdapter } from "./PreferencesAdapter.js";
|
|
6
|
+
export { SqliteAdapter } from "./SqliteAdapter.js";
|
|
7
|
+
export { SecureAdapter } from "./SecureAdapter.js";
|
|
8
|
+
export { FilesystemAdapter } from "./FilesystemAdapter.js";
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* Cache Adapter - Service Worker Cache API implementation
|
|
3
3
|
* Provides network-aware storage for offline support
|
|
4
4
|
*/
|
|
5
|
-
import { BaseAdapter } from
|
|
6
|
-
import { getObjectSize } from
|
|
7
|
-
import { StorageError, QuotaExceededError, NotSupportedError } from
|
|
5
|
+
import { BaseAdapter } from "../../core/BaseAdapter.js";
|
|
6
|
+
import { getObjectSize } from "../../utils/index.js";
|
|
7
|
+
import { StorageError, QuotaExceededError, NotSupportedError } from "../../utils/errors.js";
|
|
8
8
|
/**
|
|
9
9
|
* Cache API adapter for Service Worker environments
|
|
10
10
|
*/
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* Cookie Adapter - Browser cookie implementation
|
|
3
3
|
* Provides limited storage with 4KB per cookie limit
|
|
4
4
|
*/
|
|
5
|
-
import { BaseAdapter } from
|
|
6
|
-
import { StorageError, QuotaExceededError } from
|
|
5
|
+
import { BaseAdapter } from "../../core/BaseAdapter.js";
|
|
6
|
+
import { StorageError, QuotaExceededError } from "../../utils/errors.js";
|
|
7
7
|
/**
|
|
8
8
|
* Browser cookie adapter
|
|
9
9
|
*/
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* IndexedDB Adapter - Browser IndexedDB implementation
|
|
3
3
|
* Provides large-scale persistent storage with advanced features
|
|
4
4
|
*/
|
|
5
|
-
import { BaseAdapter } from
|
|
6
|
-
import { createDeferred, getObjectSize } from
|
|
7
|
-
import { StorageError, QuotaExceededError, TransactionError } from
|
|
5
|
+
import { BaseAdapter } from "../../core/BaseAdapter.js";
|
|
6
|
+
import { createDeferred, getObjectSize } from "../../utils/index.js";
|
|
7
|
+
import { StorageError, QuotaExceededError, TransactionError } from "../../utils/errors.js";
|
|
8
8
|
/**
|
|
9
9
|
* Browser IndexedDB adapter
|
|
10
10
|
*/
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* LocalStorage Adapter - Browser localStorage implementation
|
|
3
3
|
* Provides persistent storage with 5-10MB limit
|
|
4
4
|
*/
|
|
5
|
-
import { BaseAdapter } from
|
|
6
|
-
import { serialize, deserialize, getObjectSize } from
|
|
7
|
-
import { QuotaExceededError, SerializationError } from
|
|
5
|
+
import { BaseAdapter } from "../../core/BaseAdapter.js";
|
|
6
|
+
import { serialize, deserialize, getObjectSize } from "../../utils/index.js";
|
|
7
|
+
import { QuotaExceededError, SerializationError } from "../../utils/errors.js";
|
|
8
8
|
/**
|
|
9
9
|
* Browser localStorage adapter
|
|
10
10
|
*/
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* Memory Adapter - In-memory storage implementation
|
|
3
3
|
* Provides fast, non-persistent storage using Map
|
|
4
4
|
*/
|
|
5
|
-
import { BaseAdapter } from
|
|
6
|
-
import { deepClone } from
|
|
5
|
+
import { BaseAdapter } from "../../core/BaseAdapter.js";
|
|
6
|
+
import { deepClone } from "../../utils/index.js";
|
|
7
7
|
/**
|
|
8
8
|
* In-memory storage adapter using Map
|
|
9
9
|
*/
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* SessionStorage Adapter - Browser sessionStorage implementation
|
|
3
3
|
* Provides session-scoped storage with 5-10MB limit
|
|
4
4
|
*/
|
|
5
|
-
import { LocalStorageAdapter } from
|
|
5
|
+
import { LocalStorageAdapter } from "./LocalStorageAdapter.js";
|
|
6
6
|
/**
|
|
7
7
|
* Browser sessionStorage adapter
|
|
8
8
|
* Extends LocalStorageAdapter as the API is identical
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Web storage adapters
|
|
3
3
|
*/
|
|
4
|
-
export { MemoryAdapter } from
|
|
5
|
-
export { LocalStorageAdapter } from
|
|
6
|
-
export { SessionStorageAdapter } from
|
|
7
|
-
export { IndexedDBAdapter } from
|
|
8
|
-
export { CookieAdapter } from
|
|
9
|
-
export { CacheAdapter } from
|
|
4
|
+
export { MemoryAdapter } from "./MemoryAdapter.js";
|
|
5
|
+
export { LocalStorageAdapter } from "./LocalStorageAdapter.js";
|
|
6
|
+
export { SessionStorageAdapter } from "./SessionStorageAdapter.js";
|
|
7
|
+
export { IndexedDBAdapter } from "./IndexedDBAdapter.js";
|
|
8
|
+
export { CookieAdapter } from "./CookieAdapter.js";
|
|
9
|
+
export { CacheAdapter } from "./CacheAdapter.js";
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
package com.strata.storage;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.content.SharedPreferences;
|
|
5
|
+
import android.os.Build;
|
|
6
|
+
import androidx.security.crypto.EncryptedSharedPreferences;
|
|
7
|
+
import androidx.security.crypto.MasterKey;
|
|
8
|
+
import java.util.Set;
|
|
9
|
+
import java.util.Map;
|
|
10
|
+
|
|
11
|
+
public class EncryptedStorage {
|
|
12
|
+
private SharedPreferences encryptedPrefs;
|
|
13
|
+
private SharedPreferences.Editor editor;
|
|
14
|
+
|
|
15
|
+
public EncryptedStorage(Context context, String name) throws Exception {
|
|
16
|
+
String fileName = name != null ? name : "StrataSecureStorage";
|
|
17
|
+
|
|
18
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
19
|
+
MasterKey masterKey = new MasterKey.Builder(context)
|
|
20
|
+
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
|
|
21
|
+
.build();
|
|
22
|
+
|
|
23
|
+
encryptedPrefs = EncryptedSharedPreferences.create(
|
|
24
|
+
context,
|
|
25
|
+
fileName,
|
|
26
|
+
masterKey,
|
|
27
|
+
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
|
|
28
|
+
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
editor = encryptedPrefs.edit();
|
|
32
|
+
} else {
|
|
33
|
+
// Fallback to regular SharedPreferences for older devices
|
|
34
|
+
encryptedPrefs = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
|
|
35
|
+
editor = encryptedPrefs.edit();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public boolean set(String key, String value) {
|
|
40
|
+
editor.putString(key, value);
|
|
41
|
+
return editor.commit();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public String get(String key) {
|
|
45
|
+
return encryptedPrefs.getString(key, null);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public boolean remove(String key) {
|
|
49
|
+
editor.remove(key);
|
|
50
|
+
return editor.commit();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public boolean clear() {
|
|
54
|
+
editor.clear();
|
|
55
|
+
return editor.commit();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public Set<String> keys() {
|
|
59
|
+
return encryptedPrefs.getAll().keySet();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public boolean has(String key) {
|
|
63
|
+
return encryptedPrefs.contains(key);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
package com.strata.storage;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.database.Cursor;
|
|
5
|
+
import android.database.sqlite.SQLiteDatabase;
|
|
6
|
+
import android.database.sqlite.SQLiteOpenHelper;
|
|
7
|
+
import android.content.ContentValues;
|
|
8
|
+
import java.util.ArrayList;
|
|
9
|
+
import java.util.List;
|
|
10
|
+
import java.util.HashMap;
|
|
11
|
+
import java.util.Map;
|
|
12
|
+
|
|
13
|
+
public class SQLiteStorage extends SQLiteOpenHelper {
|
|
14
|
+
private static final int DATABASE_VERSION = 1;
|
|
15
|
+
private static final String TABLE_NAME = "strata_storage";
|
|
16
|
+
|
|
17
|
+
private static final String KEY_ID = "key";
|
|
18
|
+
private static final String KEY_VALUE = "value";
|
|
19
|
+
private static final String KEY_CREATED = "created";
|
|
20
|
+
private static final String KEY_UPDATED = "updated";
|
|
21
|
+
private static final String KEY_EXPIRES = "expires";
|
|
22
|
+
private static final String KEY_TAGS = "tags";
|
|
23
|
+
private static final String KEY_METADATA = "metadata";
|
|
24
|
+
|
|
25
|
+
public SQLiteStorage(Context context, String dbName) {
|
|
26
|
+
super(context, dbName != null ? dbName : "strata.db", null, DATABASE_VERSION);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@Override
|
|
30
|
+
public void onCreate(SQLiteDatabase db) {
|
|
31
|
+
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
|
|
32
|
+
+ KEY_ID + " TEXT PRIMARY KEY,"
|
|
33
|
+
+ KEY_VALUE + " BLOB NOT NULL,"
|
|
34
|
+
+ KEY_CREATED + " INTEGER NOT NULL,"
|
|
35
|
+
+ KEY_UPDATED + " INTEGER NOT NULL,"
|
|
36
|
+
+ KEY_EXPIRES + " INTEGER,"
|
|
37
|
+
+ KEY_TAGS + " TEXT,"
|
|
38
|
+
+ KEY_METADATA + " TEXT" + ")";
|
|
39
|
+
db.execSQL(CREATE_TABLE);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@Override
|
|
43
|
+
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
44
|
+
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
|
|
45
|
+
onCreate(db);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public boolean set(String key, byte[] value, Long expires, String tags, String metadata) {
|
|
49
|
+
SQLiteDatabase db = this.getWritableDatabase();
|
|
50
|
+
ContentValues values = new ContentValues();
|
|
51
|
+
|
|
52
|
+
long now = System.currentTimeMillis();
|
|
53
|
+
values.put(KEY_ID, key);
|
|
54
|
+
values.put(KEY_VALUE, value);
|
|
55
|
+
values.put(KEY_CREATED, now);
|
|
56
|
+
values.put(KEY_UPDATED, now);
|
|
57
|
+
|
|
58
|
+
if (expires != null) {
|
|
59
|
+
values.put(KEY_EXPIRES, expires);
|
|
60
|
+
}
|
|
61
|
+
if (tags != null) {
|
|
62
|
+
values.put(KEY_TAGS, tags);
|
|
63
|
+
}
|
|
64
|
+
if (metadata != null) {
|
|
65
|
+
values.put(KEY_METADATA, metadata);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
long result = db.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
|
|
69
|
+
db.close();
|
|
70
|
+
return result != -1;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public Map<String, Object> get(String key) {
|
|
74
|
+
SQLiteDatabase db = this.getReadableDatabase();
|
|
75
|
+
Cursor cursor = db.query(TABLE_NAME, null, KEY_ID + "=?",
|
|
76
|
+
new String[]{key}, null, null, null, null);
|
|
77
|
+
|
|
78
|
+
Map<String, Object> result = null;
|
|
79
|
+
if (cursor != null && cursor.moveToFirst()) {
|
|
80
|
+
result = new HashMap<>();
|
|
81
|
+
result.put("key", key);
|
|
82
|
+
result.put("value", cursor.getBlob(cursor.getColumnIndex(KEY_VALUE)));
|
|
83
|
+
result.put("created", cursor.getLong(cursor.getColumnIndex(KEY_CREATED)));
|
|
84
|
+
result.put("updated", cursor.getLong(cursor.getColumnIndex(KEY_UPDATED)));
|
|
85
|
+
|
|
86
|
+
int expiresIndex = cursor.getColumnIndex(KEY_EXPIRES);
|
|
87
|
+
if (!cursor.isNull(expiresIndex)) {
|
|
88
|
+
result.put("expires", cursor.getLong(expiresIndex));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
int tagsIndex = cursor.getColumnIndex(KEY_TAGS);
|
|
92
|
+
if (!cursor.isNull(tagsIndex)) {
|
|
93
|
+
result.put("tags", cursor.getString(tagsIndex));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
int metadataIndex = cursor.getColumnIndex(KEY_METADATA);
|
|
97
|
+
if (!cursor.isNull(metadataIndex)) {
|
|
98
|
+
result.put("metadata", cursor.getString(metadataIndex));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
cursor.close();
|
|
102
|
+
}
|
|
103
|
+
db.close();
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public boolean remove(String key) {
|
|
108
|
+
SQLiteDatabase db = this.getWritableDatabase();
|
|
109
|
+
int result = db.delete(TABLE_NAME, KEY_ID + " = ?", new String[]{key});
|
|
110
|
+
db.close();
|
|
111
|
+
return result > 0;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
public boolean clear() {
|
|
115
|
+
SQLiteDatabase db = this.getWritableDatabase();
|
|
116
|
+
db.delete(TABLE_NAME, null, null);
|
|
117
|
+
db.close();
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public List<String> keys() {
|
|
122
|
+
List<String> keys = new ArrayList<>();
|
|
123
|
+
String selectQuery = "SELECT " + KEY_ID + " FROM " + TABLE_NAME;
|
|
124
|
+
|
|
125
|
+
SQLiteDatabase db = this.getReadableDatabase();
|
|
126
|
+
Cursor cursor = db.rawQuery(selectQuery, null);
|
|
127
|
+
|
|
128
|
+
if (cursor.moveToFirst()) {
|
|
129
|
+
do {
|
|
130
|
+
keys.add(cursor.getString(0));
|
|
131
|
+
} while (cursor.moveToNext());
|
|
132
|
+
}
|
|
133
|
+
cursor.close();
|
|
134
|
+
db.close();
|
|
135
|
+
return keys;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public boolean has(String key) {
|
|
139
|
+
SQLiteDatabase db = this.getReadableDatabase();
|
|
140
|
+
Cursor cursor = db.query(TABLE_NAME, new String[]{KEY_ID}, KEY_ID + "=?",
|
|
141
|
+
new String[]{key}, null, null, null, null);
|
|
142
|
+
boolean exists = cursor.getCount() > 0;
|
|
143
|
+
cursor.close();
|
|
144
|
+
db.close();
|
|
145
|
+
return exists;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
package com.strata.storage;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.content.SharedPreferences;
|
|
5
|
+
import java.util.Map;
|
|
6
|
+
import java.util.Set;
|
|
7
|
+
import java.util.HashSet;
|
|
8
|
+
import org.json.JSONObject;
|
|
9
|
+
import org.json.JSONArray;
|
|
10
|
+
|
|
11
|
+
public class SharedPreferencesStorage {
|
|
12
|
+
private final SharedPreferences prefs;
|
|
13
|
+
private final SharedPreferences.Editor editor;
|
|
14
|
+
|
|
15
|
+
public SharedPreferencesStorage(Context context, String name) {
|
|
16
|
+
this.prefs = context.getSharedPreferences(name != null ? name : "StrataStorage", Context.MODE_PRIVATE);
|
|
17
|
+
this.editor = prefs.edit();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public boolean set(String key, Object value) {
|
|
21
|
+
try {
|
|
22
|
+
if (value instanceof String) {
|
|
23
|
+
editor.putString(key, (String) value);
|
|
24
|
+
} else if (value instanceof Integer) {
|
|
25
|
+
editor.putInt(key, (Integer) value);
|
|
26
|
+
} else if (value instanceof Long) {
|
|
27
|
+
editor.putLong(key, (Long) value);
|
|
28
|
+
} else if (value instanceof Float) {
|
|
29
|
+
editor.putFloat(key, (Float) value);
|
|
30
|
+
} else if (value instanceof Boolean) {
|
|
31
|
+
editor.putBoolean(key, (Boolean) value);
|
|
32
|
+
} else if (value instanceof Set) {
|
|
33
|
+
editor.putStringSet(key, (Set<String>) value);
|
|
34
|
+
} else {
|
|
35
|
+
// Convert complex objects to JSON
|
|
36
|
+
String json = value instanceof JSONObject ?
|
|
37
|
+
((JSONObject) value).toString() :
|
|
38
|
+
new JSONObject(value).toString();
|
|
39
|
+
editor.putString(key, json);
|
|
40
|
+
}
|
|
41
|
+
return editor.commit();
|
|
42
|
+
} catch (Exception e) {
|
|
43
|
+
e.printStackTrace();
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public Object get(String key) {
|
|
49
|
+
Map<String, ?> all = prefs.getAll();
|
|
50
|
+
return all.get(key);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public boolean remove(String key) {
|
|
54
|
+
editor.remove(key);
|
|
55
|
+
return editor.commit();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public boolean clear() {
|
|
59
|
+
editor.clear();
|
|
60
|
+
return editor.commit();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public Set<String> keys() {
|
|
64
|
+
return prefs.getAll().keySet();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public boolean has(String key) {
|
|
68
|
+
return prefs.contains(key);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public Map<String, ?> getAll() {
|
|
72
|
+
return prefs.getAll();
|
|
73
|
+
}
|
|
74
|
+
}
|