Skip to content

Commit

Permalink
feat(storage): Add removeOld function (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Sep 8, 2021
1 parent 908bd68 commit 698350e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
15 changes: 15 additions & 0 deletions storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ This method can also be used to store non-string values, such as numbers and boo
* [`clear()`](#clear)
* [`keys()`](#keys)
* [`migrate()`](#migrate)
* [`removeOld()`](#removeold)
* [Interfaces](#interfaces)

</docgen-index>
Expand Down Expand Up @@ -180,6 +181,7 @@ Migrate data from the Capacitor 2 Storage plugin.

This action is non-destructive. It will not remove old data and will only
write new data if they key was not already set.
To remove the old data after being migrated, call removeOld().

**Returns:** <code>Promise&lt;<a href="#migrateresult">MigrateResult</a>&gt;</code>

Expand All @@ -188,6 +190,19 @@ write new data if they key was not already set.
--------------------


### removeOld()

```typescript
removeOld() => Promise<void>
```

Removes old data with `_cap_` prefix from the Capacitor 2 Storage plugin.

**Since:** 1.1.0

--------------------


### Interfaces


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,9 @@ public void migrate(PluginCall call) {
ret.put("existing", new JSArray(existing));
call.resolve(ret);
}

@PluginMethod
public void removeOld(PluginCall call) {
call.resolve();
}
}
1 change: 1 addition & 0 deletions storage/ios/Plugin/StoragePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
CAP_PLUGIN_METHOD(keys, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(clear, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(migrate, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(removeOld, CAPPluginReturnPromise);
)
9 changes: 9 additions & 0 deletions storage/ios/Plugin/StoragePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,13 @@ public class StoragePlugin: CAPPlugin {
"existing": existing
])
}

@objc func removeOld(_ call: CAPPluginCall) {
let oldPrefix = "_cap_"
let oldKeys = UserDefaults.standard.dictionaryRepresentation().keys.filter { $0.hasPrefix(oldPrefix) }
for oldKey in oldKeys {
UserDefaults.standard.removeObject(forKey: oldKey)
}
call.resolve()
}
}
8 changes: 8 additions & 0 deletions storage/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,16 @@ export interface StoragePlugin {
*
* This action is non-destructive. It will not remove old data and will only
* write new data if they key was not already set.
* To remove the old data after being migrated, call removeOld().
*
* @since 1.0.0
*/
migrate(): Promise<MigrateResult>;

/**
* Removes old data with `_cap_` prefix from the Capacitor 2 Storage plugin.
*
* @since 1.1.0
*/
removeOld(): Promise<void>;
}
8 changes: 8 additions & 0 deletions storage/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ export class StorageWeb extends WebPlugin implements StoragePlugin {
return { migrated, existing };
}

public async removeOld(): Promise<void> {
const oldprefix = '_cap_';
const keys = Object.keys(this.impl).filter(k => k.indexOf(oldprefix) === 0);
for (const oldkey of keys) {
this.impl.removeItem(oldkey);
}
}

private get impl(): Storage {
return window.localStorage;
}
Expand Down

0 comments on commit 698350e

Please sign in to comment.